一、什麼是rsync
rsync,remote synchronize顧名思意就知道它是一款實現遠程同步功能的軟件,它在同步文件的同時,可以保持原來文件的權限、時間、軟硬鏈接等附加信息。 rsync是用 “rsync 算法”提供了一個客戶機和遠程文件服務器的文件同步的快速方法,而且可以通過ssh方式來傳輸文件,這樣其保密性也非常好,另外它還是免費的軟件。
1.1rsync 包括如下的一些特性:
能更新整個目錄和樹和文件系統;
有選擇性的保持符號鏈鏈、硬鏈接、文件屬於、權限、設備以及時間等;
對於安裝來說,無任何特殊權限要求;
對於多個文件來說,內部流水線減少文件等待的延時;
能用rsh、ssh 或直接端口做爲傳輸入端口;
支持匿名rsync 同步文件,是理想的鏡像工具;
二 工作場景
2.1 兩臺服務器之間數據拷貝
2.2 定時備份
三 三種模式
3.1 第1種方式
Local: rsync [OPTION...] SRC... [DEST]
rsync -avz/etc/hosts /tmp/ 這種情況相當於cp
[root@NFS-SER~]# rsync -avz --delete /dev/null/ /tmp/ 我有點你必須有,我沒有的你必須沒有,相當於rm 命令
sending incremental file list
null
sent 34 bytes received 15 bytes 98.00 bytes/sec
total size is 0 speedup is 0.00
[[root@NFS-SER ~]# ll /tmp/
total 0
3.2 第2種方式
Access via remote shell:
Pull:rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push:rsync [OPTION...] SRC... [USER@]HOST:DEST
[root@LNMP ~]# ll /tmp/
total 0
-rw-------. 1 root root 0 Feb 21 17:28yum.log
[root@LNMP~]# rsync -avzP -e 'ssh -p22' /tmp/ [email protected]:/tmp 把本地的推送到遠端/tmp目錄下,注意:如果/tmp後面沒有/ 那麼推送到只是tmp目錄下面的內容,如果加上/變成/tmp/ 那麼這個時候的推送就包含整個tmp目錄了
The authenticity of host '10.0.0.100(10.0.0.100)' can't be established.
RSA key fingerprint isdb:ce:a8:5a:d6:2b:23:5f:15:70:68:ce:84:38:ef:ef.
Are you sureyou want to continue connecting (yes/no)? y 這裏是確認
Please type'yes' or 'no': yes 這裏要求輸入遠端賬號的密碼
Warning: Permanently added '10.0.0.100'(RSA) to the list of known hosts.
reverse mapping checking getaddrinfo forbogon [10.0.0.100] failed - POSSIBLE BREAK-IN ATTEMPT!
[email protected]'s password:
sending incremental file list
./
yum.log
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=1/3)
.ICE-unix/
sent 114 bytes received 38 bytes 16.00 bytes/sec
total size is 0 speedup is 0.00
把遠端的數據拉回本地
[root@LNMP ~]#rsync -avzP -e 'ssh -p 22'[email protected]:/tmp/ /tmp/ 從遠端拉回來
reverse mapping checking getaddrinfo forbogon [10.0.0.100] failed - POSSIBLE BREAK-IN ATTEMPT!
[email protected]'s password:
receiving incremental file list
./
pull.txt
5 100% 4.88kB/s 0:00:00 (xfer#1, to-check=2/4)
sent 34 bytes received 146 bytes 51.43 bytes/sec
total size is 5 speedup is 0.03
[root@LNMP ~]# ll /tmp/
total 4
-rw-r--r--. 1 root root 5 Mar 3 03:58 pull.txt
-rw-------. 1 root root 0 Feb 21 17:28yum.log
[root@LNMP ~]# cat /pull.txt
cat: /pull.txt: No such file ordirectory
[root@LNMP ~]# cat /tmp/pull.txt
Pull
3.3 第3 種方式
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
四 客戶端常用參數
-v, --verbose 詳細模式輸出
-q, --quiet 精簡輸出模式
-c, --checksum 打開校驗開關,強制對文件傳輸進行校驗
-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rlptgoD
-r, --recursive 對子目錄以遞歸模式處理
-R, --relative 使用相對路徑信息
rsync foo/bar/foo.c remote:/tmp/
Rsync 參數在/tmp目錄下創建foo.c文件,而如果使用-R參數:
rsync -R foo/bar/foo.c remote:/tmp/
Rsync 參數會創建文件/tmp/foo/bar/foo.c,也就是會保持完全路徑信息。
-b, --backup 創建備份,也就是對於目的已經存在有同樣的文件名時,將老的文件重新命名爲~filename。可以使用--suffix選項來指定不同的備份文件前綴。
--backup-dir 將備份文件(如~filename)存放在在目錄下。
-suffix=SUFFIX 定義備份文件前綴
-u, --update 僅僅進行更新,也就是跳過所有已經存在於DST,並且文件時間晚於要備份的文件。(不覆蓋更新的文件)
-l, --links 保留軟鏈結
-L, --copy-links 想對待常規文件一樣處理軟鏈結
--copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹以外的鏈結
--safe-links 忽略指向SRC路徑目錄樹以外的鏈結
-H, --hard-links 保留硬鏈結
-p,--perms 保持文件權限
-o, --owner 保持文件屬主信息
-g, --group 保持文件屬組信息
-D, --devices 保持設備文件信息
-t,--times 保持文件時間信息
-S, --sparse 對稀疏文件進行特殊處理以節省DST的空間
-n, --dry-run現實哪些文件將被傳輸
-W, --whole-file 拷貝文件,不進行增量檢測
-x, --one-file-system 不要跨越文件系統邊界
-B, --block-size=SIZE 檢驗算法使用的塊尺寸,默認是700字節
-e, --rsh=COMMAND 指定替代rsh的shell程序
--rsync-path=PATH 指定遠程服務器上的rsync命令所在路徑信息
-C, --cvs-exclude 使用和CVS一樣的方法自動忽略文件,用來排除那些不希望傳輸的文件
--existing 僅僅更新那些已經存在於DST的文件,而不備份那些新創建的文件
--delete刪除那些DST中SRC沒有的文件
--delete-excluded 同樣刪除接收端那些被該選項指定排除的文件
--delete-after 傳輸結束以後再刪除
--ignore-errors 及時出現IO錯誤也進行刪除
--max-delete=NUM 最多刪除NUM個文件
--partial 保留那些因故沒有完全傳輸的文件,以是加快隨後的再次傳輸
--force 強制刪除目錄,即使不爲空
--numeric-ids 不將數字的用戶和組ID匹配爲用戶名和組名
--timeout=TIME IP超時時間,單位爲秒
-I, --ignore-times 不跳過那些有同樣的時間和長度的文件
--size-only 當決定是否要備份文件時,僅僅察看文件大小而不考慮文件時間
--modify-window=NUM 決定文件是否時間相同時使用的時間戳窗口,默認爲0
-T --temp-dir=DIR 在DIR中創建臨時文件
--compare-dest=DIR 同樣比較DIR中的文件來決定是否需要備份
-P 等同於--partial
--progress 顯示備份過程
-z, --compress 對備份的文件在傳輸時進行壓縮處理
--exclude=PATTERN指定排除不需要傳輸的文件模式
--include=PATTERN 指定不排除而需要傳輸的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
--version 打印版本信息
--address 綁定到特定的地址
--config=FILE 指定其他的配置文件,不使用默認的rsyncd.conf文件
--port=PORT 指定其他的rsync服務端口
--blocking-io 對遠程shell使用阻塞IO
-stats 給出某些文件的傳輸狀態
--progress 在傳輸時現實傳輸過程
--log-format=FORMAT 指定日誌文件格式
--password-file=FILE 從FILE中得到密碼
--bwlimit=KBPS 限制I/O帶寬,KBytes per second
-h, --help 顯示幫助信息
五 rsync配置
5.1 服務器配置
5.1.1建立配置文件
在/etc/文件目錄下創建一個rsyncd.conf的配置文件,然後寫入配置信息:
#Rsyncserver
#Created by kirk
#rsync.conf start#
uid = rsync
gid = rsync
user chroot = no
maxconnections = 200 併發連接數
timeout = 600 超時數
pid file =/var/run/rsyncd.pid 進程號放在這個文件
lock file = /var/run/rsync.lock
log file =/var/log/rsyncd.log 日誌文件,出問題就來這裏看。
igore erros
read only =false 可讀寫
list = false
hosts allow = 10.0.0.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#######################################
[backup]
comment = www by kirk
path = /backup
5.1.2啓動服務:
[root@BACKUP ~]# rsync --daemon
[root@BACKUP ~]# ps -ef|grep rsync|grep-v grep
root 1794 1 0 05:11 ? 00:00:00 rsync --daemon
[root@BACKUP ~]# netstat -lntup|greprsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1794/rsync
tcp 0 0 :::873 :::* LISTEN 1794/rsync
[root@BACKUP ~]# lsof -i:873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 1794 root 3u IPv4 15101 0t0 TCP *:rsync (LISTEN)
rsync 1794 root 5u IPv6 15102 0t0 TCP *:rsync (LISTEN)
5.1.3服務器端配置:
[root@BACKUP~]# useradd rsync -s /sbin/nologin -M 建立虛擬賬號
[root@BACKUP~]# mkdir /backup 建立備份目錄
[root@BACKUP~]# chown -R rsync /backup/ 修改備份目錄權限,客戶端可以推送數據
[root@BACKUP ~]#echo "rsync_backup:oldboy" >>/etc/rsync.password 建立密碼文件[K1]
[root@BACKUP ~]# cat /etc/rsync.password
rsync_backup:oldboy 給用戶auth users = rsync_backup設定的密碼是oldboy
5.1.4權限修改:
[root@BACKUP~]# chmod 600 /etc/rsync.password 其他人不可讀寫,安全
[root@BACKUP ~]# ll /etc/rsync.password
-rw-------. 1 root root 20 Mar 3 05:21 /etc/rsync.password
5.1.5圖解:
5.1.6加入開機自啓動:
[root@BACKUP ~]# echo "rsync--daemon" >>/etc/rc.local
[root@BACKUP ~]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after*all the other init scripts.
# You can put your own initializationstuff in here if you don't
# want to do the full Sys V style initstuff.
touch /var/lock/subsys/local
>/etc/udev/rules.d/70-persistent-net.rules
rsync --daemon
rsync --daemon
[root@BACKUP ~]#
5.2 客戶端配置
5.2.1 創建配置密碼文件
[root@NFS-SER ~]# echo"oldboy" >/etc/rsync.password
[root@NFS-SER ~]# chmod 600/etc/rsync.password
5.2.1 測試推送
[root@NFS-SER ~]# rsync -avz /tmp/[email protected]::backup --password-file=/etc/rsync.password
sendingincremental file list
./
pull.txt
yum.log
.ICE-unix/
sent 176 bytes received 53 bytes 458.00 bytes/sec
total size is 5 speedup is 0.02
[root@NFS-SER ~]# rsync-aPz /tmp/ [email protected]::backup--password-file=/etc/rsync.password
sending incremental file list
sent 89 bytes received 9 bytes 196.00 bytes/sec
total size is 5 speedup is 0.05
[root@NFS-SER ~]#
六 安全優化
6.1 綁定IP或者端口
rsync --daemon --address=10.0.0.100
七 排錯
7.1 服務器端排除
1、查看rsync服務配置文件路徑是否正確,正常默認的路徑是/etc/rsyncd.conf
2、查看配置文件裏面host allow ,host deny,允許的Ip網段是否是允許的客戶端訪問
3、查看配置文件中的path參數裏的路徑是否存在,權限是否正確
4、查看rsync服務是否啓動,查看命令爲:ps -ef|grep rsync,端口是否存在netstat -lnutp|grep 873 or lsof -i :873
5、參考iptables防火牆和selinux是否開啓允許rsync服務通過,也可以考慮關閉防火牆和selinux
6、查看服務器rsync配置的文件是否爲600的權限,文件格式是否是正確user:passwd,文件路徑和配置文件裏的secrectfiles參數對應
7、如果是推送數據,要查看下,配置rsyncd.conf文件中用戶是否對模塊下有寫的權限
7.2 客戶端排除思路
1、查看您客戶端rysnc配置的密碼文件是否一致,權限是否600,密碼文件格式是否正確
2、用Telnet連接rsync服務器ip地址873端口,查看服務是否啓動eg:telnet 10.0.0.100 873
3、客戶端執行命令是 rysnc -avzP [email protected]::backup/ /test/--password-file=/etc/rsync.password
八 crontab+rsync
1、建立rysnc的密碼文件
echo "oldboy">>/etc/rsync.password
cat /etc/rsync.password
ll /etc/rsync.password
chmod 600 /etc/rsync.password
ll /etc/rsync.password
2、創建將要備份的目錄,我這裏是創建了以ip和時間爲目錄名字的。
mkdir -p /backup/`ifconfig eth0|awk -F"[ :]+" 'NR==2 {print $4}'`_$(date +%F)
[root@LNMP scripts]# ll /backup/
total 4
drwxr-xr-x. 2root root 4096 Mar 4 09:41 10.0.0.101_2016-03-04 目錄名字
3、我這裏是要備份/cron/root 和/etc/rc.local。而且我複製他們後也用時間命名:
cp /var/spool/cron/root/backup/10.0.0.101_2016-03-04/cron_root_$(date +%F)
cp /etc/rc.local/backup/10.0.0.101_2016-03-04/rc.loacl_$(date +%F)
[root@LNMP scripts]# ll/backup/10.0.0.101_2016-03-04/
total 8
-rw-------. 1 root root 150 Mar 4 09:42 cron_rsync_2016-03-04
-rwxr-xr-x. 1 root root 315 Mar 4 09:42 rc.loacl_2016-03-04
4、複製上面的命令編寫腳本文件:
[root@LNMP scripts]# cat cron_rsync.sh
#!/bin/sh
path=/backup
dir="`ifconfig eth0|awk -F "[:]+" 'NR==2 {print $4}'`_$(date +%F)"
mkdir $path/$dir -p &&\
/bin/cp /var/spool/cron/root$path/$dir/cron_rsync_$(date +%F) &&\
/bin/cp /etc/rc.local$path/$dir/rc.loacl_$(date +%F) &&\
rsync -az $path/[email protected]::backup/ --password-file=/etc/rsync.password
5.測試腳本
[root@LAMP scripts]# sh cron_rsync.sh
6、服務器端查看測試命令
[root@BACKUP ~]# ll /backup/
total 16
drwxr-xr-x. 2 rsync rsync 4096 Mar 4 09:4110.0.0.101_2016-03-04
drwxr-xr-x. 2 rsync rsync 4096 Mar 3 16:06 10.0.0.102_2016-03-03
drwxr-xr-x. 2 rsync rsync 4096 Mar 4 10:26 10.0.0.102_2016-03-04
-rw-------. 1 rsync rsync 328 Mar 4 10:33 2016-03-04
7、創建定時任務
[root@LNMP scripts]# crontab -l
########rysnc+crontab#################
00 01 * * */bin/sh /server/scripts/cron_rsync.sh>/dev/null 2>&1 腳本放置路徑
優缺點
優點:
1、增量備份、支持scoket(daemon),集中備份(支持推拉,都是以客戶端爲參照物)
2、遠程SHELL通道模式還可以加密(SSH)傳輸,scoket(daemon)需要加密傳輸,可以利用***服務或ipsec服務
缺點:
1、大量小文件情況下同步,比對時間較長,有的時候,rsync進程可能會停止
2、同步打文件,10G這樣的打文件有時也會有問題,出現:中斷,未完整同步前,是隱藏文件
rsync長時間傳輸的限速參數:
--bwlimit=KBPS
[K1]/etc/rsync.password 這個配置文件就是在/etc/rsyncd.conf配置文件中指定的那個密碼文件和路徑。rsync_backup是指定的用戶auth users = rsync_backup。
[K2]這裏是參數P和v的對比
本文出自 “圈中一鳥” 博客,請務必保留此出處http://sgk2011.blog.51cto.com/1551358/1825320