Hello all,
I am trying to implement a better backup solution than I previously had, and my attempt at setting up Amanda previously was a failure (backups were empty, and huge time in setting it up in the first place) so I am opting for more familiar and tangible options.
Background:
Raspberry Pi4 4gb running openhabian 2.5.3, on same network as ubuntu system (referenced by the ip address in the script).
I’ve already created a user on the ubuntu system (also named openhabian), created an ssh key on my raspberry pi and successfully installed it on my ubuntu system. I have successfully run this entire script manually (including the rsync portion), with the correct files showing up on the ubuntu system.
I have already set-up a crontab entry, to be run at 4AM on the first day of the week every week (I believe)
0 4 * * 1 sh /home/openhabian/backupall
but I am expecting a permissions issue.
1. My SSH key is from the raspberry pi ‘openhabian’ user. Cron will probably not run the script as that user. It isn’t too much difficulty to add an ssh key for another user on the pi, if this is an option…
2. The script also needs to be run as sudo, to allow for the openhab-cli backup command to be run correctly. I am not sure how to best run this.
Any suggestions?
For reference the script is below, thanks to @tailor , adapted from his Original Post
backupall
#!/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 ... end distination
############################################
echo "+-+-+-+-+-+-+-+ RSync to MServer +-+-+-+-+-+-+-+"
sudo cp -vnpr "/var/lib/openhab2/backups/." "/home/openhabian/backups/openHAB openhabian-nuc"
rsync -a /home/openhabian/backup openhabian@192.168.0.100:/home/openhabian
echo "================== Done ==================="