sersync其實是利用inotify和rsync兩種軟件技術來實現數據實時同步功能的,inotify是用于監聽sersync所在服務器上的文件變化,結合rsync軟件來進行數據同步,將數據實時同步給客戶端服務器。
工作過程:在同步主服務器上開啟sersync,負責監聽文件系統的變化,然后調用rsync命令把更新的文件同步到目標服務器上,主服務器上安裝sersync軟件,目標服務器上安裝rsync服務。
1、客戶端配置
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
|
[root@localhost2 ~] # cat /etc/rsyncd.conf ##created by cai at 2018-2-24 uid= rsync gid= rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd .pid lock file = /var/run/rsync .lock log file = /var/log/rsyncd .log [data] path = /data/ ignore errors read only = false list = false hosts allow = 192.168.181.128:52000 /24 hosts deny = 0.0.0.0 /32 auth users = rsync_body secrets file = /etc/rsync .password [root@localhost2 ~] # ls -ld /data/ drwxrwxrwx. 3 rsync rsync 4096 feb 24 16:58 /data/ [root@localhost2 ~] # cat /etc/rsync.password rsync_body:admin [root@localhost2 ~] # ls -ld /etc/rsync.password -rw-------. 1 root root 17 feb 24 16:26 /etc/rsync .password #600權限 [root@localhost2 ~] # netstat -lntup| grep "rsync" tcp 0 0 0.0.0.0:873 0.0.0.0:* listen 1497 /rsync tcp 0 0 :::873 :::* listen 1497 /rsync |
2、主服務器配置
1
2
3
4
|
[root@localhost1 ~] # cat /etc/rsync.password admin [root@localhost1 ~] # ls -ld /etc/rsync.password -rw------- 1 root root 6 feb 24 03:54 /etc/rsync .password |
3、安裝sersync服務
采用inotify來對文件進行監控,當監控到文件有文件發生改變的時候,就會調用rsync實現觸發式實時同步!
安裝sersync(注意sersync是工作在rsync的源服務器上,也就是客戶端上)
1
2
3
4
5
6
7
8
9
10
11
|
[root@salt-client01 ~] # cd /usr/local/src/ [root@salt-client01 src] # tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -c /usr/local/ gnu-linux-x86/ gnu-linux-x86 /sersync2 gnu-linux-x86 /confxml .xml [root@salt-client01 src] # cd /usr/local/ [root@salt-client01 local ] # mv gnu-linux-x86 sersync [root@salt-client01 local ] # cd sersync/ [root@salt-client01 sersync] # mkdir conf bin log [root@salt-client01sersync] # mv confxml.xml conf [root@salt-client01sersync] # mv sersync2 bin/sersync |
修改配置文件
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
|
***********************************30行開始****************************** <commonparams params= "-artuz" /> #-artuz為rsync同步時的參數 <authstart= "true" users = "rsync的虛擬用戶名(rsync_backup)" passwordfile= "rsync的密碼文件" /> <userdefinedport start= "true" port= "873" /><!-- port=874 --> <timeout start= "false" time = "100" /><!--timeout=100 --> <sshstart= "false" /> ************************************第36行*********************************** <faillogpath= "自己定義的log文件夾(/usr/local/sersync/log)rsync_fail_log.sh" timetoexecute= "60" /><!--defaultevery 60mins execute once--> ******************************************************************************* *注:若有多個目錄備份可以穿件多個配置文件在啟動時的-o參數中添加即可 [root@salt-client01 conf] # diff confxml.xml confxml.xml.bak 24,25c24,25 < <localpath watch = "/data/" > #data就是本地需要同步的文件夾到服務器端的目錄 < <remote ip= "192.168.91.166" name= "data" /> #data (server的模塊名)是rsync 服務端的文件夾,也就是推送到服務器端的目標文件夾,可以配置多個, --- > <localpath watch = "/opt/tongbu" > > <remote ip= "127.0.0.1" name= "tongbu1" /> 31c31 < <auth start= "true" users = "rsync_body" passwordfile= "/etc/rsync.password" /> #true 才能生效,rsync_body同步時候虛擬賬號,后面是密碼文件 --- > <auth start= "false" users = "root" passwordfile= "/etc/rsync.pas" /> 33c33 < <timeout start= "true" time = "100" /><!-- timeout=100 --> #true 才能生效 --- > <timeout start= "false" time = "100" /><!-- timeout=100 --> 36c36 < <faillog path= "/usr/local/sersync/log/rsync_fail_log.sh" timetoexecute= "60" /><!--default every 60mins execute once--> #檢測rsync進程判斷,沒有自動啟 --- > <faillog path= "/tmp/rsync_fail_log.sh" timetoexecute= "60" /><!--default evepry 60mins execute once--> |
啟動sersync
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
|
[root@salt-client01 src] # echo 'export path=$path:/usr/local/sersync/bin'>>/etc/profile #聲明環境變量 [root@salt-client01 src] # source /etc/profile [root@salt-client01 src] # sersync2 -r -d -o /usr/local/sersync/conf/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: -d run as a daemon option: -o config xml name: /usr/local/sersync/conf/confxml .xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 daemon start,sersync run behind the console use rsync password- file : user is rsync_body passwordfile is /etc/rsync .password 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 && rsync -artuz -r --delete ./ --timeout=100 [email protected]::data --password- file = /etc/rsync .password > /dev/null 2>&1 run the sersync: watch path is: /data #此時可以看出sersync已經啟動成功了 #檢測腳本 [root@salt-client01 log] # pwd /usr/local/sersync/log [root@salt-client01 log] # vim rsync_fail_log.sh [root@salt-client01 log] # chmod +x rsync_fail_log.sh [root@salt-client01 ~] # cat /usr/local/sersync/log/rsync_fail_log.sh #!/bin/bash #purpose: check sersync whether it is alive #author: cai meng zhi sersync= "/usr/local/sersync/bin/sersync2" conf_file= "/usr/local/sersync/conf/confxml.xml" status=$( ps aux | grep 'sersync2' | grep - v 'grep' | wc -l) if [ $status - eq 0 ]; then $sersync -d -r -o $conf_file & else exit 0; fi 腳本寫好以后,添加到計劃任務中去 * /1 * * * * /bin/bash /usr/local/sersync/log/rsync_fail_log .sh > /dev/null 2>&1 測試同步: 客戶端新增文件 [root@salt-client01 data] # cp /etc/passwd 192.168.91.156.passwd [root@salt-client01 data] # ll total 4 -rw-r--r-- 1 root root 1928 nov 10 18:15 192.168.91.156. passwd -rw-r--r-- 1 root root 0 nov 10 17:27 3 服務端檢測 [root@salt-master data] # cd /data/ [root@salt-master data] # ll total 8 -rw-r--r-- 1 root root 1928 nov 10 18:15 192.168.91.156. passwd #說明已經同步過來了 -rw-r--r-- 1 root root 0 nov 10 17:27 3 drwxr-xr-x 2 root root 4096 nov 10 17:27 data 客戶端測試刪除 [root@salt-client01 data] # rm rf 192.168.91.156.passwd rm : cannot remove `rf': no such file or directory rm : remove regular file `192.168.91.156. passwd '? y [root@salt-client01 data] # ll total 0 -rw-r--r-- 1 root root 0 nov 10 17:27 3 [root@salt-client01 data] # 服務器端: [root@salt-master data] # ll total 4 -rw-r--r-- 1 root root 0 nov 10 17:27 3 #說明已經刪除掉了 drwxr-xr-x 2 root root 4096 nov 10 17:27 data |
4、常見錯誤匯總
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
|
錯誤一: @error: auth failed on module xxxxx rsync : connection unexpectedly closed(90 bytes read so far) rsync error: error in rsync protocoldata stream (code 12) at io.c(150) 說明:這是因為密碼設置錯了,無法登入成功,檢查一下 rsync . pwd ,看客服是否匹配。還有服務器端沒啟動 rsync 服務也會出現這種情況。 錯誤二: password file must not beother-accessible continuing without password file password: 說明:這是因為rsyncd.pwdrsyncd.sec的權限不對,應該設置為600。如:chmod600 rsyncd. pwd 錯誤三: @error: chroot failed rsync : connection unexpectedly closed(75 bytes read so far) rsync error: error in rsync protocoldata stream (code 12) at io.c(150) 說明:這是因為你在 rsync .conf中設置的 path 路徑不存在,要新建目錄才能開啟同步 錯誤四: rsync : failed to connect to218.107.243.2: no route to host (113) rsync error: error in socket io (code10) at clientserver.c(104) [receiver=2.6.9] 說明:防火墻問題導致,這個最好先徹底關閉防火墻,排錯的基本法就是這樣,無論是s還是c,還有ignore errors選項問題也會導致 錯誤五: @error: access denied to www fromunknown (192.168.1.123) rsync : connection unexpectedly closed(0 bytes received so far) [receiver] rsync error: error in rsync protocoldata stream (code 12) at io.c(359) 說明:此問題很明顯,是配置選項hostallow的問題,初學者喜歡一個允許段做成一個配置,然后模塊又是同一個,致使導致 錯誤六: rsync error: received sigint,sigterm, or sighup (code 20) at rsync .c(244) [generator=2.6.9] rsync error: received sigusr1 (code19) at main.c(1182) [receiver=2.6.9] 說明:導致此問題多半是服務端服務沒有被正常啟動,到服務器上去查查服務是否有啟動,然后查看下 /var/run/rsync .pid 文件是否存在,最干脆的方法是殺死已經啟動了服務,然后再次啟動服務或者讓腳本加入系統啟動服務級別然后 shutdown -r now服務器 錯誤七: rsync : read error: connection resetby peer (104) rsync error: error in rsync protocoldata stream (code 12) at io.c(604) [sender=2.6.9] 說明:原數據目錄里沒有數據存在 |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/caicairui/archive/2018/02/26/8472888.html