[www]
path = /mirror/www/
comment = my rsync site
ignore errors
read only = true
list = false
auth users = backup
hosts allow = 12.23.34.57/32
secrets file = /etc/rsyncd.secrets
[file0]
path = /mirror/file0
comment = file0
ignore errors
read only = true
list = false
auth users = backup
hosts allow = 12.23.34.57/32
secrets file = /etc/rsyncd.secrets
[file1]
path =/mirror/file1
comment = file1
ignore errors
read only = true
list = false
auth users = backup
hosts allow = 12.23.34.57/32
secrets file = /etc/rsyncd.secrets
这里分别定义了[www]、[file0]、[file1]模块,分别对应于3个需要备份的目树。这里只允许12.23.34.57备份服务器WWW的数据,并且需要认证。3个模块授权的备份用户都为BACKUP,并且用户信息保存在文件/etc/backserver.pas中,其内容如下:
[root@www /etc]# cat /etc/backserver.pas
backup:bk_passwd
并且该文件只能是Root用户可读写的,否则Rsyncd启动时会出错。这些文件配置完毕以后,就需要在WWW服务器上启动Rsyncd服务器:
[root@www /etc]# grep rsync services
rsync 873/tcp # rsync
rsync 873/udp # rsync
[root@www /etc] grep rsync inetd.conf
rsync stream tcp nowait root /usr/local/bin/rsync rsyncd -daemon
[root@www /etc]# rsync -daemon
执行完毕上面的命令后,Rsync即可启动。Rsync默认服务端口为873,服务器在该端口接收客户的匿名或者认证方式的备份请求。
执行Rsync客户端命令 下一步就要运行Rsync客户端的启动命令了(本例所指的Rsync客户端是名为BACKUP的服务器):
[backup@backup /] /usr/local/bin/rsync -vzrtopg -delete
-exclude “logs/”--exclude “conf/ssl.*/”
--progress backup@12.23.34.56::www /backup/www/
--password-file=/etc/rsync.pass
上面这个命令行中--vzrtopg里的v是代表Verbose(详细);z是代表Zip(压缩);r是代表Recursive(递归);Topg都是保持文件原有属性,如属主、时间的参数;--progress是指显示出详细的进度情况;--delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致;--exclude“logs/”是表示不对/www/logs目录下的文件进行备份。同样-exclude“conf/ssl.*/”是表示不对/www/conf/ssl.*/目录下的文件进行备份。
backup@12.23.34.56::www是表示该命令是对服务器12.23.34.56中的WWW模块进行备份,其中BACKUP表示使用BACKUP用户来对该模块进行备份。--password-file=/etc/rsync.pass来指定密码文件,这样就可以在脚本中使用而无须交互式地输入验证密码了。这里需要注意的是,这份密码文件权限属性要设得只有执行这个命令的当前用户可读,本例中是BACKUP用户。
这里将备份的内容存放在备份机的/backup/www/目录下。
[backup@backup /] /usr/local/bin/rsync -vzrtopg -delete -exclude“logs/”--exclude “conf/ssl.*/” --progress backup@12.23.34.56::www /backup/www/--password-file=/etc/rsync.pass
receiving file list ... done
a.txt
wrote 100 bytes read 990409bytes 220113.11 bytes/sec
total size is 2779708994 speedup is 2806.34
这样,Rsync同步服务就搭建好了,最后我们可以将在客户端执行的命令通过Crontab定时执行来实现自动备份,或者写一些脚本,这样Rsync同步服务的搭建就更加完美了。
一些示例脚本
1、每隔7天将数据往中心服务器做增量备份
#!/bin/sh
# This script does personal backups to a rsync backup server.You
# with a 7 day rotating backup. The incrementalswill go
# into subdirectories named after the day of the week, and thec
# full backup goes into a directory called “current”
# tridge@linuxcare.com
# directory to backup
BDIR=/home/$USER
# excludes file
EXCLUDES=$HOME/cron/excludes
# the name of the backup machine
BSERVER=owl
# your password on the backup server
export RSYNC_PASSWORD=XXXXXX
BACKUPDIR=‘date +%A’
OPTS=“--force --ignore-errors --delete-excluded
--exclude-from=$EXCLUDES
--delete --backup --backup-dir=/$BACKUPDIR -a”
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
# the following line clears the last weeks incrementaldirectory
[ -d $HOME/emptydir ]‘ ’mkdir $HOME/emptydir
rsync --delete -a $HOME/emptydir/ $BSERVER::$USER/$BACKUPDIR/
rmdir $HOME/emptydir
# now the actual transfer
rsync $OPTS $BDIR $BSERVER::$USER/current
2、备份至一个空闲的硬盘
#!/bin/sh
export PATH=/usr/local/bin:/usr/bin:/bin
LIST=“rootfs usr data data2”
for d in $LIST; do
mount /backup/$d
rsync -ax --exclude fstab --delete /$d/ /backup/$d/
umount /backup/$d
done
DAY=‘date “+%A”’
rsync -a --delete /usr/local/apache /data2/backups/$DAY
rsync -a --delete /data/solid /data2/backups/$DAY
3、对vger.rutgers.edu的Cvs树进行镜像
#!/bin/bash
cd /var/www/cvs/vger/
PATH=/usr/local/bin:/usr/freeware/bin:/usr/bin:/bin
RUN=‘lps x | grep rsync | grep -v grep | wc -l’
if [“$RUN”-gt 0 ]; then
echo already running
exit 1
fi
rsync -az vger.rutgers.edu::cvs/CVSROOT/ChangeLog
$HOME/ChangeLog
sum1=‘sum $HOME/ChangeLog’
sum2=‘sum /var/www/cvs/vger/CVSROOT/ChangeLog’
if [ “$sum1”= “$sum2”]; then
echo nothing to do
exit 0
fi
rsync -az --delete
--force vger.rutgers.edu::cvs/ /var/www/cvs/vger/
exit 0
联系客服