rsync+inotify

公司須要用到備份文件,所以整理了一篇文章,但願對讀者有所幫助。node

工做環境:vim

主機名 IP 操做系統 版本
openstack 192.168.199.7 rhel7.4 rsync  version 3.0.9
node2 192.168.199.8 rhel7.4 rsync  version 3.0.9

rhel7版本已經默認裝了rsync。ruby

編譯rsync服務的主配置文件:bash

[root@openstack ~]# vim /etc/rsyncd.conf

  1 # /etc/rsyncd: configuration file for rsync daemon mode
  2
  3 # See rsyncd.conf man page for more options.
  4
  5 # configuration example:
  6
  7 uid = nobody
  8 gid = nobody
  9 address = 192.168.199.7
 10 port = 873
 11 hosts allow = 192.168.199.8
 12 use chroot = yes   #用戶登陸進來只在固定的目錄裏
 13 max connections = 4
 14 pid file = /var/run/rsyncd.pid
 15 lock file = /var/run/rsyncd.lock
 16 log file = /var/run/rsyncd.log
 17 motd file = /etc/rsyncd.motd
 18 [wwwroot]
 19 path = /essfiles/
 20 comment = database
 21 read only = yes
 22 list = yes
 23 auth users = rsyncuser
 24 secrets file = /etc/rsync.password
[root@openstack ~]# vim /etc/rsync.password
rsyncuser:passwd123服務器

[root@openstack ~]# chmod 600 /etc/rsync.password
[root@openstack ~]# vim /etc/rsyncd.motd
welcome to back zjbq_file
[root@openstack ~]# rsync --daemon
[root@openstack ~]# ps -aux|grep rsync
root       6405  0.0  0.0 114652   312 ?        Ss   10:40   0:00 rsync --daemon
root      58231  0.0  0.0 112680   984 pts/2    S+   13:46   0:00 grep --color=auto rsync
建立須要備份的目錄dom

[root@openstack ~]# mkdir /essfiles/ssh

建立測試數據:ide

[root@openstack ~]# mkdir /essfiles/{1..10}.txt
[root@openstack ~]# ll /essfiles/
總用量 0
drwxr-xr-x. 2 root root 6 11月 18 13:51 10.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 1.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 2.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 3.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 4.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 5.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 6.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 7.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 8.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 9.txt工具

而後在備份服務器上進行備份:測試

[root@node2 ~]# mkdir /essfiles-back/    建立備份的目錄
[root@node2 essfiles-back]# rsync -avz rsyncuser@192.168.199.7::wwwroot /essfiles-back/   進行備份,此時是須要輸入密碼的。
welcome to back zjbq_file

Password:
receiving incremental file list
./
1.txt/
10.txt/
2.txt/
3.txt/
4.txt/
5.txt/
6.txt/
7.txt/
8.txt/
9.txt/

sent 105 bytes  received 299 bytes  62.15 bytes/sec
total size is 0  speedup is 0.00
[root@node2 essfiles-back]#

[root@node2 essfiles-back]# ll   查看文件已經備份過來了。
總用量 0
drwxr-xr-x. 2 root root 6 11月 18 2018 10.txt
drwxr-xr-x. 2 root root 6 11月 18 2018 1.txt
drwxr-xr-x. 2 root root 6 11月 18 2018 2.txt
drwxr-xr-x. 2 root root 6 11月 18 2018 3.txt
drwxr-xr-x. 2 root root 6 11月 18 2018 4.txt
drwxr-xr-x. 2 root root 6 11月 18 2018 5.txt
drwxr-xr-x. 2 root root 6 11月 18 2018 6.txt
drwxr-xr-x. 2 root root 6 11月 18 2018 7.txt
drwxr-xr-x. 2 root root 6 11月 18 2018 8.txt
drwxr-xr-x. 2 root root 6 11月 18 2018 9.txt

上面的備份是須要輸入密碼的,此時在本地設置一個密碼文件,而後指定密碼文件就能夠免密了。
[root@node2 ~]# vim /etc/rsync.password   
passwd123

[root@node2 essfiles-back]# rm -rf *     刪掉以前的數據
[root@node2 essfiles-back]# ll
總用量 0
[root@node2 essfiles-back]# rsync -avz rsyncuser@192.168.199.7::wwwroot --password-file=/etc/rsync.password /essfiles-back/
welcome to back zjbq_file

receiving incremental file list
./
1.txt/
10.txt/
2.txt/
3.txt/
4.txt/
5.txt/
6.txt/
7.txt/
8.txt/
9.txt/

sent 105 bytes  received 299 bytes  808.00 bytes/sec
total size is 0  speedup is 0.00
[root@node2 essfiles-back]# ll
總用量 0
drwxr-xr-x. 2 root root 6 11月 18 13:51 10.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 1.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 2.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 3.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 4.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 5.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 6.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 7.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 8.txt
drwxr-xr-x. 2 root root 6 11月 18 13:51 9.txt
此時就能夠免密備份了。

使用腳本自動備份:

[root@node2 ~]# vim rsync.sh
#!/bin/bash
rsync -avz rsyncuser@192.168.199.7::wwwroot --password-file=/etc/rsync.password /essfiles-back/

[root@node2 ~]# chmod +x rsync.sh
而後加入計劃任務就能夠實現自動備份了。

rsync+inotify實時同步

Linux 內核從 2.6.13 版本開始提供了 inotify 通知接口,用來監控文件系統的各類變化狀況,如文件存取、刪除、移動等。利用這一機制,能夠很是方便地實現文件異動告警、增量備份,並針對目錄或文件的變化及時做出響應。

 [root@openstack ~]# uname -r   查看內核版本
3.10.0-693.el7.x86_64

安裝inotify-tools工具

能夠yum安裝也能夠進行編譯安裝

這裏進行編譯安裝

[root@openstack ~]# tar zxvf inotify-tools-3.13.tar.gz -C /usr/local/src/
[root@openstack src]# cd inotify-tools-3.13/
[root@openstack inotify-tools-3.13]# ./configure --prefix=/usr/local/inotify-tools && nake && make install
設置軟鏈接或者加入到環境變量中,這樣方便調用。
[root@openstack ~]# tail -n 1 /etc/profile
export PATH=/usr/local/inotify-tools/bin:$PATH

[root@openstack ~]# source /etc/profile    使生效。
或者設置軟連接

[root@openstack ~]# ln -s /usr/local/inotify-tools/bin/* /usr/bin/

inotifywait經常使用參數:

-e  用來指定要監控哪些事件。這些事件包括: create 建立,move 移動,delete 刪除,modify 修改文件內容,attrib 屬性更改。

-m 表示持續監控

-r  表示遞歸整個目錄

-q 表示簡化輸出信息。

[root@openstack ~]# inotifywait -mrq -e create,move,delete,modify /essfiles/   

首先進行一下監測:建立,刪除,更改均可以監測到。

使用監本進行自動監測:

先解決免密登陸的問題,和備份服務器之間作雙機互信

[root@openstack ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:de+jPX1m6c7ycwcgtmCvLhplvv9Y6xV7QG0wJhw1sAE root@openstack
The key's randomart image is:
+---[RSA 2048]----+
|        Eo=+*    |
|          .= =   |
|          o o o  |
|        o.oo.o   |
|      o.S+ oo..  |
|     +    o  =.  |
|    . .  .. o +o.|
|     ....o o =o+B|
|    ...++o+ . *O=|
+----[SHA256]-----+
[root@openstack ~]# ssh-copy-id 192.168.199.8
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.199.8 (192.168.199.8)' can't be established.
ECDSA key fingerprint is SHA256:WkwmW0ea32S6aePjAp6PsRwKDPCnGVsnfAhnNzXHr2I.
ECDSA key fingerprint is MD5:a9:66:50:e4:c9:95:5a:cb:50:48:2a:2d:fb:1c:65:d9.
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
root@192.168.199.8's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.199.8'"
and check to make sure that only the key(s) you wanted were added.

[root@openstack ~]#

[root@openstack ~]# vim inotify.sh
#!/bin/bash
SRC=/essfiles/
DST=root@192.168.199.8:/essfiles-back/
/usr/local/inotify-tools/bin/inotifywait -mrq -e create,move,delete ${SRC}| while read file
do
rsync -avz --delete $SRC $DST  #如果在生產中,在服務器中刪掉文件的話,備份服務器上也會刪掉,去掉--delete參數就不會了。
done

[root@openstack ~]# chmod +x inotify.sh
[root@openstack ~]# crontab -e
* * * * * bash /root/inotify.sh

而後運行這個腳本,而後觸發動做。

讓腳本在後臺運行也能夠實現該功能。

rsync -avz --delete $SRC $DST >/dev/null 2>&1

nohup /bin/bash inotify.sh 2>1&

[root@openstack ~]# ./inotify.sh
[root@openstack essfiles]# touch {1..9}.txt

不管是建立,複製仍是修改,備份服務器上都進行了修改。

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息