I’m using rsnapshot for making regular backups of my Linux box. Neither the computer, nor the external backup disk is running all the time, therefore it would not work to simply have cron run rsnapshot at fixed times.
I want a little more flexibility than that. When the computer is off, there’s no point in making a snapshot. Fine, there’s no work left to handle that case. But in cases where the computer is running, but the backup disk is not, I want it to be recorded that something should have been done–if only the disk had been on. Then, next time the disk is running again, the recorded (queued) tasks are executed in order.
As there are multiple processes involved, for enqueueing tasks and for dequeueing them, and as the later can take some time, there’s a bit of mutual exclusing involved. Luckily, Debian GNU/Linux provides very useful helper programs in the lockfile-progs package.
if ! test -f $QUEUEFILE || ! grep -Fqs -- "$*" $QUEUEFILE ; then
echo $* >> $QUEUEFILE
test -f $PIDFILE && exit 0
test -s $QUEUEFILE || exit 0
echo $$ > $PIDFILE
if egrep -qs "\\B$SNAPDIR\\b" /etc/mtab; then
mount -o remount,rw $SNAPDIR 2> /dev/null
mount -o rw $SNAPDIR 2> /dev/null
if [ $? != 0 ]; then
rm -f $PIDFILE
if [ $was_mounted == 1 ]; then
mount -o remount,ro $SNAPDIR
rm -f $PIDFILE
trap "cleanup" EXIT TERM INT
while true; do
if [ -f $QUEUEFILE ] ; then
ARGS=`head -n1 $QUEUEFILE`
sed -i '1d' $QUEUEFILE
if [ -n "$ARGS" ]; then
nice rsnapshot $ARGS
Things are tied together by several cron jobs that define when things should, if possible, happen. Of course, this is highly dependent on the individual rsnapshot configuration.
7 12,20 * * * root /usr/local/lib/snapshot/enqueue hourly
11 20 * * * root /usr/local/lib/snapshot/enqueue daily
13 20 * * 1 root /usr/local/lib/snapshot/enqueue weekly
17 20 1 * * root /usr/local/lib/snapshot/enqueue monthly
*/5 * * * * root /usr/local/lib/snapshot/dequeue