今天在兩台伺服器同步備份在使用者權限上糾結了很多,主要關于這個問題網上的配置方法不一,源自rsync版本不一緻。
<a href="http://blog.51cto.com/attachment/201209/204002584.jpg" target="_blank"></a>
Rsync 版本
[root@mail video]# rsync –version
rsync version 3.0.6 protocol version 30
Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.
Web site: 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, no 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.
伺服器同步任務需求
伺服器A與伺服器B同步備份,這裡隻說明伺服器A同步到伺服器B,伺服器B還原到伺服器A。
考慮安全因素,使用普通使用者進行同步。
使用cronjob,定時同步。
錯誤提示
錯誤發生在rsync 3.0.6版本,64位 CentOS5.5 系統。
首頁這篇文章主要解決的錯誤是以下:
@ERROR: auth failed on module ***
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]
*** 是你/etc/rsyncd.conf 中配置
*** 是你/etc/rsyncd.conf 中配置的子產品,我這裡用
password file must not be other-accessible
continuing without password file
Password:
Rsync 配置
#vi /etc/rsyncd.conf
uid = nobody
gid = nobody
max connections = 4
read only = true
#hosts allow = 202.207.177.180
hosts allow = *
transfer logging = true
log format = %h %o %f %l %b
log file = /var/log/rsyncd.log
slp refresh = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
[web]
path = /home/admin/public_html
comment = Mirror to Hk server
list = false
auth users = lixiphp
[test]
path = /home/admin/domains/test
read only = false
auth users = lixiphp
secrets file = /etc/rsyncd.secrets
配置普通使用者密碼
[root@mail video]# vi /etc/rsyncd.secrets
格式為: username:password
rsync_user:rsyncofpass
設定權限為隻讀:
chmod 600 /etc/rsyncd.secrets
首次啟動rsync
rsync –daemon –config=/etc/rsyncd.conf
如果提示
failed to create pid file /var/run/rsyncd.pid: File exists
使用指令
rm -rf /var/run/rsyncd.pid
重新開機已經在運作的rsync
[root@mail video]# ps -ef | grep rsync
root 27284 1 0 10:26 ? 00:00:00 rsync –daemon –config=/etc/rsyncd.conf
root 30516 29986 0 18:35 pts/3 00:00:00 grep rsync
[root@mail video]# kill -9 27284
[root@mail video]# rsync –daemon –config=/etc/rsyncd.conf
這樣伺服器A配置成功!
一般錯誤都會發生在伺服器B,注意這部分的講解!
通過CentOS yum install rsync,安裝rsync服務。
在rsync安裝之後,運作以下指令同步備份:
rsync -vzrtopg –progress –delete –password-file=/home/admin/admin_backups/password.rsync rsync://[email protected]/test /home/admin/admin_backups/test
位址rsync://[email protected]/test,lixiphp為伺服器A使用者,203.171.237.245伺服器A IP位址或者域名 test為伺服器A配置子產品
密碼存放在/home/admin/admin_backups/password.rsync,這裡存放位置,可自由安排。
password.rsync内容格式為: password
rsyncofpass
chmod 600 /home/admin/admin_backups/password.rsync
解決錯誤
使用者密碼錯誤
@ERROR: auth failed on module test
檢查伺服器A存儲密碼檔案和伺服器B密碼檔案。
伺服器A密碼檔案 /etc/rsyncd.secrets 格式為: username:password
伺服器B密碼檔案 password.rsync 格式為:password
檔案權限錯誤
伺服器A密碼檔案 /etc/rsyncd.secrets 權限為600: chmod 600 /etc/rsyncd.secrets
伺服器B密碼檔案 password.rsync 權限為600:chmod 600 password.rsync
定時任務
[root@hk admin_backups]# vi backup.sh
内容如下:
#/bin/sh
rsync -vzrtopg –progress –delete –password-rsyncfile=/home/admin/admin_backups/password.rsync ://[email protected]
添加定時任務:
[root@hk admin_backups]# crontab –e
添加以下内容:
*/1 * * * * /home/admin/admin_backups/backup.sh > /dev/null 2>&1
每個一分鐘從伺服器A同步到伺服器B!
伺服器B向下備份到伺服器A
rsync -vzrtopg –progress –delete –password-file=/home/admin/admin_backups/password.rsync /home/admin/admin_backups/test rsync://[email protected]/test
請確定伺服器A同步使用者lixiphp,對子產品test所在目錄有讀、寫、執行的權限
補充:rsync+inotify 實時同步
A伺服器實時同步到B伺服器做法:
B伺服器部署rsync服務端,B伺服器配置rsync用戶端和編譯安裝inotify
實時同步原理:
inotify監測和同步腳本:
#!/bin/bash
host=192.168.1.1
src=/wwwroot/www/jythonscript/
dst=jythonscript
user=rsync
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
do
/usr/local/bin/rsync -vzrtopg --delete --progress $src $user@$host::$dst --password-file=/etc/rsyncd.password
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
inotifywait監測檔案是否發生變化,如果有變化就執行rsync,同步相應的檔案!
本文轉自 geekwolf 51CTO部落格,原文連結:http://blog.51cto.com/linuxgeek/997906