第十九章 同步工具之rsync
一、rsync基礎
1、常用同步方法
SCP、NFS、SFTP、http、samba、rsync、drbd(基於文件系統同步,效率高)
2、rsync介紹
rsync,英文全稱是remote synchronize,是一款實現遠程同步功能的免費軟件,它在同步文件的同時,可以保持原來文件的權限、時間、軟硬鏈接等附加信息。 rsync是用 “rsync 算法”提供了一個客戶機和遠程文件服務器的文件同步的快速方法,而且可以通過ssh方式來傳輸文件。甚至還可以實現只同步一個文件裏有變化的內容部分,所以可以實現快速的同步備份數據。
同時,rsync還可以實現同步本地數據、刪除文件和目錄的功能。一個rsync相當於scp、cp、rm,並且還在性能上優於它們每一個命令。
3、rsync特性
能更新整個目錄和樹和文件系統;
有選擇性的保持符號鏈鏈、硬鏈接、文件屬於、權限、設備以及時間等;
對於安裝來說,無任何特殊權限要求;
對於多個文件來說,內部流水線減少文件等待的延時;
能用rsh、ssh 或直接端口做爲傳輸入端口(rsync本身不對數據加密);
支持匿名rsync 同步文件,是理想的鏡像工具;
4、rsync在企業的工作場景
(1)服務器之間的數據同步(cron+rsync)
(2)把所有客戶服務器數據同步到備份服務器(cron+rsync)
(3)與inotify或sersync配合,做實時的數據同步(rsync+inotify/sersync)
5、rsync客戶端常用參數
(1)參數說明
-v, --verbose 詳細模式輸出。
-z, --compress 對備份的文件在傳輸時進行壓縮處理。
-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rlptgoD。
-r, --recursive 對子目錄以遞歸模式處理。
-t, --times 保持文件時間信息。
-o, --owner 保持文件屬主信息。
-p, --perms 保持文件權限。
-g, --group 保持文件屬組信息。
-D, --devices 保持設備文件信息。
-l, --links 保留軟鏈結。
-e, --rsh=command 指定使用rsh、ssh方式進行數據同步。
--exclude=PATTERN 指定排除不需要傳輸的文件模式。
--exclude-from=FILE 排除FILE中指定模式的文件。
--bwlimit=KBPS 限制I/O帶寬,KBytes per second。
(2)工作中常用參數
一般運維中經常使用的參數是-avz(相當於-vzrtopgDl)
(3)參數使用演示
a、使用--bwlimit參數限制帶寬
rsync -avz --bwlimit=100 -e 'ssh' a.log [email protected]:/wddg/log/
b、使用-avz參數
rsync -avz -e 'ssh' [email protected]:/wddg/log/
6、rsync的工作方式
一般來說,rsync大致使用3種主要的傳輸數據方式。
(1)單個主機本地之間的數據傳輸(此時類似於cp命令的功能)
a、語法
rsync [選項] 源文件或目錄 目標文件或目錄
b、實例1:普通拷貝
rsync /etc/hosts /tmp/ 等價於 cp /etc/hosts /tmp/
c、實例2:保持文件屬性
rsync –avz /etc/hosts /tmp/ 等價於cp -dpr /etc/hosts /tmp/ #-dpr=-a
d、實例3:刪除文件
rsync刪除文件或目錄的實質還是拷貝,就是將源目錄去替換目標目錄。源目錄中是什麼,目標目錄中就是什麼,目標目錄中原有的文件均被刪除
(i)首先,創建一個空目錄做爲源目錄
mkdir /srcnull
(ii)在目標目錄中創建文件
touch /data/a{1,2,3}.txt
ll /data
-rw-r--r-- 1 root root 0 Feb 20 02:45 a1.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a2.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a3.txt
(iii)刪除時,源目錄後不帶/
rsync –r -–delete /srcnull /data/ #srcnull後沒有加/,刪除不成功,沒能刪除/data下的文件
ll /data
-rw-r--r-- 1 root root 0 Feb 20 02:45 a1.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a2.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a3.txt
(iv)刪除時,源目錄後帶/
rsync –r –-delete /srcnull/ /data/ #srcnull後加/,刪除成功,刪除了/data下的文件
ll data/
total 0
(v)原因:如果src目錄後不帶斜槓,那麼是將src目錄複製到dest中,包含了src目錄,帶斜槓是把src目錄下的文件同步到dest中,不包含src目錄,會把目標目錄下所有文件刪除,再同步。
(vi)源目錄中存在文件
touch /srcnull/ddd.txt
touch /data/a{1,2,3}.txt
ll /data
-rw-r--r-- 1 root root 0 Feb 20 02:45 a1.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a2.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a3.txt
ll srcnull/
-rw-r--r-- 1 root root 0 Feb 20 02:49 ddd.txt
(vii)刪除,源目錄中的文件同步到目標目錄中了,目標目錄中的原有文件全部被刪除
rsync –r –delete /srcnull/ /data/
ll /data
-rw-r--r-- 1 root root 0 Feb 20 02:50 ddd.txt
(2)藉助rcp、ssh等通道來傳輸數據(此時類似於scp命令的功能)
a、傳統scp傳輸方式
(i)推送方式
scp -rp -P9080 /etc/hosts [email protected]:/tmp
(ii)拉取方式
scp -rp -P9080 [email protected]:/tmp /etc/hosts
b、rsync傳輸方式
(i)推送方式
rsync -avz -e 'ssh' a.log [email protected]:/wddg/log/
(ii)拉取方式
rsync -avz -e 'ssh' [email protected]:/tmp /etc/hosts
(3)以守護進程(socket)的方式傳輸數據(這是rsync本身的重要功能)
a、演示環境
主機名 |
IP |
用途 |
操作系統 |
linuxDelivery |
192.168.58.238 |
服務端A_備份服務器 |
CentOS6.8 |
StudyLinux |
192.168.58.85 |
客戶端B_發送服務器 |
CentOS6.8 |
LS-B16175 |
192.168.16.75 |
客戶端C_發送服務器 |
RedHat6.4 |
b、服務端A_生成rsync配置文件/etc/rsyncd.conf(需手動生成)
vi /etc/rsyncd.conf
#rsync-config------------------------start
#create by Study rsync 13:11 2017-02-21
##rsyncd.conf start##
uid = rsync #指定rsync服務進程的屬主
gid = rsync #指定rsync服務進程的屬組
use chroot = no #安全方面的參數,首先chroot到path參數指定的目錄下。
max connections = 200 #允許的最大連接數
timeout = 300 #超時日間
pid file = /var/run/rsyncd.pid #指定rsync的pid文件
lock file = /var/run/rsync.lock #鎖文件
log file = /var/log/rsyncd.log #日誌文件
[StudyRsync] #[]表示模塊,模塊名就是rsync客戶端看到的目錄名
path = /wddg/rsync/ #rsync在服務端的路徑
ignore errors #忽略錯誤
read only = false #指定目錄可讀可寫(rw)
list = false #不允許客戶端對共享目錄進行列表操作
hosts allow = 192.168.0.0/16 #允許訪問的主機。可以是IP,也可以是網段
hosts deny = 0.0.0.0/32 #拒絕訪問的主機
auth users = rsync_backup #空格或逗號分隔的用戶列表,爲虛擬用戶,列表中的用戶才能連接該模塊。
secrets file = /etc/rsync.password #虛擬用戶的密碼文件.格式爲用戶名:密碼
#rsync-config------------------------end
c、服務端A_以守護進程方式啓動rsync
rsync --daemon
d、服務端A_查看是否啓動成功(rsync的監聽端口爲873)
netstat -lntup | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3747/rsync
tcp 0 0 :::873 :::* LISTEN 3747/rsync
cat /var/log/rsyncd.log
2017/02/21 14:01:39 [3747] rsyncd version 3.0.6 starting, listeningon port 873
e、服務端A_新建rsync用戶
useradd rsync -s /sbin/nologin
id rsync
uid=503(rsync) gid=503(rsync) groups=503(rsync)
f、服務端A_新建rsync共享目錄
mkdir /wddg/rsync/
g、服務端A_將rsync共享目錄屬主(組)改爲rysnc
chown -R rsync.rsync /wddg/rsync
h、服務端A_新建rsync密碼文件
echo "rsync_backup:mypassword" > /etc/rsync.password
i、服務端A_降低rsync密碼文件的權限,保證安全
chmod 600 /etc/rsync.password
j、客戶端B_創建rsync密碼文件(只需密碼,不需用戶)
echo "mypassword" > /etc/rsync.password
k、客戶端B _降低rsync密碼文件的權限,保證安全
chmod 600 /etc/rsync.password
l、客戶端C_創建rsync密碼文件(只需密碼,不需用戶)
echo "mypassword" > /etc/rsync.password
m、客戶端C _降低rsync密碼文件的權限,保證安全
chmod 600 /etc/rsync.password
n、客戶端B_拉取方式獲取服務端A數據(需密碼)
rsync 參數 虛擬用戶 IP 模塊名 本地目錄
rsync -avz [email protected]::StudyRsync /wddg/share/ #需密碼
o、客戶端B_拉取方式獲取服務端A數據(指定密碼文件方式,無需密碼)
rsync -avz [email protected]::StudyRsync/wddg/share/ --password-file=/etc/rsync.password #指定密碼文件,無需密碼,自動傳輸
p、客戶端B_推送方式獲取服務端A數據(指定密碼文件方式,無需密碼)
touch /wddg/share/{1..10}
rsync -avz /wddg/share/ [email protected]::StudyRsync--password-file=/etc/rsync.password
說明:推送時,沒有/時(/wddg/share)表示推送目錄share,有/時(/wddg/share/)表示推送share下的所有文件.
q、客戶端B_再推一次數據到服務端A(rsync不會推送同樣的數據了)
rsync -avz /wddg/share/ [email protected]::StudyRsync--password-file=/etc/rsync.password
顯示結果爲:total sizeis o speedup is 0.00
r、客戶端C_拉取方式獲取服務端A數據(指定rsync協議)
rsync -avz rsync://[email protected]/StudyRsync/wddg/share/ --password-file=/etc/rsync.password
s、客戶端C_推送方式獲取服務端A數據(指定rsync協議)
rsync -avz /wddg/share/ rsync://[email protected]/StudyRsync--password-file=/etc/rsync.password
7、rsync基礎小結
(1)rsync服務端
a、配置文件/etc/rsyncd.conf
(i)文件名結尾有d,不要忘了。
(ii)文件內容要有:用戶、目錄、模塊、虛擬用戶、密碼文件名和路徑、日誌文件名和路徑
如果有多個模塊,且共用一個虛擬用戶、密碼文件名和路徑等信息的,可以把這些信息放在模塊上面的公共區域。如果希望不同模塊使用不同虛擬用戶、密碼文件名和路徑等信息的,可以在每個模塊下面保留相應信息。
示例:
uid = rsync #指定rsync服務進程的屬主
gid = rsync #指定rsync服務進程的屬組
use chroot = no #安全方面的參數,首先chroot到path參數指定的目錄下。
max connections = 200 #允許的最大連接數
timeout = 300 #超時日間
pid file = /var/run/rsyncd.pid #指定rsync的pid文件
lock file = /var/run/rsync.lock #鎖文件
log file = /var/log/rsyncd.log #日誌文件
ignore errors #忽略錯誤
read only = false #指定目錄可讀可寫(rw)
list = false #不允許客戶端對共享目錄進行列表操作
hosts allow = 192.168.0.0/16 #允許訪問的主機。可以是IP,也可以是網段
hosts deny = 0.0.0.0/32 #拒絕訪問的主機
auth users = rsync_backup #空格或逗號分隔的用戶列表,爲虛擬用戶,列表中的用戶才能連接該模塊。
secrets file = /etc/rsync.password #虛擬用戶的密碼文件.格式爲用戶名:密碼
[StudyRsync] #[]表示模塊,模塊名就是rsync客戶端看到的目錄名
path = /wddg/rsync/ #rsync在服務端的路徑
[StudyTest]
path = /wddg/test/
b、創建共享目錄。
根據配置文件中配置的目錄,創建共享目錄。
c、創建rsync用戶,並授權訪問共享目錄
根據配置文件中配置的用戶,創建相應的rsync用戶,並授權訪問共享目錄
d、創建密碼文件
(i)根據配置文件中配置的密碼文件及路徑,創建相應的密碼文件。
一定要複製配置文件中的路徑和文件名,防止出錯。
(ii)內容格式:配置文件中的虛擬用戶名:密碼
(iii)密碼文件的權限600
e、將rsync以守護進程方式自動啓動
在/etc/rc.local中添加命令:/usr/bin/rsync --daemon
echo "/usr/bin/rsync --daemon">>/etc/rc.local
tail -1 /etc/rc.local
/usr/bin/rsync --daemon
f、查看rsync日誌
根據配置文件中的日誌路徑和文件名,查看查看rsync日誌
tail /var/log/rsyncd.log
(2)rsync客戶端(可有多個)
a、密碼文件
(i)客戶端密碼文件的文件名和路徑與服務端沒有任何關係。
之所以也命名爲/etc/rsync.password是爲了便於管理。
(ii)內容格式:密碼
密碼文件中的內容只有密碼,並且必須要與服務端的密碼文件中的密碼相同。
(iii)密碼文件的權限600
b、同步
(i)推
rsync -avz /wddg/share/ rsync://[email protected]/StudyRsync--password-file=/etc/rsync.password
(ii)拉
rsync -avz rsync://[email protected]/StudyRsync/wddg/share/ --password-file=/etc/rsync.password
(3)、注意事項
a、防火牆和selinux要關閉
b、排錯時,一定要看日誌
c、排錯時,一定從部署流程整體考慮。
如果是部署時就出錯,一定是部署流程有問題。如果是運行很長一段時間後出錯,要看看有沒有被攻擊或被修改。
d、要有良好的操作習慣。
一定要多拷貝。
二、rsync深度應用之排除打包
1、演示環境
主機名 |
IP |
用途 |
操作系統 |
linuxDelivery |
192.168.58.238 |
服務端A_備份服務器 |
CentOS6.8 |
StudyLinux |
192.168.58.85 |
客戶端B_發送服務器 |
CentOS6.8 |
LS-B16175 |
192.168.16.75 |
客戶端C_發送服務器 |
RedHat6.4 |
2、檢查環境
(1)檢查服務端A是否啓動rsync
ps -ef | grep rsync
root 3747 1 0 Feb21 ? 00:00:00 rsync --daemon
(2)檢查服務端A的rsync服務端口873
lsof -i :873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 3747 root 4u IPv4 18077 0t0 TCP *:rsync (LISTEN)
rsync 3747 root 5u IPv6 18078 0t0 TCP *:rsync (LISTEN)
(3)測試rsync傳輸是否正常(由B->A)
rsync -avz /wddg/share/ [email protected]::StudyRsync--password-file=/etc/rsync.password
3、rsync排除的相關參數
--exclude=PATTERN #需排除的文件較少時用
--exclude-from=FILE #需排除的文件過多是,可以將文件名寫入一個文件中,再指定排除
4、示例1:在客戶端排除一個文件
(1)查看客戶端B上需要同步的文件夾
ls /wddg/share
10.txt 2.txt 4.txt 6.txt 8.txt a.txt c.txt e.txt g.txt
1.txt 3.txt 5.txt 7.txt 9.txt b.txt d.txt f.txt test
(2)查看服務端A上的共享目錄
ll /wddg/rsync
total 0
(3)在客戶端B將文件同步到服務端A,並排除a.txt
rsync -avz --exclude=a.txt /wddg/share/[email protected]::StudyRsync --password-file=/etc/rsync.password
sending incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
b.txt
c.txt
d.txt
e.txt
f.txt
g.txt
test/
sent 872 bytes received 319bytes 794.00 bytes/sec
total size is 0 speedup is0.00
說明:傳輸的文件中沒有a.txt
(4)查看服務端A上的共享目錄(沒有a.txt)
ls /wddg/rsync
10.txt 2.txt 4.txt 6.txt 8.txt b.txt d.txt f.txt test
1.txt 3.txt 5.txt 7.txt 9.txt c.txt e.txt g.txt
5、示例2:在客戶端排除多個針對不連續的文件。方式一:{文件1,文件2..}
#在客戶端B將文件同步到服務端A,並排除a.txt,b.txt,c.txt
rsync -avz --exclude={a.txt,b.txt,c.txt} /wddg/share/
[email protected]::StudyRsync--password-file=/etc/rsync.password
sending incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
d.txt
e.txt
f.txt
g.txt
test/
sent 768 bytes received 281bytes 2098.00 bytes/sec
total size is 0 speedup is0.00
6、示例3:在客戶端排除多個連續的文件。方式二:{文件1..文件n}
#在客戶端B將文件同步到服務端A,並排除a.txt,b.txt,c.txt,d.txt,e.txt,f.txt
rsync -avz --exclude={a..f}.txt /wddg/share/ [email protected]::StudyRsync--password-file=/etc/rsync.password
sending incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
g.txt
test/
sent 606 bytes received 224bytes 553.33 bytes/sec
total size is 0 speedup is0.00
7、示例4:在客戶端通過指定文件來排除多個連續的文件。
(1)創建內容是排除文件名的文件
vi /wddg/share/paichu.log
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
10.txt
(2)通過指定文件排除
rsync -avz --exclude-from=paichu.log/wddg/share/ [email protected]::StudyRsync--password-file=/etc/rsync.password
sending incremental file list
./
.paichu.log.swp
a.txt
b.txt
c.txt
d.txt
e.txt
f.txt
g.txt
paichu.log
test/
sent 797 bytes received 186bytes 655.33 bytes/sec
total size is 12349 speedupis 12.56
8、示例5:在服務端排除文件(通過配置文件)。
(1)在服務端A上修改配置文件,加上exclude=xxx參數
vi /etc/rsyncd.conf
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
[StudyRsync]
path = /wddg/rsync/
ignore errors
read only = false
list = false
hosts allow = 192.168.0.0/16
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
exclude=a.txt b.txt
(2)停止rsync服務
a、方法一:kill `cat/var/run/rsyncd.pid`
b、方法二:pkill rsync
c、方法三:ps -ef | greprsync 查出pid,再kill
ps -ef | grep "rsync --daemon" | grep -v grep | awk'{print $2}'
(3)啓動rsync服務
rsync --daemon
(4)在客戶端B上通過rsync傳輸數據
rsync -avz /wddg/share/ [email protected]::StudyRsync--password-file=/etc/rsync.password
sending incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
skipping daemon-excluded file "a.txt"
skipping daemon-excluded file "b.txt"
c.txt
d.txt
e.txt
f.txt
g.txt
paichu.log
test/
sent 968 bytes received 319bytes 2574.00 bytes/sec
total size is 12349 speedupis 9.60
rsync error: some files/attrs were not transferred(see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
6、排除小結
(1)客戶端
a、排除單個文件
rsync -avz --exclude={a} /wddg/share/[email protected]::StudyRsync--password-file=/etc/rsync.password
b、排除多個文件:方法一(不連續的文件名)
rsync -avz --exclude={a,c} /wddg/share/[email protected]::StudyRsync--password-file=/etc/rsync.password
c、排除多個文件:方法二(連續文件名)
rsync -avz --exclude={a..f}/wddg/share/ [email protected]::StudyRsync--password-file=/etc/rsync.password
d、排除多個文件:方法三
rsync -avz --exclude=a --exclude=d /wddg/share/ [email protected]::StudyRsync--password-file=/etc/rsync.password
d、排除多個文件:方法四(指定文件,針對不規律文件名較多時)
rsync -avz --exclude-from=paichu.log/wddg/share/ [email protected]::StudyRsync--password-file=/etc/rsync.password
(2)服務端
a、在服務端上修改配置文件/etc/rsync.conf,加上exclude=xxx參數(多個文件用空格分隔)
b、格式示例:exclude=a b /wddg/c.txt
c、與客戶端比較,服務端使用排除傳輸比較麻煩。
三、rsync深度應用之無差異同步
1、概念
差異同步(普通同步)時,如果是拉取方式(PULL)同步,只能保證服務端目錄有的,客戶端目錄一定會有,但客戶端目錄中原來有的,服務端目錄不一定會有。如果是推送方式(PUSH)同步,只能保證客戶端目錄有的,服務端目錄一定會有,但服務端目錄中原來有的,客戶端目錄不一定會有。所以稱爲有差異同步。
無差異同步就是客戶端和服務端目錄的內容完全一樣。
2、無差異同步的應用場景
在某些場合下,要求保證生產服務器的內容與備份服務器內容完全一樣。一般是2臺服務器之間,必須要求數據一致,且實時性又不是很高的情況下使用。如2臺負載均衡下面web服務器之間的同步,或者是高可用雙機配置之間的同步等。但無差異同步的影響(風險)很大,而且有很多替代方案。因此,生產場景中沒有特殊的需求,應避免使用。
(1)推送場景:備份
風險是也就是本地服務器有什麼,遠端服務器就有什麼,遠端服務器原有數據均被刪除
(2)拉取場景:代碼發佈
風險是也就是遠端服務器有什麼,本地服務器就有什麼,本地服務器原有數據均被刪除而丟失
3、使用方法:客戶端使用--delete參數
rsync -avz --delete /wddg/share/ [email protected]::StudyRsync
--password-file=/etc/rsync.password
4、示例演示
(1)刪除客戶端目錄中所有文件
rm –fr *
(2)使用—delete參數進行無差異同步
rsync -avz --delete /wddg/share/ [email protected]::StudyRsync
--password-file=/etc/rsync.password
sending incremental file list
./
deleting g
deleting f
deleting e
deleting d
deleting c
deleting b
deleting a
deleting 9
deleting 8
deleting 7
deleting 6
deleting 5
deleting 4
deleting 3
deleting 2
deleting 10
deleting 1
sent 29 bytes received 11bytes 26.67 bytes/sec
total size is 0 speedup is0.00
(3)檢查服務端目錄中文件(所有文件均被刪除,高風險)
ll /wddg/rsync
total 0
四、rsync的排錯
1、排錯思想
(1)部署流程要熟練
(2)原理要理解
(3)要學會看日誌,包括命令行輸出和日誌/var/log/rsyncd.log
2、排錯步驟
(1)先檢查配置文件
(2)再檢查的用戶對不對
(3)檢查密碼文件對不對,複製配置文件中的密碼文件名檢查,不要用手敲,要複製。
(4)檢查密碼文件屬性
(5)查看日誌文件
2、常見錯誤
(1)、密碼文件不存在
如查密碼文件不存在(含指定的密碼文件名錯誤),會在輸入密碼後報在模塊上認證失敗的錯誤。錯誤內容如下:
@ERROR: auth failed on module <modulename>
rsync error: error startingclient-server protocol (code 5) at main.c (1503) [receiver=3.0.6]
(2)密碼文件權限過大。
a、rsync要求密碼文件不能讓其它用戶訪問,如果文件權限過大,會報驗證失敗錯誤。
在日誌文件中會出現如下內容:
secrets file must not be other-accessible(see strict modes optain)
continuing without secrets file
auth failed on module xxx from unknow (IP) missing secret for user"username"
b、檢查密碼文件權限
ll /etc/rsync.password
-rw-r--r-- 1 root root 24 Feb 21 14:16 /etc/rsync.password #644,過大
c、改爲600
chmod 600 /etc/rsync.password
-rw------- 1 root root 24 Feb 21 14:16 /etc/rsync.password #600
(3)防火牆或SELinux開啓。報錯如下:
rsync: failed to connect to xx.xx.xx.xx: No route to host(113)
(4)服務端共享目錄不存在。報錯如下:
@ERROR: chdir failed
(5)服務端共享目錄權限不對,配置文件中的rsync用戶無權限訪問目錄。報錯如下:
rsync: recv_generator:mkdir xxx failed:Permission denied(13)
五、rsync相關參考資料
2、http://www.samba.org/ftp/rsync/rsync.html
3、http://www.samba.org/ftp/rsync/rsyncd.conf.html
4、man rsync
5、man rsyncd.conf
第二十章 同步工具之inotify(實時同步工具)
一、inotify簡介
1、概念
inotify是一個從2.6.13內核開始,對Linux文件系統進行高效率、細粒度、異步地監控機制,用於通知用戶空間程序的文件系統變化。可利用它對用戶空間進行安全、性能、以及其他方面的監控。Inotify 反應靈敏,用法非常簡單,並且比 cron 任務的繁忙輪詢高效得多。如果內核版本不低於 2.6.13,系統就支持 inotify。如果存在/usr/include/sys/inotify.h 文件,表明內核支持 inotify。從文件管理器到安全工具,文件系統監控對於的許多程序來說都是必不可少的。它允許監控程序打開一個獨立文件描述符,並針對事件集監控一個或者多個文件,例如打開、關閉、移動/重命名、刪除、創建或者改變屬性。第三方程序可以通過這個內核接口監控文件系統的各種變化。inotify-tools就是一款這樣的監控軟件。
2、inotify可以監視的文件系統常見事件
IN_ACCESS:文件被訪問
IN_MODIFY:文件被修改
IN_ATTRIB,文件屬性被修改
IN_CLOSE_WRITE,以可寫方式打開的文件被關閉
IN_CLOSE_NOWRITE,以不可寫方式打開的文件被關閉
IN_OPEN,文件被打開
IN_MOVED_FROM,文件被移出監控的目錄
IN_MOVED_TO,文件被移入監控着的目錄
IN_CREATE,在監控的目錄中新建文件或子目錄
IN_DELETE,文件或目錄被刪除
IN_DELETE_SELF,自刪除,即一個可執行文件在執行時刪除自己
IN_MOVE_SELF,自移動,即一個可執行文件在執行時移動自己
3、inotify的作用
inotify可以監控文件,也可以監控目錄。當監控目錄時,它可以同時監控目錄及目錄中的各子目錄及文件的。此外,inotify 使用文件描述符作爲接口,因而可以使用通常的文件I/O操作select、poll和epoll來監視文件系統的變化。
4、實現inotify的的軟件
inotify:功能簡單,性能高
sersync:國產,c封裝,功能較多,可過濾。
lsyncd:開源,輕量級的實時鏡像解決方案。
二、inotify的實現數據實時同步的原理
無論是手動執行rsync還是把rsync客戶端發出的數據同步請求命令做成週期性任務計劃,每隔一段時間不管有沒有數據變化都發出一次數據同步請求命令,同步一次數據。服務端和客戶端都有時間差。
所以,使用內核提供的inotify機制,當數據發生改變時(刪除、修改等)就觸發rsync客戶端發出數據 同步請求。從而實現數據的實時傳輸。
Rsync + inotify 機制實現的兩臺服務器數據同步如下圖如示:
三、演示環境
主機名 |
IP |
用途 |
操作系統 |
linuxDelivery |
192.168.58.238 |
服務端A_備份服務器 |
CentOS6.8 |
StudyLinux |
192.168.58.85 |
客戶端B_發送服務器 |
CentOS6.8 |
四、安裝inotify
1、配置inotify的前提條件
(1)rysnc --daemon服務正常
(2)客戶端可能正常推送和拉取數據
(3)是在客戶端配置inotify
2、檢查rsync--daemon服務是否正常,且是否能正常推送數據。
ps -ef | grep rsync
rsync -avz /wddg/share/ [email protected]::StudyRsync--password-file=
/etc/rsync.password
3、檢查客戶端系統是否支持inotify
(1)檢查系統版本
uname -r
(2)檢查系統相關參數,查看當前系統是否支持inotify
a、檢查系統相關參數
ls -l /proc/sys/fs/inotify
-rw-r--r-- 1 root root 0 4月 22 14:56 max_queued_events
-rw-r--r-- 1 root root 0 4月 22 14:56 max_user_instances
-rw-r--r-- 1 root root 0 4月 22 14:56 max_user_watches
b、參數說明:
max_queued_evnets:表示調用inotify_init時分配給inotify instance中可排隊的event的數目的最大值,超出這個值的事件被丟棄,但會觸發IN_Q_OVERFLOW事件。所以需要調大一些。
例如: echo 327679 > /proc/sys/fs/inotify/max_queued_events
max_user_instances:表示每一個real user ID可創建的inotify instatnces的數量上限。
max_user_watches:表示每個inotify instatnces可監控的最大目錄數量。如果監控的文件數目巨大,需要根據情況,適當增加此值的大小。
例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches
4、下載inotify源碼包
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
5、編譯安裝inotify源碼包
(1)解壓inotify源碼包
cd /wddg/unzip
tar zxf inotify-tools-3.14.tar.gz
(2)#配置inotify,並指定安裝路徑爲/usr/local/inotify-3.14
cd inotify-tools-3.14
./configure--prefix=/usr/local/inotify-3.14
(3)編譯inotify源碼包
make
(4)安裝inotify源碼包
make install
(5)也可能編譯時並安裝
make && make install
(6)查看是否成功
echo $?
0
(7)創建一個軟連接
ln -s /usr/local/inotify-3.14//usr/local/inotify
6、inotify文件結構
(1)進入inotify目錄
cd /usr/local/inotify
(2)查看inotify目錄
ls –l
drwxr-xr-x 2 root root 4096 Feb 21 23:10 bin #inotify的二進制命令
drwxr-xr-x 3 root root 4096 Feb 21 23:10 include #inotify程序所需的頭文件
drwxr-xr-x 2 root root 4096 Feb 21 23:10 lib #動態鏈接庫文件
drwxr-xr-x 4 root root 4096 Feb 21 23:10 share #幫助文檔
(3)查看inotify目錄結構
tree
.
|-- bin
| |-- inotifywait
| `-- inotifywatch
|-- include
| `-- inotifytools
| |-- inotify-nosys.h
| |-- inotify.h
| `-- inotifytools.h
|-- lib
| |-- libinotifytools.a
| |-- libinotifytools.la
| |-- libinotifytools.so -> libinotifytools.so.0.4.1
| |-- libinotifytools.so.0 -> libinotifytools.so.0.4.1
| `-- libinotifytools.so.0.4.1
`-- share
|-- doc
| `-- inotify-tools
| |-- doxygen.css
| |-- doxygen.png
| |-- files.html
| |-- globals.html
| |-- globals_func.html
| |-- index.html
| |--inotifytools_8c_source.html
| |-- inotifytools_8h.html
| |--inotifytools_8h_source.html
| |-- pages.html
| |-- tab_b.gif
| |-- tab_l.gif
| |-- tab_r.gif
| |-- tabs.css
| `-- todo.html
`-- man
`-- man1
|-- inotifywait.1
`-- inotifywatch.1
五、inotify之inotifywait命令常用參數詳解
1、查看幫助
./bin/inotifywait –help
2、參數詳解
(1)-r:遞歸查詢目錄
-r|--recursive Watchdirectories recursively.
(2)-q:打印監控事件的信息
-q|--quiet Print less (only print events).
(3)-m:始終保持事件監聽狀態
-m|--monitor Keep listeningfor events forever. Without this option, inotifywait will exit afterone event is received.
(4)--excludei:排除文件或目錄時,不區分大小寫。
--excludei <pattern> Like--exclude but case insensitive.
(5)--timefmt:指定時間輸出的格式
--timefmt <fmt> strftime-compatibleformat string for use with %T in --format string.
(6)--format:打印使用指定的輸出類似格式字符串
--format <fmt> Print using aspecified printf-like format string; read the man page for more details.
a、%w:顯示被監控文件的文件名;
b、%f:如果發生某事件的對象是目錄,則顯示被監控目錄的名字;默認顯示爲空串;
c、%T:使用--timefmt選項中自定義的時間格式;
d、%e 表示發生的事件
e、%Xe 事件以“X”分隔
(7)-e:通過此參數可以指定需要監控的事件
-e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted, all eventsare listened for.
3、事件(Events)詳解
(1)access:文件或目錄被讀取
access file or directory contents were read
(2)modify:文件或目錄內容被修改
modify file or directory contents were written
(3)attrib:文件或目錄屬性被改變
attrib file or directory attributes changed
(4)close:文件或目錄封閉,無論讀/寫模式
close file or directory closed, regardless of read/write mode
(5)open:文件或目錄被打開
open file or directory opened
(6)moved_to:文件或目錄被移動至另外一個目錄
moved_to file or directory moved to watched directory
(7)move:文件或目錄被移動另一個目錄或從另一個目錄移動至當前目錄
move file or directory movedto or from watched directory
(8)create:文件或目錄被創建在當前目錄
create file or directory created within watcheddirectory
(9)delete :文件或目錄被刪除
delete file or directory deleted within watched directory
(10)unmount:文件系統被卸載
unmount file system containing file or directory unmounted
六、inotifywait命令之測試
1、測試create
(1)要求:
在客戶端B上監控/wddg/share目錄的create事件,並打印出創建的文件名。打印格式爲YYYY-MM-DD HH24:MI 文件名
(2)執行命令
/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create /wddg/share
(3)新開一個窗口,在/wddg/share目錄下執行ls、cat命令,原窗口無輸出
(4)在新窗口中,在/wddg/share目錄下執行創建文件和目錄命令
touch test.txt
mkdir aa
(5)查看原窗口輸出
2017-02-22 00:15 /wddg/share/test.txt
2017-02-22 00:15 /wddg/share/aa
(6)在—format參數中使用-e,打印出是哪個要監控的事件上的操作
/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create /wddg/share
(7)在新窗口中,在/wddg/share目錄下執行創建文件和目錄命令
mkdir a
touch b.txt
(8)查看原窗口輸出
2017-02-22 00:36 CREATE,ISDIR/wddg/share/a
2017-02-22 00:36 CREATE/wddg/share/b.txt
2、測試delete
(1)執行命令
/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create,delete/wddg/share
(2)在新窗口中,在/wddg/share目錄下執行刪除文件和目錄命令
rm -f b.txt
rm -fr aa
(3)查看原窗口輸出
2017-02-22 00:43 DELETE/wddg/share/b.txt
2017-02-22 00:43 DELETE,ISDIR/wddg/share/aa
3、測試close_write(寫文件後關閉事件)
(1)執行命令
/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create,delete,close_write/wddg/share
(2)在新窗口中,在/wddg/share目錄下執行創建文件和目錄命令
touch x.txt
echo 111>a.log
(3)查看原窗口輸出
2017-02-22 00:47 CREATE/wddg/share/x.txt
2017-02-22 00:47 CLOSE_WRITE,CLOSE/wddg/share/x.txt
2017-02-22 00:48 CREATE /wddg/share/a.log
2017-02-22 00:48 CLOSE_WRITE,CLOSE/wddg/share/a.log
(4)說明:事件是可以並行監控的。CREATE和CLOSE_WRITE有部分是重合的
4、簡化事件輸出
由於在生產環境中,需要把變化的文件推送到其他服務器,並不需要時間,只需要文件的全路徑,調用rsync將其推送到服務端,所以事件的輸出可以簡化到只輸出文件的全路徑
/usr/local/inotify-3.14/bin/inotifywait-mrq --format ' %w%f' -e create /wddg/share
七、inotifywait命令之腳本編寫
1、編寫腳本
vim /server/scripts/inotify.sh
#!/bin/sh
cmd="/usr/local/inotify-3.14/bin/inotifywait"
$cmd -mrq --format '%w%f' -ecreate,delete,close_write,attrib /wddg/share |\
while read line
do
rsync -az --delete $line [email protected]::StudyRsync--password-file=/etc/rsync.password
done
2、監控模式執行inotify.sh
sh -x inotify.sh
+cmd=/usr/local/inotify-3.14/bin/inotifywait
+ read line
+ /usr/local/inotify-3.14/bin/inotifywait-mrq --format %w%f -e create,delete,close_write /wddg/share
3、檢查服務端目錄
ll /wddg/rsync
total 0
4、在新窗口中,在客戶端/wddg/share目錄中創建文件
touch a
5、查看原窗口輸出
+ rsync -az --delete /wddg/share/[email protected]::StudyRsync --password-file=/etc/rsync.password
+ read line
+ rsync -az --delete /wddg/share/[email protected]::StudyRsync --password-file=/etc/rsync.password
+ read line
6、服務端查看文件是否同步成功
ll
-rw-r--r--. 1 rsync rsync 0 Feb 22 01:14 a
7、同時創建大量文件
touch {1..1000}
8、傳輸發現有延時
ls
1 11 13 15 17 19 20 22 24 26 28 3 31 33 35 37 39 40 42 5 7 9
10 12 14 16 18 2 21 23 25 27 29 30 32 34 36 38 4 41 43 6 8 a
ls
1 11 13 15 17 19 20 22 24 26 28 3 31 33 35 37 39 40 42 44 5 7 9
10 12 14 16 18 2 21 23 25 27 29 30 32 34 36 38 4 41 43 45 6 8 a
ls
1 11 13 15 17 19 20 22 24 26 28 3 31 33 35 37 39 40 42 44 46 5 7 9
10 12 14 16 18 2 21 23 25 27 29 30 32 34 36 38 4 41 43 45 47 6 8 a
9、客戶端刪除文件
rm -f *
10、客戶端報錯,查看服務端文件,發現沒有刪除(但客戶端文件已經刪除)
rsync: link_stat "/wddg/share/9" failed: No such file ordirectory (2)
rsync error: some files/attrs were not transferred (see previouserrors) (code 23) at main.c(1039) [sender=3.0.6]
11、修改腳本,捕獲錯誤(只是跳過錯誤,還是不能刪除服務端文件)
vim /server/scripts/inotify1.sh
#!/bin/sh
cmd="/usr/local/inotify-3.14/bin/inotifywait"
$cmd -mrq --format '%w%f' -e create,delete,attrib /wddg/share |\
while read line
do
[ ! -e $line ] &&continue
rsync -arzu --delete [email protected]::StudyRsync --password-file=/etc/rsync.password>/dev/null 2>&1
done
12、修改腳本,改傳文件爲傳目錄方式(可以刪除服務端文件)
vim /server/scripts/inotify2.sh
#!/bin/sh
cmd="/usr/local/inotify-3.14/bin/inotifywait"
$cmd -mrq --format '%w%f' -ecreate,delete,close_write,attrib /wddg/share |\
while read line
do
rsync -az --delete /wddg/share/[email protected]::StudyRsync--password-file=/etc/rsync.password
done
12、標準腳本寫法(也是傳目錄)
vim /server/scripts/inotify2.sh
#!/bin/bash
#para
host01=192.168.1.160 #inotify-slave的ip地址
src=/backup/ #本地監控的目錄
dst=backup #inotify-slave的rsync服務的模塊名
user=rsync_backup #inotify-slave的rsync服務的虛擬用戶
rsync_passfile=/etc/rsync.password #本地調用rsync服務的密碼文件
inotify_home=/usr/local/inotify-3.14 #inotify的安裝目錄
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M'--format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile}$src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100$user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0
13、傳文件和傳目錄的區別
傳文件:比較精準,但刪除操作會報錯,無法在服務端刪除文件。可以另起一個刪除腳本,監控同一個目錄。
傳目錄:每次都是將目錄下的文件全部上傳,效率較低,但可以在服務端刪除文件
14、將inotify放在後臺執行
sh inotify.sh &
八、inotify壓力測試
1、檢查環境配置
(1)客戶機(客戶端B)
a、查看服務器產品名稱
dmidecode | grep "Product Name"
Product Name: VMware Virtual Platform
Product Name: 440BX Desktop Reference Platform
b、查看CPU型號
grep name /proc/cpuinfo
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
c、查看CPU個(核)數
grep "physical id" /proc/cpuinfo
physical id : 0
physical id : 2
physical id : 4
physical id : 6
physical id : 8
physical id : 10
physical id : 12
physical id : 14
d、查看內存信息
grep MemTotal /proc/meminfo
MemTotal: 264535076 kB
free -m
total used free shared buffers cached
Mem: 258335 2351 255983 1 187 338
-/+ buffers/cache: 1825 256509
Swap: 4031 0 4031
e、查看系統信息
uname -a
Linux linuxDelivery 2.6.32-642.el6.x86_64 #1 SMP Wed Apr 13 00:51:26EDT 2016 x86_64 x86_64 x86_64 GNU/Linux
(2)服務端(服務端A)
a、查看服務器產品名稱
dmidecode | grep "Product Name"
Product Name: VMware Virtual Platform
Product Name: 440BX Desktop Reference Platform
b、查看CPU型號
grep name /proc/cpuinfo
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
c、查看CPU個(核)數
grep "physical id" /proc/cpuinfo
physical id : 0
physical id : 2
physical id : 4
physical id : 6
physical id : 8
physical id : 10
physical id : 12
physical id : 14
d、查看內存信息
grep MemTotal /proc/meminfo
MemTotal: 8060636 kB
free -m
total used free shared buffers cached
Mem: 7871 1267 6604 1 178 636
-/+ buffers/cache: 451 7420
Swap: 4031 0 4031
e、查看系統信息
uname -a
Linux linuxDelivery 2.6.32-642.el6.x86_64 #1 SMP Wed Apr 13 00:51:26EDT 2016 x86_64 x86_64 x86_64 GNU/Linux
f、檢查服務端rsync --daemon服務是否啓動
ps -ef | grep rsync
root 19551 1 0Feb24 ? 00:00:00 rsync --daemon
2、測試腳本
(1)不停向指定目錄寫入文件腳本
vi /server/scripts/w.sh
#!/bin/sh
count=10
while true
do
for((i=1;i<=$count;i++))
do
/bin/cp/wddg/oldboy/10K.log /wddg/share/$i/10K_`echo $(date)$RANDOM|md5sum | cut -c1-8`.log
done
sleep 1
for((i=1;i<=$count;i++))
do
/bin/cp/wddg/oldboy/30K.log /wddg/share/$i/30K_`echo $(date)$RANDOM|md5sum | cut -c1-8`.log
done
sleep 1
for((i=1;i<=$count;i++))
do
/bin/cp/wddg/oldboy/50K.log /wddg/share/$i/50K_`echo $(date)$RANDOM|md5sum | cut -c1-8`.log
done
sleep 1
done
(2)在/wddg/share/中創建文件夾
for n in `seq 100`;do mkdir $n ;done;
(3)測試
a、客戶端
sh w.sh
b、服務端查看
ll | wc -l
c、結論
由於是虛擬機,目錄是通過NFS掛載的NAS盤,每秒只能同步6個左右的文件,性能不盡人意。
3、Linux下壓力測試工具:webbench
九、inotify性能、缺點
1、inotify的性能
併發200-300/秒,文件大小10K-500K左右,這是inotify的極限。數據同步幾乎無延時(小於1秒),再多就會有延時。
2、inotify的缺點
(1)併發不以大於200個文件
(2)每次都是全部推送
十、企業Linux運維場景數據同步方案
1、文件級別同步方案
scp、nfs、sftp、http、samba、rsync、csysn2、union
詳細見注1參考博文8:2016年linux運維人員必會開源運維工具體系
文件同步的思想:
(1)可以利用mysql、mongodb等數據庫軟件實現文件同步(寫庫-同步-讀庫-文件)
(2)雙寫,同時向兩臺服務器寫數據。
2、文件系統級別同步方案
drbd:基於文件系統同步,又稱網絡raid1。幾乎可以同步任何業務數據。
mysql數據庫的官方推薦使用drbd同步數據。
所有單點服務,如NFS、MFS等都可以用drbd
3、數據庫同步方案
(1)自身同步機制
mysql:replication(主從同步,基於SQL語句重寫)
oracle:dataguard(物理的:基於磁盤塊(block)複製;邏輯的:基於SQL語句重寫)
(2)第三方drbd
詳細見注1參考博文9:Heartbeat+DRBD+MySQL高可用架構方案與實施