目錄
- rsync服務基本介紹
-
- Rync伺服器
- 項目一:全量備份
- 項目二:增量備份
- rsync指令參數
- 項目三:代替删除(相當于全覆寫)
- 遠端資料備份
- 項目三:rsync守護程序部署
- rsync實時同步
- 關于inotify
-
- 項目四:rsync+inotify
- 項目五:inotify配置
- 項目六:對于inotify監控調整核心參數
rsync服務基本介紹
Rync伺服器
-
Rsync是一款開源的、快速的、多功能的、可實作全量及增量的本地或遠端資料同步備份的優秀工具。并且可以不進行改變原有資料的屬性資訊,實作資料的備份遷移特性。Rsync軟體适用于unix/linux/windows等多種作業系統平台。
Rsync是一個快速和非常通用的檔案複制工具。它能本地複制,遠端複制,或者遠端守護程序方式複制。它提供了大量的參數來控制其行為的各個方面,并且允許非常靈活的方式來實作檔案的傳輸複制。它以其delta-transfer算法聞名。
- rsync監聽端口:873
- rsync運作模式:C/S
全量備份
- 所有資料全部傳送
- 把原來的檔案和新的檔案一起統一傳送
- 全量複制,效率低
假設用戶端上有 file1 file2 file3 檔案,服務端上有 file1 檔案,現要将用戶端上的資料備份至服務端
完全備份方式:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPn1kMRpXTw0EVPBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLyADO0UTO0UTMwMjMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
增量備份
- 在傳輸資料之前通過一些算法通過你有的資料和我有的資料進行對比,把不一樣的資料通過網絡傳輸
- 增量複制,效率較高
假設用戶端上有 file1 file2 file3 檔案,服務端上有 file1 檔案,現要将用戶端上的資料備份至服務端
增量備份方式:
項目一:全量備份
在server1上
[[email protected] ~]# mkdir /backup
[[email protected] ~]# cd /backup/
[[email protected] backup]# touch a b c
[[email protected] backup]# ls -lh
總用量 0
-rw-r--r--. 1 root root 0 12月 30 15:57 a
-rw-r--r--. 1 root root 0 12月 30 15:57 b
-rw-r--r--. 1 root root 0 12月 30 15:57 c
[[email protected] backup]# cd
[[email protected] ~]# rpm -qa | grep rsync
rsync-3.0.9-18.el7.x86_64
[[email protected] ~]# rsync -avz /backup /opt
sending incremental file list
backup/
backup/a
backup/b
backup/c
sent 176 bytes received 73 bytes 498.00 bytes/sec
total size is 0 speedup is 0.00
[[email protected] ~]# cd /opt/
[[email protected] opt]# ls -lh
總用量 0
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[[email protected] opt]# cd
[[email protected] ~]# cd /a
[[email protected] a]# touch 1 2 3
[[email protected] a]# cd
[[email protected] ~]# rsync -avz /a/ /opt/
sending incremental file list
./
1
2
3
sent 159 bytes received 72 bytes 462.00 bytes/sec
total size is 0 speedup is 0.00
[[email protected] ~]# cd /opt/
[[email protected] opt]# ls -lh
總用量 0
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
項目二:增量備份
在server2上
[[email protected] ~]# mkdir /bak
在server1上
[[email protected] opt]# rsync -avz /backup [email protected]20.0.0.11:/bak
The authenticity of host '20.0.0.11 (20.0.0.11)' can't be established.
ECDSA key fingerprint is SHA256:kMJc6r0eXxa/3K7BTIY3Qx/1iIm3JjNxZSPZjEpfJGE.
ECDSA key fingerprint is MD5:fd:2a:2e:5d:cd:e1:0d:2b:39:fa:f9:11:b9:4d:34:09.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '20.0.0.11' (ECDSA) to the list of known hosts.
[email protected]20.0.0.11's password:
sending incremental file list
backup/
backup/a
backup/b
backup/c
sent 176 bytes received 73 bytes 45.27 bytes/sec
total size is 0 speedup is 0.00
在server2上
[[email protected] ~]# cd /bak/
[[email protected] bak]# ls -lh
總用量 0
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
[[email protected] bak]# cd backup/
[[email protected] backup]# ls -lh
總用量 0
-rw-r--r--. 1 root root 0 12月 30 15:57 a
-rw-r--r--. 1 root root 0 12月 30 15:57 b
-rw-r--r--. 1 root root 0 12月 30 15:57 c
備份成功
server1
[[email protected] opt]# cd /opt/
[[email protected] opt]# ls -lh
總用量 0
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[[email protected] opt]# rsync -avz /opt/ [email protected]20.0.0.11:/bak
[email protected]20.0.0.11's password:
sending incremental file list
./
1
2
3
rh/
//因為backup已存在,是以增量備份不會再次備份
sent 235 bytes received 77 bytes 89.14 bytes/sec
total size is 0 speedup is 0.00
server2
[[email protected] ~]# cd /bak/
[[email protected] bak]# ls -lh
總用量 0
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
增量備份成功
rsync指令參數
-a #歸檔模式傳輸, 等于-tropgDl
-v #詳細模式輸出, 列印速率, 檔案數量等
-z #傳輸時進行壓縮以提高效率
-r #遞歸傳輸目錄及子目錄,即目錄下得所有目錄都同樣傳輸。
-t #保持檔案時間資訊
-o #保持檔案屬主資訊
-p #保持檔案權限
-g #保持檔案屬組資訊
-l #保留軟連接配接
-P #顯示同步的過程及傳輸時的進度等資訊
-D #保持裝置檔案資訊
-L #保留軟連接配接指向的目标檔案
-e #使用的信道協定,指定替代 rsh 的 shell 程式
--exclude=PATTERN #指定排除不需要傳輸的檔案模式
--exclude-from=file #檔案名所在的目錄檔案
--bwlimit=100 #限速傳輸
--partial #斷點續傳
--delete #讓目标目錄和源目錄資料保持一緻
項目三:代替删除(相當于全覆寫)
注:源目錄中的檔案對目标目錄做全覆寫
server1
[[email protected] ~]# mkdir /www
[[email protected] ~]# cd /www/
[[email protected] www]# echo "this is aaa" > index.html
[[email protected] www]# ls -lh
總用量 4.0K
-rw-r--r--. 1 root root 12 12月 30 16:15 index.html
[[email protected] www]# cd
[[email protected] ~]# rsync -avz --delete /www/ [email protected]20.0.0.11:/bak
[email protected]20.0.0.11's password:
sending incremental file list
./
deleting rh/
deleting backup/c
deleting backup/b
deleting backup/a
deleting backup/
deleting 3
deleting 2
deleting 1
index.html
sent 103 bytes received 34 bytes 39.14 bytes/sec
total size is 12 speedup is 0.09
server2
[[email protected] ~]# cd /bak/
[[email protected] bak]# ls -lh
總用量 4.0K
-rw-r--r--. 1 root root 12 12月 30 16:15 index.html
替代删除成功
遠端資料備份
rsync下拉操作
server2
[[email protected] ~]# cd /opt/
[[email protected] opt]# vi 111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
[[email protected] opt]# ls -lh
總用量 4.0K
-rw-r--r--. 1 root root 133 12月 30 16:22 111
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
server1
[[email protected] ~]# rsync -avz [email protected]20.0.0.11:/opt/ /opt
[email protected]20.0.0.11's password:
receiving incremental file list
./
111
sent 34 bytes received 114 bytes 59.20 bytes/sec
total size is 133 speedup is 0.90
[[email protected] ~]# cd /opt/
[[email protected] opt]# ls -lh
總用量 4.0K
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 133 12月 30 16:22 111
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[[email protected] opt]# cat 111
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
下拉操作成功
rsync上傳操作
[[email protected] ~]# cd /opt/
[[email protected] opt]# vi 123
[[email protected] opt]# ls -lh
總用量 8.0K
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 133 12月 30 16:22 111
-rw-r--r--. 1 root root 7 12月 30 16:28 123
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[[email protected] ~]# rsync -avz /opt/ [email protected]20.0.0.11:/opt/
[email protected]20.0.0.11's password:
sending incremental file list
1
123
2
3
backup/
backup/a
backup/b
backup/c
sent 411 bytes received 150 bytes 224.40 bytes/sec
total size is 140 speedup is 0.25
server2
[[email protected] opt]# ls -lh
總用量 8.0K
-rw-r--r--. 1 root root 0 12月 30 15:59 1
-rw-r--r--. 1 root root 133 12月 30 16:22 111
-rw-r--r--. 1 root root 7 12月 30 16:28 123
-rw-r--r--. 1 root root 0 12月 30 15:59 2
-rw-r--r--. 1 root root 0 12月 30 15:59 3
drwxr-xr-x. 2 root root 33 12月 30 15:57 backup
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
上傳操作成功
項目三:rsync守護程序部署
server1
修改配置檔案
[[email protected] ~]# vi /etc/rsyncd.conf
# configuration example:
uid = nobody //匿名,删除前面注釋符
gid = nobody //匿名,删除前面注釋符
use chroot = no //不鎖定宿主目錄,删除前面注釋符
max connections = 200 //背景最大連接配接數,删除注釋符
pid file = /var/run/rsyncd.pid //删除注釋符
log file = /var/log/rsync.log //添加日志目錄
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 //指明以那些為字尾的檔案不壓縮
hosts allow = 20.0.0.0/24
address = 20.0.0.10
port 873
[wwwroot] //複制目錄修改内容,及以下内容
path = /www //目錄位置
comment = web site page //目錄描述
read only = yes //隻讀權限
auth users = zhangsan //授權賬戶名
secrets file = /etc/user.db //賬戶資訊存儲檔案(使用者資料庫檔案)
設定值使用者資訊
[[email protected] ~]# vi /etc/user.db
zhangsan:123456 //使用者名和密碼用:隔開
檢視服務狀态
[[email protected] ~]# chmod 600 /etc/user.db //授予隻讀權限
[[email protected] ~]# rsync --daemon //重新開機服務
[[email protected] ~]# netstat -anpt | grep 873 //檢視服務狀态
tcp 0 0 20.0.0.10:873 0.0.0.0:* LISTEN 56540/rsync
建立新檔案
[[email protected] ~]# cd /www/
[[email protected] www]# touch x y z
[[email protected] www]# ls -lh
總用量 4.0K
-rw-r--r--. 1 root root 12 12月 30 16:15 index.html
-rw-r--r--. 1 root root 0 12月 30 16:57 x
-rw-r--r--. 1 root root 0 12月 30 16:57 y
-rw-r--r--. 1 root root 0 12月 30 16:57 z
server2
方法1:
[[email protected] opt]# rsync -avz [email protected]20.0.0.10::wwwroot /bak
Password:
receiving incremental file list
./
x
y
z
sent 121 bytes received 250 bytes 148.40 bytes/sec
total size is 12 speedup is 0.03
[[email protected] opt]# cd /bak/
[[email protected] bak]# ls -lh
總用量 4.0K
-rw-r--r--. 1 root root 12 12月 30 16:15 index.html
-rw-r--r--. 1 root root 0 12月 30 16:57 x
-rw-r--r--. 1 root root 0 12月 30 16:57 y
-rw-r--r--. 1 root root 0 12月 30 16:57 z
成功擷取資訊,配置成功
方法2:
[[email protected] ~]# mkdir /bak2
[[email protected] ~]# rsync -avz rsync://[email protected]20.0.0.10/wwwroot /bak2
Password:
receiving incremental file list
./
index.html
x
y
z
sent 140 bytes received 301 bytes 176.40 bytes/sec
total size is 12 speedup is 0.03
[[email protected] ~]# cd /bak2
[[email protected] bak2]# ls -lh
總用量 4.0K
-rw-r--r--. 1 root root 12 12月 30 16:15 index.html
-rw-r--r--. 1 root root 0 12月 30 16:57 x
-rw-r--r--. 1 root root 0 12月 30 16:57 y
-rw-r--r--. 1 root root 0 12月 30 16:57 z
擷取資訊成功
方法三:免互動
[[email protected] ~]# vi password //放入zhangsan密碼
123456
[[email protected] ~]# ls -lh
總用量 16K
-rw-------. 1 root root 2.3K 12月 12 22:52 anaconda-ks.cfg
-rw-r--r--. 1 root root 2.3K 12月 12 22:53 initial-setup-ks.cfg
-rw-r--r--. 1 root root 228 12月 12 23:17 local.repo
-rw-r--r--. 1 root root 7 12月 30 17:07 password
drwxr-xr-x. 2 root root 6 12月 12 22:54 公共
drwxr-xr-x. 2 root root 6 12月 12 22:54 模闆
drwxr-xr-x. 2 root root 6 12月 12 22:54 視訊
drwxr-xr-x. 2 root root 6 12月 12 22:54 圖檔
drwxr-xr-x. 2 root root 6 12月 12 22:54 文檔
drwxr-xr-x. 2 root root 6 12月 12 22:54 下載下傳
drwxr-xr-x. 2 root root 6 12月 12 22:54 音樂
drwxr-xr-x. 2 root root 6 12月 12 22:54 桌面
[[email protected] ~]# chmod 600 password //設定zhangsan密碼檔案夾為隻讀權限,密碼檔案必須不能為其他人可讀
[[email protected] ~]# rsync -avz --password-file=/root/password rsync://[email protected]20.0.0.10/wwwroot /bak2
receiving incremental file list
sent 61 bytes received 139 bytes 400.00 bytes/sec
total size is 12 speedup is 0.06 //由于之前成功,是以本次沒有檔案生成
擷取資訊成功
rsync實時同步
定期同步的不足
- 執行備份的時間固定,延遲明顯、實時性差
- 當同步源長期不變化時,密集的定期任務是不必要的
實時同步的優點
- 一旦同步源出現變化,立即啟動備份
- 隻要同步源無變化,則不執行備份
關于inotify
Linux核心的inotify機制
- 從版本2.6.13開始提供
- 可以監控檔案系統的變動情況,并做出通知響應
- 輔助軟體: inotify-tools
調整inotify核心參數
- max_queue_events:監控事件隊列大小
- max_user_instances:最多監控執行個體數
- max user watches:每個執行個體最多監控檔案數
安裝inotify-tools輔助工具
- inotifywait:用于持續監控,實時輸出結果
- inotifywatch:用于短期監控,任務完成後再出結果
inotify指令
- #m:持續監聽
- #-r:使用遞歸形式監視目錄
- #-q:減少備援資訊,隻列印出需要的資訊
- #-e指定要監視的事件,多個時間使用逗号隔開
項目四:rsync+inotify
server1、server2上建立html目錄,并安裝httpd
[[email protected] ~]# mkdir -p /var/www/html
[[email protected] ~]# yum -y install httpd
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 56890/httpd
server2
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls -lh
總用量 0
[[email protected] html]# vi index.html
this is aaa!!!
server1
[[email protected] ~]# vi /etc/rsyncd.conf
[wwwroot]
path = /var/www/html //修改
comment = web site page
read only = yes
auth users = zhangsan
secrets file = /etc/user.db
[[email protected] ~]# pkill rsync
[[email protected]server1 ~]# rsync --daemon //重新開機服務
[[email protected] ~]# netstat -anpt | grep 873 //檢視狀态
tcp 0 0 20.0.0.10:873 0.0.0.0:* LISTEN 57062/rsync
server2
[[email protected] ~]# rsync -avz --delete /var/www/html/ [email protected]20.0.0.10:/var/www/html
[email protected]20.0.0.10's password:
sending incremental file list
./
index.html
sent 110 bytes received 34 bytes 41.14 bytes/sec
total size is 15 speedup is 0.10
server1
[[email protected] ~]# curl http://localhost
this is aaa!!!
檔案同步成功
項目五:inotify配置
server2
導入并解包inotify-tools-3.14.tar.gz
[[email protected] ~]# tar zxvf inotify-tools-3.14.tar.gz
[[email protected] ~]# cd inotify-tools-3.14/
[[email protected] inotify-tools-3.14]# ./configure && make && make install
[[email protected] inotify-tools-3.14]# cd
[[email protected] ~]# inotifywait -mrq -e modify,create,delete,move /var/www/html //開啟持續監聽
在xshell中再次打開一個server2視窗(以下稱為server3)
server3
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls -lh
總用量 4.0K
-rw-r--r--. 1 root root 15 12月 30 17:27 index.html
[[email protected] html]# touch a.html
[[email protected] html]# touch b.html
此時檢視到server2上監聽出現新資訊
[[email protected] html]# vi a.html
server a
此時再次檢視server2
[[email protected] html]# touch a
[[email protected] html]# rm -rf a
再次檢視server2
附:判斷是否是伺服器
server1
[[email protected] ~]# pgrep rsync
57062
server2
配置自動檢測腳本
[[email protected] ~]# vi /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,delete,move /var/www/html"
RSYNC_CMD="rsync -avz --delete --password-file=/root/password /var/www/html/ [email protected]::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ]
then $RSYNC_CMD
fi
done
server1
修改rsync配置檔案
[[email protected] ~]# vi /etc/rsyncd.conf
[wwwroot]
path = /var/www/html
comment = web site page
read only = no //修改為no
auth users = zhangsan
secrets file = /etc/user.db
[[email protected] ~]# ls -l /var/www/
總用量 0
drwxr-xr-x. 2 root root 6 11月 17 00:19 cgi-bin
drwxr-xr-x. 2 root root 24 12月 30 17:28 html
[[email protected] ~]# chmod 777 /var/www/html //賦予html目錄寫權限
[[email protected] ~]# ls -l /var/www/
總用量 0
drwxr-xr-x. 2 root root 6 11月 17 00:19 cgi-bin
drwxrwxrwx. 2 root root 24 12月 30 17:28 html
server2
server3
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# vi e.html
server e
server2上檢視
server1
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls -lh
總用量 12K
-rw-r--r--. 1 nobody nobody 9 12月 30 17:55 a.html //拷貝成功
-rw-r--r--. 1 nobody nobody 0 12月 30 17:54 b.html
-rw-r--r--. 1 nobody nobody 9 12月 30 18:21 e.html
-rw-r--r--. 1 root root 15 12月 30 17:27 index.html
[[email protected] html]# curl http://localhost/e.html
server e
項目六:對于inotify監控調整核心參數
server3
[[email protected] html]# vi /etc/sysctl.conf
//添加以下
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 104876
備注:
- fs.inotify.max_queued_events = 16384 //監控事件的最大隊列數
- fs.inotify.max_user_instances = 1024 //監控最大執行個體數
- fs.inotify.max_user_watches = 104876 //監控每個執行個體的最大檔案數
[[email protected] html]# sysctl -p //寫入系統記憶體
//調完參數需立即啟用
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 104876
配置完成