rsync+inotify實現實時同步、增量備份
阿新 • • 發佈:2018-08-25
version rev pts 常用 無需 ESS 成功 dex http
主機A:被備份的源主機
主機B:備份的目的主機
- 在linux內核中,默認的inotify機制提供了三個調控參數
[root@fudanwuxi html]# uname -r 3.10.0-693.el7.x86_64 [root@fudanwuxi html]# ll /proc/sys/fs/inotify/ total 0 -rw-r--r--. 1 root root 0 Aug 25 09:32 max_queued_events #監控事件隊列 -rw-r--r--. 1 root root 0 Aug 25 09:32 max_user_instances #最多監控實例數 -rw-r--r--. 1 root root 0 Aug 25 09:32 max_user_watches #每個實例最多監控文件數
[root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_queued_events
16384
[root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_user_instances
128
[root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_user_watches
8192
- 當要監控的目錄、文件數量較多或者變化較頻繁時,要加大這三個參數的值,如下
[root@fudanwuxi html]# vim /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). fs.inotify.max_queued_events = 32768 fs.inotify.max_user_instances = 1024 fs.inotify.max_user_watches = 305564824 ~
[root@fudanwuxi html]# sysctl -p
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 305564824
[root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_queued_events 32768 [root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_user_instances 1024 [root@fudanwuxi html]# cat /proc/sys/fs/inotify/max_user_watches 305564824
- 上傳inotify-tools-3.13.tar.gz並解壓
[root@fudanwuxi ~]# rz
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring inotify-tools-3.13.tar.gz...
100% 380 KB 380 KB/sec 00:00:01 0 Errors
[root@fudanwuxi ~]# pwd
/root
[root@fudanwuxi ~]# ll | grep inotify
-rw-r--r--. 1 root root 389473 Aug 25 11:02 inotify-tools-3.13.tar.gz
[root@fudanwuxi ~]# tar -zxvf inotify-tools-3.13.tar.gz
[root@fudanwuxi ~]# cd inotify-tools-3.13/
[root@fudanwuxi inotify-tools-3.13]# ll
total 1352
-rw-r--r--. 1 guest users 267482 Dec 30 2007 aclocal.m4
-rw-r--r--. 1 guest users 39 Dec 30 2007 AUTHORS
-rw-r--r--. 1 guest users 11301 Dec 30 2007 ChangeLog
-rwxr-xr-x. 1 guest users 45126 Dec 30 2007 config.guess
-rw-r--r--. 1 guest users 2025 Dec 30 2007 config.h.in
-rwxr-xr-x. 1 guest users 32931 Dec 30 2007 config.sub
-rwxr-xr-x. 1 guest users 690681 Dec 30 2007 configure
-rw-r--r--. 1 guest users 1829 Dec 30 2007 configure.ac
-rw-r--r--. 1 guest users 18002 Dec 30 2007 COPYING
-rwxr-xr-x. 1 guest users 15936 Dec 30 2007 depcomp
-rw-r--r--. 1 guest users 9498 Dec 30 2007 INSTALL
-rwxr-xr-x. 1 guest users 9233 Dec 30 2007 install-sh
drwxrwxrwx. 3 guest users 55 Dec 30 2007 libinotifytools
-rw-r--r--. 1 guest users 201510 Dec 30 2007 ltmain.sh
-rw-r--r--. 1 guest users 191 Dec 30 2007 Makefile.am
-rw-r--r--. 1 guest users 19389 Dec 30 2007 Makefile.in
drwxrwxrwx. 2 guest users 87 Dec 30 2007 man
-rwxr-xr-x. 1 guest users 11014 Dec 30 2007 missing
-rw-r--r--. 1 guest users 48 Dec 30 2007 NEWS
-rw-r--r--. 1 guest users 246 Dec 30 2007 README
drwxrwxrwx. 2 guest users 119 Dec 30 2007 src
- 安裝
[root@fudanwuxi inotify-tools-3.13]# ./configure
[root@fudanwuxi inotify-tools-3.13]# make -j 4
[root@fudanwuxi inotify-tools-3.13]# echo $?
0
[root@fudanwuxi inotify-tools-3.13]# make install
- ssh驗證
[root@fudanwuxi ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory ‘/root/.ssh‘.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:djfNU67i/01V6z7FvTTr0sIWDaMJEXxwjiLQUHXG8dM root@fudanwuxi
The key‘s randomart image is:
+---[RSA 2048]----+
| o+...o*+. |
| .. o+=.. |
| . . .o+ E o|
| . .. .= oo|
| S o = Bo+|
| . . + o.*=|
| o =.*|
| . *.*.|
| o.=++|
+----[SHA256]-----+
[root@fudanwuxi ~]#
[root@fudanwuxi ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host ‘192.168.10.178 (192.168.10.178)‘ can‘t be established.
ECDSA key fingerprint is SHA256:plDF/rfSdZkeOwQLGhV7M9NlvcQR78+4n1zWFE/Kw8E.
ECDSA key fingerprint is MD5:c1:79:b6:7e:a3:bb:52:50:d4:47:59:46:8c:87:ad:ee.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]‘s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh ‘[email protected]‘"
and check to make sure that only the key(s) you wanted were added.
此時ssh登錄主機B時候無需輸入密碼
- 腳本
腳本思路:只要檢測到變動或事件時候,執行rsync上行同步操作即可
在主機A上編寫腳本並執行
[root@fudanwuxi ~]# vim superbackup.sh
#!/bin/bash
inotifywait -mrq -e create,move,delete,modify /var/www/html/ | while read a b c
do
rsync -azP --delete /var/www/html/ [email protected]:/web-back
done
~
[root@fudanwuxi ~]# ./superbackup.sh
- 測試,在主機A上被備份的目錄/var/www/html下修改文件
[root@fudanwuxi html]# vim superbackup.txt
如果你看到這個文件,說明實時同步成功
!!!are you ok
~
[root@fudanwuxi html]# echo "再次新增文件,如果看到這個文件,說明OK" > superbackup1.txt
驗證,在主機B查看目的備份目錄/web-back/
[root@fudanwuxi002 ~]# ll /web-back/
總用量 24
-rw-r--r--. 1 root root 31 8月 24 23:35 1.txt
-rw-r--r--. 1 root root 31 8月 24 23:43 2.txt
-rw-r--r--. 1 root root 21 8月 24 23:09 ajie.txt
-rw-r--r--. 1 root root 14 8月 24 23:15 index.txt
-rw-r--r--. 1 root root 69 8月 25 13:24 superbackup.txt
-rw-r--r--. 1 root root 49 8月 24 22:36 testbackup.txt
[root@fudanwuxi002 ~]#
[root@fudanwuxi002 ~]# ll /web-back/
總用量 28
-rw-r--r--. 1 root root 31 8月 24 23:35 1.txt
-rw-r--r--. 1 root root 31 8月 24 23:43 2.txt
-rw-r--r--. 1 root root 21 8月 24 23:09 ajie.txt
-rw-r--r--. 1 root root 14 8月 24 23:15 index.txt
-rw-r--r--. 1 root root 57 8月 25 13:27 superbackup1.txt
-rw-r--r--. 1 root root 69 8月 25 13:24 superbackup.txt
-rw-r--r--. 1 root root 49 8月 24 22:36 testbackup.txt
[root@fudanwuxi002 ~]# cat /web-back/superbackup.txt
如果你看到這個文件,說明實時同步成功
!!!are you ok
[root@fudanwuxi002 ~]# cat /web-back/superbackup1.txt
再次新增文件,如果看到這個文件,說明OK
[root@fudanwuxi002 ~]#
- 優化腳本
將主機A下的/backup/super/V2目錄備份到主機B的/web-back目錄下
腳本如下
點我下載
在主機下的/backup/super/V2目錄先創建文件供測試用
[root@fudanwuxi ~]# mkdir -p /backup/super/V2
[root@fudanwuxi ~]# touch /backup/super/V2/index.php
[root@fudanwuxi ~]# touch /backup/super/V2/index.txt
[root@fudanwuxi ~]# touch /backup/super/V2/index.html
- 讓腳本開機後自動後臺執行
[root@fudanwuxi ~]# echo ‘/root/superbackupV2.sh &‘ >> /etc/rc.local
[root@fudanwuxi ~]# cat !$
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run ‘chmod +x /etc/rc.d/rc.local‘ to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/root/superbackupV2.sh &
[root@fudanwuxi ~]# chmod +x /etc/rc.d/rc.local
驗證
主機A重啟後修改/backup/super/V2/index.php
[root@fudanwuxi ~]# echo "這是重啟後修改,如果你看到這個文件,說明superbackupV2執行成功" > /backup/super/V2/index.php
在主機B查看備份目錄
[root@fudanwuxi002 ~]# ll /web-back/V2/
總用量 4
-rw-r--r--. 1 root root 0 8月 25 14:07 index.html
-rw-r--r--. 1 root root 86 8月 25 14:42 index.php
-rw-r--r--. 1 root root 0 8月 25 14:07 index.txt
[root@fudanwuxi002 ~]# cat /web-back/V2/index.php
這是重啟後修改,如果你看到這個文件,說明superbackupV2執行成功
[root@fudanwuxi002 ~]#
[root@fudanwuxi ~]# ps aux | grep inotify
root 976 0.0 0.0 6524 628 ? S 14:49 0:00 inotifywait -mrq -e create,move,delete,modify,attrib /backup/super/V2
- rsycn常用參數
-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rlptgoD
-p, --perms 保持文件權限
-P 等同於 --partial 保留那些因故沒有完全傳輸的文件,以是加快隨後的再次傳輸
-q, --quiet 精簡輸出模式
-r, --recursive 對子目錄以遞歸模式處理
-t, --times 保持文件時間信息
-v, --verbose 詳細模式輸出
-z, --compress 對備份的文件在傳輸時進行壓縮處理
--delete 刪除那些DST中SRC沒有的文件
- inotifywait常用參數
-m, 持續監視變化
-r, 使用遞歸形式監視目錄
-q, 減少冗余信息,只打印出需要的信息
-e, 指定要監視的事件列表
可監視的事件
事件 描述
access 訪問,讀取文件。
modify 修改,文件內容被修改。
attrib 屬性,文件元數據被修改。
move 移動,對文件進行移動操作。
create 創建,生成新文件
open 打開,對文件進行打開操作。
close 關閉,對文件進行關閉操作。
delete 刪除,文件被刪除。
rsync+inotify實現實時同步、增量備份