Archive

Posts Tagged ‘rsync’

using rsync and crontab to automatically backup sites

September 3rd, 2010 1 comment

What does rsync mean?Here the man page goes:rsync - faster, flexible replacement for rcp.rcp is a program that executes remote copying,and rsync is the upgrade version of it.Besides,rsync has its advantages on security and high efficiency matters.

Now,we want to backup sites(database files included) at a fixed period.You may think of ftp method,but considering the large scaling of sites source files(Bandwidth is so expensive!) and the insecurity of ftp protocal,the idea is deprecated.(About this method,link here to see it).

Note:

1.If no rsync installed on the source server & destination server,please install it first.

2.If no expect installed on the source server,install it first too.
#!/bin/bash
#rsync_sites.sh
#rsync sites,please crontab -e to set the circle time
sites_dirs=" /var/www/virtual/yourdomain.tld/htdocs /var/www/virtual/yourdomain2.tld/htdocs" #directorys to be backuped
sshhost='xxx.xxx.xxx.xxx' #destination server ip address
password='xxxxxx' #destination password
#using expect to auto-fill the interactive process(programmed dialogue with interactive program)
/usr/bin/expect <<EOF

spawn ssh -lroot -p22 $sshhost { [ ! -d /root/backup ] && mkdir /root/backup }
#mkdir of the destination dir to save backup files
expect "password:" #auto response
send "$password\r"
expect eof
exit
EOF
#sites_dirs="/root/"
if [ -x /usr/bin/expect ]
then
for site in $sites_dirs #loop the $site_dirs
do
site_name=`awk -F '/' '{print $5}' <<<$site` #notice:in '/var/www/virtual/yourdomain.tld/htdocs',$site_name will be yourdomain.tld
/usr/bin/expect <<EOF
spawn rsync --delete -azvv -e ssh $site root@$sshhost:/root/backup/$site_name
expect -re "password"
send "$password\r"
expect -re "Are you sure"
send "yes\r"
expect -re "please type"
send "yes\r"
expect eof
exit
EOF
done
else
echo 'please first install expect'
fi

Categories: IT Architecture, Linux, Systems Tags: