Automate backups and upload them to Google Drive

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.

  1. 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).

  1. 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.

  1. 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

  1. .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)
  1. .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
  1. 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

Captura de Pantalla 2020-03-02 a la(s) 19.53.07

That’s all, I hope it serves someone. Greetings.

6 Likes

Thanks for writing this down.

Not sure why you chose to go that route, though.
To you and all others, please be aware that openHABian comes with Amanda, a backup tool that also allows for backing up to AWS.

…and maybe it should be mentioned, that it penny works with Linux based OSes.

“it” ?
“penny works” ??

Only

1 Like

I don’t understand what you want to say. And single word replies are not funny.

Is it really so difficult? You asked for the " penny" and I answered “only”. Replace “penny” by “only” and:
“…and maybe it should be mentioned, that it only works with Linux based OSes”

1 Like

Yes it is because your post is totally lacking context. I still don’t understand what you are trying to say and I guess others don’t as well.
What is “it” - ? This tutorial ? Google Drive ? Amanda ? something else ?

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.