Last active
December 29, 2015 16:26
-
-
Save tarcisio/02af8b0aa04b61f2200d to your computer and use it in GitHub Desktop.
backup rsync
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 | |
# reference | |
# http://www.mikerubel.org/computers/rsync_snapshots/#Extensions | |
# CRON | |
# 0 */4 * * * /usr/local/bin/bkp_snapshot.sh >/dev/null 2>&1 | |
unset PATH | |
# ------------- system commands used by this script -------------------- | |
ECHO=/bin/echo | |
RM=/bin/rm | |
MV=/bin/mv | |
CP=/bin/cp | |
TOUCH=/bin/touch | |
RSYNC=/usr/bin/rsync | |
# ------------- file locations ----------------------------------------- | |
BACKUPDIR=/bkp | |
# ------------- the script itself -------------------------------------- | |
# step 1: delete the oldest snapshot, if it exists: | |
if [ -d $BACKUPDIR/home/hourly.3 ] ; then | |
$ECHO "oldest snapshot founded. Deleting!" | |
$RM -rf $BACKUPDIR/home/hourly.3 | |
fi | |
# step 2: shift the middle snapshots(s) back by one, if they exist | |
if [ -d $BACKUPDIR/home/hourly.2 ] ; then | |
$MV $BACKUPDIR/home/hourly.2 $BACKUPDIR/home/hourly.3 | |
fi | |
if [ -d $BACKUPDIR/home/hourly.1 ] ; then | |
$MV $BACKUPDIR/home/hourly.1 $BACKUPDIR/home/hourly.2 | |
fi | |
# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot | |
if [ -d $BACKUPDIR/home/hourly.0 ] ; then | |
$CP -al $BACKUPDIR/home/hourly.0 $BACKUPDIR/home/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 \ | |
/home/ $BACKUPDIR/home/hourly.0 | |
# step 5: update the mtime of hourly.0 to reflect the snapshot time | |
$TOUCH $BACKUPDIR/home/hourly.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment