rsync


rsync 遠程同步

一:rysnc 基礎操作

1   rsync 概述

rsync (Remote Sync,遠程同步),支持本地復制,或與其他SSH,rsync主機同步.

官方網站:http://rsync.samba.org/

是一個開源的快速備份工具,可以在不同主機之間鏡像同步整個目錄樹,支持增量備份,並保持鏈接和權限,且采用優化的同步算法,傳輸前執行壓縮,因此非常適用於異地備份、鏡像服務器等應用。

在遠程同步任務中,負責發起rsync同步操作的客戶機稱為發起端,而負責響應來自客戶機的rsync同步操作的服務器稱為同步源。在同步過程中,同步源負責提供文件的原始位置,發起端應對該位置具有讀取權限。



2 sync 同步操作

命令用法:

rsync [選項....] 源目錄 目標目錄


本地同步:

  • rsync [選項...] 本地目錄1 本地目錄2 #同步整個文件夾
  • rsync [選項.....] 本地目錄1/ 本地目錄2 #只同步目錄下的數據


rsync 操作選項:

選項 釋義
-r 遞歸模式,包含目錄及子目錄中的所有文件
-l(小寫L) 對於符號鏈接文件仍然復制為符號鏈接文件
-v 顯示同步過程中的詳細(verbose)信息
-z 在傳輸文件時進行壓縮(compress)
-a 歸檔模式,保留文件的權限,屬性等信息,等同於組合選項"-rlptgoD"
-p 保留文件的權限標記
-t 保留文件的時間標記
-g 保留文件的屬組標記(僅超級用戶使用)
-o 保留文件的屬主標記(僅超級用戶使用)
-H 保留硬鏈接文件
-A 保留ACL屬性信息
-D 保留設備文件及其他特殊文件
-S(--sparce) 處理稀疏文件時使用
--delete 刪除目錄位置有,而原始位置沒有的文件。
--checksum 更具校驗和(而不是文件大小,修改時間)來決定是否跳過文件.
--password-flie= 指定密碼文件

#當前,/test01目錄里有文件a.txt,b.txt。 /test02 和 /test03目錄為空
[root@host104 ~]# ls /test01 /test02 /test03
/test01:
a.txt  b.txt

/test02:

/test03:


#同步/test01 目錄到 /test02目錄下
[root@host104 ~]# rsync -a /test01 /test02

#同步/test01 目錄下文件到  /test03目錄下
[root@host104 ~]# rsync -a /test01/  /test03

[root@host104 ~]# ls /test01 /test02 /test03
/test01:
a.txt  b.txt

/test02:
test01

/test03:
a.txt  b.txt

image-20210923160749988

image-20210923152848837





二: sync +ssh 同步

2.1 配置rsync 源服務器

基本思路:

  • 建立rsyncd.conf配置文件,獨立的賬號文件
  • 啟用rsync 的--daemon模式

image-20210923172932287


2.1.1  修改配置文件 /etc/rsyncd.conf

[root@host104 ~]# systemctl  stop  firewalld
[root@host104 ~]# setenforce  0
[root@host104 ~]# rpm -q rsync

#查看rsync的默認監聽端口
[root@host104 ~]# cat  /etc/services  | grep rsync
rsync           873/tcp                         # rsync
rsync           873/udp                         # rsync


[root@host104 ~]# vim /etc/rsyncd.conf

 uid = nobody
 gid = nobody
 use chroot = yes                  #禁錮在源目錄
 address = 192.168.23.104          #監聽地址。
 port 873                          #監聽端口(UDP/TCP 873端口。
 log file = /var/run/rsyncd.log    #日志文件位置
 pid file = /var/run/rsyncd.pid    #存放進程pid文件的位置
 hosts allow = 192.168.23.0/24     #允許訪問的客戶機


 [wwwroot]                                 #模塊名稱   
 path = /var/www/html                      #源目錄的實際路徑
 comment = Document Root of web server     #描述信息
 read only = yes                           #設置為只讀
 dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   #同步時不再壓縮的文件類型
 auth users = backuper                      #授權用戶,多個賬戶用空格分隔
 secrets file = /etc/rsyncd_users.db        #設置存放賬戶信息的數據文件

如果采用匿名的方式,只要將其中的“auth users“ 和“secrets file”去掉就可以了 


2.1.2 創建備份賬戶數據文件

[root@host104 ~]# vim /etc/rsyncd_users.db
#rsync 的認證用戶和密碼。不用創建同名的系統用戶
backuper:abc123

[root@host104 ~]# chmod  600 /etc/rsyncd_users.db 



2.1.3 配置源目錄的權限

要保證所有用戶對源目錄(/var/www/html) 都有讀取權限

[root@host104 ~]# chmod  +r /var/www/html/

[root@host104 ~]# ls -ld /var/www/html/
drwxr-xr-x 2 root root 6 9月  23 16:12 /var/www/html/



2.1.4 啟動rsync 服務

#啟動rsync 服務,以堵路監聽服務的防水(守護進程)運行
[root@host104 ~]# rsync  --daemon
[root@host104 ~]# netstat -natp | grep rsync
tcp        0      0 192.168.23.104:873      0.0.0.0:*               LISTEN      82116/rsync

如果要關閉rsync服務:

kill $(cat  /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid





2.2 發起端配置

2.2.1 發起端同步的命令

查看遠端共享目錄的內容

格式: rsync -avz backuper@192.168.23.104::wwwroot

  • backuper為rsync 同步的用戶,密碼設置的是abc123

將制定的資源下載到本地的/opt目錄下備份

格式一: rsync backuper@192.168.23.104::wwwroot /opt

  • backuper為rsync 同步的用戶,密碼設置的是abc123
  • ip 后面跟的是 雙冒號::后面是 設置的共享模塊名

格式二: rsync -avz rsync://backuper@192.168.23.104/wwwroot /opt

  • 使用url 路徑。使用的是 **冒號雙斜線 :// **格式
  • backuper為rsync 同步的用戶,密碼設置的是abc123

#查看遠程 源服務器共享目錄 下有哪些內容
[root@host103 opt]# rsync  backuper@192.168.23.104::wwwroot 
Password: #輸入密碼
drwxr-xr-x          19 2021/09/23 16:49:54 .
#這就是當前 源服務器共享目錄下的內容a.txt
-rw-r--r--           0 2021/09/23 16:49:54 a.txt  


#源服務器的wwwroot 模塊下的內容備份到本地的 /opt 目錄下
[root@host103 opt]# rsync -avz backuper@192.168.23.104::wwwroot  /opt/
Password:   #輸入rsync 賬戶 backuper 的密碼

[root@host103 opt]# ls
#文件被拉取到了本地的/opt 
a.txt 


image-20210923170044520

image-20210923170305736



2.2.2 免交互式配置定期同步

#創建文件,將密碼寫入文件中
[root@host103 ~]# echo "abc123" > /etc/server.pass

#設置權限,防止密碼文件被其他用戶篡改
[root@host103 ~]# chmod  600 /etc/server.pass

[root@host103 ~]# crontab  -e
30 1 * * *  /usr/bin/rsync -az  --delete --password-file=/etc/server.pass backuper@192.168.23.104::wwwroot  /opt/
#每天1:30  j進行備份,將源服務器的wwwroot模塊下文件備份到本地的 /opt/目錄下
#--deleete   刪除源服務器沒有,而本地有的文件
#--password-file=   指定保存rsync 用戶backuper密碼的文件,以實現免交互


[root@host103 opt]# systemctl restart crond
[root@host103 opt]# systemctl enable crond

image-20210923171909692





三: rsync + inotify 機制實現實時同步

3.1 為什么使用實時同步

定期同步的不足:

  • 執行備份的時間固定,延遲明顯,實時性差
  • 當同步源長期不變化時,密集的定期任務是不必要的

實時同步的優點:

  • 一旦同步源出現變化,立即啟動備份
  • 只要同步源無變化,則不執行備份



3.2 關於inotify/

Linux 內核的inotify機制:

  • 從版本2.6.13 開始提供
  • 可以監控文件系統的變動情況,並作出通知響應
  • 輔助軟件:inotify-tools

image-20210923172720851


使用inotify通知接口,可以用來監控文件系統的各種變化情況,如文件存取、刪除、移動、修改等。利用這一機制,可以非常方便地實現文件異動告警、增量備份,並針對目錄或文件的變化及時作出響應。

將inotify機制與rsync工具相結合,可以實現觸發式備份(實時同步),即只要原始位置的文檔發生變化,則立即啟動增量備份操作,否則處於靜默等待狀態。這樣,就避免了按固定周期備份時存在的延遲性、周期過密等問題。

因為 inotify 通知機制由 Linux 內核提供,因此主要做本機監控,在觸發式備份中應用時更適合上行同步。



3.3 源服務器 修改rsync 服務配置文件,並修改目錄權限

[root@host104 ~]# vim /etc/rsyncd.conf
........
#uid和gid 修改問目錄/var/www/html 屬組和屬主
 uid = root
 gid = root
#關閉只讀,上行同步需要可以寫。
read only = no
......

#關閉rsync 服務
[root@host104 ~]# kill $(cat /var/run/rsyncd.pid)
[root@host104 ~]# rm -rf /var/run/rsyncd.pid

#啟動rsync服務
[root@host104 ~]# rsync --daemon
[root@host104 ~]# netstat -natp | grep rsync
tcp        0      0 192.168.23.104:873      0.0.0.0:*               LISTEN      83003/rsync

#修改目錄權限
[root@host104 ~]# chmod 777 /var/www/html/

image-20210923195055037

image-20210923173723184

image-20210923173650731



3.4 發起端調整inotify 內核參數

在Linux內核中,默認的inotify機制提供了三個調控參數∶max_queue_events(監控事件隊列,默認值為16384)、max_user_instances(最多監控實例數,默認值為128)、max_user_watches(每個實例最多監控文件數,默認值為8192)。當要監控的目錄、文件數量較多或者變化較頻繁時,建議加大這三個參數的值。

[root@host103 ~]# cat /proc/sys/fs/inotify/max_queued_events 
16384
[root@host103 ~]# cat /proc/sys/fs/inotify/max_user_instances 
128
[root@host103 ~]# cat /proc/sys/fs/inotify/max_user_watches 
8192


[root@host103 ~]# vim /etc/sysctl.conf 
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@host103 ~]# sysctl  -p



3.5 發起端安裝 inotify-tools

用 inotify 機制還需要安裝inotify-tools,以便提供 inotifywait、inotifywatch 輔助工具程序,用來監控、匯總改動情況。

inotifywait∶可監控modify(修改)、create(創建)、move(移動)、delete(刪除)、attrib(屬性更改)等各種事件,一有變動立即輸出結果。

inotifywatch∶可用來收集文件系統變動情況,並在運行結束后輸出匯總的變化情況。

[root@host103 ~]# cd /opt/
[root@host103 opt]# tar xf inotify-tools-3.14.tar.gz  -C /opt/
[root@host103 opt]# cd /opt/inotify-tools-3.14/
[root@host103 inotify-tools-3.14]# ./configure 
[root@host103 inotify-tools-3.14]# make && make install

可先執行“inotifywait” 命令,然后在開啟一個新終端向 /var/www/html 目錄下添加文件,移動文件,在原來的終端中跟蹤屏幕輸出結果


inotifywait -mrq -e modify,create,move,delete /var/www/html

選項 釋義
-e 用來指定要監控哪些事件
-m 表示持續監控
-r 表示遞歸監控整個目錄
-q 簡化輸出信息
[root@host103 opt]#mkdir -p  /var/www/html
#一個終端監控 目錄/var/www/html
[root@host103 html]# inotifywait -mrq -e modify,create,move,delete /var/www/html

#新開一個終端,在/var/www/html 目錄下進行增刪改操作
[root@host103 opt]# cd /var/www/html/

[root@host103 html]# touch a.txt
[root@host103 html]# touch b.txt
[root@host103 html]# mv b.txt /opt/
[root@host103 html]# echo "123" >> a.txt 
[root@host103 html]# rm -rf a.txt

image-20210923192047397

image-20210923192200073



3.6 客戶端編寫觸發式同步腳本

[root@host103 opt]# vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.23.104::wwwroot/"

$INOTIFY_CMD | while read DIRECTORY EVENT FILE
#while判斷是否接收到監控記錄



[root@host103 opt]# chmod  +x /opt/inotify.sh
[root@host103 opt]# chmod  777 /var/www/html/
[root@host103 opt]# chmod  +x /etc/rc.d/rc.local
[root@host103 opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local 

上述腳本用來檢測本機/var/www/html目錄的變動情況,一旦有更新觸發rsync 同步操作,上傳備份至服務器192.168.23.104 的wwwroot 共享目錄下。

觸發式上行同步的驗證過程如下∶

(1)在本機運行/opt/inotify.sh腳本程序。

(2)切換到本機的 /var/www/html目錄,執行增加、刪除、修改文件等操作。

(3)查看遠端服務器中的 wwwroot目錄下的變化情況。

--------------------- 客戶端------------------
#運行腳本
[root@host103 opt]# ./inotify.sh [root@host103 html]# touch abc.txt

#在/var/www/html下創建文件
[root@host103 html]# touch abc.txt
[root@host103 html]# touch 1.txt
[root@host103 html]# 


------------------- 源服務器 的/var/www/html 目錄-----------------
#在客戶端運行腳本前,有a,txt文件
[root@host104 html]# ls
a.txt

#客戶端運行腳本后,同步了客戶端的/var/www/html目錄
[root@host104 html]# ls
1.txt  abc.txt

image-20210923195301566

image-20210923194248070

image-20210923194307100



四: 利用rsync 快速刪除大量文件

假如要在linux下刪除大量文件,比如100萬、1000萬,像/usr/local/nginx/proxy_temp的nginx緩存等,那么rm -rf *可能就不好使了,因為要等待很長一段時間。在這種情況下我們可以使用rsync來巧妙處理。rsync實際用的是替換原理。

#先建立一個空的文件夾
[root@host103 opt]# mkdir /test

#創建大量的文件
[root@host103 opt]# cd /test/
[root@host103 test]# touch a{1..10000}

#准備一個空的目錄
[root@host103 test]# rm -rf /var/www/html/*

#利用rsync刪除文件.
#注意,空目錄做為源目錄,放在前面,且要加 / ,表示同步該目錄下的文件
[root@host103 test]# rsync --delete-before -a -H -v --progress --stats  /var/www/html/ /test

#test目錄下的文件被刪除
[root@host103 test]# ls



五:報錯

5.1 報錯

報錯(1)

當使用rsync + inotify 的時候,雖然能夠使用,但是客戶端監控時,一直報錯

“@ERROR: invalid uid roo
rsync error: error starting client-server protocol (code 5) at main.c(1516) [sender=3.0.9]
@ERROR: invalid uid roo
rsync error: error starting client-server protocol (code 5) at main.c(1516) [sender=3.0.9]”

image-20210923201756778


報錯(2)

"rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
rsync: failed to set permissions on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]"

image-20210923201907409


5.2 原因

配置文件里 uid和gid 配置的和 共享的模塊里的目錄的屬主和屬組不一致

image-20210924075458542

image-20210924075547732



5.3 解決

將配置文件/etc/rsyncd.conf 里的uid 和gid 分別修改為 共享目錄的屬主和屬組.

或者將共享目錄的屬主和屬組修改為 配置文件里定義的uid 和 gid 項的用戶

然后重啟rsync 服務


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM