I got a local network drive (NAS) backup method working, thought to share.
This is the script that runs when using the backup module in the emoncms browser interface.
$ sudo nano /etc/fstab
modifies linux mount points, I added a line to mount the network drive:
192.168.1.235:/media/astralship/nas/emoncms-backup /mnt/nas nfs async,rw,proto=udp 0 0
This can look very different depending on the system!
I then remounted the drives listed in fstab:
$ sudo mount -a
I had an issue that the drive was not mounted on rPi reboot. This was a problem with systemd doing things in the wrong order. I fixed it following a guide, but ended up installing a sudo cronjob anyway:
$ sudo crontab -e
I added the line:
0 23 * * * mount -a making sure to mount all drives at 11pm each day.
I wanted to have a minimal backup situation, where only the most recent backup file was copied to the network drive, IF it was newer than the NAS backup.
First, I test the backup shell script emoncms-export.sh
The default setting place the archived backup files in
… backup/config.cfg defines the destination of the backup if one wants to place it elsewhere.
At this point I consider pointing this directly to the NAS, but I want a local AND remote backup. @Ryan_S reminds me avoiding excessive SD card writes is a good idea and used a memory stick for a local backup, which in itself can meet all requirements no doubt. He also brings attention to an emonCMS update causing config.cfg to be overwritten. If this file is modified for a custom backup script action consider backing up config.cfg or making it unwritable.
I add the backup script to cron:
$ crontab -e
0 0 * * * /opt/emoncms/modules/backup/emoncms-export.sh
Running the script at midnight.
Then after a good hour of bashing away and searching the internet I came up with this script:
if [ $(ls -t /var/opt/emoncms/backup/emoncms-backup-* | head -1) -nt $(ls -t /mnt/nas/backups/backup* | head -1) ]; then cp $(ls -t /var/opt/emoncms/backup/emoncms-backup-* | head -1) /mnt/nas/backups/backup.tar.gz; fi;
Here I have the conditional if, the operator newer than -nt, then it’s copied to the NAS file called backup.tar.gz, of course, overwritten if it already exists.
I install the cronjob to run one hour after the local backup, midnight again.
$ crontab -e
adding another line after the previous:
0 1 * * * if [ $(ls -t /var/opt/emoncms/backup/emoncms-backup-* | head -1) -nt $(ls -t /mnt/nas/backups/backup* | head -1) ]; then cp $(ls -t /var/opt/emoncms/backup/emoncms-backup-* | head -1) /mnt/nas/backups/backup.tar.gz; fi
The alternative method is rsync. rsync is a useful tool with option to mitigate from excessive writing, but I was getting a lot of permission errors and it looked ugly. Hence making the above script.
Here’s an rsync example crontab to sync the local directory to the remote:
0 0 * * * rsync -azu /var/opt/emoncms/backup /mnt/nas/backups
I delete old local backups with:
rm $(find /home/pi/data/backups/emoncms* -mtime +7)
Removing backups older than 7 days… and I add it to crontab at midnight.
0 0 * * * rm $(find /home/pi/data/backups/emoncms* -mtime +7)
P.S. Thank you stackoverflow.com