vi /etc/xinetd.d/rsync
disable = yes
改成
disable = no
rsyncd.conf 是rsyncd的config檔案
vi /etc/rsyncd.conf
#uid = nobody
#gid = nobody
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[compshop]
path = /home/sites/compshop
auth users = compshop
uid = compshop
gid = compshop
secrets file = /etc/rsyncd.secrets
read only = no
[datafeed]
path = /home/sites/datafeed
auth users = datafeed
uid = datafeed
gid = datafeed
secrets file = /etc/rsyncd.secrets
read only = no
[smtemplates]
path = /home/sites/smtemplates
auth users = smtemplates
uid = smtemplates
gid = smtemplates
secrets file = /etc/rsyncd.secrets
read only = no
[smarterv2]
path = /home/sites/smarterv2
auth users = smarterv2
uid = smarterv2
gid = smarterv2
secrets file = /etc/rsyncd.secrets
read only = no
rsyncd.secrets是rsyncd的密碼檔案,裡面是寫使用者名稱和密碼,就是linux的使用者名稱和密碼
vi /etc/rsyncd.secrets
compshop:any
datafeed:any
smtemplates:any
smarterv2:any
以上是伺服器端的配置,開了這個服務以後,連接埠是873
以下是客服端的設定檔,是在另外一台電腦上的。檔案名稱是可以自己改的。
vi /home/sites/sites_rsyncd
#!/bin/bash
rsync -tvzrp --progress --password-file=/home/sites/rsyncd.secrets --delete --exclude
/home/sites/compshop/logs compshop@192.168.168.241::compshop /home/sites/compshop/
rsync -tvzrp --progress --password-file=/home/sites/rsyncd.secrets --delete --exclude
/home/sites/datafeed/logs datafeed@192.168.168.241::datafeed /home/sites/datafeed/
rsync -tvzrp --progress --password-file=/home/sites/rsyncd.secrets --delete --exclude
/home/sites/smtemplates/logs smtemplates@192.168.168.241::smtemplates /home/sites/smtemplates/
rsync -tvzrp --progress --password-file=/home/sites/rsyncd.secrets --delete --exclude
/home/sites/smarterv2/logs smarterv2@192.168.168.241::smarterv2 /home/sites/smarterv2/
chmod 744 /home/sites/sites_rsyncd
vi /home/sites/rsyncd.secrets
any
chmod 600 /home/sites/rsyncd.secrets
三、rsync命令的用法
在配置完rsync伺服器後,就可以從用戶端發出rsync命令來實現各種同步的操
作。rsync有很
多功能選項,下面就對介紹一下常用的選項:
rsync的命令格式可以為:
1. rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
2. rsync [OPTION]... [USER@]HOST:SRC DEST
3. rsync [OPTION]... SRC [SRC]... DEST
4. rsync [OPTION]... [USER@]HOST::SRC [DEST]
5. rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
6. rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
rsync有六種不同的工作模式:
1. 拷貝本地檔案;當SRC和DES路徑資訊都不包含有單個冒號":"分隔字元時就啟動這
種工作模式。
2.使用一個遠程shell程式(如rsh、ssh)來實現將本地機器的內容拷貝到遠程機器
。當DST
路徑地址包含單個冒號":"分隔字元時啟動該模式。
3.使用一個遠程shell程式(如rsh、ssh)來實現將遠程機器的內容拷貝到本地機器
。當SRC
地址路徑包含單個冒號":"分隔字元時啟動該模式。
4. 從遠程rsync伺服器中拷貝檔案到本地機。當SRC路徑資訊包含"::"分隔字元時啟動
該模式。
5. 從本地機器拷貝檔案到遠程rsync伺服器中。當DST路徑資訊包含"::"分隔字元時啟
動該模式。
6. 列遠程機的檔案清單。這類似於rsync傳輸,不過只要在命令中省略掉本地機信
息即可。
下面以執行個體來說明:
# rsync -vazu -progress terry@192.168.100.21:/terry/ /home
v詳細提示
a以archive模式操作,複製目錄、符號串連
z壓縮
u只進行更新,防止本地新檔案被重寫,注意兩者機器的時鐘的同時
-progress指顯示
以上命令是保持客戶機192.168.100.90上的/home/terry目錄和rsync伺服器上的terry目
錄同
步。該命令執行同步之前會要求你輸入terry帳號的密碼,這個帳號是我們前面
在rsyncd.secrets
檔案中定義的。如果想將這條命令寫到一個指令碼中,然後定時執行它的話,可以使
用--password-file
選項,具體命令如下:
# rsync -vazu -progress --password-file=/etc/rsync.secret
terry@192.168.100.21:/terry/ /home
要使用--password-file選項,就得先建立一個存放密碼的檔案,這裡指定
為/etc/rsync.secret。
其內容很簡單,如下:
terry:12345
同樣要修改檔案屬性如下:
# chmod 600 /etc/rsyncd.secrets
四、利用rsync保持Linux伺服器間的檔案同步執行個體
現在假設有兩台Linux伺服器A(192.168.100.21)和B(192.168.100.90),伺服器A中的
/home/terry和伺服器B中的/home/terry這兩個目錄需要保持同步,也就是當伺服器A中文
件發生
改變後,伺服器B中的檔案也要對應去改變。
我們按上面的方法,在伺服器A上安裝rsync,並將其配置為一台rsync伺服器,並
將/home/terry
目錄配置成rsync共用出的目錄。然後在伺服器B上安裝rsync,因為B只做用戶端,所以
無需配置。
然後在伺服器B,建立以下指令碼:
#!/bin/bash
/usr/loca/rsync/bin/rsync -vazu -progress --delete
--password-file=/etc/rsync.secret terry@192.168.100.21:/terry/ /home
將這個指令碼儲存為AtoB.sh,並加上可執行屬性:
# chmod 755 /root/AtoB.sh
然後,通過crontab設定,讓這個指令碼每30分鐘運行一次。執行命令:
# crontab -e
輸入以下一行:
0,30 * * * * /root/AtoB.sh
儲存退出,這樣伺服器B每個小時的0分和30分時都會自動運行一
次AtoB.sh,AtoB.sh是負責
保持伺服器B和伺服器A同步的。這樣就保證了伺服器A的所有更新在30鐘後,伺服器B 也一樣取
得了和伺服器A一樣的最新的資料。