rsync中文手冊之使用rsync實現(xiàn)網(wǎng)站鏡像和備份linux
更新時間:2008年09月17日 13:04:44 作者:
用rsync實現(xiàn)網(wǎng)站鏡像和備份 雖然是linux下的操作,但原理和windows下類似
服務(wù)器配置實例
那么在www.linuxaid.com.cn上創(chuàng)建rsyncd的配置文件/etc/rsyncd.conf,內(nèi)容如下:
uid = nobody
gid = nobody
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[www]
path = /www/
ignore errors
read only = true
list = false
hosts allow = 202.99.11.121
hosts deny = 0.0.0.0/32
auth users = backup
secrets file = /etc/backserver.pas
[web_user1]
path = /home/web_user1/
ignore errors
read only = true
list = false
hosts allow = 202.99.11.121
hosts deny = 0.0.0.0/32
uid = web_user1
gid = web_user1
auth users = backup
secrets file = /etc/backserver.pas
[web_user2]
path = /home/web_user2/
ignore errors
read only = true
list = false
hosts allow = 202.99.11.121
hosts deny = 0.0.0.0/32
uid = web_user2
gid = web_user2
auth users = backup
secrets file = /etc/backserver.pas
這里定義有四個三個模塊,分別對應(yīng)于三個需要備份的目錄樹。這里只允許202.99.11.121備份本機的數(shù)據(jù),并且需要認(rèn)證。三個模塊授權(quán)的備份用戶都為backup,并且用戶信息保存在文件/etc/backserver.pas中,其內(nèi)容如下:
backup:bk_passwd
并且該文件只能是root用戶可讀寫的,否則rsyncd啟動時會出錯。這些文件配置完畢以后,就需要在A服務(wù)器上啟動rsyncd服務(wù)器:
rsync --daemon
客戶命令示例
/usr/local/bin/rsync -vzrtopg --delete --exclude "logs/" --exclude "conf/ssl.*/" --progress backup@202.99.11.120::www /backup/www/ --password-file=/etc/rsync.pass
上面這個命令行中-vzrtopg里的v是verbose,z是壓縮,r是recursive,topg都是保持文件原有屬性如屬主、時間的參數(shù)。-- progress是指顯示出詳細的進度情況,--delete是指如果服務(wù)器端刪除了這一文件,那么客戶端也相應(yīng)把文件刪除,保持真正的一致。-- exclude "logs/" 表示不對/www/logs目錄下的文件進行備份。--exclude "conf/ssl.*/"表示不對/www/conf/ssl.*/目錄下的文件進行備份。
backup@202.99.11.120::www 表示對該命令是對服務(wù)器202.99.11.120中的www模塊進行備份,backup表示使用backup來對該模塊進行備份。
--password-file=/etc/rsync.pass來指定密碼文件,這樣就可以在腳本中使用而無需交互式地輸入驗證密碼了,這里需要注意的是這份密碼文件權(quán)限屬性要設(shè)得只有root可讀。
這里將備份的內(nèi)容存放在備份機的/backup/www/目錄下。
[root@linuxaid /]# /usr/local/bin/rsync -vzrtopg --delete --exclude "logs/" --exclude "conf/ssl.*/" --progress backup@202.99.11.120::www /backup/www/ --password-file=/etc/rsync.pass
receiving file list ... done
./
1
785 (100%)
1.py
4086 (100%)
2.py
10680 (100%)
a
0 (100%)
ip
3956 (100%)
./
wrote 2900 bytes read 145499 bytes 576.34 bytes/sec
total size is 2374927 speedup is 45.34
對其它兩個模塊操作的命令分別為:
/usr/local/bin/rsync -vzrtopg --delete --progress backup@202.99.11.120::web_user1 /backup/web_user1/ --password-file=/etc/rsync.pass
/usr/local/bin/rsync -vzrtopg --delete --progress backup@202.99.11.120::web_user2 /backup/web_user2/ --password-file=/etc/rsync.pass
可以將客戶命令通過crontab -e命令來實現(xiàn)自動備份,如crontab -e:
一些示例腳本
這里這些腳本都是rsync網(wǎng)站上的例子:
1、每隔七天將數(shù)據(jù)往中心服務(wù)器做增量備份
#!/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"
# tridge@linuxcare.com
# 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
FAQ
Q:如何通過ssh進行rsync,而且無須輸入密碼?
A:可以通過以下幾個步驟
1. 通過ssh-keygen在server A上建立SSH keys,不要指定密碼,你會在~/.ssh下看到identity和identity.pub文件
2. 在server B上的home目錄建立子目錄.ssh
3. 將A的identity.pub拷貝到server B上
4. 將identity.pub加到~[user b]/.ssh/authorized_keys
5. 于是server A上的A用戶,可通過下面命令以用戶B ssh到server B上了
e.g. ssh -l userB serverB
這樣就使server A上的用戶A就可以ssh以用戶B的身份無需密碼登陸到server B上了。
Q:如何通過在不危害安全的情況下通過防火墻使用rsync?
A:解答如下:
這通常有兩種情況,一種是服務(wù)器在防火墻內(nèi),一種是服務(wù)器在防火墻外。無論哪種情況,通常還是使用ssh,這時最好新建一個備份用戶,并且配置 sshd僅允許這個用戶通過RSA認(rèn)證方式進入。如果服務(wù)器在防火墻內(nèi),則最好限定客戶端的IP地址,拒絕其它所有連接。如果客戶機在防火墻內(nèi),則可以簡單允許防火墻打開TCP端口22的ssh外發(fā)連接就ok了。
Q:我能將更改過或者刪除的文件也備份上來嗎?
A:當(dāng)然可以:
你可以使用如:rsync -other -options -backupdir = ./backup-2000-2-13 ...這樣的命令來實現(xiàn)。
這樣如果源文件:/path/to/some/file.c改變了,那么舊的文件就會被移到./backup-2000-2-13/path/to/some/file.c,
這里這個目錄需要自己手工建立起來
Q:我需要在防火墻上開放哪些端口以適應(yīng)rsync?
A:視情況而定
rsync可以直接通過873端口的tcp連接傳文件,也可以通過22端口的ssh來進行文件傳遞,但你也可以通過下列命令改變它的端口:
rsync --port 8730 otherhost::
或者
rsync -e 'ssh -p 2002' otherhost:
Q:我如何通過rsync只復(fù)制目錄結(jié)構(gòu),忽略掉文件呢?
A:rsync -av --include '*/' --exclude '*' source-dir dest-dir
Q:為什么我總會出現(xiàn)"Read-only file system"的錯誤呢?
A:看看是否忘了設(shè)"read only = no"了
Q:為什么我會出現(xiàn)'@ERROR: invalid gid'的錯誤呢?
A:rsync使用時默認(rèn)是用uid=nobody;gid=nobody來運行的,如果你的系統(tǒng)不存在nobody組的話,就會出現(xiàn)這樣的錯誤,可以試試gid = nogroup或者其它
Q:綁定端口873失敗是怎么回事?
A:如果你不是以root權(quán)限運行這一守護進程的話,因為1024端口以下是特權(quán)端口,會出現(xiàn)這樣的錯誤。你可以用--port參數(shù)來改變。
Q:為什么我認(rèn)證失???
A:從你的命令行看來:
你用的是:
>; bash$ rsync -a 144.16.251.213::test test
>; Password:
>; @ERROR: auth failed on module test
>;
>; I dont understand this. Can somebody explain as to how to acomplish this.
>; All suggestions are welcome.
應(yīng)該是沒有以你的用戶名登陸導(dǎo)致的問題,試試rsync -a max@144.16.251.213::test test
相關(guān)文章
Linux基礎(chǔ):如何找出你的系統(tǒng)所支持的最大內(nèi)存
這篇文章主要介紹了Linux基礎(chǔ):如何找出你的系統(tǒng)所支持的最大內(nèi)存,需要的朋友可以參考下2015-04-04centos7修改網(wǎng)關(guān)和配置ip的方法示例
這篇文章主要介紹了centos7修改網(wǎng)關(guān)和配置ip的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08虛擬機安裝Linux rhel7.3操作系統(tǒng)(具體步驟)
這篇文章主要介紹了虛擬機安裝Linux rhel7.3操作系統(tǒng)(具體步驟),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10win8下XAMPP中Apache模塊無效(apache無法打開)的解決方法
win8下系統(tǒng)默認(rèn)占用80端口,導(dǎo)致apache無法打開,下面是具體的解決方法:首先以管理員權(quán)限運行c:\windows\system32\cmd.exe.....感興趣的朋友可以參考下哈,希望對大家有所幫助2013-07-07CentOS下采用Crontab實現(xiàn)PHP腳本定時任務(wù)
本篇文章主要介紹了CentOS下采用Crontab實現(xiàn)PHP腳本定時任務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05