軟件包下載:
鏈接: https://pan.baidu.com/s/1X_wtM04yNMJfnEtV1EcBfg 密碼: 9nkj
環境規劃:
HSH-file02作爲主服務器,HSH-search01作爲備服務器(主服務器上添加/修改/刪除文件後將會同步給備服務器)
HSH-file02 192.168.164.148 /home/helife/hsh-nfs/publish
HSH-search01 192.168.164.186 /home/helife/hsh-nfs/publish
[root@HSH-file02 ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.3 (Santiago)
1.主服務器檢查rsync是否已經安裝
rpm -qa | grep rsync
如果沒有安裝則進行安裝
yum -y install rsync
[root@HSH-file02 ~]# rpm -qa | grep rsync
rsync-3.0.6-9.el6.x86_64
2.主服務器檢查 xinetd 是否安裝
rpm -qa | grep xinetd
如果沒有安裝則進行安裝
yum -y install xinetd
[root@HSH-file02 ~]# rpm -qa | grep xinetd
xinetd-2.3.14-34.el6.x86_64
3.主服務器安裝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
4.信任連接配置
登入備服務器
新建用戶helife, 密碼隨意
useradd -m helife
passwd helife
然後登入主服務器helife用戶,生成祕鑰文件,注意別漏掉最後的''
ssh-keygen -t rsa -P ''
[helife@HSH-file02 .ssh]$ pwd
/home/helife/.ssh
[helife@HSH-file02 .ssh]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/helife/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/helife/.ssh/id_rsa.
Your public key has been saved in /home/helife/.ssh/id_rsa.pub.
The key fingerprint is:
84:fa:89:25:ad:d3:71:a3:19:92:05:df:56:10:cc:49 helife@HSH-file02
The key's randomart image is:
+--[ RSA 2048]----+
| . +E+. |
| o o+. |
| + + |
| = o |
| = = S |
| O B . |
| + * |
| . |
| |
+-----------------+
[helife@HSH-file02 .ssh]$ ll
total 12
-rw------- 1 helife users 1671 Feb 21 17:38 id_rsa
-rw-r--r-- 1 helife users 399 Feb 21 17:38 id_rsa.pub
-rw-r--r-- 1 helife users 802 Jun 1 2017 known_hosts
然後把生成的id_rsa.pub拷貝到備服務器helife用戶/home/helife/.ssh目錄
[helife@HSH-file02 .ssh]$ scp -P 10022 id_rsa.pub [email protected]:/home/helife/.ssh
備服務器helife用戶把祕鑰文件id_rsa.pub輸入到authorized_keys中
[helife@HSH-search01 .ssh]$ cat id_rsa.pub >> authorized_keys
[helife@HSH-search01 .ssh]$ chmod 600 /home/helife/.ssh/authorized_keys
[helife@HSH-search01 .ssh]$ ll
total 12
-rw-------. 1 helife users 399 Feb 21 17:51 authorized_keys
-rw-r--r--. 1 helife users 399 Feb 21 17:47 id_rsa.pub
-rw-r--r--. 1 helife users 405 Dec 11 2017 known_hosts
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/helife/hsh-nfs/publish
#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
#自定義名稱
[publish]
#設置需要同步的目錄
path = /home/helife/hsh-nfs/publish/
#模塊名稱與[case]自定義名稱相同
comment = publish
#默認端口
port = 873
#設置rsync運行權限(例如root)
uid = helife
#設置rsync運行權限(例如root)
gid = users
#設置超時時間
timeout = 600
#最大連接數
max connections = 200
#默認爲true,修改爲no,增加對目錄文件軟連接的備份
use chroot = no
#設置rsync服務端文件爲讀寫權限
read only = no
#不顯示rsync服務端資源列表
list = no
(2)默認以 xinetd 方式運行 rsync 服務。rsync 的 xinetd 配置文件在 /etc/xinetd.d/rsync。
要配置以 xinetd 運行的 rsync 服務需要執行如下的命令:
chkconfig rsync on
vim /etc/xinetd.d/rsync
主服務器,內容如下
[root@HSH-file02 ~]# 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
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
備服務器,內容如下
[root@HSH-search01 ~]# 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 = no
flags = IPv6
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/rsyncd.motd
內容如下
#########################################
# Hello my owner, file is tranfering.
#########################################
(4)主、備服務器啓動服務
service xinetd restart
然後如果你是用root來創建的需要同步的目錄(我這裏是/home/helife/hsh-nfs/publish/)則需要修改該目錄的權限,
因爲我們在同步時是使用剛纔創建的helife用戶,這樣我們在同步時會出現權限不足的問題
cd /home/helife/hsh-nfs/publish/
chown -R helife .
6.主服務器測試
測試同步文件到備服務器
rsync -avH --port=873 --progress --delete --exclude-from=download -e 'ssh -p 10022' /home/helife/hsh-nfs/publish/404.html [email protected]:/home/helife/hsh-nfs/publish
[helife@HSH-file02 publish]$ rsync -avH --port=873 --progress --delete --exclude-from=download -e 'ssh -p 10022' /home/helife/hsh-nfs/publish/404.html [email protected]:/home/helife/hsh-nfs/publish
sending incremental file list
404.html
1856 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 1951 bytes received 31 bytes 1321.33 bytes/sec
total size is 1856 speedup is 0.94
備服務器上,檢查下是否真的傳過去了
[root@HSH-search01 publish]# ll
total 4
-rwxr-xr-x. 1 helife users 1856 Sep 1 2017 404.html
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
vi /home/inotify/inotify.sh
內容如下
/home/inotify/bin/inotifywait爲之前安裝的 inotify-tools位置
#!/bin/bash
#源服務器同步目錄
src=/home/helife/hsh-nfs/publish/
#目標服務器rsync同步目錄模塊名稱
dst=/home/helife/hsh-nfs/publish/
#源服務器不需要同步的目錄
excludedir=/home/inotify/exclude.list
/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 --exclude-from=$excludedir -e 'ssh -p 10022' $src [email protected]:$dst
done
設置腳本權限並啓動腳本(源節點)
chmod 755 /home/inotify/inotify.sh
運行inotify.sh腳本
nohup /home/inotify/inotify.sh &
設置開機啓動(源節點)
echo "/home/inotify/inotify.sh &">>/etc/rc.local
再查看下備服務器上是否同步到了該文件,以後只要往主服務器上的/home/helife/hsh-nfs/publish中放文件/修改文件/刪除文件都會實時自動同步到備服務器。