Hi, everybody.
Objective: Create automatic or manual copies of openHAB ( not the whole system ) via OpenHAB and synchronize them with Google Drive.
First I’d like to tell you about my work: The general copies are done with rpi-clone
and the own copies of OpenHAB with the following method with the help of rclone
.
Let’s get to work.
- First install rclone.
A good tutorial that helped me initially is this, but you can access to the rclone page that also helps for installation, and then to synchronize rclone with Google Drive).
- Small test to corroborate rclone working…
rclone copy --update --verbose --transfers 30 --checkers 8 --contimeout 60s --timeout 300s --retries 3 --low-level-retries 10 --stats 10s “/home/openhabian/README.txt “backup:Backup”
Important clarifications:
A) All options are detailed on the previous page.
B) README.txt default file found in “home” of my openhabian, can be any other.
C) “backup:Backup” --> first “backup” corresponds to the name in rclone, second “Backup” corresponds to the folder that will be created in Google Drive.
- Create script to execute commands
#!/bin/bash
rcloneConfig=/home/openhabian/.config/rclone/rclone.conf
rcloneOptions="--update --verbose --transfers 30 --checkers 8 --contimeout 60s --timeout 300s --retries 3 --low-level-retries 10 --stats 10s"
echo $(date +"%Y-%m-%d %H:%M:%S"): Ejecutando copia.. &&
sudo openhab-cli backup &&
echo $(date +"%Y-%m-%d %H:%M:%S"): Copia realizada &&
sudo rclone copy $rcloneOptions --config=$rcloneConfig "/var/lib/openhab2/backups" "backup:Backup" &&
echo $(date +"%Y-%m-%d %H:%M:%S"): Copia actualizada
Give execution permissions with sudo chmod +x backup.sh
- .items
Group gBackup <"lock">
DateTime BackupResultDate "Última ejecución [%1$td.%1$tm.%1$tY - %1$tH:%1$tM]" <"speedtest_problem4"> (gBackup)
String BackupRunning "Ejecutando test... [%s]" <"speedtest_new"> (gBackup)
Switch BackupRerun "Iniciar manualmente" <"speedtest_reload2"> (gBackup)
- .rules
I created a rule by running a backup 1 day a week (Sunday at 4am) but also added the possibility to save a copy manually just in case.
val String filename = "backup.rules"
rule "Copia de seguridad"
when
Time cron "0 0 4 ? * SUN *" or
Item BackupRerun received command ON
then
logInfo(filename, "Ejecutando copia de seguridad")
BackupRunning.postUpdate("Ejecutando copia de seguridad...")
BackupResultDate.postUpdate(new DateTimeType())
var String BackupCliOutput = executeCommandLine("/etc/openhab2/scripts/backup.sh", 120*1000)
BackupRunning.postUpdate("Análisis de datos...")
if (BackupCliOutput.endsWith("Copia actualizada")) {
BackupRunning.postUpdate("-")
logInfo(filename, "Copia de seguridad actualizada")
} else {
BackupRunning.postUpdate("Error durante la ejecución")
logError(filename, "Fallo copia de seguridad. Output:\n" + BackupCliOutput + "\n\n")
}
BackupRerun.postUpdate(OFF)
end
- Remember to modify execution permissions to be able to execute the script.
And add:
Cmnd_Alias BACKUP = /usr/bin/openhab-cli backup, /usr/bin/rclone
openhab ALL=(ALL) NOPASSWD: BACKUP
That’s all, I hope it serves someone. Greetings.