Saturday, December 13, 2008

rsync advanced backup script for structured incremental backups (updated 2)


#!/bin/bash
# Copyright (C) 2008 Nayanajit Mahendra Laxaman mail: nmlaxaman@gmail.com

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

#Enable disable daily weekly monthly yearly automated backup
DAILY=true
WEEKLY=true
MONTHLY=true
YEARLY=true

#Set the number of backups to keep in each level
DAYS_TO_KEEP=7
WEEKS_TO_KEEP=4
MONTHS_TO_KEEP=4
YEARS_TO_KEEP=2

#Relative backup directories for each level
DAILY_BAK=daily
WEEKLY_BAK=weekly
MONTHLY_BAK=monthly
YEARLY_BAK=yearly

#Source(s) of backup directory
BACKUP_SOURCE=/tmp/

#Destination of backup
BACKUP_DEST=.

DAY=`date +%A`
TOMORROW_DATE=`date --date="next day" +%d`;
TOMORROW_MONTH=`date --date="next day" +%m`;
YESTERDAY=`date --date="yesterday" +%d-%m-%Y`;

END_OF_WEEK=false
END_OF_MONTH=false
END_OF_YEAR=false

[[ $DAY == Friday ]] && END_OF_WEEK=true;
[[ $TOMORROW_DATE == 01 ]] && END_OF_MONTH=true;
[[ $TOMORROW_DATE == 01 ]] && [[ $TOMORROW_MONTH == 01 ]] && END_OF_YEAR=true;

[[ -d $DAILY_BAK ]] || mkdir daily
[[ -d $WEEKLY_BAK ]] || mkdir weekly
[[ -d $MONTHLY_BAK ]] || mkdir monthly
[[ -d $YEARLY_BAK ]] || mkdir yearly

function debug(){
echo "DAILY:$DAILY
MONTHLY:$MONTHLY
YEARLY:$YEARLY
DAY:$DAY
TOMORROW_DATE:$TOMORROW_DATE
TOMORROW_MONTH:$TOMORROW_MONTH
YESTERDAY:$YESTERDAY
END_OF_MONTH:$END_OF_MONTH
END_OF_YEAR:$END_OF_YEAR"
}

#debug

cd $BACKUP_DEST

TODAY_BACKUP=`date +%d-%m-%y`;
YESTERDAY_BACKUP=$DAILY_BAK/$YESTERDAY;
LASTWEEK_BACKUP=$WEEKLY_BAK/$( ls -tr $WEEKLY_BAK/ | tail -1 )
LASTMONTH_BACKUP=$MONTHLY_BAK/$( ls -tr $MONTHLY_BAK/ | tail -1 )
LASTYEAR_BACKUP=$YEARLY_BAK/$( ls -tr $YEARLY_BAK/ | tail -1 )

if $YEARLY && $END_OF_YEAR; then
echo in
[[ -d $LASTYEAR_BACKUP ]] && cp -al $LASTYEAR_BACKUP/ $YEARLY_BAK/$TODAY_BACKUP
rsync -a --delete $BACKUP_SOURCE $YEARLY_BAK/$TODAY_BACKUP
expr $( ls $YEARLY_BAK | wc -l ) \> $YEARS_TO_KEEP && rm -fr $( ls -t $YEARLY_BAK/ | tail -1 )
exit 0
fi

if $MONTHLY && $END_OF_MONTH; then
[[ -d $LASTMONTH_BACKUP ]] && cp -al $LASTMONTH_BACKUP/ $MONTHLY_BAK/$TODAY_BACKUP
rsync -a --delete $BACKUP_SOURCE $MONTHLY_BAK/$TODAY_BACKUP
expr $( ls $MONTHLY_BAK | wc -l ) \> $MONTHS_TO_KEEP && rm -fr $( ls -t $MONTHLY_BAK/ | tail -1 )
exit 0
fi

if $WEEKLY && $END_OF_WEEK; then
[[ -d $LASTWEEK_BACKUP ]] && cp -al $LASTWEEK_BACKUP/ $WEEKLY_BAK/$TODAY_BACKUP
rsync -a --delete $BACKUP_SOURCE $WEEKLY_BAK/$TODAY_BACKUP
expr $( ls $WEEKLY_BAK | wc -l ) \> $WEEKS_TO_KEEP && rm -fr $( ls -t $WEEKLY_BAK/ | tail -1 )
exit 0
fi

if $DAILY; then
[[ -d $YESTERDAY_BACKUP ]] && cp -al $YESTERDAY_BACKUP/ $DAILY_BAK/$TODAY_BACKUP
rsync -a --delete $BACKUP_SOURCE $DAILY_BAK/$TODAY_BACKUP
expr $( ls $DAILY_BAK | wc -l ) \> $DAYS_TO_KEEP && rm -fr $( ls -t $DAILY_BAK/ | tail -1 )
fi





See bash script to keep incremental backup for backing up to remote server

No comments:

Post a Comment