{"id":4,"date":"2006-11-26T02:36:48","date_gmt":"2006-11-26T01:36:48","guid":{"rendered":"http:\/\/schuerig.de\/michael\/blog\/?p=4"},"modified":"2021-11-24T09:20:10","modified_gmt":"2021-11-24T08:20:10","slug":"rsnapshotting-on-your-own-schedule","status":"publish","type":"post","link":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/2006\/11\/26\/rsnapshotting-on-your-own-schedule\/","title":{"rendered":"RSnapshotting on your own schedule"},"content":{"rendered":"<p>I&#8217;m using <a href=\"http:\/\/www.rsnapshot.org\/\">rsnapshot<\/a> 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 <tt>cron<\/tt> run <tt>rsnapshot<\/tt> at fixed times.<\/p>\n<p>I want a little more flexibility than that. When the computer is off, there&#8217;s no point in making a snapshot. Fine, there&#8217;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&#8211;if only the disk had been on. Then, next time the disk is running again, the recorded (queued) tasks are executed in order.<\/p>\n<p>As there are multiple processes involved, for enqueueing tasks and for dequeueing them, and as the later can take some time, there&#8217;s a bit of mutual exclusing involved. Luckily, Debian GNU\/Linux provides very useful helper programs in the <tt>lockfile-progs<\/tt> package.<\/p>\n<p><strong>\/usr\/local\/lib\/snapshot\/dequeue<\/strong><br \/>\n<code><\/p>\n<pre>\r\n#! \/bin\/sh\r\n\r\nQUEUEFILE=\/var\/lib\/snapshot\/queue\r\nLOCKFILE=\/var\/lib\/snapshot\/LCK..queue\r\n\r\nlockfile-create $LOCKFILE\r\nif ! test -f $QUEUEFILE || ! grep -Fqs -- \"$*\" $QUEUEFILE ; then\r\n  echo $* >> $QUEUEFILE\r\nfi\r\nlockfile-remove $LOCKFILE\r\n<\/pre>\n<p><\/code><\/p>\n<p><strong>\/usr\/local\/lib\/snapshot\/dequeue<\/strong><br \/>\n<code><\/p>\n<pre>\r\n#! \/bin\/sh\r\n\r\nSNAPDIR=\/var\/cache\/rsnapshot\r\nQUEUEFILE=\/var\/lib\/snapshot\/queue\r\nLOCKFILE=\/var\/lib\/snapshot\/LCK..queue\r\nPIDFILE=\/var\/run\/snapshot.pid\r\n\r\ntest -f $PIDFILE && exit 0\r\ntest -s $QUEUEFILE || exit 0\r\n\r\necho $$ > $PIDFILE\r\n\r\nwas_mounted=0\r\nif egrep -qs \"\\\\B$SNAPDIR\\\\b\" \/etc\/mtab; then\r\n  was_mounted=1\r\n  mount -o remount,rw $SNAPDIR 2> \/dev\/null\r\nelse\r\n  mount -o rw $SNAPDIR 2> \/dev\/null\r\nfi\r\n\r\nif [ $? != 0 ]; then\r\n  rm -f $PIDFILE\r\n  exit 0\r\nfi\r\n\r\ncleanup ()\r\n{\r\n  if [ $was_mounted == 1 ]; then\r\n    mount -o remount,ro $SNAPDIR\r\n  else\r\n    umount $SNAPDIR\r\n  fi\r\n  rm -f $PIDFILE\r\n}\r\n\r\ntrap \"cleanup\" EXIT TERM INT\r\n\r\nwhile true; do\r\n  lockfile-create $LOCKFILE\r\n  if [ -f $QUEUEFILE ] ; then\r\n    ARGS=`head -n1 $QUEUEFILE`\r\n    sed -i '1d' $QUEUEFILE\r\n  fi\r\n  lockfile-remove $LOCKFILE\r\n\r\n  if [ -n \"$ARGS\" ]; then\r\n    nice rsnapshot $ARGS\r\n  else\r\n    break\r\n  fi\r\n\r\ndone\r\n\r\nexit 0\r\n<\/pre>\n<p><\/code><\/p>\n<p>Things are tied together by several <tt>cron<\/tt> jobs that define when things should, if possible, happen. Of course, this is highly dependent on the individual <tt>rsnapshot<\/tt> configuration.<\/p>\n<p><strong>\/etc\/cron.d\/snapshot<\/strong><br \/>\n<code><\/p>\n<pre>\r\n7   12,20       * * *   root    \/usr\/local\/lib\/snapshot\/enqueue hourly\r\n11  20          * * *   root    \/usr\/local\/lib\/snapshot\/enqueue daily\r\n13  20          * * 1   root    \/usr\/local\/lib\/snapshot\/enqueue weekly\r\n17  20          1 * *   root    \/usr\/local\/lib\/snapshot\/enqueue monthly\r\n*\/5  *          * * *   root    \/usr\/local\/lib\/snapshot\/dequeue\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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 &hellip; <a href=\"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/2006\/11\/26\/rsnapshotting-on-your-own-schedule\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,2,3],"tags":[],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-administration","category-linux","category-shell"],"_links":{"self":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":1,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":153,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/4\/revisions\/153"}],"wp:attachment":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}