Automatic backup

@TrystanLea

Whilst this is not an automated backup solution to USB, it may be a starting point?
I’m running the July 2020 emon image updated to v10.2.6 on a Raspberry Pi.
Thanks to you OEM guys there is an export backup script that can be run from the Admin webpage or from an SSH terminal with the command …

./opt/emoncms/modules/backup/emoncms-export.sh

This saves the backup to the SDHC.
I’ve taken it a bit further by then copying the backup to a USB flash drive. Excuse any verbosity as I’ve just copied & pasted from my Build Notes

A 32Gb or 64Gb flash drive is suitable but it should be USB 3.0 spec for the write speed.

The first task is to prepare the USB flash drive and to ensure it is correctly remounted at every reboot …

Do   sudo mkdir /mnt/BACKUP
Do   sudo chmod -R 777 /mnt/BACKUP      … to add full write permissions
Plug in the USB Flash Drive
Do     sudo blkid     … which will identify the USB Flash Drive as being /dev/sda1
It is necessary to use the UUID=xxxxxxxxx form of drive identification and so make a copy note of    UUID=xxxxxxxxx
Now create a filesystem on the USB Flash Drive …
Do     sudo mkfs -t ext4 /dev/sda1
Then check it with     sudo fsck /dev/sda1
Now make changes to /etc/fstab so the USB Flash Drive filesystem is mounted at start up/reboot … must use the UUID identification …
Copy the original fstab into the first (/boot) partition of the SDHC (as a precaution) …
sudo cp /etc/fstab /boot/fstab.ORIGINAL         then …
sudo cp /boot/fstab.ORIGINAL /boot/fstab.NEW       ready for some changes …
Do   sudo nano /boot/fstab.NEW       and add the following line …

UUID=xxxxxxxxx    /mnt/BACKUP   ext4   defaults,noatime,nodiratime  0    2
Save & exit
Finally …
Do     sudo cp /boot/fstab.NEW /etc/fstab       
And then    sudo reboot
After the reboot,    df -hT    should reveal the USB Flash Drive is mounted

Admittedly that’s all a bit of a pain but you only have to do it once.

Finally, create the enhanced backup script which makes an actual Backup on the SDHC and then copies it to the USB Flash Drive

Do   sudo nano /home/pi/regular.backup.sh
Then enter the following and save/exit …

#!/bin/bash
cd /opt/emoncms/modules/backup
./emoncms-export.sh
sleep 5
sudo rsync -axv /var/opt/emoncms/ /mnt/BACKUP
exit

Make the script executable with …  
 sudo chmod a+x /home/pi/regular.backup.sh
You can now do a backup at any time with … 

./home/pi/regular.backup.sh

Or the script can be incorporated into a cronjob.
To set up a cronjob which runs at 1 minute after midnight each Sunday:
Do     crontab -e   and add the following and then save/exit …

#
1 0 * * sun /home/pi/regular.backup.sh

The script will not survive a major emon image update.
It’s been running successfully for me for months together with an enhancement – sends an email to say each backup successfully happened.
And I’ve never yet needed to use the backup because of an SDHC failure – famous last words?
This may be of interest.