Rsync Client script

Rsync Client script

This is the Rsync client side shell scrpit.. You can specify setting for backup in backup.conf file and set cron job. This script will take backup of mentioned directory of respective users at given time.

1] sample backup file –

root@indiangnu.org:/home/arun# cat /etc/backup/backup.conf

#This is the Backup conf for client
#username:path_to_backup:where_to_backup:when_to_backup:time:
#when_to_backup=> daily=1,weekly=2, monthly=3
#where_to_backup=>USERNAME [server is 192.168.1.50], time=>0930 like 9:30 as 0930
root:/etc/:Server-conf/Fileserver_etc-dir:09:09:no
arun:/home/arun/public_html:arun/bkup:10:09:no
nishit:/home/nishit/public_html:nishit/bkup:11:09:no

root@indiangnu.org:/home/arun#

2] Set cronjob –

#Cronjob for Backup on Fileserver

0 08-12 * * * /etc/backup/rsync-client.sh

3] Rsync client script –

root@indiangnu.org:/home/arun# cat /etc/backup/rsync-client.sh

#!/bin/bash
#User define Function (UDF)……..
HOSTNAME=192.168.1.160

#Process backup.conf
########################################
processLine()
{
#echo $line
#username:path_to_backup:where_to_backup:when_to_backup:time:
username=$(echo $line | awk -F: ‘{ print $1 }’)
path_to_bkup=$(echo $line | awk -F: ‘{ print $2 }’)
where_to_bkup=$(echo $line | awk -F: ‘{ print $3 }’)
when_to_bkup=$(echo $line | awk -F: ‘{ print $4 }’)
time=$(echo $line | awk -F: ‘{ print $5 }’)
status=$(echo $line | awk -F: ‘{ print $6 }’)
#echo “Arg[1]–>”$username
#echo “Arg[2]–>”$path_to_bkup
#echo “Arg[3]–>”$where_to_bkup
#echo “Arg[4]–>”$when_to_bkup
#echo “Arg[5]–>”$time
#echo “Arg[6]–>”$status

#Test the backup.conf
################
if [ “$username” == “” ] ; then
echo “Record is Null”
else
#Check who is running this script root or normal user.
who_is_running=$(id -nu)
#call function to check password file

password_fun $who_is_running $username
system_time=$(date +%H)
if [ “$system_time” == “$time” ] ; then
#call backup function
backup $username $path_to_bkup $where_to_bkup $when_to_bkup $time $status
#echo “system_time=”$system_time
#echo “time=”$time
fi
fi
################
}

#Password Function
########################################
password_fun()
{
#user=”$@” #get all argument
is_root_user=$1
user=$2
#where is secret.conf file
if [ “$is_root_user” == “root” ]; then
if [ “$user” == “root” ]; then
pwd_file=”/root/.secret.conf”
else
pwd_file=”/home/$user/.secret.conf”
fi
#Check file exist or not
if [ ! -f $pwd_file ]; then
echo “$pwd_file file does not exist!”
echo “creating File..”
touch $pwd_file
echo “changing ownership and permission”
owner=”root:root $pwd_file”
echo “owner “$owner
chown root:root $pwd_file
chmod 600 $pwd_file
echo “password” > $pwd_file
#echo $(ls -l $pwd_file)
fi
else
echo “permission denied to read pwd file”
fi
}

#Set ENV variable
########################################
set_ENV_var()
{
user=$1
TERM=xterm
LANGUAGE=en_IN:en
LOGNAME=$user
if [ “$user” == “root” ]; then
USER=root
PWD=/root
HOME=/root
PATH=$PATH:/usr/bin/X11:/usr/games
else
USER=${user}
PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
HOME=/home/${user}
PWD=/home/${user}
fi
}
#Backup Function
########################################
backup()
{
username=$1
path_to_bkup=$2
where_to_bkup=$3
when_to_bkup=$4
time=$5
status=$6
echo “——————————————————————”
#command=”/usr/bin/rsync –password-file=”/home/$username/.secret.conf” $path_to_bkup -Cravz 192.168.0.5::$where_to_bkup”
echo “Backup started for ${username}……”
if [ “$username” == “root” ]; then
command=” –password-file=”/root/.secret.conf” ${path_to_bkup} -Cravz 192.168.1.50::${where_to_bkup}”
else
command=” –password-file=”/home/${username}/.secret.conf” ${path_to_bkup} -Cravz 192.168.1.50::${where_to_bkup}”
fi

#Set environment variable…
set_ENV_var $username
#check status ofBackup
if [ “$status” == “no” ] ; then
if [ -f $path_to_bkup ]; then
echo “$path_to_bkup file/dir does exist!”
/usr/bin/rsync $command
elif [ -d $path_to_bkup ]; then
echo “This dirname [$path_to_bkup] does exists”
/usr/bin/rsync $command
else
echo “Neither [$path_to_bkup] or [$path_to_bkup] exist”
fi
if [ $? -eq 0 ]; then
status=”yes”
else
status=”no”
fi
fi
echo “——————————————————————”
#Send mail to user
###########################
if [ $? -eq 0 ]; then
if [ “$username” != “root” ]; then
/etc/backup/mailclient 192.168.1.100 backupmail ${username} ${HOSTNAME}
fi
fi
#Set back env to root…
set_ENV_var root
#Update status function
#update_status $username $status $time
}

#Staus Function

########################################

update_status()
{
status_file=”/etc/backup/backup.status”
new_user=$1
new_status=$2
new_time=$3
update=”${new_user}:${new_status}:${new_time}:”
echo “update string:->”$update
echo “update::status file–>”$status_file
# Read $status_file using the file descriptors
file_string=””
count=”0″
while [ 1 ]
do
read myline || break
username=$(echo $line | awk -F: ‘{ print $1 }’)
if [ “$user” == “$username” ]; then
if [ $count -eq 0 ]; then
file_string=${file_string}${update}
count=`expr $count + 1`
else
file_string=${file_string}
fi
else
file_string=${file_string}${myline}
fi
#save string to file
echo $file_string > $status_file
echo -e “update”
cat $status_file
done < $status_file
#echo “update backup.status file function is end”
}

#Main script stars here
########################################
FILE=””
#Is file name given as command line argument
#Else read it from standard input device
if [ “$1” == “” ]; then
FILE=”/etc/backup/backup.conf”
else
FILE=”$1″
#make sure file exist and readable
if [ ! -f $FILE ]; then
echo “$FILE : does not exists”
exit 1
elif [ ! -r $FILE ]; then
echo “$FILE: can not read”
exit 2
fi
fi
#Read $FILE using the file descriptors
count=”1″
exec 3<&0
exec 0<$FILE
while read line
do
# use $line variable to process line in processLine() function
if [ $count -gt 4 ]; then
processLine $line
else
count=$(expr $count + 1)
fi

done
exec 0<&3
exit 0

root@indiangnu.org:/home/arun#

Similar Posts:

Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *