目的
在192.168.50.23與192.168.50.22之間同步/root/test文件夾
安裝xinetd
yum install xinetd
安裝rsync
yum -y install rsync
配置rsync
因為我們是雙向同步,所以每台主機都是服務器,也都是主機,所以rsyncd的配置基本上是相同的
在CentOS中,可能會沒有/etc/rsyncd.conf文件,需要手動創建
配置內容例子
log file = /var/log/rsyncd.log
lock file = /var/run/rsync.lock
motd file = /etc/rsyncd.Motd
port = 873
[rsynctest]
path = /root/test
uid = root
gid = root
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = rsync
secrets file = /etc/rsync.pas
hosts allow = *
# hosts deny = 0.0.0.0
[rsynctest]為模塊名
path為文件目錄
max connections為最大連接數
auth users為用戶名
secrets file為儲存用戶名和密碼的文件
hosts allow為允許連接的IP,多個用逗號分隔
hosts deny為不允許連接的IP,多個用逗號分隔
/etc/rsync.pas中格式為
username:password
另外需要創建一個只儲存密碼的文件,如/etc/rsync1.pas
password
注意:/etc/rsync.pas和etc/rsync1.pas的權限必須為600,不然會出錯,而且,如果在root下執行操作,這兩個文件的owner必須為root
配置xinetd
我們使用xinetd來管理rsync服務,也可以設置xinetd開機啟動,從而使rsync也開機啟動
vi /etc/xinetd.d/rsync
找到disable = yes這一行,改為disable = no
重啟xinetd
/etc/init.d/xinetd restart
設置開機啟動rsync的daemon模式
echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local
其實這里配置xinetd開機啟動就行。。。
安裝sersync
這里用的是github上的一個備份,不過不確定是不是最新的(本文寫完的時候還是最新的)
wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
如果可以科學上網,則可去 https://code.google.com/archive/p/sersync/ 下載
mkdir /usr/local/sersync
mkdir /usr/local/sersync/conf
mkdir /usr/local/sersync/bin
mkdir /usr/local/sersync/log
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
cd GNU-Linux-x86/
cp confxml.xml /usr/local/sersync/conf
cp sersync2 /usr/local/sersync/bin
chmod 777 /usr/local/sersync/bin/sersync2
當然sersync2和confxml.xml,放哪里都可以,記得設置環境變量就行
配置sersync
以192.168.50.22為例打開confxml.xml文件,然后配置
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host><!-- 設置本地IP和端口 -->
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false"><!-- 同步時忽略推送的文件(正則表達式),默認關閉 -->
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify><!-- 設置要監控的事件 -->
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/root/test"><!-- 同步文件路徑 -->
<remote ip="192.168.50.23" name="rsynctest"/><!-- 要同步的主機IP和模塊名 -->
</localpath>
<rsync>
<commonParams params="-auvzL"/><!-- rsync指令參數 -->
<auth start="true" users="rsync" passwordfile="/etc/rsync1.pas"/><!-- 用戶名和只儲存密碼的密保文件 -->
<userDefinedPort start="false" port="874"/><!-- 設置rsync遠程服務端口,遠程非默認端口則需打開自定義 -->
<timeout start="true" time="100"/><!-- 設置超時時間 -->
<ssh start="false"/><!-- 設置rsync+ssh加密傳輸模式,默認關閉,開啟需設置SSH加密證書 -->
</rsync>
<failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!-- sersync傳輸失敗日志腳本路徑,每隔60分鍾會重新執行該腳本,執行完畢會自動清空 -->
<crontab start="false" schedule="600"><!-- 設置rsync+crontab定時傳輸,默認關閉 -->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/><!-- 設置sersync傳輸后調用name指定的插件腳本,默認關閉 -->
</sersync>
<plugin name="command"><!-- 插件腳本范例,等於沒用 -->
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket"><!-- 插件腳本范例,等於沒用 -->
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN"><!-- 插件腳本范例,等於沒用 -->
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
設置環境變量
echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
source /etc/profile
設置sersync開機自動啟動
編輯/etc/rc.d/rc.local
vim /etc/rc.d/rc.local
最后一行添加
/usr/local/sersync/bin/sersync2 -d -r -o /etc/sersync/conf/confxml.xml
啟動rsync
rsync --daemon --config=/etc/rsyncd.conf
啟動sersync
sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml
定時檢測維護sersync腳本
創建/root/start_sersync.sh文件
. /etc/profile
FolderPath="/etc/sersync/conf/"
for fileName in ${FolderPath}/*.xml; do
tempFile=`basename $fileName`
/etc/sersync/bin/sersync2 -r -d -o $FolderPath$tempFile
done
創建/root/keep_sersync.sh文件
#!/bin/sh
. /etc/profile
status=$(ps aux | grep 'sersync2' | grep -v 'grep' | wc -l)
sersync_path="/etc/sersync/bin/sersync2"
xml_path="/etc/sersync/conf/"
i=0
for fileName in ${xml_path}/*.xml; do
i=$[$i+1]
done
if [ $status -ne $i ];then
killall sersync2
/root/start_sersync.sh
fi
這里在腳本中執行其他腳本或非系統默認的應用的時候,切記要用絕對路徑,不然,在crond執行腳本會失敗。
給與/root/keep_sersync.sh和/root/start_sersync.sh可執行權限
chmod 777 /root/keep_sersync.sh
chmod 777 /root/start_sersync.sh
ps -ef | grep crond
查看crond有沒有啟動,沒有啟動則
crond
設置crond開機啟動
chkconfig --level 35 crond on
編輯/etc/crontab,加上
*/10 * * * * root /root/keep_sersync.sh > /dev/null 2>&1
這里是每10分鍾調用一次/root/keep_sersync.sh腳本,時間可以自己定義,下面是格式,更多語法自行百度。
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
重啟或者重載crond
/etc/init.d/crond restart
或
/etc/init.d/crond reload
其他
重啟sersync
killall sersync2 && sersync2 -r -d -o /etc/sersync/conf/confxml.xml
查看同步日志
tailf /var/log/rsyncd.log
多服務器同步
如果要同步多台主機,則每台主機上需要創建對應其他每台主機的sersync的配置文件,且sersync需要建立同樣數量相對應的進程。
例如,要實現三台主機之間互相同步,則要在每台上都創建對應其他兩台主機的配置文件,以其中一台為例:
創建/usr/local/sersync/conf/confxml1.xml、/usr/local/sersync/conf/confxml2.xml文件
執行
sersync2 -d -r -o /usr/local/sersync/conf/confxml1.xml
sersync2 -d -r -o /usr/local/sersync/conf/confxml2.xml
同步多個模塊也差不多