1. 程式人生 > >資料同步--rsync

資料同步--rsync

rsync介紹

Rsync 是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠端資料同步備份的優秀工具,並且可以不進行改變原有資料的屬性資訊,實現資料的備份遷移特性。Rsync軟體適用於unix/linux/windows等多種作業系統平臺

rsync命令作用

rsync中有/和沒有/的目錄,是不一樣的概念:

         例:rsync /data表示檢視這個目錄

                rsync /data/ 表示檢視這個目錄下的東西

rsync==cp 、 scp、 rm、 ls

cp命令

[[email protected] ~]# cp -rp /etc/hosts /tmp/

rsync 複製目錄的時候,比如複製的是oldboy目錄

rsync -r data /tmp :data目錄後面沒有/,表示目錄本身及目錄下內容進行復制到/tmp目錄下

rsync -r data/ /tmp :data目錄後面有/ 表示只複製目錄下的內容到/tmp目錄下

scp命令

[[email protected] ~]# scp -rp data10.0.0.31:/tmp/

----------------------------------------------------------

[[email protected] ~]# rsync -rp data10.0.0.31:/tmp

[email protected]'s password:

rm刪除命令

[[email protected] ~]# rm -f data/*

[[email protected] ~]# ll data

total 0

-----------------------------------

[[email protected] ~]# rsync -r --delete data/ /tmp/

[[email protected]

~]# ll /tmp/

total 0

ls命令

[[email protected] ~]# # 檢視檔案資訊命令ls

[[email protected] ~]# rsync /tmp/

drwxrwxrwt4096 2017/05/04 09:21:42 .

[[email protected] ~]# rsync /etc/

drwxr-xr-x4096 2017/05/04 09:07:46 .

-rw-------           0 2017/03/08 12:03:35 .pwd.lock

-rw-r--r--4439 2016/04/12 15:56:23 DIR_COLORS

-rw-r--r--5139 2016/04/12 15:56:23 DIR_COLORS.256color

-rw-r--r--4113 2016/04/12 15:56:23 DIR_COLORS.lightbgcolor

----------------------------------------------------------------------------

[[email protected] ~]# rsync 10.0.0.31:/tmp     ###檢視遠端主機上的目錄屬性資訊

[email protected]'s password:

drwxrwxrwt4096 2017/05/04 09:17:06 tmp

[[email protected] ~]# rsync 10.0.0.31:/tmp/    ###檢視遠端主機上的目錄中檔案的屬性資訊

 

[email protected]'s password:

drwxrwxrwt4096 2017/05/04 09:17:06 .

-rw-------           0 2017/05/02 11:29:38 yum.log

drwxrwxrwt4096 2017/05/03 21:06:50 .ICE-unix

drwxr-xr-x4096 2017/05/04 09:17:06 oldboy


rsync增量複製和全量複製特性說明

全量:所有資料,不管是否已經存在,都重新複製一遍

增量:所有資料,只複製進行變化的資料,已經存在未發生變化的資料不進行復制

rsync軟體特點


tar zcvf backup_1.tar.gz  /opt/data  -exclude=data

#說明:在打包/opt/data時就排除了data命名的目錄和檔案。

rsync工作應用場景

定時任務+rsync方式

全網資料備份方案(備份、驗證、通知)

備份伺服器單點伺服器單點問題(多個備份伺服器、同城災備、異地災備)

實時同步+rsync方式

使用者上傳的資料資訊,進行實時的備份

rsync軟體引數

 --delete            實現無差異資料同步

 --bwlimit=RATE    實現資料傳輸過程中的限速預設單位KB

命令引數

引數說明

-v, --verbose

詳細模式輸出,傳輸時的進度等資訊

-z, --compress

傳輸時進行壓縮以提高傳輸效率,--compress-level=NUM 可按級別壓縮

-a, --archive(重要)

歸檔模式,表示以遞迴方式傳輸檔案,並保持所有檔案屬性,等於-rtopgDl

-r, --recursive 歸類-a引數

對子目錄以遞迴模式,即目錄下的所有目錄都同樣傳輸。

-t, --times     歸類-a引數

保持檔案時間資訊

-o, --owner     歸類-a引數

保持檔案屬主資訊

-p, --perms     歸類-a引數

保持檔案許可權

-g,--group     歸類-a引數

保持檔案屬組資訊

-P,--progress

顯示同步的過程及傳輸時的進度等資訊(大寫P)

-D,--devices   歸類-a引數

保持裝置檔案資訊

-l,--links     歸類-a引數

保留軟連結(小寫字母L)

-e,--rsh=COMMAND

使用的通訊協議(remote shell),指定替代rsh的shell程式。例如:ssh

--exclude=PATTERN

指定排除不需要傳輸的檔案資訊(和tar引數一樣)

--exclude-from=file

檔名所在的目錄檔案,既可以實現排除多個檔案(和rar引數一樣)

--bwlimit=PATE

可以實現限速功能。單位KB

--delete

讓目標目錄SRC和源目錄資料DST一致,即無差異同步資料

儲存同步目錄及檔案屬性:


這裡的-avzP相當於-vzrtopgDlP(還多了Dl功能),生產環境常用的引數選項為-avzP或-vzrtopgP.如果是放入指令碼中,也可以吧-v和-P去掉。這裡的--progress可以用-P代替


 rsync軟體工作方式

本地複製方式

Local:rsync [OPTION...] SRC... [DEST]

---------------------------------------+

[[email protected] backup]# rsync /etc/hosts /tmp   ###使用方法和cp一樣

[[email protected] backup]# ll /tmp

total 12

-rw-r--r--1 root root  331 May  6 19:23 hosts


隧道傳輸方式

判斷檔案沒有變化,根據增量同步原理,不進行資料傳送

Access via remote shell:

Pull: rsync [OPTION...] [[email protected]]HOST:SRC... [DEST]

Push: rsync [OPTION...] SRC... [[email protected]]HOST:DEST

--------------------------------------------+

[[email protected] ~]# rsync -rp /etc/hosts  [email protected]:/tmp

[email protected]'s password:

[[email protected] ~]# rsync -rp -e "ssh -p 22" /etc/hosts  [email protected]:/tmp   ##-e指定協議及埠

[email protected]'s password:

---------------------------------------------------------+

# rsync -avzP-e 'ssh -p 22' /etc/[email protected]:/opt

[email protected]'s password:

sending incremental file list

 

sent 30 bytesreceived 12 bytes  12.00 bytes/sec

total size is 324speedup is 7.71

判斷檔案發生變化,根據增量同步原理,進行資料傳送

 

[[email protected] ~]# rsync -avzP  -e 'ssh -p 22' /etc/hosts   [email protected]:/opt

[email protected]'s password:

sending incremental file list

hosts

         326 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

 

sent 202 bytesreceived 37 bytes  68.29 bytes/sec

total size is 326speedup is 1.36

 

[[email protected] ~]# rsync -avz  -e 'ssh -p 22' /etc/hosts   [email protected]:/opt

[email protected]'s password:

sending incremental file list

hosts

 

sent 202 bytesreceived 37 bytes  20.78 bytes/sec

total size is 325speedup is 1.36


守護程序方式

Access via rsync daemon:

Pull: rsync [OPTION...] [[email protected]]HOST::SRC... [DEST]

rsync [OPTION...] rsync://[[email protected]]HOST[:PORT]/SRC... [DEST]

Push: rsync [OPTION...] SRC... [[email protected]]HOST::DEST

rsync [OPTION...] SRC... rsync://[[email protected]]HOST[:PORT]/DEST

--------------------------------------------------------------+

###需要進行配置才可以使用守護程序的方式配置方法在下面

[[email protected] ~]# rsync -avzP /etc/hosts [email protected]::backup

Password:

sending incremental file list

hosts

         323 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

 

sent 199 bytesreceived 27 bytes  41.09 bytes/sec

total size is 323speedup is 1.43

後續內容,下章見分曉