Rsync经常使用选项:git
-v, --verbose 详细模式输出
github
-q, --quiet 精简输出模式
web
-c, --checksum 打开校验开关,强制对文件传输进行校验
shell
-a, --archive 归档模式,表示以递归方式传输文件,并保持全部文件属性,等于-rlptgoD
vim
-r, --recursive 对子目录以递归模式处理
bash
-l, --links 保留符号链结
服务器
-p, --perms 保留文件权限
ide
-t, --times 保留文件时间戳
测试
-g, --group 保留文件属组信息
ui
-o, --owner 保留文件属主信息
-D, --devices 保留设备文件即特殊文件信息
-e, --rsh=COMMAND 指定替代rsh的shell程序
-z, --compress 对备份的文件在传输时进行压缩处理
目标主机配置
# yum -y install rsync xinetd # vim /etc/rsyncd.conf # Section 1: Global settings uid = nobody gid = nobody use chroot = no max connections = 3 strict modes = yes pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log # Section 2:Directory to be synced [htdocs] path = /www/htdocs //同步过来的数据保存的位置,同步的时候有两种写法,后边会说到。 ignore errors = yes read only = no write only = no hosts allow = 172.16.0.0/16 //容许访问的主机范围 hosts deny = * list = false uid = root gid = root auth users = testuser //远程同步的时候指定的客户端用户 secrets file = /etc/rsync.passwd //加密访问用到的用到的认证密码文件,目标主机和源主机都得配置
# vim /etc/rsync.passwd testuser:P@ssw0rd # chkconfig rsync on # chkconfig xinetd on # service xinetd start
配置源
# yum -y install rsync xinetd # vim /etc/rsync.passwd P@ssw0rd
注意:这里密码文件必定不要写为testuser:P@ssw0rd,只写密码就行,不然,同步会报错,即,服务器端和客户端的此文件写法不一样。
# chmod 600 /etc/rsync.passwd # chkconfig rsync on # chkconfig xinetd on # service xinetd start
同步方式:
rsync有四种工做模式:
第一个是shell模式,也称为本地模式;
第二个是远程shell模式,其利用SSH执行底层链接和传输;
第三个是列表模式,其工做方式与ls类似,即列出源的内容;-nv
第四个模式是服务器模式。rsync以守护进程方式运行,接收文件传输请求。在使用时,能够使用rsync命令把文件发送给守护进程,也能够向它请求文件。服务器模式很是适合建立中心备份服务器或项目存储库。
shell模式:
# rsync -lr /tmp/rsync 172.16.5.11:/www/ # rsync -lr /tmp/rsync root@172.16.5.11:/www/
服务器模式
# rsync -avz --password-file=/etc/rsync.passwd /tmp/rsync testuser@172.16.5.11::htdocs
注意手动同步的时候,shell模式可能更简单一些,但如果用rsync+inotify来同步,服务器模式就方便多了。
利用inotify自动同步
1、安装inotify-tools
软件下载地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
# tar xf inotify-tools-3.14.tar.gz # cd inotify-tools-3.14 # ./configure # make # make install # echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf # ldconfig
2、建立测试目录
# mkdir -pv /www/htdocs
服务器端建立脚本
# vim /root/bin/htdocsync.sh #!/bin/bash # DESTHOST=172.16.3.4 DESTHOSTDIR=/www/htdocs/ SRCDIR=/www/htdocs/ inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $SRCDIR | while read DATE TIME DIR FILE; do FILECHANGE=${DIR}${FILE} rsync -avz --password-file=/etc/rsync.passwd $SRCDIR htdocsuser@${DESTHOST}::htdocs &>/dev/null && \ echo "At ${TIME} on ${DATE}, file $FILECHANGE was backed up via rsync" >> /var/log/websync.log done # chmod +x /root/bin/htdocsync.sh # /root/bin/htdocsync.sh