rsync是Unix下的一款应用软件,它能同步更新两处计算机的文件与目录,并适当利用差分编码以减少数据传输量。rsync中的一项同类软件不常见的重要特性是每个目标的镜像只需发送一次。 rsync可以拷贝/显示目录内容,以及拷贝文件,并可选压缩以及递归拷贝。
服务器端rsync配置
此配置方法只适用于 Centos 7
安装rsync 1 2 3 4 5 6 7 8 9 10 11 yum install rsync -y cd /tmpwget https://rsync.samba.org/ftp/rsync/src/rsync-3.2.3.tar.gz tar zvxf rsync-3.2.3.tar.gz cd rsync-3.2.3./configure --prefix=/usr/local /rsync make &&make install
一般系统都已经安装好了, 这里直接过
配置/etc/rsyncd.conf 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 # 全局配置 ----------------------------------------------------- [global] # 日志 log file = /var/log/rsyncd.log log format = %t %a %m %f %b # 记录传输文件日志 transfer logging = yes # pid文件 pid file=/var/run/rsyncd.pid # 支持max connections参数的锁文件 lock file=/var/run/rsyncd.lock # 启动服务用户 uid = root gid = root # 默认为true,修改为no,增加对目录文件软连接的备份 use chroot = no # 最大连接数 max connections = 1024 # 超时时间 timeout = 600 port = 873 address = 172.29.241.184 # 用户认证配置文件 # 格式 用户名:密码 # 权限必须为600 secrets file = /etc/rsync.pass # 是否为只读,如果是no的话,客服端可以上传文件给服务 read only = no # 设定白名单,可以指定IP段(172.18.50.1/255.255.255.0),各个Ip段用空格分开 hosts allow = 172.29.241.1/255.255.240.0 hosts deny = * # 剔除某些文件或目录,不同步 exclude = lost+found/ .git # 设置不需要压缩的文件 dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # 模块配置 ----------------------------------------------------- # 开发环境登录API日志同步模块 [dev_dlz_auth] path = /data/logs/dev/auth/ comment = dev_dlz_auth list = no ignore errors auth users = dlz # 开发环境caddb API日志同步模块 [dev_dlz_cad_db] path = /data/logs/dev/cad_db/ comment = dev_dlz_cad_db list = no ignore errors auth users = dlz # 开发环境main API日志同步模块 [dev_dlz_main] path = /data/logs/dev/main/ comment = dev_dlz_main list = no ignore errors auth users = dlz # 测试环境登录API日志同步模块 [test_dlz_auth] path = /data/logs/test/auth/ comment = test_dlz_auth list = no ignore errors auth users = dlz # 测试环境caddb API日志同步模块 [test_dlz_cad_db] path = /data/logs/test/cad_db/ comment = test_dlz_cad_db list = no ignore errors auth users = dlz # 测试环境main API日志同步模块 [test_dlz_main] path = /data/logs/test/main/ comment = test_dlz_main list = no ignore errors auth users = dlz # dwg转dxf RPC API日志同步模块 [dwg_to_dxf] path = /data/logs/dwg_to_dxf comment = dwg_to_dxf list = no ignore errors auth users = dlz
配置认证文件/etc/rsync.pass 1 2 3 4 5 # 格式 [用户名:密码] 一行一对 dlz:GLiGZ8Y5bt7Z # 设置权限 chmod 600 /etc/rsync.pass
/etc/rsyncd.conf 和 /etc/rsync.pass 的权限一定要设置为 600
rsyncd进程管理 1 2 3 systemctl start rsyncd systemctl stop rsyncd systemctl restart rsyncd
测试同步 一般同步有两种情况:
服务端 -> 客户端 分发
客户端 -> 服务端 收集
1 2 3 4 5 6 rsync -avzP --delete [email protected] ::dwg_to_dxf /home/node/dwg_to_dxf/logs/ --password-file=/etc/rsync.pass rsync -avzP --delete /home/node/dwg_to_dxf/logs/ [email protected] ::dwg_to_dxf --password-file=/etc/rsync.pass rsync -auvzP --delete /home/node/dlz-cad-db/logs/ [email protected] ::dev_dlz_cad_db --password-file=/etc/rsync_local.pass >/dev/null 2>&1
我们的场景是收集日志, 所以是客户端 -> 服务器
的模式
客户端配置 安装sersync 1 2 3 4 cd /tmpwget --no-check-certificate https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz tar zvxf sersync2.5.4_64bit_binary_stable_final.tar.gz mv GNU-Linux-x86/ /usr/local /sersync
配置 1 2 cd /usr/local /sersynccp confxml.xml confxml.xml.bak
举个现在使用的配置例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 <?xml version="1.0" encoding="ISO-8859-1"?> <head version ="2.5" > <host hostip ="localhost" port ="8008" > </host > <debug start ="false" /> <fileSystem xfs ="false" /> <filter start ="true" > <exclude expression ="(.*)\.svn" > </exclude > <exclude expression ="(.*)\.gz" > </exclude > <exclude expression ="(.*)\.git" > </exclude > <exclude expression ="^info/*" > </exclude > <exclude expression ="^static/*" > </exclude > </filter > <inotify > <delete start ="true" /> <createFolder start ="true" /> <createFile start ="true" /> <closeWrite start ="true" /> <moveFrom start ="true" /> <moveTo start ="true" /> <attrib start ="false" /> <modify start ="true" /> </inotify > <sersync > <localpath watch ="/home/node/dwg_to_dxf/logs" > <remote ip ="172.29.241.184" name ="dwg_to_dxf" /> </localpath > <rsync > <commonParams params ="-auvzP" /> <auth start ="true" users ="dlz" passwordfile ="/etc/rsync.pass" /> <userDefinedPort start ="false" port ="874" /> <timeout start ="false" time ="100" /> <ssh start ="false" /> </rsync > <failLog path ="/tmp/rsync_fail_log.sh" timeToExecute ="60" /> <crontab start ="false" schedule ="600" > <crontabfilter start ="false" > <exclude expression ="*.php" > </exclude > <exclude expression ="info/*" > </exclude > </crontabfilter > </crontab > <plugin start ="false" name ="command" /> </sersync > </head >
主要需要关注的地方
<localpath></localpath> 监控本地目录或文件
<rsync></rsync> 同步到远端的rsync服务器
管理sersync服务 1 2 3 4 killall sersync2 /usr/local /sersync2 -r -d -o /usr/local /sersync/conf/confxml.xml
如果服务器性能很差, 请使用 -n [Number] 合理降低线程数, /usr/local/sersync2 -r -d -n 2 -o /usr/local/sersync/conf/confxml.xml