五、rsync 服务器应用案例
5.1. 在服务器端TS-DEV上配置rsync 服务
a. 编辑配置文件
# vi /etc/rsyncd/rsyncd.conf
# Minimal configuration file for rsync daemon # See rsync(1) and rsyncd.conf(5) man pages for help # This line is required by the /etc/init.d/rsyncd script
# GLOBAL OPTIONS uid = root gid = root
use chroot = no
read only = yes #limit access to private LANs hosts allow=172.16.0.0/255.255.0.0 192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0 hosts deny=*
max connections = 5
pid file = /var/run/rsyncd.pid
secrets file = /etc/rsyncd/rsyncd.secrets
#lock file = /var/run/rsync.lock
motd file = /etc/rsyncd/rsyncd.motd #This will give you a separate log file log file = /var/log/rsync.log #This will log every file transferred - up to 85,000+ per user, per sync transfer logging = yes log format = %t %a %m %f %b syslog facility = local3 timeout = 300
# MODULE OPTIONS [davidhome] path = /home/david/ list=yes ignore errors auth users = david comment = David home exclude = important/ [chinatmp] path = /tmp/china/ list=no ignore errors auth users = china comment = tmp_china
b. 建立/etc/rsyncd/rsyncd.secrets文件
# vim /etc/rsyncd/rsyncd.secrets
david:asdf #格式 用户名:口令
china:jk #该用户不要求是系统用户
c. 为了密码的安全性,我们把权限设为600
# chown root:root /etc/rsyncd/rsyncd.secrets
# chmod 600 /etc/rsyncd/rsyncd.secrets
d. 建立连接到服务器的客户端看到的欢迎信息文件/etc/rsyncd/rsyncd.motd
# vim /etc/rsyncd/rsyncd.motd
+++++++++++++++++++++++++++
+ David Camp +
+++++++++++++++++++++++++++
e. 启动rsync
# /etc/init.d/xinetd restart
f. 查看873端口是否起来
# netstat -an | grep 873
如果rsync启动成功的话可以看到873端口已经在监听了。
g. 服务器端文件详细
5.2. 客户端配置
a. 客户端安装rsync
# yum -y install rsync
b. 通过rsync客户端来同步数据
场景一:
# rsync -avzP david@172.16.1.135::davidhome /tmp/david/
Password: 这里要输入david的密码,是服务器端提供的,在前面的例子中,我们用的是 asdf,输入的密码并不显示出来;输好后就回车;
注: 这个命令的意思就是说,用david 用户登录到服务器上,把davidhome数据,同步到本地目录/tmp/david/上。当然本地的目录是可以你自己定义的,比如 dave也是可以的;当你在客户端上,当前操作的目录下没有davidhome这个目录时,系统会自动为你创建一个;当存在davidhome这个目录中,你要注意它的写权限。
说明:
-a 参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
-z 传输时压缩;
-P 传输进度;
-v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;
场景二:
# rsync -avzP --delete david@172.16.1.135::davidhome /tmp/david/
这回我们引入一个 –delete 选项,表示客户端上的数据要与服务器端完全一致,如果 /tmp/david/目录中有服务器上不存在的文件,则删除。最终目的是让/tmp/david/目录上的数据完全与服务器上保持一致;用的时候要小心点,最好不要把已经有重要数所据的目录,当做本地更新目录,否则会把你的数据全部删除;
场景三:
# rsync -avzP --delete --password-file=/tmp/rsync.password david@172.16.1.135::davidhome /tmp/david/
这次我们加了一个选项 –password-file=rsync.password ,这时当我们以david用户登录rsync服务器同步数据时,密码将读取 /tmp/rsync.password 这个文件。这个文件内容只是david用户的密码。我们要如下做;
# touch /tmp/rsync.password
# chmod 600 /tmp/rsync.password
# echo "asdf"> /tmp/rsync.password
# rsync -avzP --delete --password-file=/tmp/rsync.password david@172.16.1.135::davidhome /tmp/david/
注: 这样就不需要密码了;其实这是比较重要的,因为服务器通过crond 计划任务还是有必要的;
5.3. rsync 客户端自动与服务器同步数据
编辑crontab
# crontab -e
加入如下代码:
10 0 * * * rsync -avzP --delete --password-file=/tmp/rsync.password david@172.16.1.135::davidhome /tmp/david/
表示每天0点10分执行后面的命令。
六、错误分析
- 海报