CentOS 6.5 Rsync+sersync 實現數據實時同步備份

  1. 爲何要用rsync+sersync架構php

  2. Rsync+sersynclinux

       (1):sersync能夠記錄下被監聽目錄中發生變化的(包括增長、刪除、修改)具體某一個文件或某一個目錄的名字;web

       (2):rsync在同步的時候,只同步發生變化的這個文件或者這個目錄(每次發生變化的數據相對整個同步目錄數據來講是很小的,rsync在遍歷查找比對文件時,速度很快),所以,效率很高。shell

  1. 小結:當同步的目錄數據量不大時,建議使用Rsync+Inotify-tools;當數據量很大(幾百G甚至1T以上)、文件不少時,建議使用Rsync+sersync。express

說明centos

  操做系統:CentOS 6.5服務器

  源服務器:121.41.28.253架構

  目標服務器:121.41.48.242,121.41.48.233dom

  目的:把源服務器上/var/www/synctest目錄實時同步到目標服務器的/var/www/synctest下ssh

   具體操做

                         第一部分:在兩臺目標服務器121.41.48.242和121.41.48.233執行的操做

1、在目標服務器安裝rsync服務端

一、關閉SELINUX
編輯防火牆配置文件:

[root@iZ23tvhxkkjZ /]# vi /etc/selinux/config

用 # 註釋掉下面兩行代碼:  

#SELINUX=enforcing
#SELINUXTYPE=targeted

增長下面一行代碼:

SELINUX=disabled

保存退出   

wq!

運行命令行,當即生效:

[root@iZ23tvhxkkjZ /]# setenforce 0

二、開啓防火牆tcp 873端口(rsync默認端口)

   編輯防火牆配置文件:

[root@iZ23tvhxkkjZ /]# vi /etc/sysconfig/iptables

  添加下列行到開啓22端口的後面:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

   保存,退出:             

wq!

重啓防火牆使配置生效:

[root@iZ23tvhxkkjZ /]# /etc/init.d/iptables restart

三、安裝rsync服務端軟件

安裝rsync,CentOS6.5自帶rsync,運行下列命令行,確保rsync安裝,同時安裝rsync工具xinetd

[root@iZ23tvhxkkjZ /]# yum install rsync xinetd

編輯配置文件,設置開機啓動 rsync

[root@iZ23tvhxkkjZ /]# vi /etc/xinetd.d/rsync

  修改disable的值爲no:

disable = no

   保存,退出:             

wq!

啓動xinetd(CentOS中是以xinetd來管理Rsync服務的)

[root@iZ23tvhxkkjZ /]# /etc/init.d/xinetd start

四、建立rsyncd.conf配置文件

建立配置文件:

[root@iZ23tvhxkkjZ /]# vi /etc/rsyncd.conf

添加如下代碼

log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsyncd.passwd
motd file = /etc/rsyncd.motd

[synctest]
path = /var/www/synctest/
comment = synctest
uid = root
gid = root
port=873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = root
hosts allow = 121.41.28.253
hosts deny = 121.41.28.254

5.建立用戶認證文件

配置文件

[root@iZ23tvhxkkjZ /]# vi /etc/rsyncd.passwd

添加如下內容(原服務器的用戶名和密碼)

root:123456

格式 用戶名:密碼  能夠設置多個,每行一個 用戶名:密碼 對

保存退出

:wq!

6.設置文件權限

  設置文件全部者讀取、寫入權限

[root@iZ23tvhxkkjZ etc]# chmod 600 /etc/rsyncd.conf
[root@iZ23tvhxkkjZ etc]# chmod 600 /etc/rsyncd.passwd

7.啓動rsync

啓動

[root@iZ23tvhxkkjZ etc]# /etc/init.d/xinetd start

參考指令

中止:service xinetd stop
啓動:service xinetd restart

[目標服務安裝完成 另一臺目標服務器也一樣的方法]

第二部分:在源服務器上121.41.28.253操做

如下部分在源服務器(發佈服務器)上進行操做

1、安裝rsync客戶端

一、關閉selinux

編輯防火牆配置文件

[root@iZ23i2txh8jZ /]# vi /etc/selinux/config

用#註釋掉下面兩行代碼:

#SELINUX=enforcing
#SELINUXTYPE=targeted

增長下面一行代碼

SELINUX=disabled

保存 退出

:wq!

運行命令,當即生效

[root@iZ23i2txh8jZ /]# setenforce 0

二、開啓防火牆tcp873端口

  配置防火牆

[root@iZ23i2txh8jZ /]# vi /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

保存,退出 重啓防火牆

:wq!
[root@iZ23i2txh8jZ /]# /etc/init.d/iptables restart

3.安裝rsync centos6.5自帶rsync 運行下列命令後 確保rsync安裝 同時安裝rsync工具xinetd

[root@iZ23i2txh8jZ /]# yum install rsync xinetd

編輯配置文件,設置開機啓動rsync

[root@iZ23i2txh8jZ /]# vi /etc/xinetd.d/rsync
disable = no
wq!

啓動xinetd(centos中是以xinet來管理rsync服務的)

[root@iZ23i2txh8jZ /]# /etc/init.d/xinetd start

4.建立認證密碼文件

   編輯文件

[root@iZ23i2txh8jZ /]# vi /etc/rsyncd.passwd

添加如下內容做爲密碼

123456
保存退出
:wq!

設置文件權限,只設置文件全部者具備讀取、寫入權限

[root@iZ23i2txh8jZ /]# chmod 600 /etc/rsyncd.passwd

5.測試源服務器到兩臺服務器之間的數據同步

  在源服務器上建立測試文件夾

[root@iZ23i2txh8jZ www]# mkdir /var/www/synctest
[root@iZ23i2txh8jZ www]# mkdir /var/www/synctest/test

在源服務器上運行下面命令

rsync -avH --port=873 --progress --delete /var/www/synctest/ root@121.41.48.242::synctest --password-file=/etc/rsyncd.passwd

運行完成後,分別在兩臺目標服務器上查看,在/var/www/synctest目錄下有test文件夾,說明數據同步成功。

『以上手動同步功能實現完成。下面內容爲使用sersync工具監控目錄,完成自動同步』

   2、安裝sersync工具、實時出發rsync進行同步

一、查看服務器內核是否支持inotify

列出文件目錄

[root@iZ23i2txh8jZ /]# ll /proc/sys/fs/inotify

出現下面的內容、說明服務器內核支持inotify

-rw-r--r-- 1 root root 0 Dec 25 12:03 max_queued_events
-rw-r--r-- 1 root root 0 Dec 25 15:05 max_user_instances
-rw-r--r-- 1 root root 0 Dec 25 12:03 max_user_watches

備註:centos6.5默認支持inotify

2.修改inotify默認參數(inotify默認內核參數值過小)

[root@iZ23i2txh8jZ /]# sysctl -a | grep max_queued_events
fs.inotify.max_queued_events = 327679
[root@iZ23i2txh8jZ /]#
[root@iZ23i2txh8jZ /]# sysctl -a | grep max_user_watches
fs.inotify.max_user_watches = 50000000
fs.epoll.max_user_watches = 1646100
[root@iZ23i2txh8jZ /]#
[root@iZ23i2txh8jZ /]# sysctl -a | grep max_user_instances
fs.inotify.max_user_instances = 128
[root@iZ23i2txh8jZ /]#

修改參數

[root@iZ23i2txh8jZ /]# sysctl -w fs.inotify.max_queued_events="99999999"
[root@iZ23i2txh8jZ /]# sysctl -w fs.inotify.max_user_watches="99999999"
[root@iZ23i2txh8jZ /]# sysctl -w fs.inotify.max_user_instances="65535"

參數說明:

max_queued_events:

inotify隊列最大長度,若是值過小,會出現」 Event Queue Overflow 「錯誤,致使監控文件不許確

max_user_watches:

要同步的文件包含多少目錄,能夠用:find /var/www/synctest -type d | wc -l 統計,必須保證max_user_watches值大於統計結果(這裏/var/www/synctest爲同步文件目錄)

max_user_instances:

每一個用戶建立inotify實例最大值

三、安裝sersync

sersync下載地址(很差用):https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

將sersync2.5.4_64bit_binary_stable_final.tar.gz複製到/usr/local/src目錄下。

[root@iZ23i2txh8jZ /]# cd /usr/local/src
[root@iZ23i2txh8jZ src]# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@iZ23i2txh8jZ src]# mv GNU-Linux-x86 /usr/local/sersync
[root@iZ23i2txh8jZ src]# cd /usr/local/sersync

四、配置sersync

[root@iZ23i2txh8jZ sersync]# cp confxml.xml confxml.xml-bak
[root@iZ23i2txh8jZ sersync]# vi confxml.xml

修改代碼

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
	<host hostip="localhost" port="8008"></host>
	<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="false"/>
		<closeWrite start="true"/>
		<moveFrom start="true"/>
		<moveTo start="true"/>
		<attrib start="false"/>
		<modify start="false"/>
	</inotify>

	<sersync>
		<localpath watch="/var/www/synctest">
			<remote ip="121.41.48.242" name="synctest"/>
			<remote ip="121.41.48.233" name="synctest"/>
			<!--<remote ip="192.168.8.40" name="tongbu"/>-->
		</localpath>
		<rsync>
			<commonParams params="-artuz"/>
			<auth start="true" users="root" passwordfile="/etc/rsyncd.passwd"/>
			<userDefinedPort start="false" port="874"/><!-- port=874 -->
			<timeout start="false" time="100"/><!-- timeout=100 -->
			<ssh start="false"/>
		</rsync>
		<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
		<crontab start="true" schedule="600"><!--600mins-->
			<crontabfilter start="false">
				<exclude expression="*.php"></exclude>
				<exclude expression="info/*"></exclude>
			</crontabfilter>
		</crontab>
		<plugin start="false" name="command"/>
	</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>

保存、退出

:wq!

參數說明:

localpath watch=」/var/www/synctest」:#源服務器同步目錄

121.41.48.242,121.41.48.233:#目標服務器IP地址

name=」synctest」: #目標服務器rsync同步目錄模塊名稱

users=」root」: #目標服務器rsync同步用戶名

passwordfile=」/etc/rsyncd.passwd」: #目標服務器rsync同步用戶的密碼在源服務器的存放路徑

remote ip=」121.41.48.242」: #目標服務器ip,每行一個

remote ip=」121.41.48.233」: #目標服務器ip,每行一個

failLog path=」/tmp/rsync_fail_log.sh」 #腳本運行失敗日誌記錄

start=」true」 #設置爲true,每隔600分鐘執行一次全盤同步

五、設置sersync監控開機自動執行

編輯

[root@iZ23i2txh8jZ sersync]# vi /etc/rc.d/rc.local
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
:wq!

六、添加腳本監控sersync是否正常運行

[root@iZ23i2txh8jZ crontab]# vi /home/crontab/check_sersync.sh
#!/bin/sh
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi

:wq!

添加腳本執行權限

[root@iZ23i2txh8jZ crontab]# chmod +x /home/crontab/check_sersync.sh

編輯

[root@iZ23i2txh8jZ crontab]# vi /etc/crontab
#在最後添加下面一行,每隔5分鐘執行一次腳本
*/5 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1

從新加載服務

[root@iZ23i2txh8jZ crontab]# service crond reload

七、測試

在源服務器上建立文件

mkdir /var/www/synctest/inotify_rsync_ceshi
mkdir /var/www/synctest/inotify_rsync_ceshi_new

繼續查看兩臺目標服務器121.41.48.242,121.41.48.233的/var/www/synctest下是否有inotify_rsync_ceshi_new文件夾

若是以上測試都經過,說明inotify實時觸發rsync同步腳本運行正常。

至此,Linux下Rsync+sersync實現數據實時同步完成。

相關文章
相關標籤/搜索