rsync遠程同步部署

關於rsync

Remote Sync,遠程同步,它是一個開源的快速備份工具,可以在不同主機之間鏡像同步整個目錄樹支持增量備份、保持連接和權限,且採用優化的同步算法,傳輸前執行壓縮,因此分別適用於異地備份、鏡像服務器等應用支持本地複製,或者與其他SSH、rsync主機同步

實驗部署

1、實驗環境

主機 IP地址
rsync同步源 192.168.7.128
rsync客戶機 192.168.7.129

用戶backuper,允許下行同步,操作的目錄爲 /var/www/html/

[root@localhost ~]# yum -y install httpd

2、配置rsync源(在同步服務器上操作)
(1)如果使用字符界面需要安裝rsync,圖形界面不需要

[root@localhost ~]# yum -y install rsync

(2)更改rsync配置文件rsyncd.conf(認證配置auth users、secrets file,不加則爲匿名)

[root@localhost ~]# vi /etc/rsyncd.conf
 uid = nobody
 gid = nobody
#禁錮在源目錄
 use chroot = yes                       
#監聽地址
 address = 192.168.7.128      
#監聽端口號
 port 873                                  
#日誌文件位置
 log file = /var/log/rsyncd.log         
#存放進程ID的文件位置
 pid file = /var/run/rsyncd.pid          
#允許訪問的客戶機地址
 hosts allow = 192.168.7.0/24          
#共享模塊名稱
[wwwroot]                                    
#源目錄的實際路徑
 path = /var/www/html                  
 comment = Document Root of www.51xit.top
#是否只讀
 read only =yes                              
#同步時不在壓縮的文件類型
 dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z   
#授權賬戶
 auth users =backuper   
#存放賬戶信息的數據文件                            
 secrets file = /etc/rsyncd_users.db                           

(3)rsync賬號文件
採用“用戶名:密碼”的記錄格式,每行一個用戶記錄
獨立的賬號數據,不依賴於系統賬號

[root@localhost ~]# vi /etc/rsyncd_users.db
backuper:pwd123
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db

(4)啓用rsync服務

[root@localhost ~]# rsync --daemon           
#如果要停止這個服務  kill $(cat /var/run/rsyncd.pid)
[root@localhost ~]# netstat -anpt |grep rsync
tcp        0      0 192.168.7.128:873       0.0.0.0:*               LISTEN      3405/rsync

(5)在/var/www/html目錄中,新建文件(裏面的內容隨意添加)

[root@localhost opt]# cd /var/www/html/
[root@localhost html]# cat 123.html 
test01
[root@localhost html]# cat 456.html 
test02

3、在客戶機上使用rsync備份工具
(1)rsync命令的用法:rsync [選項] 原始位置 目標位置

[root@localhost ~]# rsync /etc/fstab /test
[root@localhost ~]# ls /test
fstab
[root@localhost ~]# rsync -rl /etc/fstab /boot/grub /test
[root@localhost ~]# ls /test
fstab  grub
  • 常用選項
    -a:歸檔模式,遞歸併保留對象屬性,等同於 -rlptgoD
    -v:顯示同步過程的詳細(verbose)信息
    -z:在傳輸文件時進行壓縮(compress)
    -H:保留硬連接文件
    -A:保留ACL屬性信息
    –delete:刪除目標位置有而原始位置沒有的文件
    –checksum:根據對象的校驗和來決定是否跳過文件

(2)配置源的兩種表示方法

  • 格式1:用戶名@主機地址::共享模塊名
[root@localhost ~]# rsync -avz [email protected]::wwwroot /opt
Password: 
receiving incremental file list
./
123.html
456.html

sent 102 bytes  received 229 bytes  73.56 bytes/sec
total size is 14  speedup is 0.04
[root@localhost ~]# ls /opt
123.html  456.html  rh
  • 格式2: rsync://用戶名@主機地址/共享模塊名
[root@localhost ~]# rsync -avz rsync://[email protected]/wwwroot /test
Password: 
receiving incremental file list
./
123.html
456.html

sent 102 bytes  received 229 bytes  73.56 bytes/sec
total size is 14  speedup is 0.04
[root@localhost ~]# ls /test
123.html  456.html  fstab  grub

(3)rsync同步操作示例

[root@localhost ~]# mkdir /test/myweb
[root@localhost ~]# rsync -avzH --delete [email protected]::wwwroot  /test/myweb
Password: 
receiving incremental file list
./
123.html
456.html

sent 102 bytes  received 229 bytes  50.92 bytes/sec
total size is 14  speedup is 0.04
[root@localhost ~]# cd /test/myweb/
[root@localhost myweb]# ls
123.html  456.html
[root@localhost wwwroot]# vi /etc/server.pass
pwd123
[root@localhost wwwroot]# cat /etc/server.pass
pwd123
[root@localhost wwwroot]# chmod 600 /etc/server.pass
#測試命令是否正常
[root@localhost wwwroot]#  rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/myweb/  
[root@localhost wwwroot]# crontab -e  
#每天晚上10點半對服務器網站目錄更新一次
30 22 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /testmyweb/
[root@localhost wwwroot]# systemctl restart crond
[root@localhost wwwroot]# systemctl enable crond

4、rsync+inotify實時同步(在客戶機安裝)
(1)定期同步的不足

  • 執行備份的時間固定,延遲明顯、實時性差
  • 當同步源長期不變化時,密集的定期任務是不必要的

(2)實時同步的優點

  • 一旦同步源出現變化,立即啓動備份
  • 只要同步源無變化,則不執行備份

(3)調整inotify內核參數

  • max_queue_events:監控隊列大小
  • max_user_instances:最多監控實例數
  • max_user_watches:每個實例最多監控文件數
[root@localhost ~]# vi /etc/sysctl.conf
……
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@localhost ~]# sysctl -p

(4)安裝inotify-tools輔助工具
將inotify-tools上傳到/opt目錄下

#安裝環境包
[root@localhost ~]# yum -y install gcc gcc-c++
#編譯安裝
[root@localhost ~]# cd /opt
[root@localhost opt]# tar xzvf inotify-tools-3.14.tar.gz 
[root@localhost opt]# cd inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure
[root@localhost inotify-tools-3.14]# make && make install

(5)開啓監控

  • inotifywait:用於持續監控,實時輸出結果
  • inotifywatch:用於短期監控,任務完成後再出結果
[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete  /test/myweb

(6)通過inotifywait觸發rsync同步操作,使用while、read持續獲取監控結果,根據結果可以作進一步判斷,決定執行何種操作

[root@localhost ~]# vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /test/myweb/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /test/myweb/ [email protected]::wwwroot"
 
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
#       echo "${FILE} was rsynced" >>/opt/inotify_rsync.log

    fi
done

[root@localhost ~]# chmod +x /opt/inotify.sh 

(7)權限設置
①在同步源192.168.7.128

[root@localhost ~]# vi /etc/rsyncd.conf
#開啓讀寫
read only =no

[root@localhost ~]# chmod 777 /var/www/html/

②在客戶機192.168.7.129

[root@localhost ~]#  /opt/inotify.sh 
[root@localhost ~]#  chmod 777 /test/myweb/
#裏面插入任意數據
[root@localhost ~]#  vi /test/myweb/110.html    
#到同步源192.168.7.128查看數據是否同步
[root@localhost ~]# ll /var/www/html/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章