實現目標:
伺服器A與伺服器B同步備份,這裡只說明伺服器A同步到伺服器B,伺服器B還原到伺服器A。
考慮安全因素,使用普通使用者進行同步。
使用cronjob,定時同步。
A 伺服器上/opt/web 目錄,與B用戶端伺服器上/opt/web目錄實現同步。即:B主動與A進行同步。(A的檔案會同步到B機器上)
| 代碼如下 |
複製代碼 |
OS: Reaht AS4
A Server 192.168.0.100 /opt/web
B Server 192.168.0.60 /opt/web |
一. A Server config
1.安裝服務
| 代碼如下 |
複製代碼 |
yum install xinetd
yum install rsync
|
2. vi /etc/xinetd.d/rsync 修改 disable =yes 為 disable =no ,修改後的檔案如下
| 代碼如下 |
複製代碼 |
|
# default: off
# description: The rsync server is a good addition to an ftp server, as it
# allows crc checksumming etc.
service rsync
{
disable = no
socket_type = stream
wait = no
user = backup
server = /usr/bin/rsync
server_args = –daemon
log_on_failure += USERID
} |
3. 編輯主要設定檔/etc/rsyncd.conf (不存在則自己建立)
| 代碼如下 |
複製代碼 |
vi /etc/rsyncd.conf
uid = backup
gid = backup
use chroot = no # 不使用chroot
max connections = 4 # 最大串連數為4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log # 日誌記錄檔案
[web] # 這裡是認證的模組名,在client端需要指定
path = /opt/web # 需要做鏡像的目錄
ignore errors # 可以忽略一些無關的IO錯誤
read only = true # 唯讀
list = false # 不允許列檔案
hosts allow = 192.168.0.0 # 允許的IP地址
hosts deny = 0.0.0.0/32 # 禁止的IP 位址
auth users = backup # 認證的使用者名稱,如果沒有這行,則表明是匿名
secrets file = /etc/backup.pass #認證使用者的密碼檔案 認證檔案名稱
|
4. 編輯服務器的密碼檔案 /etc/backup.pass,檔案只能有可讀許可權 400
| 代碼如下 |
複製代碼 |
vi /etc/backup.pass #檔案格式如下
backup:123456
chmod 400 /etc/backup.pass
|
5.啟動服務
初次開機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是否已經運行:netstat -an|grep 873
| 代碼如下 |
複製代碼 |
[root@localhost web]# netstat -an|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN
tcp 0 0 :::873 :::* LISTEN
|
重啟已經在啟動並執行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
service xinetd restart,重啟 xinted 服務
rsync 系統內建, 需要使用 –deamon 方式進行啟動,服務連接埠是 TCP 873
rsync –deamon –config=/etc/rsyncd.conf
|
如果要在啟動時把服務起來,有幾種不同的方法,比如:
a、加入inetd.conf
編輯/etc/services,加入rsync 873/tcp,指定rsync的服務連接埠是873
編加/etc/inetd.conf,加入rsync stream tcp nowait backup /bin/rsync rsync –daemon
b、加入rc.local
在各種作業系統中,rc檔案存放位置不盡相同,可以修改使系統啟動時rsync –daemon載入進去。
二. B Server config
1. yum install rsync 安裝rsync服務
2. 建立rsync串連時的密碼檔案/etc/rsync_client.pass,所放目錄自己隨意安排,檔案許可權為唯讀400
vi /etc/rsync_client.pass #檔案格式如下:
#只需要配置串連時使用的密碼即可,必須與A伺服器上定義的密碼相同.
| 代碼如下 |
複製代碼 |
chmod 400 /etc/rsync_client.pass
|
3. 使用 rsync 命令串連伺服器,實現檔案同步
| 代碼如下 |
複製代碼 |
rsync -vzrtopg –progress –delete –password-file=/etc/rsync_client.pass backup@192.168.0.100::web /opt/web
|
PS:目錄同步時要儲存相同的檔案屬主資訊,需要在B伺服器上建立相同的使用者
| 代碼如下 |
複製代碼 |
rsync -qcazrtopgl –progress –delete –password-file=/etc/rsync_client.pass –exclude=”*.gz” –exclude-from=/opt/pcdir backup@192.168.0.100::web /opt/web
|
backup為認證使用者名稱;
192.168.0.100為伺服器端IP;
Web為模組名;
/opt/web 為本地的目錄
三.定時任務
| 代碼如下 |
複製代碼 |
[root@localhost admin_backups]# vi backup.sh
|
內容如下:
| 代碼如下 |
複製代碼 |
#/bin/sh
rsync -qcazrtopgl –progress –delete –password-file=/etc/rsync_client.pass –exclude=”*.gz” –exclude-from=/opt/pcdir backup@192.168.0.100::web /opt/web
|
添加定時任務:
| 代碼如下 |
複製代碼 |
[root@localhost admin_backups]# crontab –e
|
添加以下內容:
| 代碼如下 |
複製代碼 |
*/1 * * * * /home/admin/admin_backups/backup.sh > /dev/null 2>&1
|
每個一分鐘從伺服器A同步到伺服器B!
4. 使用 –execlude= 排除不需要同步的檔案尾碼名
| 代碼如下 |
複製代碼 |
| rsync -vzrtopg –progress –delete –password-file=/etc/rsync_client.pass –exclude=”*.tmp” |
backup@192.168.0.100::web /opt/web
5. 使用 –execlude-from= 排除不需要同步的目錄
| 代碼如下 |
複製代碼 |
[root@localhost opt]# rsync -vzrtopg –progress –delete –password-file=/etc/rsync_client.pass –exclude-from=/opt/pcdir backup@192.168.0.100::web /opt/web
|
/opt/pcdir 檔案的內容如下
cat /opt/pcdir
temp/
參數說明
-vzrtopg裡的
v是verbose,
z是壓縮,
r是recursive,
topg都是保持檔案原有屬性如屬主、時間的參數
—-progress
是指顯示出詳細的進度情況
–delete
是指如果伺服器端刪除了這一檔案,那麼用戶端也相應把檔案刪除,保持真正的一致
–exclude=”*.tmp” 不包含某些檔案
–execlude-from= 排除不需要同步的目錄
/opt/web/
指定CLIENT端存放鏡象目的路徑