All,
I typed up a script that will handle backing up my openHAB2 server to Bitbucket daily at 12AM (Midnight). Feel free to modify/use for GitHub or whatever GIT server you use. I went with Bitbucket over GitHub as Bitbucket offers unlimited private repositories for free whereas GitHub wants $7/month for that. Please note that Bitbucket has a 1GB soft cutoff and 2GB hard cutoff per repository, but I’m backing up more than I should and only using less than 400MB for my entire backup.
What’s nice about using GIT for backing up is that you have a very nice overview of what files are being changed. You also have a complete historical backup dating back to when you start. I can’t recommend a remote backup solution of some sort enough. While backing up to another separate device from your openHAB server is sufficient for most cases, I lean toward cloud based storage for things like this as it’s getting to be very reliable nowadays.
One more note. This is a little unorthodox as normally, you update the code in a git repository and then push it down to your server, but I’ve been too lazy to do the whole git thing (make a branch, change the code, merge the branch into master, pull it down on my server) to just change a line or 2. With that said, it works perfectly for my use at the moment. However, soon I will be moving to rsyncing to an NFS share (on my Synology NAS) daily which is then backed up daily as well. I will probably start using my git repository as a code source vs a backup since I have a few other projects that will require git repositories soon.
Ok, so here is my quick write-up:
***I’m running this on a Raspberry Pi 3 running openHAB 2.1 stable installed via the repository installation path.
My directory layout (These locations will be created in the instructions below):
/data/scripts/openhab2backup - backup script
/data/logs/ - backupscript log location
/data/openhab2backup/ - local temporary backup location
***Take note of the ()'s below i.e. (YOUR BITBUCKET ACCOUNT NAME)…
Create a Bitbucket account and a private repository called “openhab2backup”.
Create key on raspberry pi:
mkdir -p /root/.ssh
chmod 0700 /root/.ssh
ssh-keygen -t rsa -C "(YOUR E-MAIL ADDRESS)"
Copy the contents of /root/.ssh/id_rsa.pub (starts with “ssh-rsa”) into the ssh key section for main user (your main account) of Bitbucket (not a deploy key as that’s read only).
Test connection now from raspberry pi:
ssh -T (YOUR BITBUCKET ACCOUNT NAME)@bitbucket.org
Should see:
root@rpi01:~/.ssh# ssh -T (YOUR BITBUCKET ACCOUNT NAME)@bitbucket.org
logged in as (YOUR BITBUCKET ACCOUNT NAME).
You can use git or hg to connect to Bitbucket. Shell access is disabled.
root@rpi01:~/.ssh#
Set default git config:
#I just made up the user.name and user.email. It's not really important.
git config --global user.name "openhab2backup"
git config --global user.email "openhab2backup@auto.com"
git config --global push.default simple
Create Backup Location:
cd /data
git clone git@bitbucket.org:(YOUR BITBUCKET ACCOUNT NAME)/openhab2backup.git
#This will create the git directory - /data/openhab2backup
Make temp local backup and perform first directory sync manually:
mkdir -p /data/openhab2backup/configuration
mkdir -p /data/openhab2backup/userdata
mkdir -p /data/openhab2backup/runtime
rsync -r /etc/openhab2 /data/openhab2backup/configuration
rsync -r /var/lib/openhab2 /data/openhab2backup/userdata
rsync -r /usr/share/openhab2 /data/openhab2backup/runtime
Add untracked directories:
#this will show tracked and untracked files:
git status
cd /data/openhab2backup
git add configuration
git add userdata
git add runtime
Git Commit:
git commit -m “First Push”
Git Push (Can take a while the first time):
git push
Check Bitbucket now to see if code was pushed up.
Create file named “openhab2backup” and paste the contents below. Place in /data/scripts/
#This script will automatically backup the necessary openhab2 configuration files to a secondary location on this server and then push to Bitbucket. Do not rely on this local secondary backup location for a backup as it still resides on the local system.
confdir="/etc/openhab2"
userdatadir="/var/lib/openhab2"
runtimedir="/usr/share/openhab2"
logfolder="/data/logs"
logfilename="oh2backup_$(date +%Y%m%d_%H%M%S)"
oh2backupresult=$logfolder/$logfilename
backuploc="/data/openhab2backup"
#Make log folder if it doesn't already exist and create the file that will contain the backup results.
echo "---------------------------Starting--------------------------" >> $oh2backupresult
mkdir -p $logfolder
touch $oh2backupresult
#Sync Directories:
echo "-----------------Syncing openHab2 Directories----------------" >> $oh2backupresult
/usr/bin/rsync -r $confdir $backuploc/configuration
echo "$confdir --> $backuploc/configuration" >> $oh2backupresult
/usr/bin/rsync -r $userdatadir $backuploc/userdata
echo "$userdatadir --> $backuploc/userdata" >> $oh2backupresult
/usr/bin/rsync -r $runtimedir $backuploc/runtime
echo "$runtimedir --> $backuploc/runtime" >> $oh2backupresult
#Add Untracked Files:
echo "--------------------Adding Untracked Files-------------------" >> $oh2backupresult
cd /data/openhab2backup
/usr/bin/git add configuration
/usr/bin/git add userdata
/usr/bin/git add runtime
#Commit:
/usr/bin/git commit -m "generated files on `date +'%Y-%m-%d %H:%M:%S'`" >> $oh2backupresult
#Push:
echo "---------------------Pushing to Bitbucket--------------------" >> $oh2backupresult
/usr/bin/git push >> $oh2backupresult
echo "-----------------------Backup Complete-----------------------" >> $oh2backupresult
Modify Script Mode to Make Executable
chmod 0755 /data/scripts/openhab2backup
Create Cron:
crontab -e
Paste the following at the bottom and save.
00 00 * * * /data/scripts/openhab2backup