1. 程式人生 > >rsync+inotify文件同步

rsync+inotify文件同步

.sh bsp exclude 目錄權限 可靠 oot 去掉 腳本 可用

rsync+inotify文件同步

在服務器中,通常結合計劃任務、shell腳本來執行本地備份。為了進一步提高備份的可靠性,使用異地備份也是非常重要的,利用rsync工具,可以實現快速、高效的異地備份。本篇博客將配置rsync+crond實現定時備份、配置ssh+rsync+inotify實現觸發式備份

rsync概述

rsync(Remote Sync,遠程同步)是一個開源的快速備份工具,適用於異地備份、鏡像服務器等。作為一種最常用的文件備份工具,往往是Linux和UNIX系統默認安裝的基本組件之一

具有以下特性:

(1)可以在不同主機間鏡像同步整個目錄樹和文件系統

(2)可以很容易做到保持原來文件的權限、時間、軟硬鏈接等

(3)支持增量備份

(3)無須特殊權限即可安裝

(4)優化的同步算法,傳輸前執行壓縮,文件傳輸效率高

(5)可以使用 rsh、ssh 方式來傳輸文件

(6)支持匿名傳輸,以方便進行網站鏡象

192.168.175.129同步源(文件往這裏傳)

192.168.175.128客戶端(文件提交)

例子1: rsync手動同步

設置同步源:vi /etc/rsyncd.conf

#目錄是:/var/www/html,那麽這個目錄權限用戶是nginx
 uid = nginx 
 gid = nginx
 port = 873                            //監聽端口
 host all = 192.168.175.129     //監聽地址,本機地址
 use chroot = yes                   //禁錮在源目錄
 max connections = 4            
 timeout = yes                   
 hosts allow = 192.168.175.128    //允許訪問的客戶機地址
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[webroot]                          //共享模塊名稱
path=/var/www/html        //源目錄的路徑
comment = rsync files     //描述信息
ignore errors
read only = no                   //是否為只讀
list = yes                        
auth users = muyang        //授權賬戶,如果采用匿名賬戶,可以去掉
secrets file = /etc/rsync_server.passwd   
//存放授權賬戶信息的數據文件

  

//為備份賬戶建立數據文件: //用:分隔賬戶和密碼,無需建立同名系統用戶

/etc/rsync_server.passwd
muyang:rsync

  

//修改權限,避免信息泄露

 chmod 600 /etc/rsync_server.passwd

  

vim /etc/sysconfig/selinux //關閉selinux

SELINUX=disabled

  

setenforce 0 //立即生效

iptables -I INPUT -p tcp --dport 873 -j ACCEPT //允許tcp873端口通過防火墻

啟動rsync

rsync –daemon –config=/etc/rsyncd.conf
開機啟動:
vim /etc/rc.local
/usr/bin/rsync –daemon

執行同步:將128目錄下文件,拉取129目錄

rsync -avz [email protected]::webroot /root

  

例2:將128目錄下的文件同步到129目錄下

  • 安裝inotify-tools工具

??使用inotify機制還需要安裝inotify-tools,以便提供inotifywait、inotifywatch輔助工具程序,用來監控、匯總改動情況。

yum -y install epel-release
yum install inotify-tools --enablerepo=epel

  

1. 調整inotify內核參數

Linux內核中,默認的inotify機制提供了三個調控參數,分別表示監控事件隊列、最多監控實例數、每個實例最多監控文件數。當需要監控的目錄、文件數量過多時,建議加大這三個參數

[root@localhost ~]# cat /proc/sys/fs/inotify/max_queued_events 
16384                                               //監控事件隊列默認16384
[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_instances 
128                                                 //最多監控實例數默認128
[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_watches 
8192                                                //每個實例最多監控文件數默認8192
[root@localhost ~]# vim /etc/sysctl.conf         //如果監控文件過多,建議加大數量 
fs.inotify.max_queued_events=16384               //添加三行
fs.inotify.max_user_instances=1024
fs.inotify.max_user_watches=1048576
[root@localhost ~]# sysctl -p                    //立即生效

  

inotifywait:可監控modify(修改)、create(創建)、move(移動)、delete(刪除)、attrib(屬性更改)等各種事件,一有變動立即輸出結果

inotifywatch:可用來收集文件系統變動情況,並在運行結束後輸出匯總的變化情況

-m 表示持續監控

-r 表示遞歸整個目錄

-q 表示簡化輸出信息

-e 用來指定監控哪些事件

在rsync同步源啟用密鑰對驗證

[root@localhost html]# vim /etc/ssh/sshd_config   //修改sshd服務配置文件
PubkeyAuthentication yes                          //找到這兩項,去掉註釋
AuthorizedKeysFile      .ssh/authorized_keys
[root@localhost html]# service sshd reload        //重載服務

  

在rsync發起端創建ssh密鑰對,並將公鑰導入到服務器公鑰文本

[root@localhost ~]# ssh-keygen -t rsa         //使用RSA算法生成密鑰對
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   
Enter passphrase (empty for no passphrase): 
Enter same passphrase again:                 //按三下Enter鍵,實現無口令登錄
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]    //將公鑰文件上傳到rsync同步源
The authenticity of host ‘192.168.175.129 (192.168.175.129)‘ can‘t be established.
RSA key fingerprint is e5:c6:c7:96:30:eb:04:00:da:e9:43:72:67:d7:ff:e3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.175.129‘ (RSA) to the list of known hosts.
[email protected]‘s password:                                             //輸入同步源用戶密碼
Now try logging into the machine, with "ssh ‘[email protected]‘", and check in:
  .ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting
[root@localhost ~]# ssh [email protected]            //成功登錄sync同步源
[root@localhost ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1C:B4:FB  
          inet addr:192.168.175.129  Bcast:192.168.175.255  Mask:255.255.255.0

  

退回到128服務器,編寫觸發式同步腳本

這個腳本是有問題的(file列表是循環形式觸發rsync ,等於有10個文件發生更改,就觸發10次rsync全量同步(簡直就是噩夢),那還不如直接寫個死循環的rsync全量同步得了)

[root@localhost ~]# vim rsync.sh               //編寫腳本,若進程已經存在,則忽略本次備份,避免並發執行rsync備份
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete /var/www/html/ [email protected]:/var/www/html"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
    fi
done
[root@localhost ~]# chmod +x rsync.sh           //添加執行權限,並且服務器開機自動運行此腳本    
[root@localhost ~]# echo ‘/root/rsync.sh‘ >> /etc/rc.local

6. 驗證腳本是否正確

(1)128f服務器執行腳本,

(2)並新開一個連接到128服務器上,切換到本機/var/www/html目錄,執行增、刪、改等操作,

(3)查看129服務器下/var/www/html 文件及文件是否有了變化

rsync+inotify文件同步