Skip to content

Instantly share code, notes, and snippets.

@AysadKozanoglu
Created November 8, 2017 11:22
Show Gist options
  • Save AysadKozanoglu/a937c7a1614c14df0088efec1d1cfae0 to your computer and use it in GitHub Desktop.
Save AysadKozanoglu/a937c7a1614c14df0088efec1d1cfae0 to your computer and use it in GitHub Desktop.
3 days retain backup solutio n for remote server data
#!/bin/bash
#
#author: Aysad Kozanoglu
#
#
# Directory in which to store the backups
ROOT_DIR=/mnt/backup
REMOTE_USER=root
REMOTE_PATH=/mnt/drbd/var/mail/vmail/espresto.com
REMOTE_SERVER=bart
LOG_FILE=/var/log/rsync-backup.log
DATE=$(date '+%Y-%m-%d %H:%M:%S')
# step 1: delete oldest snapshot
if [ -d $ROOT_DIR/daily.3 ]; then
echo "$DATE deleting oldest snaphost ">> $LOG_FILE
rm -rf $ROOT_DIR/daily.3
fi
# step 2: shift middle backups back by one if they exist
if [ -d $ROOT_DIR/daily.2 ]; then
mv $ROOT_DIR/daily.2 $ROOT_DIR/daily.3
fi
if [ -d $ROOT_DIR/daily.1 ]; then
mv $ROOT_DIR/daily.1 $ROOT_DIR/daily.2
fi
# step 3: make a hardlink copy of the latest backup
if [ -d $ROOT_DIR/daily.0 ]; then
cp -al $ROOT_DIR/daily.0 $ROOT_DIR/daily.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!
echo -e "$DATE Begin with rsync \n" >> $LOG_FILE
echo -e "=================================\n" >> $LOG_FILE
echo $DATE >> $LOG_FILE
rsync -avzh --out-format="%t %f %b" --delete $REMOTE_USER@$REMOTE_SERVER:$REMOTE_PATH $ROOT_DIR/daily.0 >> $LOG_FILE 2>&1
# step 5: update the mtime of daily.0 to reflect the snapshot time
mkdir $ROOT_DIR/daily.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment