1,rsync簡介
Rsync是一個快速和異常靈活的文件複製工具,它能夠通過remote shell (rsh,ssh)或remote rsync daemon 來實現本地到或從遠程主機的複製。它提供了大量選項來控制行爲的方方面面,能夠彈性定義複製文件的屬性,通過delta-transfer 算法,只發送源和目標不同的文件來降低網絡數據傳輸量。rsync作爲增強的copy命令廣泛的用於鏡像和備份。
rsync的額外特性:
1、支持複製文件保存文件的軟硬鏈接,屬主,屬組,權限等。
2、類似於GNU tar的exclude ,exclude from選項。
3、 CVS排除模式,忽略CVS要求的相同文件。
4、能夠便用任何遠程shell,如rcp、ssh等方式來傳輸文。
5、不要求超級用戶特 權。
6、流水式文件傳輸最小化傳輸延時。
7、支持匿名或驗證的rsync。
在沒有建立rsync服務器的情況下,我們可以利用 rsync命令直接進行快速的差異備份:
2,命令格式:
#rsync [option] 源路徑 目標路徑
其中:
[option]:
a:使用 archive模式,等於-rlptgoD,即保持原有的文件權限
z:表示傳輸時壓縮數據
v:顯示到屏幕中
e:使用遠程 shell程序(可以使用rsh或ssh)
--delete:精確保存副本,源主機刪除的文件,目標主機也會同步刪除
--include=PATTERN: 不排除符合PATTERN的文件或目錄
--exclude=PATTERN:排除所有符合PATTERN的文件或目錄
--password- file:指定用於rsync服務器的用戶驗證密碼
源路徑和目標路徑可以使用如下格式:
rsync://[USER@]Host[:Port]/Path <--rsync服務器路徑
[USER@]Host::Path <--rsync服務器的另一種表示形式
[USER@]Host:Path <--遠程路徑
LocalPath <--本地路徑
※ 需要注意的是,來源或目的路徑最少要有一個是本地路徑,如果忽略本地路徑,則只會列出遠端的文件列表。
例子:
#rsync -ave ssh test:/home/ftp/pub/ /home/ftp/pub/
把源路徑中遠端test機器上的/home/ftp/pub/目錄中的內容,通過rsync同步到本地的/home/ftp/pub/目錄下。
◎ 小心源路徑結尾時候的/號,後綴/通知rsync複製該目錄的內容,但不復制目錄本身。
例如:
#rsync -ave ssh test:/home/ftp/pub /home/ftp/
則會把pub目錄整個同步到本地/home/ftp/路徑中
#rsync -azv --delete rsync://[email protected]/blog /var/www/html/
通過linuxing登陸到192.168.1.100中,同步rsync服務器的blog項到本地的/var/www/html/,並刪除本地上 源路徑中不存在的文件或目錄。
※千萬要注意--delete參數,在使用此參數的時候,建議用絕對路徑指定本地目錄,防止清空當前目錄。
3,服務器端設置:
修改配置文件
vi /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no # 不使用chroot
max connections = 4
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
[test] #rsync區段
path = /var/www/html/test #需要同步的目錄
comment = test folder #註釋
ignore errors #忽略錯誤
read only = yes #只讀
list = no #不能列表
auth users = webrsync #連接rsync服務的用戶名
secrets file = /etc/rsyncd.secrets #指定存放帳號密碼的位置
創建帳號密碼文件:
vi /etc/rsync.pass
webrsync:myrsynpass
保 存後,需要保證用戶是root,權限是600,否則會出現驗證錯誤。
chown root.root /etc/rsync.pass
chmod 600 /etc/rsync.pass
啓動服務:
rsync --daemon
設成開機啓動的兩種方式:
a,修改inetd.conf下的rsync
編輯/etc/services,加入rsync 873/tcp,指定rsync的服務端口是873
修改/etc /xinetd.conf下的rsync
sed -i -e "/disable/{ s/yes/no/ }" /etc/xinetd.d/rsync
service xinetd.d restart
b,將命令寫到啓動文件中
echo "rsync --daemon" >> /etc/rc.local
4,客設端設置:
rsync -vzrtopg --progress --delete [email protected]::test /tmp/
password:
receiving file list ... done
下面這個命令行中-vzrtopg裏的v是verbose,z是壓縮,r是recursive,topg都是保持文件原有屬性如 屬主、時間的參數。--progress是指顯示出詳細的進度情況,--delete是指如
果服務器端刪除了這一文件,那麼客戶端也相應把文件刪除,保持真正的一致。後面的webrsync@ip中,webrsync是指定密碼文件中的用 戶名,之後的::test這一inburst是模塊名
,也就是在/etc/rsyncd.conf中自定義的名稱。最後的/tmp是備份到本地的目錄名。
在客戶端通過指過--password-file,可以不用輸入密碼,這樣在腳本只不有輸入密碼,這求在客戶端也創建密碼文件:
vi /etc/rsync.pass,只輸入要驗證用戶的密碼,chmod 600 /etc/rsync.pass
然後重新運行命令
rsync -vzrtopg --progress --delete [email protected]::test /tmp/ --password-file=/etc/rsync.pass
5,設成定時運行
crontab -e
0 2 * * *
rsync -vzrtopg --progress --delete [email protected]::test /tmp/ --password-file=/etc/rsync.pass
或者編寫腳本:
rsync.sh
#!/bin/sh
DATE=`date +%w`
rsync -vzrtopg --progress --delete [email protected]::test /tmp/ --password-file=/etc/rsync.pass > /var/log/rsync.$DATE
chmod +x rsync.sh
添加到cron裏運行。
6,其它使用方法:
比如我的客戶端想要複製服務器端的兩個或更多個文件或目錄,可通過下面的方式:
rsync -av host:file1 :file2 host:file{3,4} /dest/
rsync -av host::modname/file{1,2} host::modname/file3 /dest/
rsync -av host::modname/file1 ::modname/file{3,4}
這裏如我的客戶端要從服務器端同步兩個文件夾,test和 web
在服務器端的rsyncd.conf
[test]
path = /var/www/html/test #注意這裏是test不是test/,它將在目標目錄下新建文件夾test而不是複製test下的所有文件
comment = test folder
ignore errors
read only = yes
list = no
auth users = webrsync
secrets file = /etc/rsyncd.secrets
[web]
path = /var/www/html/web #同上
comment = web folder
ignore errors
read only = yes
list = no
auth users = webrsync
secrets file = /etc/rsyncd.secrets
在客戶端運行:
rsync -vzrtopg --progress --delete [email protected]::test [email protected]:web /tmp --password-file=/etc/rsync.pass
會在客戶端的 tmp目錄下新建兩個文件夾test和web,將兩個文件夾下的內容同步過來。
7,參考腳本:
1、每隔七天將數據往中心服務器做增量備份
#!/bin/sh
# This script does personal backups to a rsync backup server. You will end up
# with a 7 day rotating incremental backup. The incrementals will go
# into subdirectories named after the day of the week, and the current
# full backup goes into a directory called "current"
# [email protected]
# directory to backup
BDIR=/home/$USER
# excludes file - this contains a wildcard pattern per line of files to exclude
EXCLUDES=$HOME/cron/excludes
# the name of the backup machine
BSERVER=owl
# your password on the backup server
export RSYNC_PASSWORD=XXXXXX
########################################################################
BACKUPDIR=`date +%A`
OPTS="--force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES
--delete --backup --backup-dir=/$BACKUPDIR -a"
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
# the following line clears the last weeks incremental directory
[ -d $HOME/emptydir ] || mkdir $HOME/emptydir
rsync --delete -a $HOME/emptydir/ $BSERVER::$USER/$BACKUPDIR/
rmdir $HOME/emptydir
# now the actual transfer
rsync $OPTS $BDIR $BSERVER::$USER/current
2、備份至一個空閒的硬盤
#!/bin/sh
export PATH=/usr/local/bin:/usr/bin:/bin
LIST="rootfs usr data data2"
for d in $LIST; do
mount /backup/$d
rsync -ax --exclude fstab --delete /$d/ /backup/$d/
umount /backup/$d
done
DAY=`date "+%A"`
rsync -a --delete /usr/local/apache /data2/backups/$DAY
rsync -a --delete /data/solid /data2/backups/$DAY
3、對vger.rutgers.edu的cvs樹進行鏡像
#!/bin/bash
cd /var/www/cvs/vger/
PATH=/usr/local/bin:/usr/freeware/bin:/usr/bin:/bin
RUN=`lps x | grep rsync | grep -v grep | wc -l`
if [ "$RUN" -gt 0 ]; then
echo already running
exit 1
fi
rsync -az vger.rutgers.edu::cvs/CVSROOT/ChangeLog $HOME/ChangeLog
sum1=`sum $HOME/ChangeLog`
sum2=`sum /var/www/cvs/vger/CVSROOT/ChangeLog`
if [ "$sum1" = "$sum2" ]; then
echo nothing to do
exit 0
fi
rsync -az --delete --force vger.rutgers.edu::cvs/ /var/www/cvs/vger/
exit 0
4、利用find的一種巧妙方式
rsync -avR remote:'`find /home -name "*.[ch]"`' /tmp/
可以用這種方法列出需要備份的文件列表——這種方法似乎比較少人用到。