如何保持Linux伺服器間的檔案同步 (更新版)

tonykorn97發表於2008-04-03

上面有一篇地址如下:

http://tonykorn97.itpub.net/post/6414/458827

有些地方不是太詳細,下面的是重新更新過的。


配置rsync
配置/etc/rsyncd.conf檔案:

###########################################################################

uid = nobody
gid = nobody
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log

[export]
path = /export
ignore errors
read only = true
list = false
hosts allow = 192.168.1.252
hosts deny = 192.168.1.0/24

[user01]
path = /home/user01/backup
ignore errors
read only = true
list = false
hosts allow = 192.168.1.252
hosts deny = 192.168.1.0/24

[user02]
path = /home/user02/user02
ignore errors
read only = true
list = false
hosts allow = 192.168.1.252
hosts deny = 192.168.1.0/24

###########################################################################

修改啟動rsync檔案
[root@tonykorn97 etc]# cat /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it
# allows crc checksumming etc.
service rsync
{
# disable = yes
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[root@tonykorn97 etc]#

客戶端同步:
rsync -vzrtopg --progress export@192.168.1.250::export /export


經過測試用下面的方法比較好,可以刪除那些DST中SRC沒有的檔案
rsync -vzrtopg --progress --delete 192.168.1.250::export /export
rsync -vzrtopg --progress --delete 192.168.1.250::user02 /home/user02/user02
rsync -vzrtopg --progress --delete 192.168.1.250::user01 /home/user01/backup


我們傳輸的命令模式是:
rsync [OPTION]... [USER@]HOST::SRC [DEST]

引數說明:
-v, --verbose 詳細模式輸出
-z, --compress 對備份的檔案在傳輸時進行壓縮處理
-r, --recursive 對子目錄以遞迴模式處理
-t, --times 保持檔案時間資訊
-o, --owner 保持檔案屬主資訊
-p, --perms 保持檔案許可權
-g, --group 保持檔案屬組資訊

--progress 顯示備份過程
--delete 刪除那些DST中SRC沒有的檔案
--exclude "data/" 表示不對/home/user01/backup/data/目錄下的檔案進行備份

下面的命令可以傳輸不包含某個目錄的檔案
rsync -vzrtopg --delete --exclude "data/" --progress 192.168.1.250::user02 /home/user02/user02
rsync -vzrtopg --delete --exclude "data/" --progress 192.168.1.250::user01 /home/user01/backup

製作一個指令碼:
[root@tonykorn9702 export]# cat autoexport.sh
#!/bin/bash
/usr/bin/rsync -vzrtopg --progress --delete 192.168.1.250::export /export
/usr/bin/rsync -vzrtopg --progress --delete 192.168.1.250::user02 /home/user02/user02
/usr/bin/rsync -vzrtopg --progress --delete 192.168.1.250::user01 /home/user01/backup
[root@tonykorn9702 export]# chmod 755 autoexport.sh
[root@tonykorn9702 export]# ./autoexport.sh
receiving file list ...
473 files to consider
./
autoexport.sh
73 100% 71.29kB/s 0:00:00 (xfer#1, to-check=471/473)

sent 92 bytes received 9076 bytes 18336.00 bytes/sec
total size is 147219805 speedup is 16058.01
[root@tonykorn9702 export]#

透過crontab設定,讓這個指令碼每30分鐘執行一次。執行命令:
# crontab -e
輸入以下一行:
30 * * * * * /export/autoexport.sh

上面的是整點的30分之想,後來更改為:
20,50 * * * * /export/autoexport.sh
讓20和50分的時候執行。


出現的問題:
同步的時候有一些檔案同步不了,
[root@tonykorn9702 user01]# rsync -vzrtopg --delete --exclude "backup/data/" --progress 192.168.1.250::user01 /home/user01
receiving file list ...
rsync: opendir ".ssh" (in user01) failed: Permission denied (13)
22 files to consider
IO error encountered -- skipping file deletion
./
rsync: send_files failed to open ".bash_history" (in user01): Permission denied (13)
rsync: send_files failed to open ".viminfo" (in user01): Permission denied (13)

sent 127 bytes received 755 bytes 1764.00 bytes/sec
total size is 86396 speedup is 97.95
rsync error: some files could not be backupferred (code 23) at main.c(1298) [generator=2.6.8]
[root@tonykorn9702 user01]#
在伺服器端執行:
[root@tonykorn97 user01]# ls -al
total 60
drwxr-xr-x 5 user01 users 4096 Apr 4 2007 .
drwxr-xr-x 13 root root 4096 Oct 15 08:36 ..
-rwx--x--x 1 user01 users 7053 Apr 3 16:49 .bash_history
-rwxr-xr-x 1 user01 users 24 Jan 5 2007 .bash_logout
-rwxr-xr-x 1 user01 users 191 Jan 5 2007 .bash_profile
-rwxr-xr-x 1 root root 631 Jan 17 2007 .bashrc
-rwxr-xr-x 1 user01 users 237 Jan 5 2007 .emacs
-rwxr-xr-x 1 user01 users 120 Jan 5 2007 .gtkrc
drwxr-xr-x 3 user01 users 4096 Jan 16 2007 .kde
drwx--x--x 2 user01 users 4096 Mar 30 2007 .ssh
drwxr-xr-x 3 user01 users 4096 Mar 24 16:35 backup
-rwx--x--x 1 user01 users 3812 Apr 4 2007 .viminfo
-rwxr-xr-x 1 user01 users 136 Jan 5 2007 .zshrc
[root@tonykorn97 user01]# chmod +r .bash_history
[root@tonykorn97 user01]# chmod +r .viminfo

重新傳輸的時候出現:
[root@tonykorn9702 user01]# rsync -vzrtopg --delete --exclude "backup/data/" --progress 192.168.1.250::user01 /home/user01
receiving file list ...
rsync: opendir ".ssh" (in user01) failed: Permission denied (13)
22 files to consider
IO error encountered -- skipping file deletion
.bash_history
7053 100% 6.73MB/s 0:00:00 (xfer#1, to-check=20/22)
.viminfo
3812 100% 3.64MB/s 0:00:00 (xfer#2, to-check=10/22)

sent 127 bytes received 2563 bytes 5380.00 bytes/sec
total size is 86396 speedup is 32.12
rsync error: some files could not be backupferred (code 23) at main.c(1298) [generator=2.6.8]
[root@tonykorn9702 user01]#


[root@tonykorn9702 user01]# rsync -vzrtopg --delete --exclude "user02/data/" --progress posftp@192.168.1.250::user02 /home/user02
receiving file list ...
rsync: link_stat "." (in user02) failed: Permission denied (13)
0 files to consider

sent 19 bytes received 17 bytes 72.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files could not be backupferred (code 23) at main.c(1298) [receiver=2.6.8]
[root@tonykorn9702 user01]#


出現這些問題的原因估計應該是使用者目錄下的隱藏檔案造成的。
原來/etc/rsyncd.conf檔案中設定的目錄是/home/user01,後臺設定的是/home/user01/backup.設定後就可以同步。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/312079/viewspace-245851/,如需轉載,請註明出處,否則將追究法律責任。

相關文章