溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

rsync遠程同步中的定期同步和實時同步怎么用

發(fā)布時間:2021-09-26 16:52:44 來源:億速云 閱讀:160 作者:柒染 欄目:系統(tǒng)運維

本篇文章給大家分享的是有關(guān)rsync遠程同步中的定期同步和實時同步怎么用 ,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

關(guān)于rsync .

一款快速增量備份工具

    1.Remote Sync,遠程同步
    2.支持本地復制,或者與其他SSH、rsync主機同步
    3.官方網(wǎng)站: 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.獨立的賬號數(shù)據(jù),不依賴于系統(tǒng)賬號

啟用rsync服務

1.通過--daemon獨自提供服務 [執(zhí)行kill $(catIvar/run/rsyncd.pid)關(guān)閉rsync服務]

使用rsync備份工具

rsync命令的用法:

rsync [選項] 原始位置 目標位置
1.常用選項:

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

配置源的兩種標識方法:格式1:用戶名@主機地址::共享模塊名<br/" rel="nofollow">br/>格式1:用戶名@主機地址::共享模塊名<br/格式2:rsync://用戶名@主機地址/共享模塊名
[root@localhost ~]# rsync -avz
backuper@ 192.168.4.200::wwwroot /root
[root@localhost ~]# rsync -avz
rsync://backuper@ 192.1 68.4.200/wwwroot /root

實驗操作:

環(huán)境準備:兩臺主機

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
#以上內(nèi)容去掉注釋

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

[wwwroot]
path = /var/www/html
comment = www.kgc.com
read only = yes
auth users = backuper
secrets file = /etc/rsyncd_users.db
#在dont下一行插入以上內(nèi)容:共享模塊
#修改完成后按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 backuper@192.168.18.148::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://backuper@192.168.18.148::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 backuper@192.168.18.148::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.執(zhí)行備份的時間固定,延遲明顯、實時性差
2.當同步源長期不變化時,密集的定期任務是不必要的

實時同步的優(yōu)點

1.一旦同步源出現(xiàn)變化,立即啟動備份
2.只要同步源無變化,則不執(zhí)行備份

關(guān)于inotify

Linux內(nèi)核的inotify機制

1.從版本2.6.13開始提供
2.可以監(jiān)控文件系統(tǒng)的變動情況,并作出通知響應
3.輔助軟件: inotify-tools

rsync+inotify實時同步

調(diào)整inotify內(nèi)核參數(shù):

    max_queue_events:監(jiān)控隊列大小
    maxuser instances:最多監(jiān)控實例數(shù)
    max_ user_watches::每個實例最多監(jiān)控文件數(shù)
[root@localhost ~ ]# vi /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

安裝inotify-tools輔助工具

    inotifywait:用于持續(xù)監(jiān)控,實時輸出結(jié)果
    inotifywatch:用于短期監(jiān)控,任務完成后再出結(jié)果

[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete
/var/www/html
Setting up watches. Beware: since -r was given, this may take a while! 
Watches established.
/var/www/html/ CREATE index.php
/var/www/html/ MODIFY index.php
/var/www/html/ MOVED_FROM index.php
/var/www/htmI/ MOVED_TO test.php
-m:持續(xù)進行監(jiān)控

-r:遞歸監(jiān)控所有子對象

-q:簡化輸出信息

-e:指定要監(jiān)控哪些事件類型

實驗操作:

在client發(fā)起端中的操作:

[root@client html]# vim /etc/sysctl.conf
#需要在發(fā)起端開啟監(jiān)控
#在末行下插入以下內(nèi)容
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       #刷新數(shù)據(jù)
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/
#進入監(jiān)控狀態(tài),監(jiān)控本地的html文件中的更新,創(chuàng)建,移動,刪除

此時無法進行操作,我們需要再開一個遠程連接以進行操作

[root@client ~]# cd /var/www/html/
[root@client html]# ls
index.html
[root@client html]# touch abc       #創(chuàng)建新的abc文件
[root@client html]# rm -rf abc      #刪除abc

此時會在監(jiān)控的操作界面顯示同步到了此動作:

/var/www/html/ CREATE abc       #同步到創(chuàng)建動作
/var/www/html/ DELETE abc       #同步到刪除動作

我們可以使用:監(jiān)控觸發(fā)動作,然后調(diào)取rsync進行同步

在監(jiān)控客戶先使用Ctrl+c停止監(jiān)控,然后創(chuàng)建腳本,操作如下:

[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/ backuper@192.168.18.148::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      #關(guān)閉只讀功能
#修改完成后按Esc退出插入模式,輸入:wq保存退出

執(zhí)行腳本
[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
#此時正常運轉(zhuǎn)rsync

在CentOS 7-5 client端開啟監(jiān)控:

[root@client opt]# ./inotify.sh
#此時監(jiān)控開啟

再打開另一個7-5的遠程連接開始寫內(nèi)容:

[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

以上就是rsync遠程同步中的定期同步和實時同步怎么用 ,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI