Rsync+sersync實現數據實時同步
目錄
Rsync+sersync實現數據實時同步
一、rsync概述
二、rsync運行原理
三、rsync部署
四、備份測試
五、使用非系統用戶備份數據
5.1 rsync的配置文件介紹
5.2 配置備份目錄
5.3 使用rsync用戶備份測試
5.4 pull拉取數據
六、rsync+sersync 實現數據實時同步
6.1 數據同步原理
6.2 部署rsync+sersync
6.3 設置rsync+sersync開機自啟
七、總結
一、rsync概述
Rsync(Remote Sync)是Linux系統下的數據鏡像備份工具。該工具可以實現遠程同步、不同主機之間的同步,也能實現全量備份和增量備份,保持數據鏈接和權限,并采用優化的同步算法,傳輸前對數據進行壓縮,故該工具非常適合架構集中式備份或異地備份。也支持本地復制或與ssh、rsync同步。
官網地址:https://rsync.samba.org/
優點:
-
scp無法備份大量數據,而rsync備份、統計、比較一起進行。
-
可以備份整個目錄樹和文件系統,并保持文件原來的權限、時間、軟硬鏈接。
-
安裝較容易,無需特殊權限。
-
同步快速,首次同步完全備份,再次同步增量備份。
-
可以使用scp和ssh等方式傳輸備份文件
-
支持匿名傳輸
-
選擇性保持:符號鏈接、硬鏈接、文件屬性、權限、時間等
-
傳輸速度快:壓縮再傳輸、解壓再使用,減少帶寬。
# 查看版本信息
[root@server ~]# rsync --version
rsync version 3.1.2 protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,append, ACLs, xattrs, iconv, symtimes, prealloc
?
rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.
備份分類:
-
完全備份:所有文件進行備份
-
差異備份:備份自上次完全備份以來所有的修改
-
增量備份:備份自上次備份依賴所作的修改 (備份的量最小)
二、rsync運行原理
rsync采用C/S模式,即點到點的傳輸。通過xinetd服務監聽873端口,再讓xinetd服務下的rsync服務作出響應。
源主機:需要同步數據的服務器 (以數據為參照物)
目標主機:存放服務器同步數據的主機
數據同步方式:push 和 pull
-
推push:主動同步,把數據發送給目標主機。服務器開銷大,適合后端服務器較少的情況。【服務器備份推給rsync客戶端存放,主動模式】
目標主機配置為 rsync 服務端,源主機周期性的使用 rsync 命令把要同步的目錄推過去。
-
拉pull:所有客戶端主機去服務器上面拉數據,導致數據傳輸緩慢。【rsync客戶端去服務器上拉數據,存放到客戶端上,被動模式】
源主機配置為 rsync 服務端,目的主機周期性的使用 rsync 命令把要同步的目錄拉過來。
[root@targetpc ~]# systemctl start rsyncd
[root@targetpc ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemonLoaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)Active: active (running) since 六 2025-06-21 21:53:40 CST; 16min agoMain PID: 5256 (rsync)Tasks: 1CGroup: /system.slice/rsyncd.service└─5256 /usr/bin/rsync --daemon --no-detach
?
6月 21 21:53:40 targetpc systemd[1]: Started fast remote file cop....
Hint: Some lines were ellipsized, use -l to show in full.
?
#查看端口號, 通過xinetd服務監聽端口號 /etc/xinetd.d/
[root@targetpc xinetd.d]# netstat -antup | grep 873
tcp ? ? ? ?0 ? ? ?0 192.168.157.130:873 ? ? 0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?5256/rsync ? ? ? ? ?
udp ? ? ? ?0 ? ? ?0 0.0.0.0:55276 ? ? ? ? ? 0.0.0.0:* ? ? ? ? ? ? ? ? ? ? ? ? ? 873/avahi-daemon: r
udp ? ? ? ?0 ? ? ?0 0.0.0.0:5353 ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? ? ? ? ? ? ? 873/avahi-daemon: r
三、rsync部署
# rsync服務由xinetd服務進行管理,故也需要安裝xinetd服務
# 客戶端和服務器都需要安裝xinetd服務和rsync服務
[root@server ~]# yum install xinetd rsync -y
[root@client ~]# yum install xinetd rsync -y
?
# rsync運行在daemon模式
[root@server ~]# rsync --daemon
[root@client ~]# rsync --daemon
?
# 查看是否監聽873端口號
[root@server ~]# netstat -antup | grep 873
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:873 ? ? ? ? ? ? 0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?18927/rsync ? ? ? ?
tcp6 ? ? ? 0 ? ? ?0 :::873 ? ? ? ? ? ? ? ? :::* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?18927/rsync
[root@client ~]# netstat -antup | grep 873
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:873 ? ? ? ? ? ? 0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?9367/rsync ? ? ? ? ?
tcp6 ? ? ? 0 ? ? ?0 :::873 ? ? ? ? ? ? ? ? :::* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?9367/rsync ?
參數 | 作用 |
---|---|
-a | –archive archive mode權限保存模式【遞歸、保持屬性】(包含以下所有屬性) |
-r | 遞歸處理 |
-p | 保留文件原有屬性 |
-t | 保留文件原有時間 |
-g | 保留屬組 |
-o | 保留檔案所有者 |
-D | 保留device咨詢 |
-l | 復制所有的連接 |
-z | 壓縮傳輸 |
-H | 保留硬鏈接文件 |
-A | 保留文件的ACL屬性 |
-P | –progress |
--version | 輸出rsync版本信息 |
-v | 顯示輸出過程 |
-u | 僅進行更新 |
--port | 指定端口號,默認873 |
--delete | 刪除那些目標位置有的文件而備份源沒有的文件,最大程度的保持一致。 |
--password-file | 指定密碼文件 |
--bwlimit | 限制I/O帶寬 |
--filter | 需要過濾的文件 |
--exclude | 需要過濾的文件 |
--progress | 顯示備份過程 |
–avz | 常用:保留權限、顯示同步過程、壓縮傳輸 |
四、備份測試
# 由于rsync備份是會保持目錄及其文件的權限、時間、軟硬連接不變的,那要求備份在ssh傳輸過程中一路順暢,不會因權限不足而備份失敗。
# 這里創建test用戶用于測試
[root@server ~]# useradd test && echo 123456 | passwd --stdin test
更改用戶 test 的密碼 。
passwd:所有的身份驗證令牌已經成功更新。
[root@client ~]# useradd test && echo 123456 | passwd --stdin test
更改用戶 test 的密碼 。
passwd:所有的身份驗證令牌已經成功更新。
# 服務端上創建測試目錄/data
# g+s權限的作用在于/data目錄下新增的文件的所屬組都會是test
# setfacl為test用戶創建在/data的rwx權限
[root@server ~]# mkdir /data
[root@server ~]# chown -R test:test /data
[root@server ~]# chmod g+s /data
[root@server ~]# setfacl -m u:test:rwx /data
[root@server ~]# cp -r /boot/* /data
?
# 客戶端上創建備份存放目錄
[root@client ~]# mkdir -p /data/backup/
[root@client ~]# chown test:test /data/backup/
[root@client ~]# ll -d /data/backup/
drwxr-xr-x. 2 test test 6 8月 ? 6 23:52 /data/backup/
# 備份/data目錄下的數據到客戶端上
[root@server ~]# rsync -avz /data/ test@192.168.115.112:/data/backup
test@192.168.115.112's password:
sending incremental file list
./
。。。省略備份信息。。。
sent 138,094,794 bytes received 6,296 bytes ?5,211,361.89 bytes/sec
total size is 152,364,618 speedup is 1.10
# 客戶端上驗證備份內容,可以看到有數據傳輸進去
[root@client ~]# du -sh /data/backup/
146M /data/backup/
?
# 如果同步的時候,使用的不是ssh的22號端口,而是其他端口,比如222,要加-e參數指定端口號
rsync -avz /data/ ?-e "ssh -p 222" test@192.168.115.112:/data/backup
五、使用非系統用戶備份數據
5.1 rsync的配置文件介紹
rsync的配置文件:/etc/rsync.conf
# /etc/rsyncd.conf
#全局參數:對rsync服務器生效,優先級較低
port ? ? # rsync占用端口號,默認是873
address ?# 監聽地址,一般是目標主機的IP地址
uid ? ? ?# 運行進程的用戶
gid ? ? ?# 運行進程的用戶組
max connections ?# 最大連接數
lock file ? # 最大連接數的鎖文件
motd file ? # 同步登錄后的提示語,填寫歡迎同步信息,自行創建
log file ? ?# 日志文件
pid file ? ?# 進程PID文件,自動生成
hosts allow ?# 允許同步的主機
?
#模塊參數:針對某一個目錄定義的參數,優先級較高
[mod_name] ? ?# 同步目錄名
comment ? ? ? # 描述信息
path ? ? ? ? ?# 同步目錄
read only ? ? # 同步目錄的讀寫權限
exclude ? ? ?
exclude from
include
include from
auth users ? # 備份的用戶,自動創建,與系統用戶無關
secrets file ?# 存放rsync用戶的密碼文件
hosts allow
hosts deny
list
timeout
?
# 啟動rsync就會生成一個對應的pid
[root@server ~]# ll /var/run/rsyncd.pid
-rw-r--r--. 1 root root 6 8月 ? 8 09:46 /var/run/rsyncd.pid
[root@server ~]# cat /var/run/rsyncd.pid
18933
5.2 配置備份目錄
# 查看下/etc/rsyncd.conf的默認內容
# 可以使用man rsyncd.conf 命令查看更多選項信息
[root@server ~]# ll /etc/rsyncd.conf
-rw-r--r--. 1 root root 458 4月 ?11 2018 /etc/rsyncd.conf
[root@server ~]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
?
# See rsyncd.conf man page for more options.
?
# configuration example:
?
# uid = nobody (運行賬戶)(生產環境下要注意)
# gid = nobody ? (運行組)
# use chroot = yes (鎖定當前組)
# max connections = 4 (最多連接四個賬戶)
# pid file = /var/run/rsyncd.pid ?
# exclude = lost+found/
# transfer logging = yes (傳輸登錄)
# timeout = 900
# ignore nonreadable = yes
# dont compress ? = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 ? (同步過程中包含這些文件)
?
# [ftp]
# ? ? ? path = /home/ftp
# ? ? ? comment = ftp export area
-
服務器:serverpc 192.168.157.129 (需要進行數據備份的主機)
-
客戶端:targetpc 192.168.157.130 (備份存儲的主機)
一、客戶端
1、編寫客戶端/etc/rsyncd.conf文件,并且創建目錄/data/backup
push
# 在客戶端上編寫rsync配置文件,創建一個存放備份的同步目錄
[root@targetcp ~]# vim /etc/rsyncd.conf
port=873
address = 192.168.157.130 ?#本機IP
uid = root ? ? ?
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = 192.168.157.0/24 ? ?#目標機的IP
[data] ? ?#(邏輯名 網絡名)
path = /data/backup
comment = bakcup data
read only = false
list = yes ? #是否列出
auth users = rsyncuser
secrets file = /etc/rsync.passwd ?#獨立系統用戶的驗證
~ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
mkdir -P /
2、創建文件rsync.passwd寫入用戶和密碼,并將密碼文件賦權
# 創建密碼文件,格式 ? 用戶名:密碼
# 設置密碼文件權限為600或者700
[root@targetpc ~]# vim /etc/rsync.passwd
[root@targetpc ~]# cat /etc/rsync.passwd
rsyncuser:123456
[root@targetpc ~]#chmod 600 /etc/rsync.passwd
3、寫入歡迎信息
[root@targetpc ~]# echo "Welcome to Backup Server" > /etc/rsyncd.motd
4、創建備份目錄
[root@targetpc ~]#mkdir -p /data/backup
5、啟動rsync服務
systemctl start rsyncd##前提關閉防火墻和安全上下文#查看監聽端口號
[root@targetpc ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 192.168.157.130:873 *:*
二、服務器
1、回到數據源,也就是服務器serverpc手動push,創建目錄/data自定義該目錄的文件
[root@sourcepc data]# ls
aaa ccc f2 GNU-Linux-x86
bbb f1 f3 sersync2.5.4_64bit_binary_stable_final.tar.gz
2、然后開始同步文件
[root@sourcepc data]# rsync -avz /data/* rsyncuser@192.168.157.130::data
Welcome to Backup ServerPassword:
sending incremental file list
f1
f2
f3
aaa/
bbb/
ccc/sent 439 bytes received 90 bytes 36.48 bytes/sec
total size is 2,541,385 speedup is 4,804.13
3、去客戶端查看驗證,并在客戶機上創建一個文件file1
[root@targetpc backup]# ls
aaa ccc f2 GNU-Linux-x86
bbb f1 f3 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@targetpc backup]# touch file1
[root@targetpc backup]# ls
aaa f1 file1
bbb f2 GNU-Linux-x86
ccc f3 sersync2.5.4_64bit_binary_stable_final.tar.gz
4、在服務機上刪除file1,刪除只會將服務機備份源沒有的,而客戶端目標位置有的文件刪除,最大程度保持一致
[root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.157.130::data
Welcome to Backup ServerPassword:
sending incremental file list
deleting file1
./sent 344 bytes received 32 bytes 27.85 bytes/sec
total size is 2,541,385 speedup is 6,759.00
5、在客戶端驗證文件是否存在
#此時發現文件file1并不存在,說明同步成功
[root@targetpc backup]# ls
aaa ccc f2 GNU-Linux-x86
bbb f1 f3 sersync2.5.4_64bit_binary_stable_final.tar.gz
三、可以不用輸密碼直接同步
1、在服務器上創建與客戶端密碼相同的文件,并賦予相同的權限
[root@sourcepc ~]# vim /etc/rsync.passwd
[root@sourcepc ~]# cat /etc/rsync.passwd
123456
[root@sourcepc ~]# chmod 600 /etc/reync.passwd
2、在服務器上驗證免密傳輸
#固定格式 --password-file=密碼文件
[root@sourcepc data]# rsync -avz /data rsyncuser@192.168.157.130::data --password-file=/etc/rsync.passwd
Welcome to Backup Serversending incremental file list
data/
data/f3
data/sersync2.5.4_64bit_binary_stable_final.tar.gz
data/GNU-Linux-x86/
data/GNU-Linux-x86/confxml.xml
data/GNU-Linux-x86/nohup.out
data/GNU-Linux-x86/sersync2
data/aaa/sent 1,454,725 bytes received 131 bytes 138,557.71 bytes/sec
total size is 2,541,385 speedup is 1.75
# 啟動xinetd服務,并加入到開機自啟中
[root@client ~]# systemctl start xinetd
[root@client ~]# systemctl enable xinetd
# 如果已經啟動了rsync服務,要使用非系統用戶登錄,則必須先把原先的rsync停下來。
[root@client ~]# systemctl stop rsyncd
[root@client ~]# rsync --daemon --config=/etc/rsyncd.conf# 開放873端口號,允許通過873端口號同步數據 (防火墻已關閉,可以不用做)
[root@client ~]# firewall-cmd --permanent --zone=public --add-port=873/tcp
success
[root@client ~]# firewall-cmd --reload
success
5.3 使用rsync用戶備份測試
#
rsync用戶一般是第一個普通用戶,即UID為1001,故備份的所有者和所屬組都為1001
# 服務端推送數據到共享目錄位置
#rsync 選項 需要備份的目錄 rsync用戶名@存放備份的服務器IP::共享模塊名 --password-file=密碼文件
[root@server ~]# rsync -avz --delete /data/ rsyncuser@192.168.115.112::data
Welcome to Backup ServerPassword:
sending incremental file list
。。。省略同步信息。。。
sent 138,094,811 bytes received 6,297 bytes 6,137,827.02 bytes/sec
total size is 152,364,618 speedup is 1.10
# 如果想不輸入密碼,可以使用--password-file參數指定密碼
[root@server ~]# cat /etc/rsync.passwd
123456
[root@server ~]# chmod 600 /etc/rsync.passwd
[root@server ~]# rsync -avz /data rsyncuser@192.168.157.130::data --password-file=/etc/rsync.passwd
# 如果想定時同步數據,可以設置定時任務。
[root@server ~]# vim autobackup.sh
[root@server ~]# cat autobackup.sh
#!/bin/bash
rsync -avz /data rsyncuser@192.168.115.112::data --password-file=/etc/rsync.passwd
[root@server ~]# chmod +x autobackup.sh
[root@server ~]# crontab -e
50 19 * * * bash /root/autobackup.sh
5.4 pull拉取數據
以上的同步過程都是服務端主動推送數據給目標主機,這里演示下目標主機主動拉取數據進行同步。
# 服務端上配置/etc/rsyncd.conf文件
[root@server ~]# vim /etc/rsyncd.conf
port=873
address = 192.168.115.111
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = 192.168.43.0/24
[data]
path = /data
comment = master data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd
# 認證文件
[root@server ~]# cat /etc/rsync.passwd
rsyncuser:123456
# 服務端上啟動rsync
[root@server ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@server ~]# netstat -antup | grep 873
tcp 0 0 192.168.115.111:873 0.0.0.0:* LISTEN 11040/rsync
# 看到以上結果,說明服務端的rsync服務已經配置完成。
# 開放873端口號,允許通過873端口號拉取數據
[root@server ~]# firewall-cmd --permanent --zone=public --add-port=873/tcp
success
[root@server ~]# firewall-cmd --reload
success
# 目標主機上拉取數據
[root@client ~]# rsync -avz rsyncuser@192.168.115.111::data /data/backup/
Welcome to Backup ServerPassword:
receiving incremental file list
。。。省略拉取過程。。。
sent 6,342 bytes received 138,094,913 bytes 11,048,100.40 bytes/sec
total size is 152,364,627 speedup is 1.10
六、rsync+sersync 實現數據實時同步
6.1 數據同步原理
1、為什么要是用rsync+sersync
sersync是基于inotify開發的,可以記錄目錄中發生變化的內容,具體到某個文件或者目錄,在使用rsync同步的時候,只同步發生變化的文件或者目錄。【增量同步】
2、rsync+inotify-tools 與 rsync+sersync 架構的區別?
①inotify-tools只能記錄目錄發生了變化,但是不能具體到某個文件或者目錄。
②rsync同步不清楚哪些文件或目錄發生了變化,就會整個目錄都同步,效率低。
3、同步過程:
①源服務器上開啟sersync記錄指定路徑的文件系統變化情況。
②源服務器上使用rsync命令把變化的數據同步到目標服務器上。
③源服務器上配置sersync服務,目標服務器安裝rsync服務。
6.2 部署rsync+sersync
sersync服務端【同步服務器】:192.168.115.111
rsync客戶端【存放備份,目標服務器】:192.168.115.112
由于sersync只能監控到差異變化,同步還是需要rsync去做,所以先確保rsync運行正常。可以測試一遍,看能否做完全同步。
[root@client ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@client ~]# netstat -antup | grep rsync
tcp 0 0 192.168.115.112:873 0.0.0.0:* LISTEN 9298/rsync
# 添加到開機自啟
[root@client ~]# vim /etc/rc.d/rc.local
rsync --daemon --config=/etc/rsyncd.conf
###備份源上測試
[root@server ~]# rsync -avz /data rsyncuser@192.168.115.112::data --password-file=/etc/rsync.passwd
開始部署sersync守護進程
# 解壓并重命名為sersync
[root@server ~]# cd /opt
[root@server opt]# ll
總用量 712
-rw-r--r--. 1 root root 727290 8月 9 09:52 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@server opt]# tar xzf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@server opt]# ll
總用量 712
drwxr-xr-x. 2 root root 41 10月 26 2011 GNU-Linux-x86
-rw-r--r--. 1 root root 727290 8月 9 09:52 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@server opt]# mv GNU-Linux-x86 sersync
[root@server opt]# ll
總用量 712
drwxr-xr-x. 2 root root 41 10月 26 2011 sersync
-rw-r--r--. 1 root root 727290 8月 9 09:52 sersync2.5.4_64bit_binary_stable_final.tar.gz
# 修改sersync的配置文件
# sersync服務總共就兩個文件,一個配置文件,一個啟動文件
[root@server ~]# cd /opt/sersync/
[root@server sersync]# ll
總用量 1772
-rwxr-xr-x. 1 root root 2109 8月 9 09:59 confxml.xml
-rwxr-xr-x. 1 root root 1810128 10月 26 2011 sersync2
[root@server ~]# vim /opt/sersync/confxml.xml
# 定義監控路徑、同步的目標主機、共享模塊
23 <sersync>
24 <localpath watch="/data">
25 <remote ip="192.168.115.112" name="data"/>
26 </localpath>
# 開啟用戶認證,并設置賬號和密碼
29 <auth start="true" users="rsyncuser" passwordfile="/etc/rsync.passwd"/>
# 開啟sersync守護進程同步數據,即實時同步。
# -r參數,先做一次完全同步,再做差異同步。
[root@server ~]# /opt/sersync/sersync2 -d -r -o /opt/sersync/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: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: /opt/sersync/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 rsyncuser
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 && rsync -artuz -R --delete ./ rsyncuser@192.168.115.112::data --password-file=/etc/rsync.passwd >/dev/null 2>&1
[root@server ~]# run the sersync:
watch path is: /data # 可以看到監控的是/data
# 打開監控,查看實時同步情況
# 當目錄/data下的文件或者目錄有任何的變化,都會被監控到,并同步到目標主機上。
[root@server data]# watch ls -l
從以上測試可以看出,rsync+sersync已經部署完成,可以正常的同步數據到目標主機上。
6.3 設置rsync+sersync開機自啟
# 設置開機自啟
[root@server ~]# vim /etc/rc.d/rc.local
/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
# 如sersync正常運行,通過ps命令可以看到進程已經開啟。
[root@server ~]# ps aux | grep sersync2 | grep -v 'grep'| wc -l
1
# 檢查sersync是否正常運行
[root@server ~]# vim /opt/check_sersync.sh
#!/bin/sh
while true
do
sersync="/opt/sersync/sersync2"
confxml="/opt/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ] ; thenecho "sersync未開啟,現在開啟"echo "==============================="$sersync -d -r -o $confxml echo "==============================="bash $0
elseecho "sersync已正常開啟"exit 0;
fi
sleep 1
done
[root@server ~]# chmod +x /opt/check_sersync.sh
[root@server ~]# bash /opt/check_sersync.sh
# 設置兩分鐘檢查一次sersync是否正常運行
[root@server ~]# crontab -e
*/2 * * * * sh /opt/check_sersync.sh &> /dev/null
一、部署rsync+sersync
-
serverpc服務端【同步服務器】:serverpc 192.168.157.129
-
targetpc客戶端【存放備份,目標服務器】:targetpc:192.168.157.130
1、由于sersync只能監控到差異變化,同步還是需要rsync去做,所以先確保rsync運行正常。可以測試一遍,看能否完全同步
[root@sourcepc data]# rz #導入軟件包
[root@sourcepc data]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@sourcepc data]# ls
aaa f3 GNU-Linux-x86 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@sourcepc data]# cd GNU-Linux-x86/
[root@sourcepc GNU-Linux-x86]# ls
confxml.xml nohup.out sersync2##將所有的false改為true觸發
[root@sourcepc GNU-Linux-x86]# <inotify><delete start="true"/> #當文件或目錄被刪除時,觸發監控事件<createFolder start="true"/> #當新文件夾被創建時,觸發監控事件<createFile start="true"/> #當新文件被創建時,觸發監控事件<closeWrite start="true"/> #當文件被寫入并關閉時(即寫入操作完成)<moveFrom start="true"/> #當文件或文件夾被移動(剪切)出監控目錄時,觸發事件<moveTo start="true"/> #當文件或文件夾被移動(剪切)到監控目錄時,觸發事件<attrib start="true"/> #當文件的屬性(如權限、所有者、時間戳等)被修改時,觸發事件<modify start="true"/> #當文件內容被修改(但未關閉寫入)時,觸發事件</inotify><sersync><localpath watch="/data"> #監視的目錄<remote ip="192.168.157.130" name="data"/> #監視到的內容同步到誰,同步名data<!--<remote ip="192.168.8.39" name="tongbu"/>--><!--<remote ip="192.168.8.40" name="tongbu"/>--></localpath><rsync><commonParams params="-artuz"/><auth start="true" users="rsyncuser" passwordfile="/etc/rsync.passwd"/>#users=改為rsyncuser#passwordfile=改為密碼文件<userDefinedPort start="false" port="874"/><!-- port=874 --> #不自定義端口號<timeout start="false" time="100"/><!-- timeout=100 --><ssh start="false"/></rsync>
2、啟動服務(因為在當前目錄,直接./執行)
[root@sourcepc GNU-Linux-x86]# ./sersync2 -d -r -o ./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: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: ./confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
will ignore the inotify createFile event
daemon start,sersync run behind the console
use rsync password-file :
user is rsyncuser
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 && rsync -artuz -R --delete ./ rsyncuser@192.168.157.130::data --password-file=/etc/rsync.passwd >/dev/null 2>&1
3、創建一個文件或目錄觀察監控
[root@sourcepc data]# mkdir abc##在客戶端上傳輸成功
[root@targetpc backup]# ls
aaa f3 sersync2.5.4_64bit_binary_stable_final.tar.gz
abc GNU-Linux-x86##在服務器上監測 (-d 放在后臺)
[root@sourcepc GNU-Linux-x86]# strace ./sersync2 -r -o ./confxml.xml
inotify_add_watch(3, "/data/aaa", IN_MODIFY|IN_ATTRIB|IN_CLOSE_WRITE|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE) = 3
open("/data/aaa", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 5
getdents(5, /* 2 entries */, 32768) = 48
getdents(5, /* 0 entries */, 32768) = 0
close(5) = 0
inotify_add_watch(3, "/data/abc", IN_MODIFY|IN_ATTRIB|IN_CLOSE_WRITE|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE) = 4
open("/data/abc", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 5
getdents(5, /* 2 entries */, 32768) = 48
getdents(5, /* 0 entries */, 32768) = 0
close(5) = 0
inotify_add_watch(3, "/data/123", IN_MODIFY|IN_ATTRIB|IN_CLOSE_WRITE|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE) = 5
open("/data/123", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 5
getdents(5, /* 2 entries */, 32768) = 48
getdents(5, /* 0 entries */, 32768) = 0
close(5) = 0
getdents(4, /* 0 entries */, 32768) = 0
close(4) = 0
fcntl(3, F_GETFL) = 0 (flags O_RDONLY)
read(3,
##更改名稱
[root@sourcepc data]# mv abc ABC
[root@targetpc backup]# ls
123 ABC GNU-Linux-x86
aaa f3 sersync2.5.4_64bit_binary_stable_final.tar.gz
4、腳本編寫
#!/bin/bash
systemctl status rsyncd &> /dev/null
if [ $? -ne 0 ];then
systemctl start rsyncd
firead -p "請輸入ip地址:" ip
read -p "請輸入創建的文件名稱:" file
read -p "請輸入跨機使用的密碼:" passwd
mask=`echo $(echo $ip | cut -d. -f1,2,3).0/24`
echo "port=873
address = $ip
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = $mask
[$file]
path = /$file/backup
comment = bakcup data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd">/etc/rsyncd.conf
touch /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
echo "rsyncuser:$passwd">/etc/rsync.passwd
mkdir -p /$file/backup
echo "Welcome to Backup server">/etc/rsyncd.motdsystemctl restart rsyncd
七、總結
-
rsync可以進行數據的同步,可以使用推和拉兩種方式。推即源主機推數據到目標主機,拉即目標主機從源主機上拉數據。
-
push:服務器向客戶端推送數據,要在目標主機上配置一個共享目錄,在服務端上使用rsync命令推送數據給目標主機。
-
pull:客戶端向服務器拉去數據,需要把服務器上的同步目錄配置成一個共享目錄,然后客戶端去這個共享目錄上拉去數據。
-
僅使用rsync同步數據,不會記錄數據的變化,每次同步都是同步整個目錄,不適合大量數據的同步。
-
結合使用rsync+sersync,sersync負責監控源主機上同步目錄的數據變化,rsync負責同步變化的部分,極大地提高了同步的效率。