Easy backup to remote share (e.g. NAS)

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
jerry
Posts: 3
Joined: Saturday 04 November 2017 12:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Easy backup to remote share (e.g. NAS)

Post by jerry »

Hi all,

I have created a solution to backup files and folders to a remote CIFS file share (in my case Synology). It does not require FTP and is very easy to use. I could not find an easy manual to do this, so here you go. Basic Linux knowledge is required.

Step 1: create a file share on your target device (e.g. Synology, QNAP or Windows) and create a user with write permissions. There are plenty of manuals how to do this. My remote folder is called \\diskstation\User\domoticz

Step 2: create a folder on your Domoticz server to mount the share (I use /diskstation):

Code: Select all

sudo mkdir /diskstation
Step 3: mount the remote share on your domoticz server (I have a pi). The best way is to add this line to /etc/fstab (do this as root!) so it is reconnected after a reboot:

Code: Select all

sudo vi /etc/fstab
add this line to the bottom (use your own pathnames and username/password):

Code: Select all

//192.168.1.100/User/domoticz   /diskstation    cifs    _netdev,username=<yourusername>,password=<yourpassword>,dir_mode=0755,file_mode=0755,uid=1000,gid=1000     0       0
mount the share:

Code: Select all

sudo mount /diskstation
check if you can access the share and create a subfolder (use the following command one by one and check output):

Code: Select all

mount /diskstation
ls -l /diskstation
touch /diskstation/testfile
ls -l /diskstation
rm /diskstation/testfile
mkdir /diskstation/backup
Step 4: I use a small script to backup the database and scripts. It is very easy to backup more files/folders:

Code: Select all

vi ~/backup_domoticz.sh
The script I use:

Code: Select all

#!/bin/bash
DESTDIR="/diskstation/backup"
DOMO_IP="192.168.1.99"  # Domoticz IP
DOMO_PORT="8080"        # Domoticz port

/usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /$DESTDIR/domoticz_backup.db
cp -r /home/pi/domoticz/scripts/ $DESTDIR
Step 5: Add the script to cron (note: the scripts does not require root in my config, but you may need to add sudo):

Code: Select all

crontab -e
and add the line:

Code: Select all

50 23 * * * ~/backup_domoticz.sh
And you're done. Hope you like it!
Smiggel
Posts: 36
Joined: Friday 23 March 2018 17:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Contact:

Re: Easy backup to remote share (e.g. NAS)

Post by Smiggel »

Thanks for the tutorial! It works excellent. :-)
jake
Posts: 742
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Easy backup to remote share (e.g. NAS)

Post by jake »

Thanks for sharing the script and the how-to!
Do I need to make the script executable? If so, how?
DAVIZINHO
Posts: 234
Joined: Sunday 27 August 2017 18:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Spain
Contact:

Re: Easy backup to remote share (e.g. NAS)

Post by DAVIZINHO »

great job!
aditionally if you want to remove files older than X days you can use this:

Code: Select all

find /synobackups/* -mtime +30 -exec rm {} \;
when /synobackups/ is the folder thant i mount and 30 is the X days you want to delete
jeroennus
Posts: 2
Joined: Monday 28 May 2018 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Easy backup to remote share (e.g. NAS)

Post by jeroennus »

After almost losing all my script en config this week I tried a couple of backup solutions. Liked this one the most because I don't have to use FTP on my Qnap. Thanks for posting this!!
knielen
Posts: 46
Joined: Sunday 10 September 2017 9:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Easy backup to remote share (e.g. NAS)

Post by knielen »

Great tool!

The script now generates the same filename every day, so you do an override on your existing files. If you want to have a new file every day you have to add the date to the filename;

Code: Select all

/usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /$DESTDIR/domoticz_backup_$(date +"%Y%m%d").db
Or whole script with deleting older files:

Code: Select all

#!/bin/bash
DESTDIR="/diskstation/backup"
DOMO_IP="192.168.1.99"  # Domoticz IP
DOMO_PORT="8080"        # Domoticz port

/usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /$DESTDIR/domoticz_backup_$(date +"%Y%m%d").db
cp -r /home/pi/domoticz/scripts/ $DESTDIR
find $DESTDIR/* -mtime +30 -exec rm {} \;
User avatar
HansieNL
Posts: 957
Joined: Monday 28 September 2015 15:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Easy backup to remote share (e.g. NAS)

Post by HansieNL »

I'm using a NFS share and use the following.
domoticz_backup:

Code: Select all

#!/bin/bash
### PLACE THIS SCRIPT IN:/home/pi/domoticz/scripts
DOMO_IP="192.168.1.11"  # Domoticz IP 
DOMO_PORT="8181"        # Domoticz port 
### END OF USER CONFIGURABLE PARAMETERS
TIMESTAMP=`/bin/date +%Y%m%d%H%M`

BACKUPDBFILE="domoticz_$TIMESTAMP.db"
BACKUPFILES="domoticz_$TIMESTAMP.tar.gz"
 
# Create backup and make tar archives
/usr/bin/curl -s http://$DOMO_IP:$DOMO_PORT/backupdatabase.php > /mnt/backupz/$BACKUPDBFILE
sudo tar --exclude='domoticz.db-shm' --exclude='domoticz.db-wal' -zcvf /mnt/backupz/$BACKUPFILES -T /home/pi/domoticz/scripts/domoticz_backup.lst
 
# Delete backups older than 14 days
/usr/bin/find /mnt/backupz/ -name '*.db' -mtime +14 -delete
/usr/bin/find /mnt/backupz/ -name 'domoticz_*.tar.gz' -mtime +14 -delete
I've made a list of most important files I wanna backup too.
domoticz_backup.lst:

Code: Select all

/etc/fail2ban/filter.d/domoticz.conf
/etc/fail2ban/jail.local
/etc/fstab
/etc/init.d/domoticz.sh
/etc/iptables.firewall.rules
/etc/logrotate.d/domoticz
/etc/monit/monitrc
/etc/network/if-pre-up.d/firewall
/var/spool/cron/crontabs/pi
/etc/nginx/sites-available/default
/etc/motd
/home/pi
And of course added a crontab job:

Code: Select all

0 3 * * * sudo bash /home/pi/domoticz/scripts/domoticz_backup.sh
Blah blah blah
User avatar
andrehj
Posts: 44
Joined: Sunday 06 January 2019 14:26
Target OS: -
Domoticz version:
Contact:

Re: Easy backup to remote share (e.g. NAS)

Post by andrehj »

Thanks @jerry for the script. I do run into a few problems:

1. The script has to be made executable with chmod +x backup_domoticz.sh
2. The file domoticz_backup.db which is created on my NAS is 12MB, much smaller than domoticz.db (39 MB) on my Pi. I wonder if everything gets backupped?
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests