Using inotify and rsync to push the shared directory of nfs server to backup server in real time
1.1 View environment 1.1.1 the environment of the backup server
[root@backup nfsbackup] # uname-a
Linux backup 2.6.32-696.el6.x86_64 # 1 SMP Tue Mar21 19:29:05 UTC 2017 x86 "64 GNU/Linux
Status of iptables
[root@backup nfsbackup] # / etc/init.d/iptables status
Iptables: Firewall is not running.
Status of XElinux
[root@backup nfsbackup] # getenforce
Disabled
Check to see if the rsync service starts
[root@backup nfsbackup] # ss-lntup | grep "rsync"
Tcp LISTEN 0 5: 873: * users: ("rsync", 1294pr. 5)
Tcp LISTEN 0 5 *: 873 *: * users: ("rsync", 1294 pr. 3)
Check the directory of the push is module nfsbackup
[nfsbackup]
Path = / nfsbackup/
Ignore errors
Read only = false
List = false
Hosts allow = 172.16.1.0 Compact 24
# hosts deny = 0.0.0.0amp 32
Auth users = rsync_backup
Secrets file = / etc/rsync.password
1.1.2 nfs server side
[root@nfs01] # uname-a
Linux nfs01 2.6.32-696.el6.x86_64 # 1 SMP Tue Mar 2119 SMP Tue Mar 2915 05 UTC 2017 x86 "64 GNU/Linux
Iptables statu
[root@nfs01 ~] # / etc/init.d/iptables status
Iptables: Firewall is not running.
XElinux statu
[root@nfs01 ~] # getenforce
Disabled
The shared directory we use is nfsbackup
[root@nfs01] # showmount-e
Export list for nfs01:
/ app/r 172.16.1.0/24
/ app/w 172.16.1.0/24
/ data 10.0.0.0/24
/ www 172.16.1.0/24
/ nfsbackup 172.16.1.0/24
Check to see if inotify is available, if there is no download
[root@nfs01 ~] # rpm-qa inotify-tools
Inotify-tools-3.14-1.el6.x86_64
Download method
Yum install inotify-tools-y
Note: inotify-tools belongs to epel source.
View epel
[root@nfs01 ~] # yum repolist
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
* base:mirrors.aliyun.com
* epel:mirrors.aliyun.com
* extras:mirrors.aliyun.com
* updates:mirrors.aliyun.com
Repo id repo name status
Base CentOS-6-Base-mirrors.aliyun.com 6706
Epel Extra Packages for Enterprise Linux6-x86mm 64 12350
Extras CentOS-6-Extras-mirrors.aliyun.com 45
Updates CentOS-6-Updates-mirrors.aliyun.com 383
Repolist: 19484
Change method (change to Ali's source)
# change the default yum source of the system to domestic
Http://mirrors.aliyun.com/
Mv / etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.backup
Wget-O / etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
# # extended yum Source-Supplementary
Epel Source-fedora
Wget-O / etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
Complete in one step:
Mv / etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.backup
Wget-O / etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
Wget-O / etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
Our goal is to push the / nfsbackup module to the backup module on the backup side whenever there is a file or directory change on the NFS side, keeping it up-to-date in real time.
1.2 nfs server setup 1.2.1 creation script
1. Use inotify to monitor the changes of directories and files in / nfsbackup
2. Use the while loop to push the changes of directories and files in / nfsbackup to the nfsbackup module of the backup server
Vim watch.sh
#! / bin/bash
IP= `hostname-i`
NFSBP=/nfsbackup/
# Monitor the change of / nfsbackup and send it to while for line-by-line execution
Inotifywait-mrq $NFSBP-- format "% w% f"-e create,delete,close_write,moved_to | while read line
Do
Rsync-aLz-- delete $NFSBP rsync_backup@172.16.1.41::nfsbackup/$IP/--password-file=/etc/rsync.password
# push the $line file to nfsbackup under 172.16.1.41
Done
1.2.2 testing requires multiple windows
Window 1, [root@nfs01 scripts] # / bin/bash / server/scripts/watch.sh
Window 2, [root@nfs01 nfsbackup] # touch {1.. 4} .log
[root@nfs01 nfsbackup] # echo "Great" > a.txt
Window 3, [root@backup nfsbackup] # ll 172.16.1.31 /
Total 0
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:381.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:382.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:383.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:384.log
[root@backup nfsbackup] # ll 172.16.1.31/
Total 4
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:381.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:38 2.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:383.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:384.log
-rw-r--r-- 1 rsync rsync 7 Jun 28 16:40a.txt
[root@backup nfsbackup] # cat172.16.1.31/a.txt
Greatness
1.2.3 add to boot self-startup
Echo'/ bin/bash / server/scripts/watch.sh & / etc/rc.local
View
[root@nfs01 scripts] # tail-1 / etc/rc.local
/ bin/bash / server/scripts/watch.sh &
Note: & means to execute in the background.
[root@nfs01 scripts] # jobs
[1] + Running / bin/bash/server/scripts/watch.sh &
[root@nfs01 nfsbackup] # jobs-p View the process number of the background command
3362
[root@nfs01 nfsbackup] # kill 3362 the process of deleting this process number