Recommended way to backup/restore OH2 configurations and things?

Depending on what version of OH you are running, there is a backup and restore script in /usr/share/openhab2/bin (I think that is where it is). If you are running openHABian, there is a full system backup setup and configured for you as well (Amanda is what it is called I think) that you can run from openhabian-config.

You can look at that backup and restore script and see what gets backups and restored. I’m pretty sure that is is the entire contents of /etc/openhab2, /var/lib/openhab2/jsondb, and perhaps /var/lib/openhab2/persistence.

It is all in /var/lib/openhab2/jsondb. Note that this is a human readable file.

From the latest stable version of openHAB (2.2) it would be easier for apt/yum/openHABian user to use the command:

sudo openhab-cli backup

Which places a file in /var/lib/openhab2/backups, which can then be restored using:

sudo openhab-cli restore /var/lib/openhab2/backups/filename.zip

This backs up both text config from /etc/openhab and PaperUI config from /var/lib/openhab/

12 Likes

Thanks a lot for your immediate responses !
This is what I was looking for.

Since I’m on the latest OH version, I will try “sudo openhab-cli backup” once I finished my current step.

Hi all,

I’m not very familiar to Linux and have various problems to fully understand the Amanda setup therefore I’m looking for a more simple option to manually generate a FULL system backup

Is this command

sudo openhab-cli backup

generating a FULL backup of the entire system incl. all things, items, sitemaps, rules, bindings, and personal settings? can it be be used to restore openHAB on an all new openhabian installation on a new SD card?

If I follow the instructions written in the OH documentation
https://docs.openhab.org/installation/linux.html#backup-and-restore a zip file is generated containing a ‘config’ and a ‘userdata’ folder. is this enough restoring my system on a new openhabian installation?

Up to now I was using:

# backup current installation with settings
TIMESTAMP=`date +%Y%m%d_%H%M%S`;
mkdir  ~/openhab2-backup-$TIMESTAMP
cp -arv /etc/openhab2 ~/openhab2-backup-$TIMESTAMP/conf
cp -arv /var/lib/openhab2 ~/openhab2-backup-$TIMESTAMP/userdata

# restart openhab instance
sudo systemctl start openhab2.service

I’m still not sure if this is enough for a full system restore in case of SD card crash. Maybe you can help me a little understanding what I need to do for getting a working backup solution.

Is there any alternative way to simply clone the entire SD card?

Thanks for your help,
Ben

all things, items, sitemaps, rules, bindings, and personal settings

The backup command for openhab will backup all these things EXCEPT manual .jar files that you’ve added yourself (if any). You’d simply need to add these back in afterwards. You may also need to add the openHAB user to the dialout and tty groups if you did this previously.

It won’t backup anything else related to Linux.

sudo openhab-cli restore <filename> can restore that file on a new system as long as it has openHAB (and Java 8) installed.

But I wouldn’t take my word for it. Generate a backup zipfile, get a new SD card and try it, that way if the restore isn’t as sucessful as you’d like, we can help with that. :slight_smile:

Hi Benjy,

Thanks for your reply. I’ll get a new SD card and give it a try within the next days.
I know the Win32 Disk Imager as a clone tool but I’m confused since there are 2 partitions on my openhabian SD card. for that reason I was asking if there is a way to clone the entire SD card including all partitions.

There is a way to clone the whole card with all partitions via linux dd command.
I use it from time to time to have an SD card backup in case the card fails.

sudo dd if=/dev/mmcblk0 of=/media/hd1/image_sd_2018_01_08.img

Backup to image file: (mmcblk0 is the whole SD-card, with both Partitions mmcblk0p1+mmcblk0p2); this results in an image file that has the size of the card.
I have an external HDD mounted at /media/hd1.
This takes 1…1.5h for a 16GB SD card on a RPI3.

I’m pretty sure setting bs to 1M would accelerate the process

sudo dd if=/dev/mmcblk0 of=/media/hd1/image_sd_2018_01_08.img bs=1M

Udo, thanks for the hint, I will try it!

Thanks for your suggestions…

unfortunately I cannot manage to get it work.
I’m using a MacBook Pro. Once I type your suggested command

 sudo dd if=/dev/mmcblk0 of=~/Desktop/BBOxxx.img bs=1m

into terminal.app I get the response:

dd: /dev/mmcblk0: No such file or directory

typing

diskutil list

I can see that this sample SD Card is described as

/dev/disk2 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *15.9 GB    disk2
   1:                  Apple_HFS BBO                     15.9 GB    disk2s1

If I change the command to:

sudo dd if=/dev/disk2 of=~/Desktop/BBOxx.img bs=1m

the file BBOxx.img is generated but whatever content I put onto the SD card the feedback looks always like this

dd: /dev/disk2: Input/output error
2052+0 records in
2052+0 records out
2151677952 bytes transferred in 111.981351 secs (19214610 bytes/sec)

If I try to copy the SD card with the openhabian installation which has 2 partition I can also see

dd: /dev/disk2: Input/output error

Do you have any idea what’s wrong?

Thanks again…

Hi again…

after several hours of trial and error I finally found a way to generate a working clone of the entire openhabian SD card inclusive all partitions and so on.

I mounted an hdd to my Raspberry and entered the following command:

sudo dd if=/dev/mmcblk0 of=/media/usb-backup/openhab_clon_2018_XX_XX.img bs=1M

It took approx. 1 hour but in the end I got a working clone on my hdd. Since the clone is of exact the same size the the SD card I used Pishrink to shrink the size of the clone image.

Hopefully this helps other openhabian users who want to generate an exact clone of their SD card for backup reasons.

4 Likes

I used this approach successfully to backup and restore. My only concern is Grafana and InfluxDB. Both of them are not included with “sudo openhab-cli backup”, right?

That’s correct, unfortunately the scripts distributed with openHAB will only look at the openHAB side. You’ll need to backup data attached to other services along side it.

I’m using raspiBackup for a long time, backs up everything (either via dd, tar, rsync or whatever …) You may backup to any media, sd, usb, nas, …
And the most important thing: restore works too :+1:

2 Likes

@aksnet, thanks for your scripts, very interesting. Just one question : why didn’t you consider using amanda as a basis for your backups?
I did set Amanda up, have it running and yes, what I see is that it is not backing up influxdb & grafana stuff but I was thinking that might be possible to add it to the amanda config file and yet you did choose another way, so I thought I’d ask the question :slight_smile:

Thanks,

@thefathefa, When i started to work on the script i guess amanda was not yet implemented or only in some beta versions already.
So i have never looked into it to be honest.
I just opened the 2 main Amanda pages and will see if a switch might be an option.

BR
Andreas

Sure it is. Just add the directories you want to backup to /etc/amanda/<yourconfig>/disklist .

@mstormi, would it be possible to stop the grafana service as well before the backup via amanda? Or do i have to do this manually before starting amanda?

Andreas

You can write a simple script to do this and run Amanda afterwards.
Then again I don’t think it’s required to stop Grafana, it’s more of a myth. Try to backup a runnning Grafana and to then restore it if to prove this if you feel uncomfortable about this.

I’m using a custom script for backing up openHAB, Grafana and InfluxDB on my Ubuntu machine. The 3 zip files get afterward synced to Google Drive (Grive2). The script get executed automatically with a dedicated Cron job.

#!/usr/bin/env bash

#####################################################
# Date with Timestamp
#####################################################
echo "+-+-+-+-+-+-+ Set Timestamp  +-+-+-+-+-+-+-+-+"
DATE=`date +%Y_%m_%d-%H_%M_%S`

#####################################################
# Backup openHAB
#####################################################
echo "+-+-+-+-+-+-+ openHAB Backup +-+-+-+-+-+-+-+-+"
sudo openhab-cli backup

#####################################################
# Backup Grafana
#####################################################
echo "+-+-+-+-+-+-+ Grafana Backup +-+-+-+-+-+-+-+-+"
#Stop Grafana service
sudo systemctl stop grafana-server
#Backing up ini file
mkdir /var/lib/openhab2/backups/tmp_grafana/
mkdir /var/lib/openhab2/backups/tmp_grafana/$DATE/
sudo cp -arv /etc/grafana/grafana.ini /var/lib/openhab2/backups/tmp_grafana/$DATE/grafana.ini
#Backing up a database
sudo cp -arv /var/lib/grafana/grafana.db /var/lib/openhab2/backups/tmp_grafana/$DATE/grafana.db
#Start Grafana service
sudo systemctl start grafana-server
#Create Zip File
cd /var/lib/openhab2/backups/tmp_grafana/$DATE
sudo zip -r /var/lib/openhab2/backups/grafana-backup-$DATE.zip ./*
#delete tmp folder
rm -rf /var/lib/openhab2/backups/tmp_grafana



#####################################################
# Backup Influxdb
#####################################################
echo "+-+-+-+-+-+-+ Influxdb Backup +-+-+-+-+-+-+-+"

#Backing up the metastore
influxd backup /var/lib/openhab2/backups/tmp_influxdb/$DATE/
#Backing up a database
influxd backup -database openhab_db /var/lib/openhab2/backups/tmp_influxdb/$DATE/
#Backing up conf file
cp -arv /etc/influxdb/influxdb.conf /var/lib/openhab2/backups/tmp_influxdb/$DATE/influxdb.conf
#Create Zip File
cd /var/lib/openhab2/backups/tmp_influxdb/$DATE
zip -r /var/lib/openhab2/backups/influx-backup-$DATE.zip ./*
#delete tmp folder
rm -rf /var/lib/openhab2/backups/tmp_influxdb


############################################
# Sync to Google Drive
############################################
sudo cp -vnpr "/var/lib/openhab2/backups/." "/home/smarty/grive/@Backups/openHAB smarty-nuc"
cd "/home/smarty/grive/"
grive -s "@Backups/openHAB smarty-nuc"

# vim: filetype=sh
6 Likes