一款快速增量備份工具

Remote Sync,遠(yuǎn)程同步
支持本地復(fù)制,或者與其他SSH,rsync主機(jī)同步

配置rsync源服務(wù)器
rsync同步源

指?jìng)浞莶僮鞯倪h(yuǎn)程服務(wù)器,也稱為備份源

配置rsync源
基本思路

建立rsync.conf配置文件,獨(dú)立的賬號(hào)文件
啟用rsync的--daemon模式

應(yīng)用示例

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

配置文件rsyncd.conf

需手動(dòng)建立,語法類似于Samba配置
認(rèn)證配置auth users,secrets file,不加則為匿名

rsync賬號(hào)文件

采用“用戶名:密碼”的記錄格式,每行一個(gè)用戶記錄
獨(dú)立的賬號(hào)數(shù)據(jù),不依賴于系統(tǒng)賬號(hào)

啟用rsync服務(wù)

通過--daemon獨(dú)自提供服務(wù)
執(zhí)行kill $(cat /var/run/rsync.pid)關(guān)閉rsync服務(wù)

使用rsync備份工具
rsync命令的用法

rsync [選項(xiàng)] 原始位置 目標(biāo)位置

常用選項(xiàng)

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

配置源的兩種表示方法

格式1:用戶名@主機(jī)地址::共享模塊名
格式2:rsync://用戶名@主機(jī)地址/共享模塊名

rsync實(shí)時(shí)同步
定期同步的不足

執(zhí)行備份的時(shí)間固定,延遲明顯,實(shí)時(shí)性差
當(dāng)同步源長期不變化時(shí),密集的定期任務(wù)是不必要的

實(shí)時(shí)同步的優(yōu)點(diǎn)

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

關(guān)于 inotify(安裝在發(fā)起端的)

Inotify 是一個(gè) Linux特性,它監(jiān)控文件系統(tǒng)操作,比如讀取、寫入和創(chuàng)建。Inotify 反應(yīng)靈敏,用法非常簡(jiǎn)單,并且比 cron 任務(wù)的繁忙輪詢高效得多。
可以監(jiān)控文件系統(tǒng)的變化情況,并作出通知響應(yīng);
輔助軟件:inotify-tools

實(shí)驗(yàn)環(huán)境

rsyncd服務(wù)器 192.168.13.128
client服務(wù)器   192.168.13.129

1,在rsyncd服務(wù)器上修改配置文件

[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  ##pid文件
address = 192.168.13.128    ##監(jiān)聽地址
port = 873    ##端口號(hào)
log file = /var/log/rsyncd.log   ##日志文件路徑
hosts allow = 192.168.13.0/24   ##允許地址段訪問
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   ##不需要壓縮的類型

[wwwroot]      ##共享模塊名
path = /var/www/html    ##路徑
comment = www.kgc.com  ##定義名稱
read only = yes    ##只讀
auth users = backuper   ##身份驗(yàn)證用戶名
secrets file = /etc/rsyncd_users.db    ##密碼文件

[root@rsyncd ~]# vim /etc/rsyncd_users.db  ##創(chuàng)建密碼文件
backuper:123123  ##用戶名:密碼
[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db   ##給root用戶讀寫權(quán)限
[root@rsyncd ~]# rsync --daemon   ##開啟rsync服務(wù)
[root@rsyncd ~]# netstat -ntap | grep rsync   ##查看端口
tcp     0     0 192.168.13.128:873      0.0.0.0:*    LISTEN    36346/rsync 
[root@rsyncd ~]# systemctl stop firewalld.service   ##關(guān)閉防火墻
[root@rsyncd ~]# setenforce 0
[root@rsyncd ~]# yum install httpd -y   ##安裝httpd服務(wù)
[root@rsyncd ~]# cd /var/www/html/
[root@rsyncd html]# echo this is test web > index.html   ##創(chuàng)建網(wǎng)頁信息
[root@rsyncd html]# cd ../
[root@rsyncd www]# chmod 777 html/   ##給最大權(quán)限,方便任意用戶操作

2,在客戶端服務(wù)器上,拉取同步源rsyncd

[root@client ~]# systemctl stop firewalld.service  ##關(guān)閉防火墻
[root@client ~]# setenforce 0
[root@client ~]# rpm -q rsync  ##檢查是否安裝rsync服務(wù)
rsync-3.0.9-18.el7.x86_64
[root@client ~]# yum install httpd -y  ##安裝httpd服務(wù)
[root@client ~]# cd /var/www/
[root@client www]# chmod 777 html/  ##給最大權(quán)限
[root@client www]# rsync -avz backuper@192.168.13.128::wwwroot /var/www/html/ 
##拉取共享模塊
##輸入密碼  
[root@client www]# cat html/index.html    ##查看同步情況
this is test web
[root@client www]# rm -rf html/index.html 
[root@client www]# vim /etc/server.pass  ##創(chuàng)建本地的密碼文件
123123
[root@client www]# chmod 600 /etc/server.pass   ##給權(quán)限
[root@client www]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.13.128::wwwroot /var/www/html/ 
##指定本地密碼文件,刪除目標(biāo)位置有而原始位置沒有的文件,實(shí)現(xiàn)免交互

3,在客戶機(jī)上安裝inotify監(jiān)控

[root@client www]# vim /etc/sysctl.conf   ##修改內(nèi)核參數(shù)文件
fs.inotify.max_queued_events = 16384   ##隊(duì)列
fs.inotify.max_user_instances = 1024     ##每個(gè)隊(duì)列中的實(shí)例數(shù)
fs.inotify.max_user_watches = 1048576  ##每個(gè)實(shí)例中的文件數(shù)
[root@client www]# sysctl -p  ##加載
[root@client www]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/   ##掛載
Password for root@//192.168.100.3/LNMP-C7:  
[root@client www]# cd /mnt/
[root@client mnt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/   ##解壓inotify到/opt下
[root@client mnt]# cd /opt/
[root@client opt]# cd inotify-tools-3.14/
[root@client inotify-tools-3.14]# yum install gcc gcc-c   make -y   ##安裝環(huán)境必要的組件
[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/    
##進(jìn)行監(jiān)控
##重啟開啟一個(gè)客戶機(jī)的終端
[root@client ~]# cd /var/www/html/
[root@client html]# touch abc
[root@client html]# rm -rf abc 
##在監(jiān)控上的客戶機(jī)上查看
/var/www/html/ CREATE abc
/var/www/html/ DELETE abc

4,在客戶機(jī)創(chuàng)建腳本,通過inotifywait觸發(fā)rsync同步操作腳本

[root@client inotify-tools-3.14]# cd /opt/
[root@client opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD=inotifywait -mrq -e modify,create,move,delete /var/www/html/
RSYNC_CMD=rsync -avz --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.13.128::wwwroot/
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
 do
     if [ $(pgrep rsync | wc -l) -le 0 ]; then
            $RSYNC_CMD
     fi
done
[root@client opt]# chmod  x inotify.sh  ##給執(zhí)行權(quán)限

##確保服務(wù)端和客戶端的權(quán)限都為最大

5,在rsyncd服務(wù)器上修改配置文件

[root@rsyncd www]# vim /etc/rsyncd.conf
read only = no  ##關(guān)閉只讀
[root@rsyncd www]# netstat -natp | grep rsync
tcp     0    0 192.168.13.128:873    0.0.0.0:*      LISTEN      36346/rsync         
[root@rsyncd www]# kill -9 36346   ##關(guān)閉
[root@rsyncd www]# netstat -natp | grep rsync
[root@rsyncd www]# rm -rf /var/run/rsyncd.pid   ##刪除pid文件
[root@rsyncd www]# rsync --daemon    ##開啟rsync服務(wù)

6,在客戶機(jī)上執(zhí)行inotify腳本文件

[root@client opt]# ./inotify.sh
##重新開啟一個(gè)客戶機(jī)終端
[root@client html]# echo this is test > test.txt  ##添加文本
##查看監(jiān)控服務(wù)信息
[root@client opt]# ./inotify.sh 
sending incremental file list
./
rsync: failed to set times on /. (in wwwroot): Operation not permitted (1)
test.txt

sent 121 bytes  received 30 bytes  302.00 bytes/sec
total size is 30  speedup is 0.20
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
sending incremental file list

sent 66 bytes  received 8 bytes  148.00 bytes/sec
total size is 30  speedup is 0.41

7,在rsync服務(wù)器上查看

[root@rsyncd www]# cd html/
[root@rsyncd html]# ls
index.html  test.txt   ##同步完成
##刪除也是同步的

謝謝閱讀!

更多關(guān)于云服務(wù)器域名注冊(cè),虛擬主機(jī)的問題,請(qǐng)?jiān)L問三五互聯(lián)官網(wǎng):www.shinetop.cn

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。郵箱:3140448839@qq.com。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明出處:三五互聯(lián)知識(shí)庫 » rsync遠(yuǎn)程同步——(實(shí)戰(zhàn)!)

登錄

找回密碼

注冊(cè)