本地及遠端rsync同步 步驟二

2021-09-24 04:12:22 字數 4376 閱讀 5891

3)驗證–delete選項的作用

在/opt/dir2/目錄下新增測試檔案3.txt,將測試子目錄2.dir改名為3.dir,現在與/opt/dir1/目錄的內容是不一致的:

[root@pc205 ~]# cat /proc/cpuinfo > /opt/dir2/3.txt 

[root@pc205 ~]# mv /opt/dir2/2.dir /opt/dir2/4.dir

[root@pc205 ~]# ls -l /opt/dir1/ /opt/dir2/

/opt/dir1/:

總用量 8

-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt

drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir

/opt/dir2/:

總用量 16

-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt

-rw-r--r--. 1 root root 721 4月 26 17:06 3.txt //增加的測試檔案

drwxr-xr-x. 2 root root 4096 4月 26 16:51 4.dir //改名後的目錄

drwxr-xr-x. 3 root root 4096 4月 26 16:51 dir1

未新增–delete選項時,rsync同步操作是單向的,只是把源目錄的文件增量更新到目標目錄,而目標目標殘留的其他文件不會做任何處理:

[root@pc205 ~]# rsync -a /opt/dir1/  /opt/dir2

[root@pc205 ~]# ls -l /opt/dir1/ /opt/dir2/

/opt/dir1/:

總用量 8

-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt

drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir

/opt/dir2/:

總用量 20

-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt

drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir //新同步來的子目錄

-rw-r--r--. 1 root root 721 4月 26 17:06 3.txt

drwxr-xr-x. 2 root root 4096 4月 26 16:51 4.dir

drwxr-xr-x. 3 root root 4096 4月 26 16:51 dir1

若要保持兩邊一致,即刪除「目標目錄下有而源目錄下沒有」的文件,可以新增–delete選項。修改前一條操作如下,可以看到兩個目錄的內容完全相同(多餘文件被刪除):

[root@pc205 ~]# rsync -a --delete /opt/dir1/  /opt/dir2

[root@pc205 ~]# ls -l /opt/dir1/ /opt/dir2/

/opt/dir1/:

總用量 8

-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt

drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir

/opt/dir2/:

總用量 8

-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt

drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir

[root@pc205 ~]#

4)驗證-v選項的作用

未新增-v選項時,rsync同步操作是靜默執行的,除非報錯否則無螢幕輸出:

[root@pc205 ~]# rsync -a --delete /opt/dir1/  /opt/dir2
而新增-v選項後,可以觀察rsync同步操作的過程資訊,方便了解細節:

[root@pc205 ~]# rsync -a -v --delete /opt/dir1/  /opt/dir2

sending incremental file list

sent 70 bytes received 13 bytes 166.00 bytes/sec

total size is 497 speedup is 5.99

5)驗證-n選項的作用

選項-n主要用來模擬同步過程,而並不會真正的執行,此選項通常與-v一起使用。比如說,可以先清空/opt/dir2/目錄,然後rsync結合-nv選項來了解一下指定的同步會執行哪些操作:

[root@pc205 ~]# rsync -a -nv --delete /opt/dir1/  /opt/dir2    

sending incremental file list

./1.txt //會同步1.txt檔案

2.dir/ //會同步2.dir子目錄

sent 79 bytes received 22 bytes 202.00 bytes/sec

total size is 497 speedup is 4.92 (dry run)

[root@pc205 ~]# ls -l /opt/dir1/ /opt/dir2/

/opt/dir1/:

總用量 8

-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt

drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir

/opt/dir2/: //實際上並未執行

總用量 0

把上述同步操作中的-n選項去掉,才會真正地執行同步操作:

[root@pc205 ~]# rsync -a -v --delete /opt/dir1/  /opt/dir2 

sending incremental file list

./1.txt

2.dir/

sent 616 bytes received 38 bytes 1308.00 bytes/sec

total size is 497 speedup is 0.76

[root@pc205 ~]# ls -l /opt/dir1/ /opt/dir2/

/opt/dir1/:

總用量 8

-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt

drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir

/opt/dir2/: //已經執行同步

總用量 8

-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt

drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir

步驟二:rsync+ssh同步

[root@pc205 ~]# rsync -az --delete [email protected]:/boot /fromssh

[email protected]'s password: //驗證對方口令

[root@pc205 ~]# ls /fromssh/ //確認同步結果

boot

2)將本地的/etc/selinux目錄上傳備份到遠端主機

[root@pc205 ~]# du -sh /etc/selinux/ //確認待同步的目錄

19m /etc/selinux/

[root@pc205 ~]# rsync -az /etc/selinux [email protected]:/opt/

[email protected]'s password: //驗證對方口令

然後切換到遠端主機192.168.4.5上確認同步結果:

[root@svr5 ~]# du -sh /opt/selinux/ //確認同步結果

19m /opt/selinux/

rsync遠端檔案同步

伺服器端 配置rsyncd.conf.位置 etc rsyncd.conf 密碼檔案 啟動rsync服務 rsync daemon 客戶端 密碼檔案裡只需要記錄密碼,不需要記錄使用者名稱.此密碼需要和伺服器端密碼檔案內密碼一致.chown user group 密碼檔案 chmod 0600 密碼檔...

Rsync遠端同步配置

rsync服務端配置 安裝rsync軟體包 yum y install rsync 生成rsyncd.conf配置檔案,具體引數可man rsyncd.conf cat etc rsyncd.conf 建立rsync使用者並啟動服務 useradd s sbin nologin m rsync rs...

使用rsync支援遠端同步

很多時候,我們需要將一些大的檔案進行跨網傳輸,乙個檔案動不動幾十g,當你的頻寬資源緊張的時候,傳輸這樣幾個檔案會花10小時,甚至幾天。nc傳輸檔案,通過以下方法,將加快檔案傳輸,希望對大家有所幫助。環境 server1 源位址 server2 目標位址 需求 將server1上的某個目錄檔案拷貝到s...