首先手頭有兩臺linux服務器
系統爲CentOS
1臺作爲主服務器,另1臺作爲同步服務器(主服務器上添加/修改/刪除文件後將會同步給同步服務器)
首先先檢查主服務器是否安裝所需的工具
1.檢查rsync是否已經安裝
rpm -qa | grep rsync
如果沒有安裝則進行安裝
yum -y install rsync
2.檢查 xinetd 是否安裝
rpm -qa | grep xinetd
如果沒有安裝則進行安裝
yum -y install xinetd
3.安裝 inotify-tools
首先需要下載 inotify-tools
http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
然後上傳到主服務上,我這裏放在 /home
目錄下
解壓
tar zxvf inotify-tools-3.14.tar.gz
進入文件夾
cd inotify-tools-3.14
配置(我這裏直接安裝在 /home/inotify
中了)
./configure --prefix=/home/inotify
編譯安裝
make & make install
如下出現Done
就安裝完畢了
4.信任連接配置
登入同步服務器
新建用戶 user-rsync , 密碼隨意
useradd -m user-rsync
passwd user-rsync
然後登入主服務器,生成祕鑰文件,注意別漏掉最後的''
ssh-keygen -t rsa -P ''
然後把生成的id_rsa.pub拷貝到同步服務器上,xx.xx.xx.xx爲同步服務器ip
在這之前先在同步服務器上新建user-rsync文件夾,我放在了 /home 目錄下
cd /root/
scp .ssh/id_rsa.pub user-rsync@xx.xx.xx.xx:/home/user-rsync/id_rsa.pub
會提示輸入密碼,輸入剛纔創建user-rsync用戶時的密碼。
拷貝完畢後修改用戶權限,把祕鑰文件輸入到authorized_keys中(需要我們先自己在user-rsync下創建.ssh文件夾)
chmod 700 /home/lms-rsync/.ssh
cat /home/user-rsync/id_rsa.pub >> /home/user-rsync/.ssh/authorized_keys
chmod 600 /home/user-rsync/.ssh/authorized_keys
然後登錄主服務器嘗試連接同步服務器,xx.xx.xx.xx爲同步服務器
ssh user-rsync@xx.xx.xx.xx
出現如下圖就表示連接上了,輸入exit
退出(我這裏用的用戶名是bms-rsync)
能夠成功連接同步服務器後
5.配置文件同步服務器
在同步服務器上也安裝rsync和xinetd
(1) rsync 配置
mkdir /etc/rsyncd #創建配置目錄
touch /etc/rsyncd/rsyncd.conf #創建主配置文件
chmod 600 /etc/rsyncd/rsyncd.conf #修改用戶文件權限
編輯配置文件
vi /etc/rsyncd/rsyncd.conf
內容如下
設置需要同步的目錄改成自己需要進行同步的,我這裏是 /home/filetest
#pid文件的存放位置
pid file = /var/run/rsync.pid
#日誌文件位置,啓動rsync後自動產生這個文件,無需提前創建
log file = /var/log/rsync.log
#支持max connections參數的鎖文件
lock file=/var/run/rsync.lock
#rsync啓動時歡迎信息頁面文件位置
motd file = /etc/rsyncd/rsyncd.motd
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
#自定義名稱
[filetest]
#設置需要同步的目錄
path = /var/filetest/
#模塊名稱與[case]自定義名稱相同
comment = filetest
#默認端口
port = 873
#設置rsync運行權限爲root
uid = root
#設置rsync運行權限爲root
gid = root
#設置超時時間
timeout = 600
#最大連接數
max connections = 200
#默認爲true,修改爲no,增加對目錄文件軟連接的備份
use chroot = no
#設置rsync服務端文件爲讀寫權限
read only = no
#不顯示rsync服務端資源列表
list = no
(2)CentOS 默認以 xinetd 方式運行 rsync 服務。rsync 的 xinetd 配置文件在 /etc/xinetd.d/rsync。
要配置以 xinetd 運行的 rsync 服務需要執行如下的命令:
chkconfig rsync on
vim /etc/xinetd.d/rsync
內容如下
service rsync
{
disable=no
socket_type=stream
wait=no
user=root
server=/usr/bin/rsync
server_args=--daemon --config=/etc/rsyncd/rsyncd.conf
log_on_failure += USERID
}
(3)配置rsyncd.motd,開始傳送的時候會顯示(備份節點)
vi /etc/rsyncd.motd
內容如下
###############################
# Hello my owner, file is tranfering.
###############################
(4)啓動服務
service xinetd restart
然後如果你是用root來創建的需要同步的目錄(我這裏是/home/filetest
)則需要修改該目錄的權限,因爲我們在同步時是使用剛纔創建的user-rsync
用戶,這樣我們在同步時會出現權限不足的問題
cd /home/filetest
chown -R user-rsync .
6.主服務器測試
然後我在主服務 /home
目錄下也新建了一個filetest
文件夾
在主服務器的文件夾 /home/filetest
文件夾中增加文件 test.txt 。
touch /home/filetest/test.txt
測試同步文件到同步服務器
當然文件夾位置可以自己指定
rsync -avH --port=873 --progress --delete /home/filetest/ user-rsync@xx.xx.xx.xx:/home/filetest
檢查下是否真的傳過去了
7.配置 inotify-tools
當然我們實際使用時不可能上傳了一個文件然後還要手動運行rsync命令,都是需要實時自動同步的,這時候就需要 inotify-tools了。
主服務器
(1)修改內核參數(源節點)
vi /etc/sysctl.conf
修改參數,添加
:
# inotify queue max length
fs.inotify.max_queued_events=99999999
# includ file directory
fs.inotify.max_user_watches=99999999
# user create max instances
fs.inotify.max_user_instances=65535
(2)編寫監控腳本 inotify.sh
mkdir /usr/local/shell
vi /usr/local/shell/inotify.sh
內容如下
/home/inotify/bin/inotifywait爲之前安裝的 inotify-tools位置
#!/bin/bash
#源服務器同步目錄
src=/home/filetest/
#目標服務器rsync同步目錄模塊名稱
dst=/home/filetest/
/home/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T%w%f%e' -e modify,create,delete,attrib $src | while read file
do
rsync -avH --port=873 --progress --delete $src [email protected]:$dst
done
設置腳本權限並啓動腳本(源節點)
chmod 777 /usr/local/shell/inotify.sh
設置開機啓動(源節點)
echo "/usr/local/shell/inotify.sh &">>/etc/rc.local
重啓主服務器後測試(當然也可以不重啓,這裏重啓只是檢驗下設置的開機啓動是否有效)
先把之前傳到同步服務器上的test.txt刪除
然後主服務器執行
touch /home/filetest/test.txt
再查看下同步服務器上是否同步到了該文件,以後只要往主服務器上的/home/filetest中放文件/修改文件/刪除文件都會實時自動同步到同步服務器。我們不需要做其他任何操作。