Rsync
Remote Synchronize ,是Linux默認安裝的一個遠程數據同步工具,可通過LAN/WAN快速同步多台主機間的文件。
Rsync可以通過rsh或ssh使用,也能以daemon模式去運行。
在以daemon方式運行時Rsync server會打開一個873 端口,等待客戶端去連接。連接時,Rsync server會檢查口令是否相符,若通過口令查核,則可以開始進行文件傳輸。第一次連通完成時,會把整份文件傳輸一次,以后則就只需進行增量備份。
rsync的特性:
1)支持拷貝特殊文件如鏈接文件,設備等
2)可以有排除指定文件或者目錄同步的功能,相當於打包命令tar的排除功能。
3)可以做到保持原文件或者目錄的權限,時間,軟硬鏈接,屬組,主等所有屬性均不改變
4)可以實現增量備份,既只同步發生變化的數據
5)可以使用rcp,rsh,ssh等方式來配合傳輸文件
6)可以通過socket傳輸文件和數據
7)支持匿名的認證模式傳輸
安裝:
Linux默認安裝了的,沒有的話就使用yum命令安裝。
# rpm -qa | grep rsync
rsync-3.0.6-12.el6.x86_64
# rsync --version
rsync version 3.0.6 protocol version 30
Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
append, ACLs, xattrs, iconv, symtimes
rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.
配置:
配置文件是rsyncd.conf,路徑在/etc/目錄下,沒有的話,vim建一個。
啟動rsync會有問題,要去掉下文中的#備注。
# vim /etc/rsyncd.conf
# /etc/rsyncd:configuration file for rsync daemon mode.
# see rsyncd.conf man page for more options.
# configuration example:
port = 873 #監聽端口,默認是873
address = 192.168.10.0/24 #服務器監聽的地址,可省略
uid = root #守護進程所屬的uid,默認是nobody,為了沒有文件或目錄的權限問題,設置為root
gid = root #守護進程的gid
fake super = yes
use chroot = no #chroot,改變程序執行時所參考的根目錄位置,在傳輸文件之前,
#服務器守護程序在將chroot 到文件系統中的目錄中
max connections = 3000 #客戶端最大連接數
log file = /var/log/rsyncd.log #指定rsync守護進程的日志文件,而不是將日志發給syslog
pid file = /var/run/rsyncd.pid #指定進程的pid文件
lock file = /var/run/rsyncd.lock #指定進程的鎖文件存放路徑
log format = %t %a %m %f %b #日志格式
incoming chmod = Du=rwx,Dog=rx,Fu=rw,Fgo=r #客戶端在服務器上的訪問權限
syslog facility = local3 #指定rsync發送日志消息給syslog時的消息級別
timeout = 1200 #超時時間
list = no #是否可查看服務器所提供的同步目錄
&include /etc/rsyncd.d #更多的rsync配置文件
#模塊定義,主要是定義服務器哪個目錄要被同步
#【backup】 #要同步的目錄
#comment = this is module for backup #注釋
#path = /backup #目錄路徑
#ignore errors #忽略I/O錯誤
#read only = no #默認為ture,不讓客戶端上傳文件到服務器上.
#list = no
#auth users = zhu #虛擬用戶
#secrets file = /etc/rsyncd.d/pass.server #虛擬用戶密碼存放地址
# mkdir /etc/rsyncd.d/ # vim /etc/rsysd.d/pass.server zhu:111 test:111
# chmod 600 /etc/rsyncd.d/pass.server
啟動:
# /usr/local/bin/rsync --daemon --config=/etc/rsyncd.conf
查看服務進程和端口:
# ps -ef | grep rsync | grep -v grep root 12330 1 0 01:21 ? 00:00:00 /usr/local/bin/rsync --daemon --config=/etc/rsyncd.conf # netstat -luntup | grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 12330/rsync
加入開機自啟動:
# echo "/usr/local/bin/rsync --daemon --profix=/etc/rsyncd.conf" >> /etc/rc.local # # tail -1 /etc/rc.local /usr/local/bin/rsync --daemon --profix=/etc/rsyncd.conf #
設置xinetd服務
extended internet daemon,xinetd是新一代的網絡守護進程服務程序,又叫超級Internet服務器。經常用來管理多種輕量級Internet服務。
[root@rsync ~]# cat /etc/xinetd.d/rsync service rsync { disable = no socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = –daemon log_on_failure += USERID }
[root@rsync ~]# service xinetd restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ] [root@rsync ~]# [root@rsync ~]# ps -ef | grep xinetd | grep -v grep root 11902 1 0 00:57 ? 00:00:00 xinetd -stayalive -pidfile /var/run/xinetd.pid [root@rsync ~]#
客戶端配置:
看能不能鏈接到服務端的873端口:
[root@kickstart ~]# telnet 192.168.10.17 873 Trying 192.168.10.17... Connected to 192.168.10.17. Escape character is '^]'. @RSYNCD: 31.0 @ERROR: protocol startup error Connection closed by foreign host.
這樣說明服務端的rsync服務開啟,端口開放,正常可連接。
創建鏈接服務端rsync的密碼文件,並修改權限:
[root@kickstart ~]# cat /etc/rsync.passwd 111 [root@kickstart ~]# ll /etc/rsync.passwd -rw-------. 1 root root 4 Sep 4 01:59 /etc/rsync.passwd [root@kickstart ~]#
同步嘗試:
[root@kickstart ~]# rsync -avz test@192.168.10.17::backup /root/test/ --password-file=/etc/rsync.passwd receiving incremental file list 1.txt 2.txt 3.txt sent 80 bytes received 205 bytes 570.00 bytes/sec total size is 11 speedup is 0.04 [root@kickstart ~]#
[root@kickstart ~]# ll test/
total 4
-rw-r--r--. 1 root root 0 Sep 4 00:46 1.txt
-rw-r--r--. 1 root root 0 Sep 4 00:46 2.txt
-rw-r--r--. 1 root root 11 Sep 4 02:02 3.txt
Rsync常用選項
可以用 rsync --help查看詳細命令選項說明。
選項 | 說明 |
---|---|
-a, ––archive | 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等價於 -rlptgoD (注意不包括 -H) |
-r, ––recursive | 對子目錄以遞歸模式處理 |
-l, ––links | 保持符號鏈接文件 |
-H, ––hard-links | 保持硬鏈接文件 |
-p, ––perms | 保持文件權限 |
-t, ––times | 保持文件時間信息 |
-g, ––group | 保持文件屬組信息 |
-o, ––owner | 保持文件屬主信息 (super-user only) |
-D | 保持設備文件和特殊文件 (super-user only) |
-z, ––compress | 在傳輸文件時進行壓縮處理 |
––exclude=PATTERN | 指定排除一個不需要傳輸的文件匹配模式 |
––exclude-from=FILE | 從 FILE 中讀取排除規則 |
––include=PATTERN | 指定需要傳輸的文件匹配模式 |
––include-from=FILE | 從 FILE 中讀取包含規則 |
––copy-unsafe-links | 拷貝指向SRC路徑目錄樹以外的鏈接文件 |
––safe-links | 忽略指向SRC路徑目錄樹以外的鏈接文件(默認) |
––existing | 僅僅更新那些已經存在於接收端的文件,而不備份那些新創建的文件 |
––ignore-existing | 忽略那些已經存在於接收端的文件,僅備份那些新創建的文件 |
-b, ––backup | 當有變化時,對目標目錄中的舊版文件進行備份 |
––backup-dir=DIR | 與 -b 結合使用,將備份的文件存到 DIR 目錄中 |
––link-dest=DIR | 當文件未改變時基於 DIR 創建硬鏈接文件 |
––delete | 刪除那些接收端還有而發送端已經不存在的文件 |
––delete-before | 接收者在傳輸之前進行刪除操作 (默認) |
––delete-during | 接收者在傳輸過程中進行刪除操作 |
––delete-after | 接收者在傳輸之后進行刪除操作 |
––delete-excluded | 在接收方同時刪除被排除的文件 |
-e, ––rsh=COMMAND | 指定替代 rsh 的 shell 程序 |
––ignore-errors | 即使出現 I/O 錯誤也進行刪除 |
––partial | 保留那些因故沒有完全傳輸的文件,以是加快隨后的再次傳輸 |
––progress | 在傳輸時顯示傳輸過程 |
-P | 等價於 ––partial ––progress |
––delay-updates | 將正在更新的文件先保存到一個臨時目錄(默認為 “.~tmp~”),待傳輸完畢再更新目標文件 |
-v, ––verbose | 詳細輸出模式 |
-q, ––quiet | 精簡輸出模式 |
-h, ––human-readable | 輸出文件大小使用易讀的單位(如,K,M等) |
-n, ––dry-run | 顯示哪些文件將被傳輸 |
––list-only | 僅僅列出文件而不進行復制 |
––rsyncpath=PROGRAM | 指定遠程服務器上的 rsync 命令所在路徑 |
––password-file=FILE | 從 FILE 中讀取口令,以避免在終端上輸入口令,通常在 cron 中連接 rsync 服務器時使用 |
-4, ––ipv4 | 使用 IPv4 |
-6, ––ipv6 | 使用 IPv6 |
––version | 打印版本信息 |
––help | 顯示幫助信息 |
-
若使用普通用戶身份運行 rsync 命令,同步后的文件的屬主將改變為這個普通用戶身份。
-
若使用超級用戶身份運行 rsync 命令,同步后的文件的屬主將保持原來的用戶身份。
-a 包含-rtplgoD -r 同步目錄時要加上,類似cp時的-r選項 -v 同步時顯示一些信息,讓我們知道同步的過程 -l 保留軟連接 -L 加上該選項后,同步軟鏈接時會把源文件給同步 -p 保持文件的權限屬性 -o 保持文件的屬主 -g 保持文件的屬組 -D 保持設備文件信息 -t 保持文件的時間屬性 --delete 刪除DEST中SRC沒有的文件 --exclude 過濾指定文件,如--exclude “logs”會把文件名包含logs的文件或者目錄過濾掉,不同步 -P 顯示同步過程,比如速率,比-v更加詳細 -u 加上該選項后,如果DEST中的文件比SRC新,則不同步 -z 傳輸時壓縮