多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## 服务器角色: NFS服务器,sersync服务端.rsync客户端 NFS备份服务器,rsync的服务端 ## sersync下载地址: ``` https://code.google.com/archive/p/sersync/downloads ``` ## 部署计划 - NFS服务器创建好对应的用户www - NFS备份服务器端安装配置rsync服务端 - 创建相应的备份目录,手动测试rsync是否可以正常传输 - NFS服务器配置sersync配置文件,多目录传输 - 启动sersync,测试是否可以正常运行 - 手动写入文件,测试是否可以传输,目录权限是否正确 - 测试控制传输速率 - 测试软链接是否正常传输 ## 部署过程 ### NFS服务器端 ### 创建用户 ``` [root@nfs-194 ~]# useradd www -M [root@nfs-194~]# id www uid=1001(www) gid=1001(www) groups=1001(www) ``` ### 创建目录 ``` [root@nfs-194 ~]# mkdir -p /data/{www,bbs} [root@nfs-194 ~]# chown -R www:www /data/ [root@nfs-194 ~]# ls -lh /data/ total 8.0K drwxr-xr-x 2 www www 4.0K Mar 19 11:53 bbs drwxr-xr-x 2 www www 4.0K Mar 19 11:53 www ``` ### 配置rsync服务端 查看rsync版本 ``` [root@nfs-194 ~]# rsync --version rsync version 3.1.1 protocol version 31 ``` ### 创建rsync配置文件 注意, 把下面的注释去掉,不然可能会报错 ``` [root@nfs-194 ~]# vim /etc/rsyncd.conf [root@nfs-194 ~]# cat /etc/rsyncd.conf uid = www #rsync使用的用户 gid = www #rsync使用的用户组 use chroot = yes #如果需要把软链接也一起传输,则必须要配置为true,默认为no max connections = 200 #设置最大连接数,默认为0,负值为关闭这个模块 timeout = 300 #默认为0,建议为300-600(5-10)分钟 pid file = /var/run/rsyncd.pid #pid文件 lock file = /var/run/rsync.lock #lock锁 log file = /var/log/rsyncd.log #log文件 ignore errors #忽略I/O错误 read only = false #是否只读 list = false #是否允许客户端可以查看可用模块列表,默认为可以 hosts allow = 172.16.1.0/24 #允许同步的网段,写内网地址 #hosts deny = 0.0.0.0/32 #不允许同步的网段,可以不写 auth users = rsync_backup #系统不存在的用户,只用于同步认证 secrets file = /etc/rsync.passwd #用于密码认证的文件,里面有同步用户名和密码,客户端只需要密码即可 [www] #备份模块,可以制定不同的备份目录,客户端可以指定不同的模块 path = /data/www #www模块的备份目录 [bbs] #bbs备份模块 path = /data/bbs #bbs备份模块的备份目录 ``` ### 创建rsync密码文件并更改权限 ``` [root@nfs-194 ~]# vim /etc/rsync.passwd [root@nfs-194 ~]# cat /etc/rsync.passwd rsync_backup:vim26 [root@nfs-194 ~]# chmod 600 /etc/rsync.passwd [root@nfs-194 ~]# ls -lh /etc/rsync.passwd -rw------- 1 root root 19 Mar 19 12:02 /etc/rsync.passwd ``` ### 使rsync以deamon模式启动 ``` [root@nfs-194 ~]# /etc/init.d/rsync start [ ok ] Starting rsync (via systemctl): rsync.service. [root@nfs-88 ~]# ps -ef|grep rsync root 924 1 0 12:04 ? 00:00:00 /usr/bin/rsync --daemon --no-detach root 943 786 0 12:04 pts/0 00:00:00 grep rsync ``` ### NFS备份服务器端 ### 查看rsync版本 ``` [root@nfs-88 ~]# rsync --version rsync version 3.1.1 protocol version 31 ``` ### 创建用户和目录 ``` [root@nfs-88 ~]# useradd www -M [root@nfs-88 ~]# id www uid=1001(www) gid=1001(www) groups=1001(www) [root@nfs-88 ~]# mkdir -p /data/{www,bbs} [root@nfs-88 ~]# chown -R www:www /data/ [root@nfs-88 ~]# ls -lh /data/ total 8.0K drwxr-xr-x 2 www www 4.0K Mar 19 12:06 bbs drwxr-xr-x 2 www www 4.0K Mar 19 12:06 www ``` ### 创建rsync密码文件 ``` [root@nfs-88 ~]# vim /etc/rsync.passwd [root@nfs-88 ~]# cat /etc/rsync.passwd vim26 [root@nfs-88 ~]# chmod 600 /etc/rsync.passwd [root@nfs-88 ~]# ls -lh /etc/rsync.passwd -rw------- 1 root root 6 Mar 19 12:07 /etc/rsync.passwd ``` ##NFS服务端测试 ### 创建测试文件 ``` [root@nfs-88 ~]# cd /data/www/ [root@nfs-88 www]# echo "88www" > 88www.txt [root@nfs-88 www]# cat 88www.txt 194www [root@nfs-88 www]# ls -lh total 0 -rw-r--r-- 1 root root 0 Mar 19 12:21 88www.txt ``` ### 测试传输www ``` [root@nfs-88 www]# rsync -avzP --password-file=/etc/rsync.passwd /data/www/ rsync_backup@172.16.1.194::www sending incremental file list ./ 88www.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2) rsync: chgrp ".88www.txt.RYv0pa" (in www) failed: Operation not permitted (1) sent 126 bytes received 128 bytes 508.00 bytes/sec total size is 0 speedup is 0.00 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1183) [sender=3.1.1] ``` 结果显示失败,因为88www.txt的权限是root, 我们修改一下权限在测试一下 ``` [root@nfs-88 www]# chown www:www 88www.txt [root@nfs-88 www]# ls -lh 88www.txt -rw-r--r-- 1 www www 0 Mar 19 12:21 88www.txt [root@nfs-88 www]# rsync -avzP --password-file=/etc/rsync.passwd /data/www/ rsync_backup@172.16.1.194::www sending incremental file list ./ 88www.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2) sent 124 bytes received 46 bytes 340.00 bytes/sec total size is 0 speedup is 0.00 ``` 这一次没有提示失败,然后查看一下NFS备份端有没有文件生成 ``` [root@nfs-194 www]# ls -lh total 4.0K -rw-r--r-- 1 www www 6 Mar 19 12:26 88www.txt [root@nfs-194 www]# cat 88www.txt 88www ``` ### 测试传输bbs www测试没有问题,我们再测试传输一下bbs,步骤和上面一样 ``` [root@nfs-88 ~]# cd /data/bbs/ [root@nfs-88 bbs]# echo "88bbs" > 88bbs.txt [root@nfs-88 bbs]# cat 88bbs.txt 88bbs [root@nfs-88 bbs]# chown -R www:www 88bbs.txt [root@nfs-88 bbs]# ls -lh total 4.0K -rw-r--r-- 1 www www 6 Mar 19 12:53 88bbs.txt [root@nfs-88 bbs]# rsync -avzP --password-file=/etc/rsync.passwd /data/bbs/ rsync_backup@172.16.1.194::bbs sending incremental file list ./ 88bbs.txt 6 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2) sent 131 bytes received 46 bytes 354.00 bytes/sec total size is 6 speedup is 0.03 ``` ### NFS备份端查看 ``` [root@nfs-194 ~]# cd /data/bbs/ [root@nfs-194 bbs]# ls -lh total 4.0K -rw-r--r-- 1 www www 6 Mar 19 12:53 88bbs.txt [root@nfs-194 bbs]# cat 88bbs.txt 88bbs ``` ## NFS服务器部署sersync 下载地址 ``` https://code.google.com/archive/p/sersync/downloads ``` 解压安装 ``` [root@nfs-88 ~]# cd /opt/ [root@nfs-88 opt]# wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz [root@nfs-88 opt]# ls -lh total 712K -rw-r--r-- 1 root root 711K Jan 24 2016 sersync2.5.4_64bit_binary_stable_final.tar.gz [root@nfs-88 opt]# tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz [root@nfs-88 opt]# ls -lh total 716K drwxr-xr-x 2 root root 4.0K Oct 26 2011 GNU-Linux-x86 -rw-r--r-- 1 root root 711K Jan 24 2016 sersync2.5.4_64bit_binary_stable_final.tar.gz [root@nfs-88 opt]# mv GNU-Linux-x86/ sersync [root@nfs-88 opt]# ls -lh total 716K drwxr-xr-x 2 root root 4.0K Oct 26 2011 sersync -rw-r--r-- 1 root root 711K Jan 24 2016 sersync2.5.4_64bit_binary_stable_final.tar.gz [root@nfs-88 opt]# tree sersync sersync ├── confxml.xml └── sersync2 ``` ### 规范目录结构 ``` [root@nfs-88 sersync]# cd /opt/sersync/ [root@nfs-88 sersync]# ls confxml.xml sersync2 [root@nfs-88 sersync]# mkdir {bin,conf,logs} [root@nfs-88 sersync]# tree . ├── bin ├── conf ├── confxml.xml ├── logs └── sersync2 [root@nfs-88 sersync]# mv confxml.xml conf [root@nfs-88 sersync]# mv sersync2 bin/sersync [root@nfs-88 sersync]# tree . ├── bin │   └── sersync ├── conf │   └── confxml.xml └── logs ``` ### 备份配置文件 ``` [root@nfs-88 sersync]# cp conf/confxml.xml conf/confxml.xml.bak [root@nfs-88 sersync]# cd conf/ [root@nfs-88 conf]# ls confxml.xml confxml.xml.bak ``` ### 修改配置文件内容,这里只贴出主要的配置 每个备份模块都要有自己的一个单独的配置文件,不然同步的时候可能会出现问题 ``` [root@nfs-88 conf]# cat -n bbs_confxml.xml 1 <?xml version="1.0" encoding="ISO-8859-1"?> 2 <head version="2.5"> 3 <host hostip="localhost" port="8008"></host> 4 <debug start="false"/> 5 <fileSystem xfs="false"/> 6 <filter start="false"> 7 <exclude expression="(.*)\.svn"></exclude> 8 <exclude expression="(.*)\.gz"></exclude> 9 <exclude expression="^info/*"></exclude> 10 <exclude expression="^static/*"></exclude> 11 </filter> 12 <inotify> 13 <delete start="true"/> 14 <createFolder start="true"/> 15 <createFile start="false"/> 16 <closeWrite start="true"/> 17 <moveFrom start="true"/> 18 <moveTo start="true"/> 19 <attrib start="false"/> 20 <modify start="false"/> 21 </inotify> 22 23 <sersync> 24 <localpath watch="/data/bbs"> 25 <remote ip="172.16.1.194" name="bbs"/> 26 </localpath> 27 <rsync> 28 <commonParams params="-artuz"/> 29 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/> 30 <userDefinedPort start="false" port="874"/><!-- port=874 --> 31 <timeout start="false" time="100"/><!-- timeout=100 --> 32 <ssh start="false"/> 33 </rsync> 34 <failLog path="/opt/sersync/logs/bbs_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> ``` ### 写入环境变量 ``` [root@nfs-88 conf]# echo 'export PATH=/opt/sersync/bin:$PATH'>>/etc/profile [root@nfs-88 conf]# tail -1 /etc/profile export PATH=/opt/sersync/bin:$PATH [root@nfs-88 conf]# source /etc/profile [root@nfs-88 conf]# which sersync /opt/sersync/bin/sersync ``` ### 测试传输 在测试之前我们先在多创建一些目录,测试一下第一次传输会不会完全同步一次 ``` [root@nfs-88 conf]# cd /data/www/ [root@nfs-88 www]# ls 88www.txt [root@nfs-88 www]# [root@nfs-88 www]# for i in $(seq 1 1000);do echo "${i}_www" > www_${i}.txt;done; [root@nfs-88 www]# ls 88www.txt www_1.txt www_300.txt www_401.txt www_502.txt www_603.txt www_704.txt www_805.txt www_906.txt www_1000.txt www_200.txt www_301.txt www_402.txt www_503.txt www_604.txt www_705.txt www_806.txt www_907.txt www_100.txt www_201.txt www_302.txt www_403.txt www_504.txt www_605.txt www_706.txt www_807.txt www_908.txt www_101.txt www_202.txt www_303.txt www_404.txt www_505.txt www_606.txt www_707.txt www_808.txt www_909.txt www_102.txt www_203.txt www_304.txt www_405.txt www_506.txt www_607.txt www_708.txt www_809.txt www_90.txt www_103.txt www_204.txt www_305.txt www_406.txt www_507.txt www_608.txt www_709.txt www_80.txt www_910.txt www_104.txt www_205.txt www_306.txt www_407.txt www_508.txt www_609.txt www_70.txt www_810.txt www_911.txt ======================================== ``` 然后使用rsync传输,启动后的界面.这里没有使用-d命令,而是前台启动.方便我们排错 ``` [root@nfs-88 www]# sersync -r -o /opt/sersync/conf/www_confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -r rsync all the local files to the remote servers before the sersync work option: -o config xml name: /opt/sersync/conf/www_confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.passwd config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data/www && rsync -artuz -R --delete ./ rsync_backup@172.16.1.194::www --password-file=/etc/rsync.passwd >/dev/null 2>&1 run the sersync: watch path is: /data/www ``` 查看194的www目录,发现已经同步过来了 ``` [root@nfs-194 ~]# cd /data/www/ [root@nfs-194 www]# ls 88www.txt www_1.txt www_300.txt www_401.txt www_502.txt www_603.txt www_704.txt www_805.txt www_906.txt www_1000.txt www_200.txt www_301.txt www_402.txt www_503.txt www_604.txt www_705.txt www_806.txt www_907.txt www_100.txt www_201.txt www_302.txt www_403.txt www_504.txt www_605.txt www_706.txt www_807.txt www_908.txt www_101.txt www_202.txt www_303.txt www_404.txt www_505.txt www_606.txt www_707.txt www_808.txt www_909.txt www_102.txt www_203.txt www_304.txt www_405.txt www_506.txt www_607.txt www_708.txt www_809.txt www_90.txt www_103.txt www_204.txt www_305.txt www_406.txt www_507.txt www_608.txt www_709.txt www_80.txt www_910.txt www_104.txt www_205.txt www_306.txt www_407.txt www_508.txt www_609.txt www_70.txt www_810.txt www_911.txt www_105.txt www_206.txt www_307.txt www_408.txt www_509.txt www_60.txt www_710.txt www_811.txt www_912.txt ``` 然后我们测试一下新增文件会不会同步过来,保持sersync的前端窗口,然后新开一个窗口 这里我们在88上进行了创建文件夹并移动文件的操作 ``` [root@nfs-88 www]# mkdir www_test [root@nfs-88 www]# mv *.txt www_test/ [root@nfs-88 www]# ls -lh total 36K drwxr-xr-x 2 root root 36K Mar 19 13:49 www_test ``` 在194服务器上观察发现也相应的执行了创建和移动的操作,只不过速度不是即使完成的,之所以会这样是因为每个文件的变化都是由inotify监控的,所以相当于记录了每个文件的移动位置而不是执行shell命令. ``` [root@nfs-194 www]# ls|wc -l 473 [root@nfs-194 www]# ls|wc -l 460 [root@nfs-194 www]# ls|wc -l 442 [root@nfs-194 www]# ls|wc -l 423 [root@nfs-194 www]# ls|wc -l 406 [root@nfs-194 www]# ls|wc -l 390 [root@nfs-194 www]# ls|wc -l 43 [root@nfs-194 www]# ls|wc -l 24 [root@nfs-194 www]# ls|wc -l 1 [root@nfs-194 www]# ls -lh total 36K drwx------ 2 www www 36K Mar 19 13:49 www_test ``` ### 一致性同步测试 通过观察前台启动的日志输出我们可以看到rsync默认是带有--delete也就是完全同步的参数的 ``` execute command: cd /data/www && rsync -artuz -R --delete ./ rsync_backup@172.16.1.194::www --password-file=/etc/rsync.passwd >/dev/null 2>&1 ``` 所以备份的目录会强制和对端的NFS保持一直. 这里我们假如在备份端创建一个文件或目录,然后在服务端创建文件,看会不会把备份端的给删掉 备份端 ``` [root@nfs-194 www]# touch www_194.txt [root@nfs-194 www]# ls -lh total 36K -rw-r--r-- 1 root root 0 Mar 19 13:56 www_194.txt drwx------ 2 www www 36K Mar 19 13:49 www_test ``` 服务端 ``` [root@nfs-88 www]# touch www_88.txt [root@nfs-88 www]# ls -lh total 36K -rw-r--r-- 1 root root 0 Mar 19 13:57 www_88.txt drwxr-xr-x 2 root root 36K Mar 19 13:49 www_test ``` 再次查看备份端,发现并没有被删掉 ``` [root@nfs-194 www]# ls -lh total 36K -rw-r--r-- 1 root root 0 Mar 19 13:56 www_194.txt -rw------- 1 www www 0 Mar 19 13:57 www_88.txt drwx------ 2 www www 36K Mar 19 13:49 www_test ``` 然后我们在服务端手动删掉刚才创建的文件 ``` [root@nfs-88 www]# rm www_88.txt rm: remove regular empty file ‘www_88.txt’? y [root@nfs-88 www]# ls -lh total 36K drwxr-xr-x 2 root root 36K Mar 19 13:49 www_test ``` 再次查看备份端,发现被删掉了. ``` [root@nfs-194 www]# ls -lh total 36K -rw-r--r-- 1 root root 0 Mar 19 13:56 www_194.txt drwx------ 2 www www 36K Mar 19 13:49 www_test ``` 结论猜测. 为什么手动在194上创建的目录没有触发删除条件呢? 是因为rsync启动的时候会全部扫描一次需要备份的目录的文件列表. 然后同步完成后的更改就由inotify负责记录. 而我们手动创建的目录是在sersync启动之后创建的,所以rsync启动时的文件列表里并没有这个文件 其次,inotify只负责记录服务端的更改,所以inotify里也没有我们要记录的文件列表 猜测 加入重新启动一次sersync,重新触发一次--delete可能会删掉 猜测验证 重启sersyc ``` [root@nfs-88 www]# sersync -r -o /opt/sersync/conf/www_confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -r rsync all the local files to the remote servers before the sersync work option: -o config xml name: /opt/sersync/conf/www_confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.passwd config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data/www && rsync -artuz -R --delete ./ rsync_backup@172.16.1.194::www --password-file=/etc/rsync.passwd >/dev/null 2>&1 run the sersync: watch path is: /data/www ^C [root@nfs-88 www]# sersync -r -o /opt/sersync/conf/www_confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -r rsync all the local files to the remote servers before the sersync work option: -o config xml name: /opt/sersync/conf/www_confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.passwd config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data/www && rsync -artuz -R --delete ./ rsync_backup@172.16.1.194::www --password-file=/etc/rsync.passwd >/dev/null 2>&1 run the sersync: watch path is: /data/www ``` 然后观察备份端的目录变化,可以发现原来手动创建的文件已经被删除了.验证了我们的结论是正确的 ``` [root@nfs-194 www]# ls -lh total 36K -rw-r--r-- 1 root root 0 Mar 19 13:56 www_194.txt drwx------ 2 www www 36K Mar 19 13:49 www_test [root@nfs-194 www]# ls -lh total 36K drwx------ 2 www www 36K Mar 19 13:49 www_test ``` 上面这种情况是因为我们启动sersyc的时候使用了-r选项,也就是说启动的时候会先完整同步一次 Sersync同步参数说明 ``` Sersync参数说明 -r -r参数作用是在开启实时监控之前对主服务器目录与远程目标机目录进行一次整体同步,如果需要将sersync运行前,主服务器目录下已经存在的所有文件或目录全部同步到远端,则需要指定-r参数运行sersync,将本地与远程整体同步一次**。说明:如果设置了过滤器,即在xml配置文件中,filter为true,则暂时不能使用-r参数进行整体同步。** –o confxml.xml 不指定-o参数时,sersync可执行文件目录下的默认配置文件confxml.xml,如果需要使用其他的配置文件,可以使用-o参数指定其他配置文件,通过-o参数,我们可以指定多个不同的配置文件,从而实现sersync多进行多实例的数据同步。 –n number -n参数为指定默认的线程池的现充总数。 ``` ### 对比sersync不同的启动参数 使用-r ``` [root@nfs-88 www]# sersync -r -o /opt/sersync/conf/www_confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -r rsync all the local files to the remote servers before the sersync work option: -o config xml name: /opt/sersync/conf/www_confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.passwd config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data/www && rsync -artuz -R --delete ./ rsync_backup@172.16.1.194::www --password-file=/etc/rsync.passwd >/dev/null 2>&1 run the sersync: watch path is: /data/www ``` 不使用-r ``` [root@nfs-88 www]# sersync -o /opt/sersync/conf/www_confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -o config xml name: /opt/sersync/conf/www_confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.passwd config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate run the sersync: watch path is: /data/www ``` 可见使用-r则在启动sersync前执行了一条--delete的语句 ``` rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data/www && rsync -artuz -R --delete ./ rsync_backup@172.16.1.194::www --password-file=/etc/rsync.passwd >/dev/null 2>&1 run the sersync: ``` ### 测试软链接 由于我们的rsync服务端配置文件里的use chroot 设置了no.默认是不会 测试 1.在88服务端创建软链接,然后查看194会不会同步创建 ``` [root@nfs-88 www]# ls -lh total 36K lrwxrwxrwx 1 root root 9 Mar 19 14:23 www_ln -> www_test/ drwxr-xr-x 2 root root 36K Mar 19 13:49 www_test ``` 查看194的www目录会发现,虽然已经创建了,也显示是软链接,但是链接的目标目录位置不对,由于不存在这个目录,所以也就就不去 ``` [root@nfs-194 www]# ls -lh total 36K lrwxrwxrwx 1 www www 24 Mar 19 14:24 www_ln -> /rsyncd-munged/www_test/ drwx------ 2 www www 36K Mar 19 13:49 www_test [root@nfs-194 www]# cd www_ln -bash: cd: www_ln: No such file or directory ``` 更改rsync的配置文件,设置use chroot = yes.然后重启rsync,在重新传输一次 ``` [root@nfs-194 www]# vim /etc/rsyncd.conf [root@nfs-194 www]# grep "chroot" /etc/rsyncd.conf use chroot = yes [root@nfs-194 www]# /etc/init.d/rsync restart [ ok ] Restarting rsync (via systemctl): rsync.service. [root@nfs-194 www]# ps -ef|grep rsync root 5491 1 0 14:28 ? 00:00:00 /usr/bin/rsync --daemon --no-detach root 5510 785 0 14:28 pts/0 00:00:00 grep rsync ``` 重启sersync, 重新同步一次 然后在观察一次备份服务器的www目录,发现已经修改过来了,并且可以正常进入 ``` [root@nfs-194 www]# ls -lh total 36K lrwxrwxrwx 1 www www 9 Mar 19 14:28 www_ln -> www_test/ drwx------ 2 www www 36K Mar 19 13:49 www_test [root@nfs-194 www]# cd www_ln [root@nfs-194 www_ln]# ls -lh |wc -l 1002 ``` 结论: 通过修改rsync的use chroot = yes选项可以同步并保留正确的软链接目录属性 ### 控制传输速率 如果传输的文件太多,而服务器本身也提供业务,如果全部给rsync用来同步传输文件可能会使网卡占满, 所以这里我们可以通过rsync的参数--bwlimit来控制传输速率,由于我们使用了sersync, 所以我们就尝试在sersync的配置文件里修改参数 ``` [root@nfs-88 www]# cat -n /opt/sersync/conf/www_confxml.xml 28 <commonParams params="-artuz --bwlimit=10240"/> ``` 这里配置文件里的rsync区块内我们添加了rsync控制速率的参数,然后重启sersync,查看是否生效 ``` [root@nfs-88 www]# sersync -r -o /opt/sersync/conf/www_confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -r rsync all the local files to the remote servers before the sersync work option: -o config xml name: /opt/sersync/conf/www_confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.passwd config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data/www && rsync -artuz --bwlimit=10240 -R --delete ./ rsync_backup@172.16.1.194::www --password-file=/etc/rsync.passwd >/dev/null 2>&1 run the sersync: watch path is: /data/www ``` 可见,日志输出里是是已经添加了控制速率的命令,但是我们并不知道有没有生效,所以我们上传一个大文件,然后测试 ``` execute command: cd /data/www && rsync -artuz --bwlimit=10240 -R --delete ./ rsync_backup@172.16.1.194::www --password-file=/etc/rsync.passwd >/dev/null 2>&1 ``` 为了不影响传输实验,我们先停掉sersync服务,然后上传一个上G的文件,然后使用iftop命令查看网卡流量 安装iftop ``` [root@nfs-88 www]# apt-get install iftop ``` 上传大镜像 ``` [root@nfs-88 www]# ls -lh total 4.1G -rw-r--r-- 1 root root 4.1G Mar 19 14:52 CentOS-7-x86_64-DVD-1511.iso lrwxrwxrwx 1 root root 9 Mar 19 14:23 www_ln -> www_test/ drwxr-xr-x 2 root root 36K Mar 19 13:49 www_test drwxr-xr-x 2 root root 4.0K Mar 19 14:37 www_test2 ``` 重新启动sersync ``` [root@nfs-88 www]# /opt/sersync/bin/sersync -r -o /opt/sersync/conf/www_confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -r rsync all the local files to the remote servers before the sersync work option: -o config xml name: /opt/sersync/conf/www_confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.passwd config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data/www && rsync -artuz --bwlimit=10240 -R --delete ./ rsync_backup@172.16.1.194::www --password-file=/etc/rsync.passwd >/dev/null 2>&1 ``` 新窗口启动iftop,监听内网网卡eth1,使用-B显示M单位 ![](https://box.kancloud.cn/4bb9befd8511f39cc0e613dfdda38e45_1977x1045.png) 可以看到右下角的rates控制在了9M左右, 没有超过我们限制的10M,也就是说控制传输速率的限制是生效的. 为了对比测试,现在删掉控制速率的参数,然后再测试一下 首先删掉备份端的镜像,然后去掉速率参数重新测试一下 删掉控制速率的参数,然后重新启动sersync ``` [root@nfs-88 www]# /opt/sersync/bin/sersync -r -o /opt/sersync/conf/www_confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -r rsync all the local files to the remote servers before the sersync work option: -o config xml name: /opt/sersync/conf/www_confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.passwd config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data/www && rsync -artuz -R --delete ./ rsync_backup@172.16.1.194::www --password-file=/etc/rsync.passwd >/dev/null 2>&1 ``` 然后iftop查看网卡发现rates速率显示的是22M,可见传输速率是没有限制的. ![](https://box.kancloud.cn/329b2b81144ae6cac592a55e5fc55d22_1977x1045.png) ### 使用supervision管理sersync进程 ``` root@nfs-88:~# cat /etc/supervisor/conf.d/program.conf [program:bbs] command = /opt/sersync/bin/sersync -r -o /opt/sersync/conf/bbs_confxml.xml -n 5 autostart = true startsecs = 5 autorestart = true startretries = 3 user = root redirect_stderr = true stdout_logfile_maxbytes = 20MB stdout_logfile_backups = 20 stdout_logfile = /opt/sersync/logs/bbs_sersync.log ```