I got a local network drive (NAS) backup method working, thought to share.
1. Using emoncms-export.sh
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
It’s not backing up to a tidy folder called ‘backups’, so I change ~/backup/config.cfg finding the lines:
Destination location of exported backup .tar.gz - Must be writable
simply creating a new folder and changing it to:
At this point I consider pointing this directly to the NAS, but I want a local AND remote backup.
I add the backup script to cron:
$ crontab -e
0 0 * * * /home/pi/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 ~/data/backups/emoncms-backup-* | head -1) -nt $(ls -t /mnt/nas/backups/backup* | head -1) ]; then cp $(ls -t ~/data/backups/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 0 * * * if [ $(ls -t ~/data/backups/emoncms-backup-* | head -1) -nt $(ls -t /mnt/nas/backups/backup* | head -1) ]; then cp $(ls -t ~/data/backups/emoncms-backup-* | head -1) /mnt/nas/backups/backup.tar.gz; fi
The alternative method is rsync, which only copies files if modified. rsync is a useful tool, but I was getting a lot of permission errors and it looked ugly. Hence making the above script.
Here’s an rsync example crontab:
0 0 * * * rsync -azu /home/pi/data/backups /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
2. Running sudo php backup.php from usefulscripts.
- Creating the dependent folders in the target backup directory.
- Editing the backup.php file.
- Observing the incremental nature of the backup, saving disk space and disk activity.
backup.php.zip (1.6 KB)