小菜狗的云計算之旅,學習了解rsync+sersync實現數據實時同步(詳細操作步驟)

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負責同步變化的部分,極大地提高了同步的效率。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/913321.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/913321.shtml
英文地址,請注明出處:http://en.pswp.cn/news/913321.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

牛客周賽Round 99(Go語言)

A題 (A.go) 思路總結: 這道題要求判斷一個整數中是否包含連續的兩個9。 核心思路是將輸入的整數轉換為字符串&#xff0c;然后遍歷這個字符串&#xff0c;檢查是否存在相鄰的兩個字符都是9。如果找到了&#xff0c;就立即停止遍歷并輸出"YES"&#xff1b;如果遍歷完…

紅外圖像小目標檢測熱力圖可視化系統

原創代碼&#xff0c;可以工程修改含界面。

供應鏈管理:指標評估方式分類與詳解

一、指標評估方式分類與詳解 評估維度評估方式核心方法適用場景示例數據來源內部數據評估從企業ERP、MES、CRM等系統提取生產、財務、客戶等數據。成本、效率、質量等內部管理指標評估。生產成本數據&#xff08;MES系統&#xff09;、客戶滿意度&#xff08;CRM系統&#xff…

基于 Rust 的前端工具基本實現

1. Rust 環境安裝 1.1. 安裝 Rust Rust 提供了一個非常方便的安裝工具 rustup,可以通過以下命令安裝 Rust: curl --proto =https --tlsv1.2 -sSf https://sh.rustup.rs | sh 這個命令會安裝 Rust 編譯器 rustc、包管理工具 cargo 以及其他相關工具。 1.2. 配置環境變量 …

大模型關鍵字解釋

&#x1f4a1; 一、模型結構關鍵詞 1. Transformer Transformer 是一種專門用來“理解文字”的神經網絡結構。就像一個聰明的秘書&#xff0c;能同時看懂整段話的所有詞之間的關系&#xff0c;而不是像老式模型那樣一句一句讀。 &#x1f449; 舉例&#xff1a;以前的模型像…

空調和烘干機的使用

開關 制冷 選擇上下掃風 那個就下來了 烘干機 電源鍵 長按3s以上直到菜單顯示 選擇小件 不要快烘 至少1個半小時 才可以烘干

極簡的神經網絡反向傳播例子

我之前一直沒搞清楚&#xff0c;神經網絡為什么要求導&#xff1f;反向傳播又是什么&#xff1f;于是到現在深究回來…… 本質就是擬合一個未知函數。 高中的數理統計就學過最小二乘法這種回歸方法&#xff08;? 代表自己的預測y&#xff0c;這個表達要記住&#xff09;&…

01-什么是強化學習

什么是強化學習 1. 定義 強化學習&#xff08;Reinforcement Learning, RL&#xff09;是一種使智能體&#xff08;Agent&#xff09;通過與環境&#xff08;Environment&#xff09;不斷交互&#xff0c;學習如何在不同情境下采取行動以獲得最大化累積獎勵的機器學習方法。 強…

淘寶直播數字人:音視頻算法工程技術

本專題是我們打造智能數字人的部分實踐總結。我們將探討六大核心環節&#xff1a;LLM文案生產賦予數字人思考和內容生成能力&#xff0c;如同其“大腦”&#xff1b;LLM互動能力則聚焦對話邏輯與擬人化交流&#xff0c;是實現自然交互的關鍵&#xff1b;TTS&#xff08;語音合成…

MySQL回表查詢深度解析:原理、影響與優化實戰

引言 作為后端開發或DBA&#xff0c;你是否遇到過這樣的場景&#xff1a; 明明給字段加了索引&#xff0c;查詢還是慢&#xff1f;EXPLAIN一看&#xff0c;執行計劃里type是ref&#xff0c;但數據量不大卻耗時很久&#xff1f; 這時候&#xff0c;你很可能遇到了MySQL中常見的…

任務管理器看不到的內存占用:RAMMap 深度分析指南

前言&#xff1a;任務管理器看不到的內存真相 在日常使用 Windows 系統時&#xff0c;我們有時會遇到一種令人費解的情況&#xff1a; 剛剛開機&#xff0c;什么軟件都沒運行&#xff0c;系統內存卻已經占用了 7&#xff5e;8 GB。 打開任務管理器一看&#xff0c;前幾個進程加…

從傳統倉庫到智能物流樞紐:艾立泰的自動化蛻變之旅

在物流行業智能化浪潮中&#xff0c;艾立泰從依賴人工的傳統倉庫轉型為智能物流樞紐&#xff0c;其自動化升級路徑為行業提供了典型范本。?曾幾何時&#xff0c;艾立泰倉庫內人工搬運、紙質單據流轉、手工盤點是常態&#xff0c;效率低下、差錯率高、人力成本攀升等問題制約發…

408第三季part2 - 計算機網絡 - 滑動窗口

理解 幀本質就是一堆二進制&#xff0c;后面會將幀的格式 流量控制就是 B&#xff1a;急急急急急急 A&#xff1a;別急 A控制B&#xff0c;B控制C&#xff0c;C控制D&#xff0c;但D無法控制A&#xff0c;這就是相鄰節點 abc在發送的過程中發送完了 怎么才能繼續發送呢 沒…

RedHat高可用集群深度解析與優化

一、RHCS核心組件深度解析1. Corosync&#xff08;消息層&#xff09;通信機制改進說明&#xff1a; Totem協議采用環形令牌傳遞機制&#xff0c;在10節點以下集群中使用UDP/IP組播&#xff08;224.0.0.12&#xff09;&#xff0c;超過10節點建議改用UDP/UDP單播。典型配置示例…

為什么使用 XML Schema?

為什么使用 XML Schema? XML(可擴展標記語言)是一種廣泛使用的標記語言,它被設計用來存儲和傳輸數據。XML Schema 是一種用于定義 XML 文檔結構的語言,它為 XML 文檔提供了嚴格的驗證機制。以下是使用 XML Schema 的幾個主要原因: 1. 結構化數據定義 XML Schema 允許開…

ESP32藍牙學習筆記

藍牙 官網&#xff1a;https://www.bluetooth.com/zh-cn/learn-about-bluetooth/tech-overview/ 概述 分類&#xff1a;Bluetooth經典、Bluetooth低能耗(LE) GAP 通用訪問配置文件(Generic Access Profile, GAP)簡稱GAP&#xff0c;該Profile保證不同的Bluetooth產品可以互…

C#擴展方法全解析:給現有類型插上翅膀的魔法

C#擴展方法全解析&#xff1a;給現有類型插上翅膀的魔法 在 C# 的類型系統中&#xff0c;當我們需要為現有類型添加新功能時&#xff0c;傳統方式往往意味著繼承、重寫或修改源代碼 —— 但如果是string、int這樣的系統類型&#xff0c;或是第三方庫中的密封類&#xff0c;這些…

YOLOv11在邊緣計算設備上的部署與優化:從理論到實踐

邊緣計算與YOLOv11的融合背景 邊緣計算的崛起與核心價值 邊緣計算作為一種分布式計算范式&#xff0c;正深刻改變著人工智能應用的部署方式。其核心在于將數據處理從云端下沉到網絡邊緣&#xff0c;在靠近數據源的位置完成計算任務。根據國際數據公司&#xff08;IDC&#xf…

Solidity——pure 不消耗gas的情況、call和sendTransaction區別

/ pure: 純純牛馬 function addPure(uint256 _number) external pure returns(uint256 new_number){ new_number _number 1; }不會消耗gas對吧。傳的不是狀態變量 你的理解基本對了&#xff0c;但我們來更嚴謹、深入地回答這個問題。 ? 你這段 pure 函數代碼&#xff1a; …

柔性電路芯片賦能腦機接口:技術融合、應用突破與前景展望

柔性電路芯片賦能腦機接口:技術融合、應用突破與前景展望 一、引言 1.1 研究背景與意義 在科技飛速發展的時代,柔性電路芯片與腦機接口的融合展現出巨大的潛力,為醫療、科研等多個領域帶來了新的機遇與變革。 從醫療領域來看,隨著人口老齡化的加劇以及神經系統疾病患者…