How To Automate OpenHAB Backup

You learn in this How To Automate OpenHAB Backup to backup your OpenHAB to an external storage drive. The How To consists of a simple Step by Step guide.


Backup OpenHAB To An External Drive

Backup OpenHAB To An External Drive

Index of How To Automate OpenHAB BackupTo An External Drive


This How To Backup OpenHAB To An External Drive is leveraging on the in OpenHAB 3 (openhab ian) existing backup command that is

openhab-cli backup --full

The environment used is as the following:

I am using a greater backup hard drive as the hard drive on which the system sits. 512 GB versus 1 TB. The reason is because i have other applications running on my OpenHAB openhabian Raspberry Pi 4 than OpenHAB. Further, i like in some cases to have multiple backups and finally, i am also running a database for OpenHAB on the same Raspberry Pi 4 and this needs backup too.

How To Automate OpenHAB Backup To An External Drive

This how to focuses on 3 key phases:

  • Find out some details required about your backup drive 
  • Mount the backup drive and automate it
  • Create a backup script
  • Test for success

Find Out The Details About Your Backup Drive

It is important in this moment, that you have not yet plugged your backup drive into your Raspberry Pi. However, you will in this first phase login into your Raspberry’s command line and execute a command to find out your “as is situation”. To do so we leverage on the lsblk command. The below shows how its looking like in my case.

openhabian@OpenHAB:~ $ sudo lsblk
sda      8:0    0 476.9G  0 disk
├─sda1   8:1    0   256M  0 part /boot
└─sda2   8:2    0 476.7G  0 part /
zram0  254:0    0     1G  0 disk [SWAP]
zram1  254:1    0   750M  0 disk /opt/zram/zram1
zram2  254:2    0     1G  0 disk /opt/zram/zram2
openhabian@OpenHAB:~ $

Now, lets plugin the brand new USB backup drive. Wait a couple of seconds and enter again the lsblk command.

openhabian@OpenHAB:~ $ lsblk
sda      8:0    0 476.9G  0 disk 
├─sda1   8:1    0   256M  0 part /boot
└─sda2   8:2    0 476.7G  0 part /
sdb      8:16   0 931.5G  0 disk 
├─sdb1   8:17   0 476.8G  0 part 
└─sdb2   8:18   0 454.6G  0 part 
zram0  254:0    0     1G  0 disk [SWAP]
zram1  254:1    0   750M  0 disk /opt/zram/zram1
zram2  254:2    0     1G  0 disk /opt/zram/zram2
openhabian@OpenHAB:~ $

If you compare the two outputs, than you will notice immediately that there are in the second output, after the lsblk command 3 additional lines. These lines are called sdb, sdb1 and sdb2. These sdb1 and sdb2 are the two partitions of the plugged in backup hard drive. In my case, this is perfect, as I will be backing up OpenHAB to sdb1 and the other stuff that I have on the system to sdb2. For your case, you need to check how this looks like. Most likely you will find after plugging the backup hard drive new entries called sdbX, sdcX, sddX, sdeX, etc. This depends on how many drives you attached to your Raspberry. Also you need to check for you, that the partitioning is made in a way you like it. Otherwise, partition your backup hard drive in the way you prefer it before you continue with this how to. Now lets assume that you are OK with the as is situation and lets assume your portion onto which you like to backup is sdb1. 

Mount the Harddrive

First, let’s create a mount point. 

sudo mkdir -p /mnt/sdb1-backup-openhab

Secondly, let’s mount sdb1 to the created mount point.

sudo mount -t auto /dev/sdb1 /mnt/sdb1-backup-openhab

Third, let’s check whether it worked by executing the below command.

openhabian@OpenHAB:~ $ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/root      ext4      469G   24G  421G   6% /
devtmpfs       devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs          tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs          tmpfs     1.6G  9.7M  1.6G   1% /run
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
/dev/sda1      vfat      253M   51M  202M  21% /boot
/dev/zram1     ext4      721M  211M  459M  32% /opt/zram/zram1
overlay1       overlay   721M  211M  459M  32% /var/lib/openhab/persistence
/dev/zram2     ext4      974M  3.8M  903M   1% /opt/zram/zram2
overlay2       overlay   974M  3.8M  903M   1% /var/log
tmpfs          tmpfs     795M     0  795M   0% /run/user/1000
/dev/sdb1      ext4      469G   28K  445G   1% /mnt/sdb1-backup-openhab
openhabian@OpenHAB:~ $

We notice the last line. This is the newly mounted backup drive (sdb1) into the folder of /mnt/sdb1-backup-openhab. To this folder you will be backing up in the future and as we continue with this how to.

We done now with making available the backup drive. Let’s ensure this drive is mounted automated after reboots etc. Note: This assumes you keep the backup drive plugged into your Raspberry and you do not unplug it. If you go for a solution which in includes to unplug the drive, then there is nothing to do anymore, but you will have to mount the drive anytime you plug it into your Raspberry.


4th, let’s automate this, allowing the drive / partition to be mounted any time, even after system reboot. To do so we need to find out the partitions so called UUID. This UUID is important as we have to enter it into fstab. fstab is used by your system to find out which partition it needs to mount at boot time.

Whats the UUID? Leverage on the blkid command.

openhabian@OpenHAB:~ $ sudo blkid
/dev/sda1: UUID="8F55-538F" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="c6e1249c-01"
/dev/sda2: LABEL="OpenHAB" UUID="18a987df-feca-44aa-aa4d-949a60515379" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="c6e1249c-02"
/dev/zram0: LABEL="zram-config0" UUID="e05a7317-3065-4021-9b18-15f5b6ff7262" TYPE="swap"
/dev/zram1: UUID="254007f8-45f8-40bd-b2c9-fa01ad2d136e" BLOCK_SIZE="4096" TYPE="ext4"
/dev/zram2: UUID="112b0e6c-3e0f-4001-ba2e-767290cb14ed" BLOCK_SIZE="4096" TYPE="ext4"
/dev/sdb1: UUID="50759581-d562-46fa-a448-791330197dc8" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="3f9b107e-8038-6942-a232-0bb273a2049d"
openhabian@OpenHAB:~ $

class=””>As you can see in the last line, the UUID of sdb1 is “50759581-d562-46fa-a448-791330197dc8”. In your case it will be different. Whatever your UUID number is, it is the number that you either copy&paste or you note down.

Lets add the partition to fstab now. This is simple:

sudo vi /etc/fstab

Within fstab add the below line, whilst of course you will be using your UUID (the one you copy & pasted or noted down) instead of my UUID

UUID=50759581-d562-46fa-a448-791330197dc8 /mnt/exdisk fstype defaults,auto,users,rw,nofail 0 0

Once done, then you like to reboot your system to check whether it worked out. If it did not work out, then, please carefully follow the instructions above again. If it worked out, then please continue with the next step that is create a backup script.

Create a backup script

To automate the backup process we need to create a script. After automation of the automatic mounting of the backup drive in the previous chapter, we start now to script. Don’t worry, i will be telling you how you can adjust my backup script to make it fit your purpose. Key is, we will be leveraging on the OpenHAB openhabian openhab-cli backup –full option.

First we create the place the backup script will be logging into.

sudo touch /var/log/openhab/openhab_backup_script.log

Secondly you create a directory in which you will be storing the backup script.

sudo mkdir /home/openhabian/backup-script/

Third, create the backup script. 

sudo vi /home/openhabian/backup-script/

Copy the below into your backup script, but within this script you replace the following setting by your needs.

# Clone your openHAB disk to another disk (e.g. USB drive) 
# Simple backup script 
# echo outputs into the file calledopenhab_backup_script.log
# -> you can change the directory and filename to something else 
# -> the default backup script stores logs into the /var/log/openhab/ directory 
# The backup script leverages on rsync and openhab-cli backup. You need to install rsync on OpenHABian 
# More information on 
echo "" >>  /var/log/openhab/openhab_backup_script.log 
echo "Executing Script /home/openhabian/backup-script/ "  >> /var/log/openhab/openhab_backup_script.log
echo "" >> /var/log/openhab/openhab_backup_script.log
echo "" >> /var/log/openhab/openhab_backup_script.log
echo "-----------------------------------------------------"  >> /var/log/openhab/openhab_backup_script.log
echo "Date: $TODAY                     Host:$HOST"  >> /var/log/openhab/openhab_backup_script.log
echo "-----------------------------------------------------" >> /var/log/openhab/openhab_backup_script.log
echo “Starting backup leveraging on sudo openhab-cli backup --full” >> /var/log/openhab/openhab_backup_script.log
echo "" >> /var/log/openhab/openhab_backup_script.log
sudo openhab-cli backup --full   >> /var/log/openhab/openhab_backup_script.log 2>&1
mkdir -p /mnt/sdb1-backup-openhab/OpenHAB-Backups  >> /var/log/openhab/openhab_backup_script.log 2>&1
echo "Syncing leveraging rsync -azP  /var/lib/openhab/backups /mnt/sdb1-backup-openhab/OpenHAB-Backups " >> /var/log/openhab/openhab_backup_script.log
/usr/bin/rsync -azP  /var/lib/openhab/backups /mnt/sdb1-backup-openhab/OpenHAB-Backups  >> /var/log/openhab/openhab_backup_script.log 2>&1
echo "End Backup" >> /var/log/openhab/openhab_backup_script.log

Next step is now that you make this script executable by hacking the below into your command line.

sudo chmod u+x /home/openhabian/backup-script/

Finally, we leverage on crontab to ask for daily execution.

sudo crontab -e

Add the below lien to execute the backup script daily at 02:35am

35 2 * * * /home/openhabian/backup-script/>

Test for success

  • Once you got all of this running, you maybe like to double check after a couple of nights / days weather everything is running as you expect it. You also can check the log files under /var/log/openhab_backup_script.log
  • Alternatively, run ./home/openhabian/backup-script/ and check whether it is executing successfully.

Additional Information to Automate OpenHAB Backup To An External Drive

Some additional information that might help you or are of your interests

Follow me

It would be amazing if you follow To follow leverage on

  • Click to follow me on Twitter
  • Bookmark this page and comeback from time to time

Help and Comments

I am really looking forward for you to contact me if for example you found a better option or other idea then in this how to. Also, please touch base if you found an error or anything not working or if you have something that you would love to be added to this how to. Simply click this link to touch base with me.

Linking and Recommending the HowTo or the

I would love to see you are recommending this how to or link it to your website. Also, I would love if you link or recommend the whole Please feel free to do so! In case you like to touch base regarding this topic with me, then simply click this link. I look forward!