I decided to improve a little bit on the already excellent solutions everybody else offered.
This is my first ever bash script, if you find something wrong with it, please let me know.
Code:
#!/bin/bash
SRCEFILE=/data/freenas-v1.db
DESTDIR=/mnt/myvolume/mydataset/configbackups
DESTARCHIVEFILE=${DESTDIR}/freenas-v1_$(date +%Y%m%d%H%M%S).db
DESTLASTFILE=${DESTDIR}/freenas-v1_last.db
#if the destination file does not exist or md5 does not match
if [ ! -f ${DESTLASTFILE} ] || [ $(/sbin/md5 -q ${SRCEFILE}) != $(/sbin/md5 -q ${DESTLASTFILE}) ]; then
/bin/cp ${SRCEFILE} ${DESTARCHIVEFILE};
/bin/cp -f ${SRCEFILE} ${DESTLASTFILE};
fi
Here is how it works. The latest version of the configuration is saved into
freenas-v1_last.db file. The history of old versions is saved in files with corresponding date and time appended.
Before copying, the script compares md5 hash of the current configuration file with the last saved; and only makes copies if the hash (and, by extension, the configuration) differs.
Modify the DESTDIR, in the script, to match where you want to store configuration backups. You can save the script into a file on one of your datasets somewhere (make sure the file has execute permission), and call it from a
chron, as explained in the first post of this thread, only instead of “sh” put “bash”. I.e. the command becomes:
bash /mnt/myvolume/mydataset/scripts/cfgbackup.sh