RSnapshotting on your own schedule

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.

/usr/local/lib/snapshot/dequeue

#! /bin/sh

QUEUEFILE=/var/lib/snapshot/queue
LOCKFILE=/var/lib/snapshot/LCK..queue

lockfile-create $LOCKFILE
if ! test -f $QUEUEFILE || ! grep -Fqs -- "$*" $QUEUEFILE ; then
  echo $* >> $QUEUEFILE
fi
lockfile-remove $LOCKFILE

/usr/local/lib/snapshot/dequeue

#! /bin/sh

SNAPDIR=/var/cache/rsnapshot
QUEUEFILE=/var/lib/snapshot/queue
LOCKFILE=/var/lib/snapshot/LCK..queue
PIDFILE=/var/run/snapshot.pid

test -f $PIDFILE && exit 0
test -s $QUEUEFILE || exit 0

echo $$ > $PIDFILE

was_mounted=0
if egrep -qs "\\B$SNAPDIR\\b" /etc/mtab; then
  was_mounted=1
  mount -o remount,rw $SNAPDIR 2> /dev/null
else
  mount -o rw $SNAPDIR 2> /dev/null
fi

if [ $? != 0 ]; then
  rm -f $PIDFILE
  exit 0
fi

cleanup ()
{
  if [ $was_mounted == 1 ]; then
    mount -o remount,ro $SNAPDIR
  else
    umount $SNAPDIR
  fi
  rm -f $PIDFILE
}

trap "cleanup" EXIT TERM INT

while true; do
  lockfile-create $LOCKFILE
  if [ -f $QUEUEFILE ] ; then
    ARGS=`head -n1 $QUEUEFILE`
    sed -i '1d' $QUEUEFILE
  fi
  lockfile-remove $LOCKFILE

  if [ -n "$ARGS" ]; then
    nice rsnapshot $ARGS
  else
    break
  fi

done

exit 0

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.

/etc/cron.d/snapshot

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