Created
May 30, 2018 11:13
-
-
Save herry13/d24c36da778579c59135b556cfc381f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ---------------------------------------------------------------------- | |
# mikes handy rotating-filesystem-snapshot utility | |
# ---------------------------------------------------------------------- | |
# this needs to be a lot more general, but the basic idea is it makes | |
# rotating backup-snapshots of /home whenever called | |
# ---------------------------------------------------------------------- | |
# Reference: | |
# - http://www.mikerubel.org/computers/rsync_snapshots/#Extensions | |
# ---------------------------------------------------------------------- | |
unset PATH # suggestion from H. Milz: avoid accidental use of $PATH | |
# ------------- system commands used by this script -------------------- | |
ID=/usr/bin/id; | |
ECHO=/bin/echo; | |
MOUNT=/bin/mount; | |
RM=/bin/rm; | |
MV=/bin/mv; | |
CP=/bin/cp; | |
TOUCH=/bin/touch; | |
RSYNC=/usr/bin/rsync; | |
# ------------- file locations ----------------------------------------- | |
REMOTE_LOCATION="user@remote.com:/path/to/source" | |
BACKUP_LOCATION="/path/to/backup/dir" | |
# ------------- the script itself -------------------------------------- | |
# rotating snapshots | |
# step 1: delete the oldest snapshot, if it exists: | |
if [ -d $BACKUP_LOCATION/hourly.3 ] ; then \ | |
$RM -rf $BACKUP_LOCATION/hourly.3 ; \ | |
fi ; | |
# step 2: shift the middle snapshots(s) back by one, if they exist | |
if [ -d $BACKUP_LOCATION/hourly.2 ] ; then \ | |
$MV $BACKUP_LOCATION/hourly.2 $BACKUP_LOCATION/hourly.3 ; \ | |
fi; | |
if [ -d $BACKUP_LOCATION/hourly.1 ] ; then \ | |
$MV $BACKUP_LOCATION/hourly.1 $BACKUP_LOCATION/hourly.2 ; \ | |
fi; | |
# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot, | |
# if that exists | |
if [ -d $BACKUP_LOCATION/hourly.0 ] ; then \ | |
$CP -al $BACKUP_LOCATION/hourly.0 $BACKUP_LOCATION/hourly.1 ; \ | |
fi; | |
# step 4: rsync from the system into the latest snapshot (notice that | |
# rsync behaves like cp --remove-destination by default, so the destination | |
# is unlinked first. If it were not so, this would copy over the other | |
# snapshot(s) too! | |
$RSYNC \ | |
-va --delete \ | |
-e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" \ | |
$REMOTE_LOCATION $BACKUP_LOCATION/hourly.0 ; | |
# step 5: update the mtime of hourly.0 to reflect the snapshot time | |
$TOUCH $BACKUP_LOCATION/hourly.0 ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment