{"id":2988,"date":"2016-02-24T20:11:10","date_gmt":"2016-02-24T20:11:10","guid":{"rendered":"https:\/\/qbytes.cloud\/?p=2988"},"modified":"2016-02-24T20:11:10","modified_gmt":"2016-02-24T20:11:10","slug":"incremental-backup-script","status":"publish","type":"post","link":"https:\/\/www.qbytes.cloud\/index.php\/2016\/02\/24\/incremental-backup-script\/","title":{"rendered":"Incremental backup script"},"content":{"rendered":"<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\n# ----------------------------------------------------------------------\n# mikes handy rotating-filesystem-snapshot utility\n# ----------------------------------------------------------------------\n# this needs to be a lot more general, but the basic idea is it makes\n# rotating backup-snapshots of \/home whenever called\n# ----------------------------------------------------------------------\n\nunset PATH  # suggestion from H. Milz: avoid accidental use of $PATH\n\n# ------------- system commands used by this script --------------------\nID=\/usr\/bin\/id;\nECHO=\/bin\/echo;\n\nMOUNT=\/bin\/mount;\nRM=\/bin\/rm;\nMV=\/bin\/mv;\nCP=\/bin\/cp;\nTOUCH=\/bin\/touch;\n\nRSYNC=\/usr\/bin\/rsync;\n\n\n# ------------- file locations -----------------------------------------\n\nMOUNT_DEVICE=\/dev\/hdb1;\nSNAPSHOT_RW=\/root\/snapshot;\nEXCLUDES=\/usr\/local\/etc\/backup_exclude;\n\n\n# ------------- the script itself --------------------------------------\n\n# make sure we&#039;re running as root\nif (( `$ID -u` != 0 )); then { $ECHO &quot;Sorry, must be root.  Exiting...&quot;; exit; } fi\n\n# attempt to remount the RW mount point as RW; else abort\n$MOUNT -o remount,rw $MOUNT_DEVICE $SNAPSHOT_RW ;\nif (( $? )); then\n{\n    $ECHO &quot;snapshot: could not remount $SNAPSHOT_RW readwrite&quot;;\n    exit;\n}\nfi;\n\n\n# rotating snapshots of \/home (fixme: this should be more general)\n\n# step 1: delete the oldest snapshot, if it exists:\nif &#x5B; -d $SNAPSHOT_RW\/home\/hourly.3 ] ; then         \\\n$RM -rf $SNAPSHOT_RW\/home\/hourly.3 ;                \\\nfi ;\n\n# step 2: shift the middle snapshots(s) back by one, if they exist\nif &#x5B; -d $SNAPSHOT_RW\/home\/hourly.2 ] ; then         \\\n$MV $SNAPSHOT_RW\/home\/hourly.2 $SNAPSHOT_RW\/home\/hourly.3 ; \\\nfi;\nif &#x5B; -d $SNAPSHOT_RW\/home\/hourly.1 ] ; then         \\\n$MV $SNAPSHOT_RW\/home\/hourly.1 $SNAPSHOT_RW\/home\/hourly.2 ; \\\nfi;\n\n# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot,\n# if that exists\nif &#x5B; -d $SNAPSHOT_RW\/home\/hourly.0 ] ; then         \\\n$CP -al $SNAPSHOT_RW\/home\/hourly.0 $SNAPSHOT_RW\/home\/hourly.1 ; \\\nfi;\n\n# step 4: rsync from the system into the latest snapshot (notice that\n# rsync behaves like cp --remove-destination by default, so the destination\n# is unlinked first.  If it were not so, this would copy over the other\n# snapshot(s) too!\n$RSYNC                              \\\n    -va --delete --delete-excluded              \\\n    --exclude-from=&quot;$EXCLUDES&quot;              \\\n    \/home\/ $SNAPSHOT_RW\/home\/hourly.0 ;\n\n# step 5: update the mtime of hourly.0 to reflect the snapshot time\n$TOUCH $SNAPSHOT_RW\/home\/hourly.0 ;\n\n# and thats it for home.\n\n# now remount the RW snapshot mountpoint as readonly\n\n$MOUNT -o remount,ro $MOUNT_DEVICE $SNAPSHOT_RW ;\nif (( $? )); then\n{\n    $ECHO &quot;snapshot: could not remount $SNAPSHOT_RW readonly&quot;;\n    exit;\n} fi;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#!\/bin\/bash # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- # mikes handy rotating-filesystem-snapshot utility # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- # this needs to be a lot more general, but the basic idea is it makes # rotating backup-snapshots of \/home whenever called # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- unset PATH # suggestion from H. Milz: avoid accidental use of $PATH # &#8212;&#8212;&#8212;&#8212;- system commands used by this script &#8230; <a title=\"Incremental backup script\" class=\"read-more\" href=\"https:\/\/www.qbytes.cloud\/index.php\/2016\/02\/24\/incremental-backup-script\/\" aria-label=\"Read more about Incremental backup script\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-2988","post","type-post","status-publish","format-standard","hentry","category-shell-scripts"],"_links":{"self":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/posts\/2988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/comments?post=2988"}],"version-history":[{"count":0,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/posts\/2988\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/media?parent=2988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/categories?post=2988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/tags?post=2988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}