Skip to content

Instantly share code, notes, and snippets.

@sergei-maertens
Last active October 24, 2015 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergei-maertens/ac6b8ee2562edcff10fc to your computer and use it in GitHub Desktop.
Save sergei-maertens/ac6b8ee2562edcff10fc to your computer and use it in GitHub Desktop.
Backup Modelbrouwers
#!/bin/env bash
#
# Adapted from http://webgnuru.com/linux/rsync_incremental.php
#
USER='modelbrouw'
HOST='modelbrouwers.nl'
DAYS_BETWEEN_FULL=30 # number of days between full backups.
TARGET_SUFFIX="media"
TARGET_BASE="/run/media/bbt/40d67de8-f4b6-4707-9947-f96f44efd7bf/"
SOURCE="/home/modelbrouw/django/production/$TARGET_SUFFIX"
today=`date -I`
prev_day=$today # check if backup from today exists
get_target() {
root_dir=$TARGET_BASE$1
if [[ $2 ]]; then
root_dir=$root_dir/$2
fi
echo $root_dir
}
LOG=`get_target $today 'rsync_inc.log'`
# do the date calculations
# determine if a full/incremental backup should be taken
run_backup() {
i=0
PREV_BACKUP=`get_target $prev_day`
while [ $i -lt 100 ] && [ ! -d $PREV_BACKUP ]
do
i=$[i+1]
prev_day=`date -I -d "$i day ago"`
PREV_BACKUP=`get_target $prev_day`
done
if [ $i -eq '0' ]; then
echo "Backup already created"
# exit
else
echo $i 'days since last backup'
fi
# create the target directory
TARGET=`get_target $today`
echo -e 'Backup target:' $TARGET "\n"
mkdir -p $TARGET
if [ $i -lt $DAYS_BETWEEN_FULL ]; then
echo -e "creating incremental backup, based on" $prev_day "\n"
incremental_backup
else
echo -e "creating full backup \n"
source ./backup.sh
fi
}
incremental_backup() {
DEST=`get_target $today $TARGET_SUFFIX`
COMP=`get_target $prev_day $TARGET_SUFFIX`
echo 'remote source directory:' $SOURCE
echo 'backup destination:' $DEST
SSH="-e ssh $USER@$HOST:$SOURCE/"
OPT="-rvLtShP"
echo $LOG
rsync $OPT --link-dest=$COMP $SSH $TARGET/$TARGET_SUFFIX > $LOG
}
run_backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment