Last active
August 29, 2015 13:56
-
-
Save tomnomnom/8871769 to your computer and use it in GitHub Desktop.
Rsync with --link-dest wrapper
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 | |
DATE_FMT="%Y-%m-%dT%H:%M:%S" | |
BACKUP_PREFIX="backup" | |
E_ARGS=1 | |
E_NO_SOURCE_DIR=2 | |
E_NO_DEST_DIR=3 | |
E_RSYNC_FAIL=4 | |
if [ $# -lt 2 ]; then | |
echo "Usage: ${0} <source dir> <dest dir>" | |
exit $E_ARGS | |
fi | |
SOURCE_DIR=${1%/} | |
DEST_DIR=${2%/} | |
if [ ! -d $SOURCE_DIR ]; then | |
echo "'$SOURCE_DIR' is not a directory" | |
exit $E_NO_SOURCE_DIR | |
fi | |
if [ ! -d $DEST_DIR ]; then | |
echo "'$DEST_DIR' is not a directory" | |
exit $E_NO_DEST_DIR | |
fi | |
SOURCE_DIR=$(readlink -f $SOURCE_DIR) | |
DEST_DIR=$(readlink -f $DEST_DIR) | |
DATE_STR=$(date "+$DATE_FMT") | |
BACKUP_DIR_NAME="$BACKUP_PREFIX-$DATE_STR" | |
# If there's no 'current' backup then we need to make an initial backup | |
CURRENT_LINK="$DEST_DIR/current" | |
if [ ! -h "$CURRENT_LINK" ]; then | |
echo "Creating initial backup in $DEST_DIR/$BACKUP_DIR_NAME" | |
rsync -aP "$SOURCE_DIR/" "$DEST_DIR/$BACKUP_DIR_NAME" | |
if [ $? -ne 0 ]; then | |
echo "Rsync failed; aborting" | |
exit $E_RSYNC_FAIL | |
fi | |
else | |
PREV_BACKUP_DIR_NAME=$(readlink -f $CURRENT_LINK) | |
echo "Creating incremental backup using '$PREV_BACKUP_DIR_NAME' as a base" | |
rsync -aP --link-dest="$PREV_BACKUP_DIR_NAME" "$SOURCE_DIR/" "$DEST_DIR/$BACKUP_DIR_NAME" | |
if [ $? -ne 0 ]; then | |
echo "Rsync failed; aborting" | |
exit $E_RSYNC_FAIL | |
fi | |
unlink "$CURRENT_LINK" | |
fi | |
# Set the 'current' backup | |
ln -s $BACKUP_DIR_NAME $DEST_DIR/current | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment