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).


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.
#rsync sites,please crontab -e to set the circle time

#you can limit rsync speed by using "rsync -av --bwlimit=20000 swapfile.img swapfile.img2", this will limit speed up to 20M/s
sites_dirs=" /var/www/virtual/yourdomain.tld/htdocs /var/www/virtual/yourdomain2.tld/htdocs" #directorys to be backuped
sshhost='' #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
if [ -x /usr/bin/expect ]
for site in $sites_dirs #loop the $site_dirs
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
echo 'please first install expect'

Categories: IT Architecture, Linux, Systems Tags: