Sunteți pe pagina 1din 3

#!

/bin/bash
# Date : 19th Septempber 2009
# Purpose : To automate incremental backup in a machine
# Contact : For any comments and suggestions email me at
sachan13b.piyush@gmail.com
#

color_ok="\\033[1;32m"
color_error="\\033[1;38m"
color_normal="\\033[0;39m"
color_input="\\033[1;34m"
color_output="\\033[1;20m"
BOOL=false

iscase()
{
read CHOICE
case $CHOICE in
1) BOOL=true
CRONS=hourly
echo -e "making crontab entry in /etc/cron.hourly/rsync_scripts"
echo -e " generating logfile in
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log"
mkdir /root/rsync.logwatch$SERVIP
#
#saving a new entry in crontab hourly
#
grep "/root/scripts/rsync_local_$SERVIP.$CRONS.sh >>
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log" /etc/cron.hourly/rsync_scripts >
/dev/null
STATUS=$?
if [ "$STATUS" -ne 0 ]; then
echo -e "/root/scripts/rsync_local_$SERVIP.$CRONS.sh >>
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log" >> /etc/cron.hourly/rsync_scripts
chmod 755 /etc/cron.hourly/rsync_scripts
echo -e "$color_ok Done....$color_normal"
fi;;
2) BOOL=true
CRONS=daily
echo -e "$color_error making crontab entry in /etc/cron.daily/rsync_scripts"
echo -e " generating logfile in
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log"
mkdir /root/rsync.logwatch$SERVIP
#
#saving a new entry in crontab hourly
#
grep "/root/scripts/rsync_local_$SERVIP.$CRONS.sh >>
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log" /etc/cron.daily/rsync_scripts >
/dev/null
STATUS=$?
if [ "$STATUS" -ne 0 ]; then
echo -e "/root/scripts/rsync_local_$SERVIP.$CRONS.sh >>
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log" >> /etc/cron.daily/rsync_scripts
chmod 755 /etc/cron.daily/rsync_scripts
echo -e "$color_ok Done....$color_normal"
fi;;
3) BOOL=true
CRONS=weekly
echo -e "$color_error making crontab entry in
/etc/cron.weekly/rsync_scripts"
echo -e " generating logfile in
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log"
mkdir /root/rsync.logwatch$SERVIP
#
#saving a new entry in crontab hourly
#
grep "/root/scripts/rsync_local_$SERVIP.$CRONS.sh >>
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log" /etc/cron.weekly/rsync_scripts >
/dev/null
STATUS=$?
if [ "$STATUS" -ne 0 ]; then
echo -e "/root/scripts/rsync_local_$SERVIP.$CRONS.sh >>
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log" >> /etc/cron.weekly/rsync_scripts
chmod 755 /etc/cron.weekly/rsync_scripts
echo -e "$color_ok Done....$color_normal"
fi ;;
4) BOOL=true
CRONS=monthly
echo -e "$color_error making crontab entry in
/etc/cron.monthly/rsync_scripts"
echo -e " generating logfile in
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log"
mkdir /root/rsync.logwatch$SERVIP
#
#saving a new entry in crontab hourly
#
grep "/root/scripts/rsync_local_$SERVIP.$CRONS.sh >>
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log" /etc/cron.monthly/rsync_scripts >
/dev/null
STATUS=$?
if [ "$STATUS" -ne 0 ]; then
echo -e "/root/scripts/rsync_local_$SERVIP.$CRONS.sh >>
/root/rsync.logwatch$SERVIP/$SERVIP.rsync.log" >> /etc/cron.monthly/rsync_scripts
chmod 755 /etc/cron.monthly/rsync_scripts
echo -e "$color_ok Done....$color_normal"
fi;;
esac
}

echo -e "$color_normal Enter the ip/name of server machine : $color_input\c"


read SERVIP
ls /root/.ssh/server.key.$SERVIP /root/.ssh/server.key.$SERVIP.pub > /dev/null
STATUS=$?
if [ "$STATUS" -ne 0 ]; then
echo -e "$color_error No previous key found for this ip, Creating one...\n Note:
Do not use passphrase when asked just press enter ..!"
echo -e "$color_normal Enter the key file name to be saved as
/root/.ssh/server.key.ip $color_output"
ssh-keygen -t rsa -C $SERVIP
echo -e "$color_normal Enter the path of key file name you just created :
$color_input\c"
read KEY
echo -e "$color_normal "
ssh-copy-id -i "$KEY.pub" $SERVIP
else
KEY=`echo "/root/.ssh/server.key.$SERVIP"`
fi

echo -e "$color_normal Enter path of server directory to be backuped :


$color_input\c"
read SERVBACKFOLD
echo -e "$color_normal Enter local path where to be save your backup :
$color_input\c"
read LOCALBACKFOLD
mkdir -p $LOCALBACKFOLD

echo -e " Enter your choice for crontab scheduling \n


1. Hourly\n
2. Daily\n
3. Weekly\n
4. Monthy\n"

until $BOOL = 'false'


do
echo -e "Enter correct choice : \c"
iscase
done

echo -e "$color_error Creating and saving rsync_local_$SERVIP.$CRONS.sh under


/root/scripts/"
mkdir -p /root/scripts
#
#generating script to save on file
#
echo "#!/bin/bash
echo -e
\"--------------------------------------------------------------------------------
------------\\n\"
echo -e \"Starting backup on \`date +%d\` \`date +%B\` \`date +%Y\` at \`date
+%r\`\"
echo -e
\"--------------------------------------------------------------------------------
------------\\n\"
rsync -avz --progress --numeric-ids --rsh=ssh --delete $SERVIP:$SERVBACKFOLD
$LOCALBACKFOLD -e \"ssh -i $KEY\"" >> /root/scripts/rsync_local_$SERVIP.$CRONS.sh
chmod 755 /root/scripts/rsync_local_$SERVIP.$CRONS.sh
echo -e "$color_ok Done....$color_error"
#
#end of script
#
echo -e "$color_normal "
chkconfig crond on
service crond restart