[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單位

可以看到右下角的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,可見傳輸速率是沒有限制的.

### 使用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
```
- 運維筆記
- 零: 安裝部署篇
- Zabbix
- HAproxy
- Nginx
- Apache
- Tomcat
- Mysql
- Redis
- ELK
- MongoDB
- hadoop
- GIt
- JDK
- Docker
- OpenVPN
- iRedMail
- GitLab
- ESXi
- Jenkins
- NFS
- rsync
- Python
- Keepalived
- 軟件打包篇
- 私有倉庫篇
- kafka
- zookeeper
- Spark
- Linux基礎篇
- 1.1 Linux系統介紹
- 1.3 系統優化
- 1.4 問題總結
- Linux核心命令
- 聲明
- 1-文件和目錄操作命令
- 1.3 tree: 以樹形結構顯示目錄下的內容
- 2-文件過濾及內容編輯處理命令
- find:查找目錄下的文件
- 3-文本處理三劍客
- 4-Linux信息顯示與搜索文件命令
- du:統計磁盤空間使用情況
- 5-文件備份與壓縮命令
- rsync:文件同步工具
- 6-Linux用戶管理及用戶信息查詢命令
- 7-Linux磁盤與文件系統管理命令
- 8-Linux進程管理命令
- 9-Linux網絡管理命令
- 10-Linux系統管理命令
- 11-Linux系統常用內置命令
- 服務相關
- Nginx
- 安裝部署
- 文件路徑說明
- 服務啟動管理
- 配置文件說明
- json格式日志的配置文件
- https代理
- nginx負載均衡代理websocket
- 服務優化
- 維護腳本
- 問題總結
- rewrite帶?跳轉
- nginx查看默認安裝的模塊
- HAproxy
- 2.2.1 安裝部署
- 2.2.2 文件路徑說明
- 2.2.3 服務啟動管理
- 2.2.4 配置文件說明
- ha代理websocket
- 2.2.5 集群高可用
- 2.2.6 服務優化
- 2.2.7 維護腳本
- 2.2.8 問題總結
- PHP
- 2.3.1 安裝部署
- keepalived
- 配置文件注釋
- 配置多組VIP
- Java
- 安裝部署java
- ansible部署java
- supervisor
- supervisor安裝部署測試
- iptables
- CentOS7安裝配置iptables
- pm2相關
- kafka相關
- kafka和zookeeper集群安裝部署
- nodejs
- 安裝部署nodejs
- sersync
- sersync備份圖片服務
- gitlab相關
- gitlab安裝部署
- gitlab強制修改密碼
- gitlab不同的連接方式
- jenkins相關
- 安裝部署jenkins
- python相關
- python虛擬環境
- debian安裝pyhton3.6
- Turnserver服務器搭建
- NFS相關
- 固定NFS和rsync端口
- go相關
- 安裝go
- maven相關
- debian私有倉庫搭建
- 翻墻
- linux下的百度云盤
- 私有網盤owncloud部署
- crontab定時任務
- 數據庫相關
- mongodb
- 重用操作命令
- 副本集配置文件
- 慢查詢設置
- 數據備份恢復以及數據導入導出
- 從庫允許只讀設置
- redis
- redis單節點安裝部署
- 配置文件注釋
- redis分析工具rdbtools使用
- redis數據導入導出集群工具
- redis內存信息解釋
- redis警告優化
- PHP會話session保存到redis集群
- redis啟動關閉腳本
- elk相關
- elastersearch常用命令
- elasticsearch6所需配置文件
- elasticsearch6的head插件安裝
- elk6安裝腳本
- filebeat收集php日志多行轉換
- filebeat自定義index
- elk-dockercompes配置
- docker部署elk收集runtime日志
- elasticsearch6添加新節點報錯
- elasticsearch查看索引
- docker部署es+filebeat+kibana
- mysql相關
- mysql日志
- mysql密碼過期
- mysql用戶授權訪問庫
- mysql安裝部署
- MariaDB安裝部署
- 大數據相關
- hadoop相關
- Ambari2.6離線安裝hadoop
- Ambari安裝出現的問題
- 檢測hadoop當前運行了哪些服務端口腳本
- 監控相關
- CentOS7安裝Zabbix3.4
- docker安裝zabbix
- 運維腳本
- 根據配置文件檢查服務端口運行狀態
- nginx日志分析
- ngixn日志合并腳本
- nginx查詢關鍵鏈接響應時間
- 圖片同步腳本
- 批量獲取iptables設置的端口然后驗證本機端口是否存活
- 按日期統計不同接口的響應時間
- php進程假死狀態定時清理
- 運維自動化
- deb打包命令
- ansible相關
- ansible部署
- ansible配置推送
- 編程語言
- 1-SHELL
- 2-Python
- 3-GO
- 有趣的工具
- vim
- 聲明
- 第1章: Vim解決問題的方式
- 技巧1-認識 . 命令
- 技巧2-不要自我重復
- 技巧3-以進為退
- 技巧4-執行,重復,回退
- 技巧5-查找并手動替換
- 技巧6-認識 . 范式
- 第2章: 普通模式
- 第3章: 插入模式
- 第4章: 可視模式
- 第5章: 命令行模式
- 第6章: 管理多個文件
- 第7章: 打開及保存文件
- 第8章: 用動作命令在文檔中移動
- 第9章: 在文件間跳轉
- 第10章: 復制與粘貼
- 第11章: 宏
- 第12章: 按模式匹配及按原義匹配
- 第13章: 查找
- 第14章: 替換
- 第15章: global命令
- 第16章: 通過ctags建立索引并用其瀏覽源代碼
- 第17章: 編譯代碼并通過Quickfix列表瀏覽錯誤信息
- 第18章: 通過grep,vimgrep以及其他工具對整個工程進行查找
- 第19章: 自動補全
- 第20章: 利用Vim的拼寫檢查器查找并更正拼寫錯誤
- 第21章: 接下來干什么
- 附錄A 根據個人喜好定制Vim
- 終端命令記錄回放工具
- screen使用
- iftop查看網絡流量
- dna螺旋
- shell下的俄羅斯方塊
- 正經英語
- 有意思
- 問題記錄
- python相關
- pip安裝缺少openssl和libssl
- shell相關
- debian下執行數組變量報錯
- 服務相關問題
- ububtu安裝apache2報錯
- php升級mongo拓展插件
- elk節點分片失敗
- 操作系統相關問題
- 運維記錄
- 數據庫相關
- mongo3.4安裝以及優化參數腳本
- mongodb版本升級及優化
- redis動態取消rdb保存配置
- 代理負載均衡相關
- haproxy匹配ua規則分離搜索引擎流量
- haproxy不記錄某個域名或多個域名的日志
- 其他相關
- ImageMagick升級
- NFS強制卸載掛載
- 命令相關
- curl獲取指定域名ip的狀態碼
- awk相關記錄
- rsync傳輸限速指定ssh端口
- 操作系統相關
- centos使用阿里源
- ubuntu更換國內源
- 查看操作系統版本
- 內核優化
- Centos7修改語言為英文
- debian安裝xfs格式化工具
- 查看磁盤信息
- debian的啟動管理工具
- debian安裝vmtools
- debian重啟網卡不生效的解決
- 容器虛擬化
- Docker相關
- dokcer安裝
- docker常用命令
- docker簡單腳本
- Dockerfile相關
- 帶ssh的debian鏡像
- deocker創建簡單鏡像
- 官方文檔
- docker-compose安裝
- 報錯
- docker鏡像加速
- k8s相關
- k8s常用命令
- k8s名詞解釋
- k8s相關的 yaml文件
- VM相關
- 使用VMwareWorkstation批量操作linux虛擬機
- 在windows下使用shell批量操作Vmwarworkstation
- windows下批量操作虛擬機
- ESXI相關
- vsphere網絡相關
- 小愛好
- 不方便展示