Rsync是一款開源的,快速的,多功能的,可實現增量的本地貨遠程數據鏡像同步備份的工具。
Rsync特性
1.支持拷貝特殊文件,如連接、設備等
2.可以有排除指定文件或目錄同步的功能,相當於打包命令tar
3.可以做到保持原來文件或目錄的權限、時間、軟硬連接等所有屬性不變
4.可實現增量同步,即只同步發生變化的數據,因此數據傳輸效率很高
5.可以使用rcp、rsh、ssh等方式來配合傳輸文件,也可以通過socket連接
6.支持匿名或認證的進程模式傳輸,方便進行數據備份及鏡像
Rsync命令及參數
rsync [OPTION...] SRC... [DEST]
常用參數選項說明
-v (verbose) 詳細模式輸出,傳輸時的進度信息
-z (compress) 傳輸時進行壓縮,--compress-level=NUM可按照級別壓縮
-r (recursive) 對子目錄進行遞歸,目錄下的所有目錄都同樣傳輸
-t (times) 保持文件時間信息
-o (owner) 保持文件屬組信息
-p (perms) 保持文件權限
-g (group) 保持文件屬組信息
-P (progress) 顯示同步的過程及傳輸是的進度
-a (archive) 歸檔模式,表示以遞歸方式傳輸文件,並保持文件屬性,等於-rtopgDl
-e (rsh=COMMEND) 使用的信道協議,指定替代rsh的shell程序,如ssh
-D (devices) 保持設備文件信息
-l (links) 保留軟連接
--exclude=PATTERN 指定排除不需要傳輸的文件模式
Rsync工作方式
1.本地數據傳輸(local-only mode)
rsync [OPTION...] SRC... [DEST]
1.rync是同步命令
2.[OPTION...]是同步時的參數選項
3.SRC是源,即待拷貝的文件、目錄或分區
4.[DEST]是目的文件、目錄或分區
實例
[test@C64-5-S ~]$ rsync -avz /etc /tmp
sending incremental file list
etc/
etc/.pwd.lock
etc/DIR_COLORS
etc/DIR_COLORS.256color
2.通過rcp、ssh等通道傳輸
拉取:rsync [OPTION...] [USER@]HOST:SRC... [DEST]
推送:rsync [OPTION...] SRC... [USER@]HOST:DEST
1.rync是同步命令
2.[OPTION...]是同步時的參數選項
3.[USER@]HOST...爲Rsync同步的遠程的連接用戶和主機地址
4.SRC是源,即待拷貝的文件、目錄或分區,和HOST之間用一個冒號連接
5.[DEST]是目的文件、目錄或分區
實例
由於之前已經做過服務器間的免祕鑰認證,所以這裏直接執行命令就可以了
[syner@C64-5-S ~]$ rsync -avzP -e 'ssh -p 52113' [email protected]:/home/syner/temp ~
receiving incremental file list
temp/
temp/tmp.txt
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)
sent 34 bytes received 114 bytes 296.00 bytes/sec
total size is 0 speedup is 0.00
[syner@C64-5-S ~]$ rsync -avzP ~/temp/ -e 'ssh -p 52113' [email protected]:/home/syner/temp
sending incremental file list
sent 57 bytes received 12 bytes 138.00 bytes/sec
total size is 0 speedup is 0.00
3.以守護進程方式傳輸數據
拉取:1. rsync [OPTION...] [USER@]HOST::SRC... [DEST]
2. rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
推送:1. rsync [OPTION...] SRC... [USER@]HOST::DEST
2. rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
做之前要確保服務端S與客戶端B、C之前雙向的ssh key免祕鑰已經配置好了
首先檢查雙向ssh免祕鑰是否已經配置好
[syner@C64-5-S ~]$ ssh -p 52113 2.2.2.6 /sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:79:36:89
inet addr:2.2.2.6 Bcast:2.2.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe79:3689/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:60154 errors:0 dropped:0 overruns:0 frame:0
TX packets:30705 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:14749577 (14.0 MiB) TX bytes:26731727 (25.4 MiB)
[syner@C64-5-S ~]$ ssh -p 52113 2.2.2.7 /sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:BA:45:99
inet addr:2.2.2.7 Bcast:2.2.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feba:4599/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:52654 errors:0 dropped:0 overruns:0 frame:0
TX packets:7334 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:15527023 (14.8 MiB) TX bytes:946738 (924.5 KiB)
[syner@C64-6-B ~]$ ssh -p 52113 2.2.2.5 /sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:CA:07:AA
inet addr:2.2.2.5 Bcast:2.2.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feca:7aa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:81709 errors:0 dropped:0 overruns:0 frame:0
TX packets:39786 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:30099514 (28.7 MiB) TX bytes:27115850 (25.8 MiB)
[syner@C64-7-C ~]$ ssh -p 52113 2.2.2.5 /sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:CA:07:AA
inet addr:2.2.2.5 Bcast:2.2.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feca:7aa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:81736 errors:0 dropped:0 overruns:0 frame:0
TX packets:39805 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:30103541 (28.7 MiB) TX bytes:27120153 (25.8 MiB)
配置服務端
(1)編寫配置文件
[root@C64-5-S ~]# vi /etc/rsyncd.conf
#rsync_config
uid=root
gid=root
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[syner]
path = /home/syner
ignore = errors
read only = false
list = false
hosts allow = 2.2.2.6/32,2.2.2.7/32
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
exclude=/home
~
~
參數說明
uid = root | rsync使用的用戶,缺省uid爲-2,通常爲nobody |
gid = root |
rsync使用的組(用戶所在的組),缺省gid爲-2,通常爲nobody |
use chroot = no |
如果爲true,daemon會在給客戶端傳輸文件前“chroot to the path”,這是rsync安全的一個配置, 因爲我們大多數都是在內網使用rsync,所以不配置也可以 |
max connection = 200 | 設置最大連接數,默認爲0,意思是無限制,負值爲關閉這個模塊 |
timeout = 400 | 默認爲0,永不超時,建議改爲300-600(5-10分鐘) |
pid file = /var/run/rsyncd.pid |
rsync daemon啓動後將其進程PID寫入此文件,如果這個文件已經存在,rsync進程不會覆蓋該 文件,而是會終止 |
lock file = /var/run/rsyncd.lock | 指定lock文件來支持“max connection”參數,是得總連接數不會超過限制 |
log file = /var/log/rsyncd.log | 不設置或設置錯誤,rsync會使用syslog輸出相關日誌信息 |
ignore errors | 忽略IO錯誤 |
read only = false | 指定客戶端是否可以上傳文件,默認對所有模塊都爲true |
list = false | 是否允許客戶端查看可用模塊列表,默認爲true |
hosts allow = 2.2.2.6/32 | 指定可以連接的客戶端主機名或IP地址或地址段,默認無此參數,即都可以連接 |
host deny = 0.0.0.0/32 | 指定不可以連接的客戶端的主機名或IP地址或地址段,默認無此參數,即都可以連接 |
auth user = rsync_backup |
指定以空格或逗號分隔的用戶可以使用哪些模塊,用戶不需要再本地系統中存在,默認 所有用戶無密碼訪問(anonymous rsync) |
secrets file = /etc/rsync.password | 指定用戶名和密碼存放的文件,格式 用戶名:密碼 密碼不超過8位 |
[syner] | 模塊名稱,需要用中括號括起來,起名沒有特殊要求,但是最好是有意義的名稱,方便維護 |
path = /home/syner | 在這個模塊中,daemon使用的文件系統或目錄,目錄的權限要和配置文件中的權限一致 |
#exclude = /home | 排除指定文件或目錄,相對路徑 |
(2)配置用於rsync同步的賬號、密碼及賬號文件權限
[root@C64-5-S ~]# echo "rsync_backup:syner" >/etc/rsync.password
[root@C64-5-S ~]# chmod 600 /etc/rsync.password
[root@C64-5-S ~]# cat /etc/rsync.password
rsync_backup:syner
[root@C64-5-S ~]# ll /etc/rsync.password
-rw------- 1 root root 19 Sep 24 04:24 /etc/rsync.password
(3)以守護進程方式啓動rsync服務
[root@C64-5-S ~]# rsync --daemon
[root@C64-5-S ~]# ps -ef | grep rsync|grep -v grep
root 11589 1 0 04:29 ? 00:00:00 rsync --daemon
[root@C64-5-S ~]# netstat -lnt| grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN
tcp 0 0 :::873 :::* LISTEN
[root@C64-5-S ~]# lsof -i tcp:873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 11589 root 4u IPv4 61287 0t0 TCP *:rsync (LISTEN)
rsync 11589 root 5u IPv6 61288 0t0 TCP *:rsync (LISTEN)
可選參數
--daemon | 以守護進程方式啓動rsync服務 |
--address | 綁定指定IP地址提供服務 |
--config=FILE | 更改配置文件路徑,而不是默認的/etc/rsyncd.conf |
--port=PORT | 更改其他端口提供服務,而不是缺省的873端口 |
(4)設置rsync服務開機自啓動
[root@C64-5-S ~]# echo "/usr/bin/rsync --daemon">>/etc/rc.local
[root@C64-5-S ~]# tail -3 /etc/rc.local
/etc/init.d/rpcbind start
/etc/init.d/nfs start
/usr/bin/rsync --daemon
關閉rsync服務
[root@C64-5-S ~]# pkill rsync
啓動rsync服務
[root@C64-5-S ~]# rsync --daemon
也可以通過編寫腳本當成服務進行控制
[root@C64-5-S ~]# vi /etc/init.d/rsyncd
#!/bin/sh
#author Paul
#
#rsync start/stop rsync service
#
#chkconfig: 35 13 91
#description:this is rsync service management shell script
#process name:rsyncd
#source function library
. /etc/rc.d/init.d/functions
start(){
rsync --daemon
if [ $? -eq 0 -a `ps -ef | grep -v grep|grep rsync|wc -l` -gt 0 ];then
action "Starting Rsync:" /bin/true
else
action "Starting Rsync:" /bin/false
fi
}
stop(){
pkill rsync
if [ `ps -ef | grep -v grep | grep "rsync --daemon" | wc -l` -lt 1 ];then
action "Stopping Rsync:`ps -ef | grep -v grep | grep rsync | wc -l` "/bin/true
else
action "Stopping Rsync:`ps -ef | grep -v grep |grep "rsync --daemon"|wc -l`" /bin/false
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart|reload)
$0 stop
$0 start
;;
*)
編譯
[root@C64-5-S ~]# yum -y install dos2unix
Loaded plugins: fastestmirror, security
Setting up Install Process
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
extras/primary_db | 26 kB 00:00
updates | 3.4 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package dos2unix.x86_64 0:3.1-37.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================
Package Arch Version Repository Size
==============================================================================================
Installing:
dos2unix x86_64 3.1-37.el6 base 16 k
Transaction Summary
==============================================================================================
Install 1 Package(s)
Total download size: 16 k
Installed size: 18 k
Downloading Packages:
dos2unix-3.1-37.el6.x86_64.rpm | 16 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : dos2unix-3.1-37.el6.x86_64 1/1
Verifying : dos2unix-3.1-37.el6.x86_64 1/1
Installed:
dos2unix.x86_64 0:3.1-37.el6
Complete!
[root@C64-5-S ~]# dos2unix /etc/init.d/rsyncd
dos2unix: converting file /etc/init.d/rsyncd to UNIX format ...
授權
[root@C64-5-S ~]# chmod 700 /etc/init.d/rsyncd
加入自啓動服務
[root@C64-5-S ~]# chkconfig rsyncd on
[root@C64-5-S ~]# chkconfig --list rsyncd
rsyncd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
這樣就可以以服務的形式進行啓停
[root@C64-5-S init.d]# /etc/init.d/rsyncd stop
Terminated
[root@C64-5-S init.d]# /etc/init.d/rsyncd start
Starting Rsync: [ OK ]
配置客戶端
配置rsync的密碼(這裏不需要賬號了),並修改權限
[root@C64-6-B ~]# echo "syner" > /etc/rsync.password
[root@C64-6-B ~]# chmod 600 /etc/rsync.password
測試
[syner@C64-6-B ~]$ tree
.
└── test
└── test.txt
1 directory, 1 file
[syner@C64-6-B ~]$ rsync -avzP /home/syner/test [email protected]::syner --password-file=/etc/rsync.password
sending incremental file list
test/
test/test.txt
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)
sent 106 bytes received 31 bytes 274.00 bytes/sec
total size is 0 speedup is 0.00
在服務端檢查是否推送到了
[syner@C64-5-S ~]$ tree
.
└── test
└── test.txt
1 directory, 1 file
另一種推送語法
rsync -avzP /home/syner/test rsync://[email protected]/syner --password-file=/etc/rsync.password
抓取數據
[syner@C64-6-B ~]$ rsync -avzP rsync://[email protected]:/syner /tmp --password-file=/etc/rsync.password
[syner@C64-6-B tmp]$ rsync -avzP [email protected]::syner /tmp --password-file=/etc/rsync.password
P.S.在推送時可以加--delete參數,意思是如果我推送目錄中沒有的,遠端目錄也應該沒有(即刪除多餘項目),這樣做可以保證rsync兩端目錄保持高度一致。