Linux服務-rsync

Linux服務-rsyncc++

1. rsync簡介

rsync是linux系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync能夠遠程同步,支持本地複製,或者與其餘SSH、rsync主機同步。shell

2. rsync特性

rsync支持不少特性:數據庫

  • 能夠鏡像保存整個目錄樹和文件系統
  • 能夠很容易作到保持原來文件的權限、時間、軟硬連接等等
  • 無須特殊權限便可安裝
  • 快速:第一次同步時rsync會複製所有內容,但在下一次只傳輸修改過的文件。rsync在傳輸數據的過程當中能夠實行壓縮及解壓縮操做,所以可使用更少的帶寬
  • 安全:可使用scp、ssh等方式來傳輸文件,固然也能夠經過直接的socket鏈接
  • 支持匿名傳輸,以方便進行網站鏡象
  • 多用於大批量小文件,大的文件如數據庫文件通常用nfs,samba

3. rsync的ssh認證協議

rsync命令來同步系統文件以前要先登陸remote主機認證,認證過程當中用到的協議有2種vim

  • ssh協議
  • rsync協議centos

    rsync server端不用啓動rsync的daemon進程,只要獲取remote host的用戶名和密碼就能夠直接rsync同步文件
    rsync server端由於不用啓動daemon進程,因此也不用配置文件/etc/rsyncd.conf安全

ssh認證協議跟scp的原理是同樣的,若是在同步過程當中不想輸入密碼就用ssh-keygen -t rsa打統統道bash

//這種方式默認是省略了 -e ssh 的,與下面等價:
[root@cwh ~]# rsync -avz test -e 'ssh' root@192.168.112.149:/root
-a  //文件宿主變化,時間戳不變
    -z  //壓縮數據傳輸
root@192.168.112.149's password: 
sending incremental file list

sent 71 bytes  received 17 bytes  25.14 bytes/sec
total size is 0  speedup is 0.00
//在149上查看
[root@149 ~]# ls
test

//若是ssh協議端口號不是默認的須要添加-p選項指定端口

4. rsync命令

//Rsync的命令格式經常使用的有如下三種:
    rsync [OPTION]... SRC DEST
    rsync [OPTION]... SRC [USER@]HOST:DEST
    rsync [OPTION]... [USER@]HOST:SRC DEST

//對應於以上三種命令格式,rsync有三種不一樣的工做模式:
1.拷貝本地文件
[root@cwh ~]# rsync -avz test/ bbb/
sending incremental file list
created directory bbb
./
aaa

sent 101 bytes  received 64 bytes  330.00 bytes/sec
total size is 0  speedup is 0.00
[root@cwh ~]# ls bbb/
aaa

2.使用一個遠程shell程序(如rsh、ssh)來實現將本地機器的內容拷貝到遠程機器
[root@cwh ~]# rsync -avz bbb root@192.168.112.149:/root
root@192.168.112.149's password: 
sending incremental file list
bbb/
bbb/ccc

sent 117 bytes  received 39 bytes  104.00 bytes/sec
total size is 0  speedup is 0.00
//在149上查看
[root@149 ~]# ls bbb/
ccc

3.使用一個遠程shell程序(如rsh、ssh)來實現將遠程機器的內容拷貝到本地機器
[root@cwh ~]# rm -rf bbb/
[root@cwh ~]# rsync -avz root@192.168.112.149:/root/bbb /root
root@192.168.112.149's password: 
receiving incremental file list
bbb/
bbb/ccc

sent 47 bytes  received 117 bytes  65.60 bytes/sec
total size is 0  speedup is 0.00
[root@cwh ~]# ls
anaconda-ks.cfg
bbb

//rsync經常使用選項:
    -a, --archive       //歸檔
    -v, --verbose       //囉嗦模式
    -q, --quiet         //靜默模式
    -r, --recursive     //遞歸
    -p, --perms         //保持原有的權限屬性
    -z, --compress      //在傳輸時壓縮,節省帶寬,加快傳輸速度
    --delete            //在源服務器上作的刪除操做也會在目標服務器上同步

5. rsync+inotify

rsync優勢:服務器

rsync與傳統的cp、tar備份方式相比,rsync具備安全性高、備份迅速、支持增量備份等優勢,經過rsync能夠解決對實時性要求不高的數據備份需求,例如按期的備份文件服務器數據到遠端服務器,對本地磁盤按期作數據鏡像等。網絡

rsync缺點:

  • rsync同步數據時,須要掃描全部文件後進行比對,進行差量傳輸。若是文件數量達到了百萬甚至千萬量級,掃描全部文件將是很是耗時的。並且正在發生變化的每每是其中不多的一部分,這是很是低效的方式
  • rsync不能實時的去監測、同步數據,雖然它能夠經過linux守護進程的方式進行觸發同步,可是兩次觸發動做必定會有時間差,這樣就致使了服務端和客戶端數據可能出現不一致,沒法在應用故障時徹底的恢復數據

改進:

Inotify是一種強大的、細粒度的、異步的文件系統事件監控機制,linux內核從2.6.13起,加入了Inotify支持,經過Inotify能夠監控文件系統中添加、刪除,修改、移動等各類細微事件,利用這個內核接口,第三方軟件就能夠監控文件系統下文件的各類變化狀況,而inotify-tools就是這樣的一個第三方軟件。
在前面有講到,rsync能夠實現觸發式的文件同步,可是經過crontab守護進程方式進行觸發,同步的數據和實際數據會有差別,而inotify能夠監控文件系統的各類變化,當文件有任何變更時,就觸發rsync同步,這樣恰好解決了同步數據的實時性問題

rsync+inotify實驗:

環境說明:

服務器類型 IP地址 應用 操做系統
源服務器 172.16.12.128 rsync
inotify-tools
腳本
centos7/redhat7
目標服務器 172.16.12.129 rsync centos7/redhat7

實驗需求:

部署rsync+inotify同步/etc目錄至目標服務器149的/cwh/下。

//第一步將服務端和客戶端的防火牆和selinux關閉
    //服務端
[root@cwhsever ~]# systemctl stop firewalld
[root@cwhsever ~]# setenforce 0
    //客戶端
[root@cwhclient ~]# systemctl stop firewalld
[root@cwhclient ~]# setenforce 0

//第二步安裝rsync服務端軟件
    //服務端
[root@cwhsever ~]# yum -y install rsync
    //客戶端
[root@cwhclient ~]# yum -y install rsync

//第三步在客戶機上設置rsyncd.confd額配置文件
#部署rsync+inotify同步/etc目錄至目標服務器149的/cwh/下

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  //用戶認證配置文件,裏面保存用戶名稱和密碼,必須手動建立這個文件

[test_from_149] //自定義同步名稱,相似samba掛載目錄
        path = /cwh/ //rsync客戶端數據存放路徑,服務端的數據將同步至此目錄  
        uid = root  //設置rsync運行權限爲root
        gid = root  //設置rsync運行權限爲root
        port = 873  //設置rsync端口
        ignore errors   //表示出現錯誤忽略錯誤
        use chroot = no //默認爲true,修改成no,增長對目錄文件軟鏈接的備份,不改成no沒法備份軟鏈接
        read only = no  //設置rsync服務端爲讀寫權限,只讀沒法同步
        list = no    //不顯示rsync服務端資源列表
        max connections = 200   //對大鏈接數
        timeout = 600   //設置超時時間
        auth users = admin  //執行數據同步的用戶名,能夠設置多個用逗號隔開,要與寫入/etc/rsync.pass的用戶名一致
        hosts allow = 172.16.12.128   //(能夠不設置)容許進行數據同步的客戶端IP地址,能夠設置多個,用英文狀態下逗號隔開
        hosts deny = 192.168.1.1      //(能夠不設置)禁止數據同步的客戶端IP地址,能夠設置多個,用英文狀態下逗號隔開

//第四步建立用戶認證文件(也就是寫在rsyncd.conf中的用戶配置文件)
[root@cwhclient ~]# echo 'admin:123456' > /etc/rsync.pass
[root@cwhclient ~]# cat /etc/rsync.pass 
admin:123456

//第五步設置用戶認證文件權限
[root@cwhclient ~]# chmod 600 /etc/rsync.pass

//第六步在客戶端上啓動rsync服務並設置開機自啓動
[root@cwhclient ~]# systemctl restart rsyncd
[root@cwhclient ~]# systemctl enable rsyncd

//第七步查看客戶機上rsync的端口是否打開
[root@cwhclient ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      5       *:873                 *:*                  
LISTEN     0      128     *:111                 *:*                  
LISTEN     0      128     *:22                  *:*                  
LISTEN     0      100    127.0.0.1:25                  *:*                  
LISTEN     0      5      :::873                :::*                  
LISTEN     0      128    :::111                :::*                  
LISTEN     0      128    :::22                 :::*                  
LISTEN     0      100       ::1:25                 :::*

//下面操做在源服務器端操做
//第八步關閉服務端防火牆與SELINUX
[root@cwhsever ~]# systemctl stop firewalld
[root@cwhsever ~]# setenforce 0

//第九步配置yum源(最好是163的網絡源)
[root@cwhserver ~]# cd /etc/yum.repos.d/
[root@cwhserver yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@cwhserver ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@cwhserver ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo

//第十步裝好163網絡源後安裝epel源
[root@cwhserver ~]# yum -y install epel-release

//第十一步安裝rsync服務端軟件,只須要安裝,不要啓動,不須要配置(若是以前已經安裝則不須要再次安裝)
[root@cwhsever ~]# yum -y install rsync

//第十二步建立認證密碼文件,須要將密碼寫入/etc/rsyncd.pass(若是沒有該目錄則建立要與客戶端一制)中密碼要與客戶端寫入pass文件中的密碼一致
[root@cwhsever ~]# echo '123456' > /etc/rsync.pass //注意若是已經建立了該pass目錄不要使用重定向
[root@cwhsever ~]# chmod 600 /etc/rsync.pass

//第十二步在源服務器上建立測試目錄,而後在源服務器同步到客戶端
[root@cwhsever ~]# mkdir aaa
[root@cwhsever ~]# touch aaa/bbb
[root@cwhsever ~]# rsync -avH --port 873 --progress --delete /root/aaa admin@192.168.112.149::cwh_from_149 --password-file=/etc/rsync.pass
sending incremental file list
aaa/
aaa/bbb
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/2)

sent 124 bytes  received 47 bytes  342.00 bytes/sec
total size is 0  speedup is 0.00

    //在客戶端查看
[root@cwhclient ~]# ls /cwh/
aaa
[root@cwhclient ~]# ls /cwh/aaa/
bbb
//說明測試成功

//第十三步安裝inotify-tools工具,實時觸發rsync進行同步(注意內核等級是否支持inotify)
[root@cwhsever ~]# yum -y install make gcc gcc-c++
[root@cwhsever ~]# yum -y install inotify-tools

//第十四步寫同步腳本,此步乃最最重要的一步,請慎之又慎。讓腳本自動去檢測咱們制定的目錄下,文件發生的變化,而後再執行rsync的命令把它同步到咱們的服務器端去

[root@localhost ~]# mkdir /scripts/
[root@localhost ~]# vim /scripts/inotify.sh
#!/bin/bash
host=192.168.112.149    //目標服務器的ip(備份服務器)
src=/etc/   //在源服務器上所要監控的備份目錄(此處能夠自定義,可是要保證存在)
des=cwh_from_149    //自定義的模塊名,須要與目標服務器上定義的同步名稱一致
password=/etc/rsync.pass    //執行數據同步的密碼文件
user=admin  //執行數據同步的用戶名
inotifywait=/usr/bin/inotifywait    //命令

$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files;do
    rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

//第十五步
[root@localhost ~]# nohup bash /scripts/inotify.sh &    //這條命是將腳本放入後臺一直運行,不重啓就一直運行,若是須要開機自動運行須要寫入/etc/rc.local文件中

//第十六步在源服務器上生成一個新文件,並在客戶端查看
[root@localhost ~]# touch /etc/lcr090
[root@cwhclient ~]# ls /cwh/ |grep lcr090
lcr090    //能夠看出腳本運行成功

//第十七步設置腳本開機自動啓動:
[root@localhost ~]# chmod a+x /etc/rc.local 
[root@localhost ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@localhost ~]# tail -5 /etc/rc.d/rc.local
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh
//能夠看出啓動腳本的命令已經寫入了配置文件中

//第十八步查看重啓以後腳本運行結果
[root@localhost ~]# reboot
[root@localhost ~]# ps -ef|grep inotify
root       1002    995  0 18:22 ?        00:00:00 /bin/bash /scripts/inotify.sh
root       1009   1002  0 18:22 ?        00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc/
root       1010   1002  0 18:22 ?        00:00:00 /bin/bash /scripts/inotify.sh
root       1536   1518  0 18:23 pts/1    00:00:00 grep --color=auto inotify
//能夠看出重啓後腳本已經在運行

[root@localhost ~]# touch /etc/hellocwh //在服務端建立一個文件

[root@cwhclient ~]# ls /cwh/ |grep hellocwh 
hellocwh
//在客戶端能夠看出已經同步過去了
相關文章
相關標籤/搜索