Wednesday, December 17, 2008

Advanced structured incremental backup system with rsync

Tree scripts associated with this setup
1. backup_cs.sh
The main script which runs in client server (client of the rsync server). Which first script 'backup_ss_before.sh' in server and do the syncing process and run 'backup_ss_after.sh' script and finish the backup.
2. backup_ss_before.sh
The script which performing required background setting of the backup location
3. backup_ss_after.sh
The script which does the cleaning process after backing up the files
4.Before using this script you have to enable password less ssh/rsync login to remote host which you want to backup data.
#1. ssh user@remote_host
#2. ssh-keygen -t rsa
#3. Don not give any phrase as password, leave it blank when asked
#4. scp /home/user/.ssh/id_rsa.pub user@localhost
#5. get back to localhost and;
#6. cat id_rsa.pub >> /home/user/.ssh/authorized_keys
#now you can rsyn without entering password

backup_cs.sh

#!/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

#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=

#Destination of backup
BACKUP_SERVER="@:"
REMOTE_EXEC="ssh @"

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;

#cd $BACKUP_DEST

[[ -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


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 )

$REMOTE_EXEC lms_backup_ss_before



if $YEARLY && $END_OF_YEAR; then
rsync -a --delete $BACKUP_SOURCE $BACKUP_SERVER/$YEARLY_BAK/$TODAY_BACKUP
exit 0
fi

if $MONTHLY && $END_OF_MONTH; then
rsync -a --delete $BACKUP_SOURCE $BACKUP_SERVER/$MONTHLY_BAK/$TODAY_BACKUP
exit 0
fi

if $WEEKLY && $END_OF_WEEK; then
rsync -a --delete $BACKUP_SOURCE $BACKUP_SERVER/$WEEKLY_BAK/$TODAY_BACKUP
exit 0
fi

if $DAILY; then
echo in
rsync -av --delete $BACKUP_SOURCE $BACKUP_SERVER/$DAILY_BAK/$TODAY_BACKUP
fi

$REMOTE_EXEC lms_backup_ss_after




backup_ss_before.sh

#!/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

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


#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;

cd $BACKUP_DEST

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


TODAY_BACKUP=`date +%d-%m-%y`;

if [[ -d $DAILY_BAK/$TODAY_BACKUP ]]; then
exit 1
fi

YESTERDAY_BACKUP=
if [[ -d $DAILY_BAK/$YESTERDAY ]]; then
YESTERDAY_BACKUP=$DAILY_BAK/$YESTERDAY;
else
YESTERDAY_BACKUP=$DAILY_BAK/$( ls -tr $DAILY_BAK/ | tail -1 );
fi
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 && [[ -d $LASTYEAR_BACKUP ]]; then
cp -al $LASTYEAR_BACKUP/ $YEARLY_BAK/$TODAY_BACKUP
exit 0
fi

if $MONTHLY && $END_OF_MONTH && [[ -d $LASTMONTH_BACKUP ]]; then
cp -al $LASTMONTH_BACKUP/ $MONTHLY_BAK/$TODAY_BACKUP
exit 0
fi

if $WEEKLY && $END_OF_WEEK && [[ -d $LASTWEEK_BACKUP ]]; then
cp -al $LASTWEEK_BACKUP/ $WEEKLY_BAK/$TODAY_BACKUP
exit 0
fi

if $DAILY && [[ -d $YESTERDAY_BACKUP ]]; then
cp -al $YESTERDAY_BACKUP $DAILY_BAK/$TODAY_BACKUP
fi


backup_ss_after.sh

#!/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.

#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

#Destination of backup
BACKUP_DEST=

cd $BACKUP_DEST

expr $( ls $YEARLY_BAK | wc -l ) \> $YEARS_TO_KEEP && rm -fr $YEARLY_BAK/$( ls -t $YEARLY_BAK/ | tail -1 )
expr $( ls $MONTHLY_BAK | wc -l ) \> $MONTHS_TO_KEEP && rm -fr $MONTHLY_BAK/$( ls -t $MONTHLY_BAK/ | tail -1 )
expr $( ls $WEEKLY_BAK | wc -l ) \> $WEEKS_TO_KEEP && rm -fr $WEEKLY_BAK/$( ls -t $WEEKLY_BAK/ | tail -1 )
expr $( ls $DAILY_BAK | wc -l ) \> $DAYS_TO_KEEP && rm -fr $DAILY_BAK/$( ls -t $DAILY_BAK/ | tail -1 )

No comments:

Post a Comment