1. 程式人生 > >Rsync 同步工具

Rsync 同步工具

detail 鏈接文件 figure cgp stack profile see enc ridge

1.Rsync 簡介

Rsync(remote synchronize)rsync是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠程數據同步備份的優秀工具rsync軟件適用於unix/linux/windows等多種操作系統平臺,rsync和ssh帶的scp命令比較相似,但又優於scp命令的功能,scp每次都是全量拷貝,而rsync可以進行增量拷貝。當然,rsync還可以在本地主機的不同分區或目錄之間全量及增量的復制數據,這又類似cp命令,但同樣也優於cp命令,cp每次都是全量拷貝,而rsync可以增量拷貝.利用rsync還可以實現刪除文件和目錄功能,這又相當於rm命令。

2.rsync特性

支持拷貝 特殊文件如鏈接文件,設備等。
可以有排除指定文件或目錄同步的功能,相當於打包命令tar的排除功能。
可以做到保持原文件或目錄的權限、時間、軟硬鏈接、屬主、組等所有屬性均不改變。
可實現增量同步,即可同步發生變化的數據,因此數據傳輸效率很高。
可以使用rcp,rsh,ssh等方式來配合傳輸文件(rsync本身不對數據加密)。
可以通過socket傳輸文件和數據。
支持匿名的或認證(無需系統用戶)的進程模式傳輸,可實現方便安全的進行數據備份及鏡像。

3.rsync應用場景

兩臺服務器之間數據同步。

把所有客戶服務器數據同步到備份服務器,生產場景集群架構服務器備份方案。

rsync結合inotify的功能做實時的數據同步

4.rsync源碼安裝

技術分享圖片
1 https://download.samba.org/pub/rsync/src/
最新下載鏈接地址 技術分享圖片
 1 [root@zrlog ~]# cd /usr/src/
 2 [root@zrlog src]# wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz
3 [root@zrlog src]# ls 4 debug kernels rsync-3.1.2.tar.gz 5 [root@zrlog src]# tar -zxvf rsync-3.1.2.tar.gz 6 [root@zrlog src]# cd rsync-3.1.2/ 7 [root@zrlog rsync-3.1.2]# ./configure --prefix=/usr/local/rsync 8 [root@zrlog rsync-3.1.2]# make && make install 9 [root@zrlog ~]# vim /etc/profile.d/rsync.sh 10 [root@zrlog ~]# cat !$ 11 cat /etc/profile.d/rsync.sh 12 export PATH=/usr/local/rsync/bin:$PATH 13 [root@zrlog ~]# source !$ 14 source /etc/profile.d/rsync.sh 15 [root@zrlog ~]# which rsync 16 /usr/local/rsync/bin/rsync 17 [root@zrlog ~]# rsync --version 18 rsync version 3.1.2 protocol version 31 19 Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others. 20 Web site: http://rsync.samba.org/ 21 Capabilities: 22 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, 23 socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, 24 append, no ACLs, xattrs, iconv, symtimes, prealloc 25 26 rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you 27 are welcome to redistribute it under certain conditions. See the GNU 28 General Public Licence for details.
rsync安裝
5.語法介紹

Local:  rsync [OPTION...] SRC... [DEST]
Access via remote shell:
  Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
  Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

Access via rsync daemon:
  Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
        rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
  Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
        rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
rsync 常用差數介紹


-v    --verbose    詳細模式輸出,傳輸時的進度信息
-z    --compress    傳輸時進行壓縮以提高傳輸效率, --compress-level=NUM可按級別壓縮
-a    --archive    歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rtopgDl
-r    --recursive    對子目錄以遞歸模式,即目錄下的所有目錄都同樣傳輸,註意是小寫r
-t    --times    保持文件時間信息
-o    --owner    保持文件屬主信息
-p    --perms    保持文件權限
-g    --group    保持文件屬組信息
-P    --progress    顯示同步的過程及傳輸時的進度等信息
-D    --devices    保持設備文件信息
-l    --links    保持軟鏈接
-e    --rsh=COMMAND    使用的信道協議,指定替代rsh的shell程序,例如ssh
--exclude=PATTERN    指定排除不需要傳輸的文件模式
--exclude-from=FILE    從文件中讀取指定排除不需要傳輸的文件模式
以上參數為在生產環境中的常用參數,基本使用已足夠,相關的參數還有非常多,了解更多可以man rsync。最常用的參數avz相當於vzrtopgDl,生產參數-avz或者用-vzrtopg
技術分享圖片
 1 [root@zrlog ~]# ls
 2 anaconda-ks.cfg  nginx.conf  oneinstack-full.tar.gz  xtrabackup-1.6.7.tar.gz
 3 [root@zrlog ~]# rsync -avzP xtrabackup-1.6.7.tar.gz /backup/
 4 sending incremental file list
 5 xtrabackup-1.6.7.tar.gz
 6      12,345,435 100%   25.86MB/s    0:00:00 (xfr#1, to-chk=0/1)
 7 
 8 sent 12,224,253 bytes  received 35 bytes  8,149,525.33 bytes/sec
 9 total size is 12,345,435  speedup is 1.01
10 [root@zrlog ~]# ll /backup/
11 total 12060
12 -rw-r--r-- 1 root root 12345435 Dec 18  2012 xtrabackup-1.6.7.tar.gz
rsync Local案例 技術分享圖片
 1 [root@zrlog ~]# hostname -I
 2 10.2.2.125 
 3 [root@zrlog ~]# cd /backup/
 4 [root@zrlog backup]# ls
 5 xtrabackup-1.6.7.tar.gz
 6 [root@zrlog backup]# touch stu{1..100}
 7 [root@zrlog backup]# ls
 8 stu1    stu14  stu2   stu25  stu30  stu36  stu41  stu47  stu52  stu58  stu63  stu69  stu74  stu8   stu85  stu90  stu96
 9 stu10   stu15  stu20  stu26  stu31  stu37  stu42  stu48  stu53  stu59  stu64  stu7   stu75  stu80  stu86  stu91  stu97
10 stu100  stu16  stu21  stu27  stu32  stu38  stu43  stu49  stu54  stu6   stu65  stu70  stu76  stu81  stu87  stu92  stu98
11 stu11   stu17  stu22  stu28  stu33  stu39  stu44  stu5   stu55  stu60  stu66  stu71  stu77  stu82  stu88  stu93  stu99
12 stu12   stu18  stu23  stu29  stu34  stu4   stu45  stu50  stu56  stu61  stu67  stu72  stu78  stu83  stu89  stu94  xtrabackup-1.6.7.tar.gz
13 stu13   stu19  stu24  stu3   stu35  stu40  stu46  stu51  stu57  stu62  stu68  stu73  stu79  stu84  stu9   stu95
14 
15 [lcx@cgp ~]$ hostname -I
16 10.2.2.137 
17 [lcx@cgp ~]$ sudo mkdir /backup
18 [lcx@cgp ~]$ ll /backup/
19 total 0
20 
21 
22 [root@zrlog ~]# rsync /backup/ -avzP -e "ssh -p 22" root@10.2.2.137:/backup/
23 root@10.2.2.137s password: 
24 sending incremental file list
25 ./
26 stu1
27               0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=100/102)
28 ........
29 xtrabackup-1.6.7.tar.gz
30      12,345,435 100%   16.73MB/s    0:00:00 (xfr#101, to-chk=0/102)
31 
32 sent 12,228,591 bytes  received 1,941 bytes  627,206.77 bytes/sec
33 total size is 12,345,435  speedup is 1.01
34 
35 
36 [lcx@cgp ~]$ ls /backup/
37 stu1    stu14  stu2   stu25  stu30  stu36  stu41  stu47  stu52  stu58  stu63  stu69  stu74  stu8   stu85  stu90  stu96
38 stu10   stu15  stu20  stu26  stu31  stu37  stu42  stu48  stu53  stu59  stu64  stu7   stu75  stu80  stu86  stu91  stu97
39 stu100  stu16  stu21  stu27  stu32  stu38  stu43  stu49  stu54  stu6   stu65  stu70  stu76  stu81  stu87  stu92  stu98
40 stu11   stu17  stu22  stu28  stu33  stu39  stu44  stu5   stu55  stu60  stu66  stu71  stu77  stu82  stu88  stu93  stu99
41 stu12   stu18  stu23  stu29  stu34  stu4   stu45  stu50  stu56  stu61  stu67  stu72  stu78  stu83  stu89  stu94  xtrabackup-1.6.7.tar.gz
42 stu13   stu19  stu24  stu3   stu35  stu40  stu46  stu51  stu57  stu62  stu68  stu73  stu79  stu84  stu9   stu95
rsync Acess via remote shell PUSH案例 技術分享圖片
 1 [root@zrlog ~]# rm -rf /backup/*
 2 [root@zrlog ~]# ll /backup/
 3 total 0
 4 [root@zrlog ~]# rsync -avzP -e "ssh - p 22"[email protected]:/backup/ /backup
 5 [email protected]‘s password: 
 6 receiving incremental file list
 7 ./
 8 stu1
 9               0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=100/102)
10 .....
11 xtrabackup-1.6.7.tar.gz
12      12,345,435 100%   11.46MB/s    0:00:01 (xfr#101, to-chk=0/102)
13 
14 sent 1,933 bytes  received 12,227,643 bytes  1,630,610.13 bytes/sec
15 total size is 12,345,435  speedup is 1.01
16 [root@zrlog ~]# ls /backup/
17 stu1    stu14  stu2   stu25  stu30  stu36  stu41  stu47  stu52  stu58  stu63  stu69  stu74  stu8   stu85  stu90  stu96
18 stu10   stu15  stu20  stu26  stu31  stu37  stu42  stu48  stu53  stu59  stu64  stu7   stu75  stu80  stu86  stu91  stu97
19 stu100  stu16  stu21  stu27  stu32  stu38  stu43  stu49  stu54  stu6   stu65  stu70  stu76  stu81  stu87  stu92  stu98
20 stu11   stu17  stu22  stu28  stu33  stu39  stu44  stu5   stu55  stu60  stu66  stu71  stu77  stu82  stu88  stu93  stu99
21 stu12   stu18  stu23  stu29  stu34  stu4   stu45  stu50  stu56  stu61  stu67  stu72  stu78  stu83  stu89  stu94  xtrabackup-1.6.7.tar.gz
22 stu13   stu19  stu24  stu3   stu35  stu40  stu46  stu51  stu57  stu62  stu68  stu73  stu79  stu84  stu9   stu95
rsync Acess via remote shell PULL案例

Rsync 同步工具