rsync遠程同步(定期同步,實時同步)

本章結構

1.rsync同步簡介
2.配置rsync備份源
3.rsync命令基本用法
4.rsync備份操作示例
5.rsync+inotify結合使用

關於rsync .

一款快速增量備份工具

1.Remote Sync,遠程同步
2.支持本地複製,或者與其他SSH、rsync主機同步
3.官方網站: http://rsync.samba.org

在這裏插入圖片描述

配置rsync源服務器

rsync同步源:

指備份操作的遠程服務器,也稱爲備份源

在這裏插入圖片描述

配置rsync源

基本思路:

1.建立rsyncd.conf配置文件、獨立的賬號文件
.啓用rsync的–daemon模式

應用示例:

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

配置文件rsyncd.conf

1.需手動建立,語法類似於Samba配置
2.認證配置auth users、secrets file,不加則爲匿名

rsync賬號文件

1.採用“用戶名:密碼”的記錄格式,每行一個用戶記錄
2.獨立的賬號數據,不依賴於系統賬號

啓用rsync服務

1.通過–daemon獨自提供服務 [執行kill $(catIvar/run/rsyncd.pid)關閉rsync服務]

使用rsync備份工具

rsync命令的用法:

rsync [選項] 原始位置 目標位置

1.常用選項:

-a:歸檔模式,遞歸併保留對象屬性,等同於-rlptgoD
-v:顯示同步過程的詳細(verbose)信息
-z:在傳輸文件時進行壓縮(compress)
-H:保留硬連接文件
-A:保留ACL屬性信息
–delete:刪除目標位置有而原始位置沒有的文件
–checksum:根據對象的校驗和來決定是否跳過文件

配置源的兩種標識方法:

在這裏插入圖片描述

Demo:

環境準備:兩臺主機

CentOS 7-4作爲同步源:192.168.18.148

**CentOS 7-5作爲客戶機:192.168.18.145 **

CentOS 7-4源端的操作:

[root@localhost ~]# hostnamectl set-hostname rsyncd
[root@localhost ~]# su
[root@rsyncd ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[root@rsyncd ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
pid file = /var/run/rsyncd.pid
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
#以上內容去掉註釋

address = 192.168.18.148		#本機IP地址
port 873		#開端口
log file = /var/rsyncd.log		#指定日誌文件 
hosts allow = 192.168.18.0/24	#允許網段訪問
#在pid下行添加以上內容

[wwwroot]
path = /var/www/html
comment = www.kgc.com
read only = yes
auth users = backuper
secrets file = /etc/rsyncd_users.db
#在dont下一行插入以上內容:共享模塊
#修改完成後按Esc退出插入模式,輸入:wq保存退出

#添加密碼文件
[root@rsyncd ~]# vim /etc/rsyncd_users.db
backuper:abc123		#需要和共享模塊中的auth_users名稱一致
#添加完成後按Esc退出插入模式,輸入:wq保存退出
[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db
[root@rsyncd ~]# rsync --daemon
[root@rsyncd ~]# netstat -ntap | grep rsync
tcp        0      0 192.168.18.148:873      0.0.0.0:*          LISTEN      6150/rsync
#此時873端口開啓

[root@rsyncd ~]# systemctl stop firewalld.service
[root@rsyncd ~]# setenforce 0
[root@rsyncd ~]# yum install httpd -y
[root@rsyncd html]# cd ..
[root@rsyncd www]# chmod 777 html/
[root@rsyncd www]# ls -l
總用量 0
drwxr-xr-x. 2 root root  6 8月   8 19:42 cgi-bin
drwxrwxrwx. 2 root root 24 12月 16 08:41 html
 

CentOS 7-5客戶機的操作:

[root@localhost ~]# hostnamectl set-hostname client
[root@localhost ~]# su
[root@client ~]# systemctl stop firewalld.service
[root@client ~]# setenforce 0
[root@client ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[root@client ~]# yum install httpd -y
[root@client ~]# cd /var/www/html/
#此時文件中是空的沒有文件的
[root@client html]# cd ..
[root@client www]# chmod 777 html/
[root@client www]# ls -l
總用量 0
drwxr-xr-x. 2 root root 6 8月   8 19:42 cgi-bin
drwxrwxrwx. 2 root root 6 8月   8 19:42 html

#同步方法一:
[root@client www]#  rsync -avz [email protected]::wwwroot /var/www/html/
Password:		#此時輸入密碼abc123,敲回車
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  12.44 bytes/sec
total size is 17  speedup is 0.07
[root@client www]# cd html/
[root@client html]# ls		#此時index.html文件被同步
index.html
[root@client html]# cat index.html
this is test web

#同步方法二:
[root@client www]#  rsync -avz rsync://[email protected]::wwwroot /var/www/html/
Password:		#此時輸入密碼abc123,敲回車
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  12.44 bytes/sec
total size is 17  speedup is 0.07
[root@client www]# cd html/
[root@client html]# ls		#此時index.html文件被同步
index.html
[root@client html]# cat index.html
this is test web

此時我們如果使用計劃性任務,就需要考慮到免交互的問題:

[root@client html]# vim /etc/server.pass
abc123
#寫入密碼信息後按Esc退出插入模式,輸入:wq保存退出
[root@client html]# chmod 600 /etc/server.pass
[root@client html]# rsync -avz --delete --password-file=/etc/server.pass [email protected]::wwwroot /var/www/html/		#用此條命令可以直接進入
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  510.00 bytes/sec
total size is 17  speedup is 0.07
[root@client html]# ls
index.html
[root@client html]# cat index.html
this is test web

#後面就可以在crontab -e中添加計劃任務了

rsync實時同步

定期同步的不足

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

實時同步的優點

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

關於inotify

Linux內核的inotify機制

1.從版本2.6.13開始提供
2.可以監控文件系統的變動情況,並作出通知響應
3.輔助軟件: inotify-tools

在這裏插入圖片描述

rsync+inotify實時同步

調整inotify內核參數:

max_queue_events:監控隊列大小
max_user_ instances:最多監控實例數
max_ user_watches::每個實例最多監控文件數

在這裏插入圖片描述

安裝inotify-tools輔助工具:

inotifywait:用於持續監控,實時輸出結果
inotifywatch:用於短期監控,任務完成後再出結果

在這裏插入圖片描述

-m:持續進行監控

-r:遞歸監控所有子對象

-q:簡化輸出信息

-e:指定要監控哪些事件類型

Demo:

在client發起端中的操作:

[root@client html]# vim /etc/sysctl.conf
#需要在發起端開啓監控
#在末行下插入以下內容
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
#添加完成後按Esc退出插入模式,輸入:wq保存退出

[root@client html]# sysctl -p		#刷新數據
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

#加載inotofy管理工具
[root@client html]# mkdir /aaa
[root@client html]# mount.cifs //192.168.10.190/rpm /aaa
Password for root@//192.168.10.190/rpm:
[root@client html]# cd /aaa
[root@client aaa]# ls
Discuz_X3.4_SC_UTF8.zip           nginx-1.12.2.tar.gz
error.png                         php
extundelete-0.2.4.tar.bz2         redis-5.0.7.tar.gz
haproxy-1.5.19.tar.gz             ruby-2.4.1.tar.gz
httpd-2.4.29.tar.bz2              ruby.png
hzw.jpeg                          squid
inotify-tools-3.14.tar.gz         TC
[root@client aaa]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/
[root@client opt]# cd /opt/inotify-tools-3.14/
[root@client inotify-tools-3.14]# ls
aclocal.m4    config.h.in   COPYING     libinotifytools  man      src
AUTHORS       config.sub    depcomp     ltmain.sh        missing
ChangeLog     configure     INSTALL     Makefile.am      NEWS
config.guess  configure.ac  install-sh  Makefile.in      README
[root@client inotify-tools-3.14]# yum install gcc gc-c++ make -y
[root@client inotify-tools-3.14]# ./configure
[root@client inotify-tools-3.14]# make && make install
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
#進入監控狀態,監控本地的html文件中的更新,創建,移動,刪除
此時無法進行操作,我們需要再開一個遠程連接以進行操作
[root@client ~]# cd /var/www/html/
[root@client html]# ls
index.html
[root@client html]# touch abc		#創建新的abc文件
[root@client html]# rm -rf abc		#刪除abc
此時會在監控的操作界面顯示同步到了此動作:
/var/www/html/ CREATE abc		#同步到創建動作
/var/www/html/ DELETE abc		#同步到刪除動作

我們可以使用:監控觸發動作,然後調取rsync進行同步

在監控客戶先使用Ctrl+c停止監控,然後創建腳本,操作如下:
[root@client inotify-tools-3.14]# cd /opt/
[root@client opt]# ls
inotify-tools-3.14  rh
[root@client opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ [email protected]::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVEVT FILE
do
        if [ $(pgrep rsync | wc -l) -le 0 ] ; then
            $RSYNC_CMD
        fi
done
#添加完成後按Esc退出插入模式,輸入:wq保存退出
[root@client opt]# chmod +x inotify.sh
[root@client opt]# ls -l /var/www/
總用量 0
drwxr-xr-x. 2 root root  6 8月   8 19:42 cgi-bin
drwxrwxrwx. 2 root root 24 12月 16 10:00 html
此時還需要注意到CentOS 7-4 rsync服務器端的配置文件:
[root@rsyncd www]# vim /etc/rsyncd.conf
read only = no		#關閉只讀功能
#修改完成後按Esc退出插入模式,輸入:wq保存退出

執行腳本
[root@rsyncd ~]# netstat -ntap | grep rsync
tcp        0      0 192.168.18.148:873      0.0.0.0:*          LISTEN      2768/rsync
[root@rsyncd ~]# kill -9 2768		#殺死該進程
[root@rsyncd ~]# rsync --daemon		#啓動rsync
[root@rsyncd ~]# failed to create pid file /var/run/rsyncd.pid: File exists
#提示有pid文件存在
[root@rsyncd ~]# cd /var/run/
[root@rsyncd run]# ls
abrt          dhclient-ens33.pid  lock            radvd           syslogd.pid
alsactl.pid   dmeventd-client     log             rpcbind         systemd
atd.pid       dmeventd-server     lsm             rpcbind.sock    tmpfiles.d
auditd.pid    faillock            lvm             rsyncd.pid      tuned
avahi-daemon  firewalld           lvmetad.pid     samba           udev
certmonger    gdm                 mdadm           sepermit        udisks2
chrony        gssproxy.pid        media           setrans         user
chronyd.pid   gssproxy.sock       mount           setroubleshoot  utmp
console       httpd               named           sm-notify.pid   vmware
crond.pid     initramfs           netreport       spice-vdagentd  xtables.lock
cron.reboot   ksmtune.pid         NetworkManager  sshd.pid
cups          libvirt             plymouth        sudo
dbus          libvirtd.pid        ppp             sysconfig
[root@rsyncd run]# cat rsyncd.pid
2768
[root@rsyncd run]# rm -rf rsyncd.pid		#刪除此pid文件
[root@rsyncd run]# rsync --daemon		#再次啓動
[root@rsyncd run]# netstat -ntap | grep rsync		#此時會生成新的pid號
tcp        0      0 192.168.18.148:873      0.0.0.0:*         LISTEN      5416/rsync
[root@rsyncd run]# cat rsyncd.pid
5416
#此時正常運轉rsync
在CentOS 7-5 client端開啓監控:
[root@client opt]# ./inotify.sh
#此時監控開啓
再打開另一個7-5的遠程連接開始寫內容:
[root@client html]# echo "this is test" > test.txt
此時文件同步到CentOS 7-4 rsync服務器端,我們可以進行查詢:
[root@rsyncd run]# cd /var/www/html/
[root@rsyncd html]# ls
index.html  test.txt
[root@rsyncd html]# cat test.txt
this is test

以上就實現了實時同步!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章