1. 程式人生 > >Rsync+sersync實現文件實時備份

Rsync+sersync實現文件實時備份

execute ret 技術分享 存放位置 find a-z 顯示 googl dport

第一部分:在目標服務器192.168.0.217上操作
一、在OA文件備份服務器安裝Rsync服務端
1、關閉SELINUX
vi /etc/selinux/config #編輯防火墻配置文件
代碼如下:

#SELINUX=enforcing #註釋掉
#SELINUXTYPE=targeted #註釋掉
SELINUX=disabled #增加
:wq! #保存,退出

setenforce 0 #立即生效

2、開啟防火墻tcp 873端口(Rsync默認端口)

3、安裝Rsync服務端軟件
yum install rsync xinetd #安裝
vi /etc/xinetd.d/rsync #編輯配置文件,設置開機啟動rsync(7版本服務器可不配置)

代碼如下:

service rsync

{

disable = no

flags = IPv6

socket_type = stream

wait = no

user = root

server = /usr/bin/rsync

server_args = --daemo
n
log_on_failure += USERID

}

:wq! #保存退出

/etc/init.d/xinetd start #啟動(CentOS中是以xinetd來管理Rsync服務的)

4.配置rsync,rsync的配置文件是/etc/rsyncd.conf,配置如下:
5.
vi /etc/rsyncd.conf #創建配置文件,添加以下代碼

代碼如下:

log file = /var/log/rsyncd.log #日誌文件位置,啟動rsync後自動產生這個文件,無需提前創建
pidfile = /var/run/rsyncd.pid #pid文件的存放位置
lock file = /var/run/rsync.lock #支持max connections參數的鎖文件
secrets file = /etc/rsync.pass #用戶認證配置文件,裏面保存用戶名稱和密碼,後面會創建這個文件
motd file = /etc/rsyncd.Motd #rsync啟動時歡迎信息頁面文件位置(文件內容自定義)
uid = root #設置rsync運行權限為root
gid = root #設置rsync運行權限為root
port=873 #默認端口
use chroot = no #默認為true,修改為no,增加對目錄文件軟連接的備份
[Seeyon] #自定義名稱
path = /home/Seeyon/ #rsync服務端數據目錄路徑
comment = Seeyon #模塊名稱與[Seeyon]自定義名稱相同
ignore errors  #忽略錯誤

read only = no #設置rsync服務端文件為讀寫權限
list = no #不顯示rsync服務端資源列表
max connections = 200 #最大連接數
timeout = 600 #設置超時時間
auth users = root #執行數據同步的用戶名,可以設置多個,用英文狀態下逗號隔開
hosts allow = * #允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開(*代表任意地址)
#hosts deny = X.X.X.X #禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開

:wq! #保存,退出

上方需要註意的地方:secrets file這個是配置同步的密碼文件的。[Seeyon]這個是配置同步模塊的名稱,path是配置同步的目錄,hosts allow是允許同步的主機,hosts deny:拒絕同步的主機

5.創建同步的用戶與密碼的文件,既第4步的secrets file這個配置選項中的文件。/etc/rsync.passwd,同進要設置這個文件的權限為600

echo "root:baidu.c0m" >> /etc/rsync.pass
chmod 600 /etc/rsync.pass

6.創建同步的目錄:既第4步的path配置選項中的目錄。
mkdir /home/Seeyon

7、啟動rsync
復制代碼
rsync --daemon --config=/etc/rsyncd.conf (後臺運行rsync)
接著重啟一下xinetd

systemctl restart xinetd (重起)
systemctl stop xinetd(停止)
systemctl start xinetd(啟動)

8.查看是否啟動成功
netstat –anutp | grep 873
出現如下數據則為啟動成功:
技術分享圖片

9.設置開機啟動
(方法一)
echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local

(方法二)
/usr/bin/rsync –daemon –config=/etc/rsyncd.conf

第二部分:在源服務器192.168.0.218上操作

一、安裝Rsync客戶端

1、關閉SELINUX
vi /etc/selinux/config #編輯防火墻配置文件

代碼如下:

#SELINUX=enforcing #註釋掉
#SELINUXTYPE=targeted #註釋掉
SELINUX=disabled #增加
:wq! 保存退出

setenforce 0 立即生效

2、開啟防火墻tcp 873端口(Rsync默認端口,做為客戶端的Rsync可以不用開啟873端口)

3、安裝Rsync客戶端端軟件

代碼如下:

whereis rsync #查看系統是否已安裝rsync,出現下面的提示,說明已經安裝
rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz

yum install xinetd #只安裝xinetd即可,RHEL7中是以xinetd來管理rsync服務的

yum install rsync xinetd #如果默認沒有rsync,運行此命令進行安裝rsync和xinetd

vi /etc/xinetd.d/rsync #編輯配置文件,設置開機啟動rsync

代碼如下:

service rsync

{

disable = no

flags = IPv6

socket_type = stream

wait = no

user = root

server = /usr/bin/rsync

server_args = --daemo
n
log_on_failure += USERID

}

3.1#啟動(RHEL7中是以xinetd來管理rsync服務的)
systemctl start xinetd

4、創建密碼文件,同OA文件備份服務器一樣,不過這個文件只要保存一個密碼就行了,不用用戶名,權限也是600
vi /etc/rsync.passwd #編輯文件,添加以下內容

代碼如下:

baidu.c0m #密碼
:wq! 保存退出
chmod 600 /etc/rsync.passwd #設置文件權限,只設置文件所有者具有讀取、寫入權限即可

5、測試是否能夠連接OA備份服務器
rsync –a [email protected]:: (結果如下圖則為成功)

技術分享圖片

二、安裝sersync工具,實時觸發rsync進行同步
1、查看服務器內核是否支持inotify
ll /proc/sys/fs/inotify #列出文件目錄,出現下面的內容,說明服務器內核支持inotify(無需復制代碼進行制作)

代碼如下:
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
備註:Linux下支持inotify的內核最小為2.6.13,可以輸入命令:uname -a查看內核
CentOS 5.X 內核為2.6.18,默認已經支持inotify

2、修改inotify默認參數(inotify默認內核參數值太小)

查看系統默認參數值:
sysctl -a | grep max_queued_events
結果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
結果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
結果是:fs.inotify.max_user_instances = 128

修改參數:
代碼如下:

sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"

參數說明:
max_queued_events:
inotify隊列最大長度,如果值太小,會出現" Event Queue Overflow "錯誤,導致監控文件不準確
max_user_watches:
要同步的文件包含多少目錄,可以用:find /home/Seeyon -type d | wc -l 統計,必須保證max_user_watches值大於統計結果(這裏/home/Seeyon為同步文件目錄)
max_user_instances:
每個用戶創建inotify實例最大值

3、安裝sersync
sersync下載地址:https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

mkdir /usr/local/sersync
mkdir /usr/local/sersync/conf
mkdir /usr/local/sersync/bin
mkdir /usr/local/sersync/log
tar zxvf sersync2.5_32bit_binary_stable_final.tar.gz
cd GNU-Linux-x86/
cp confxml.xml /usr/local/sersync/conf
cp sersync2 /usr/local/sersync/bin

4、配置sersync

代碼如下:
vi confxml.xml 編輯,修改下面的代碼

代碼如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
   #設置本地IP和端口
   <host hostip="localhost" port="8008"></host>
   #開啟DUBUG模式  
   <debug start="false"/>
   #開啟xfs文件系統
   <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="/home/Seeyon">
   # 遠程IP和rsync模塊名  
           <remote ip="192.168.0.217" name="Seeyon"/>  
           <!--<remote ip="192.168.8.39" name="tongbu"/>-->
           <!--<remote ip="192.168.8.40" name="tongbu"/>-->
       </localpath>
       <rsync>
   #rsync指令參數
           <commonParams params="-auvzP"/>
   #rsync同步認證
           <auth start="true" users="root" passwordfile="/etc/rsync.passwd"/>
   #設置rsync遠程服務端口,遠程非默認端口則需打開自定義
           <userDefinedPort start="false" port="873"/><!-- port=873 -->
   #設置超時時間
           <timeout start="true" time="100"/><!-- timeout=100 -->
   #設置rsync+ssh加密傳輸模式,默認關閉,開啟需設置SSH加密證書
           <ssh start="false"/>
       </rsync>
    #sersync傳輸失敗日誌腳本路徑,每隔60會重新執行該腳本,執行完畢會自動清空。
       <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
    #設置rsync+crontab定時傳輸,默認關閉
       <crontab start="false" schedule="600"><!--600mins-->
           <crontabfilter start="false">
               <exclude expression="*.php"></exclude>
               <exclude expression="info/*"></exclude>
           </crontabfilter>
       </crontab>
   #設置sersync傳輸後調用name指定的插件腳本,默認關閉
       <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="/home/Seeyon.":#源服務器同步目錄
192.168.0.217:#目標服務器IP地址
name="Seeyon": #目標服務器rsync同步目錄模塊名稱
users="root": #目標服務器rsync同步用戶名
passwordfile="/etc/rsync.passwd": #目標服務器rsync同步用戶的密碼在源服務器的存放路徑
remote ip="192.168.0.217": #目標服務器ip,每行一個
failLog path="/tmp/rsync_fail_log.sh" #腳本運行失敗日誌記錄
start="true" #設置為true,每隔600分鐘執行一次全盤同步

  1. 設置環境變量:
    #echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
    #source /etc/profile

    7..啟動sersync
    sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
    註:重啟操作如下:
    killall sersync2 && sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml

8、設置sersync監控開機自動執行
vi /etc/rc.d/rc.local #編輯,在最後添加一行

代碼如下:
/usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml #設置開機自動運行腳本
:wq! #保存退出
9、添加腳本監控sersync是否正常運行
vi /home/crontab/check_sersync.sh 編輯,添加以下代碼

代碼如下:

#!/bin/sh
sersync="/usr/local/sersync/bin/sersync2"
confxml="/usr/local/sersync/conf/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! #保存退出
chmod +x /home/crontab/check_sersync.sh #添加腳本執行權限
vi /etc/crontab #編輯,在最後添加下面一行

*/1 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1 #每隔1分鐘執行一次腳本
service crond reload #重新加載服務

Rsync+sersync實現文件實時備份