Inotify + rsync for real-time synchronization deployment, inotifyrsync
1. Architecture Planning
1.1.1 Architecture Planning preparation
Server System |
Role |
IP |
Centos6.7 x86_64 |
NFS-server-inotify-tools) |
192.168.1.14 |
Centos6.7 x86_64 |
Rsync server (rsync Hot Standby server) |
192.168.1.17 |
1.1.2 Architecture
2.1 pre-deployment check
2.1.1 check whether the rsync Hot Standby server daemon is up
1 [root@backup data]# lsof -i:8732 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME3 rsync 3071 root 3u IPv4 18684 0t0 TCP *:rsync (LISTEN)4 rsync 3071 root 5u IPv6 18685 0t0 TCP *:rsync (LISTEN)5 [root@backup data]# ps -ef|grep rsync|grep -v grep6 root 3071 1 0 Dec02 ? 00:00:00 rsync --daemon7 [root@backup data]#
2.1.2 the NFS server is a client relative to the rsync server. Therefore, you must configure a client password.
1 [root@nfs-server ~]# echo "zhurui" >>/etc/rsync.password2 [root@nfs-server ~]# cat /etc/rsync.password 3 zhurui4 [root@nfs-server ~]# chmod 600 /etc/rsync.password 5 [root@nfs-server ~]# ll /etc/rsync.password 6 -rw-------. 1 root root 7 Jan 31 00:49 /etc/rsync.password7 [root@nfs-server ~]#
2.1.3 manually push data from the NFS client/data/directory to the rsync Hot backup Server/backup Directory
1 [root @ nfs-server data] # showmount-e localhost # Check the shared directory 2 Export list for localhost: 3/data 192.168.1.0/24 4 [root @ nfs-server ~] # Cd/data/5 [root @ nfs-server data] # ll 6 total 12 7-rw-r --. 1 nfsnobody 0 Jan 30 0:05 beautiful.txt 8-rw-r --. 1 nfsnobody 9 Jan 30 f11.txt 9-rw-r --. 1 root 0 Jan 30 fs.txt 10 drwxr-xr-x. 2 nfsnobody 4096 Jan 30 huang11-rw-r --. 1 nfsnobody 0 Jan 30 ree0000txt 12-rw-r --. 1 root 0 Jan 30 test1.txt13-rw-r --. 1 nfsnobody 0 Jan 30 wd.txt 14 drwxr-xr-x. 2 nfsnobody 4096 Jan 30 zhurui15-rw-r --. 1 nfsnobody 0 Jan 30 zhurui1.txt16-rw-r --. 1 nfsnobody root 0 Jan 30 zhurui.txt 17 [root @ nfs-server/] # rsync-avz/data/rsync_backup@192.168.1.17: backup/-- password-file =/etc/rsync. password ## push Data command 18 sending incremental file list19. /20 beautiful.txt 21 bytes fs.txt 23 ree0000txt 24 test1.txt25 wd.txt 26 zhurui.txt 27 bytes huang/29 zhurui/30 31 sent 572 bytes encoded ed 171 bytes 114.31 bytes/sec32 total size is 9 speedup is 0.0133 [root @ nfs-server/] #
2.1.4 rsync server check/backup directory data
3.1 Service deployment
3.1.1 install inotify on the NFS server
Inofity-tools:Http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
1) check whether the current system supports inotify
1 [root @ nfs-server tools] # uname-r # kernel version, which starts from kernel2.6.13 and supports 2 2.6.32-573. el6.x86 _ 64 3 [root @ nfs-server tools] # ls-l/proc/sys/fs/inotify/# Check whether 4 total 05-rw-r -- 1 root 0 Jan 31 0:34 max_queued_events6-rw-r -- 1 root 0 Jan 31 0:34 max_user_instances7-rw-r -- 1 root 0 Jan 31 0:34 max_user_watches8 ## three files are supported.
2) download the inotify source code package
1 [root@nfs-server tools]# ll2 total 3523 -rw-r--r--. 1 root root 358772 Mar 14 2010 inotify-tools-3.14.tar.gz
3) Compile and install inotify
1 [root@nfs-server tools]# tar zxf inotify-tools-3.14.tar.gz 2 [root@nfs-server tools]# cd inotify-tools-3.143 [root@nfs-server inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.144 [root@nfs-server inotify-tools-3.14]# make&&make install
1 [root @ nfs-server inotify-tools-3.14] # ln-s/usr/local/inotify-tools-3.14 // usr/local/inotify-tools # create soft link 2 [root @ nfs-server inotify-tools-3.14] # cd .. 3 [root @ nfs-server local] # ll 4 total 44 5 drwxr-xr-x. 2 root 4096 Sep 23 2011 bin 6 drwxr-xr-x. 2 root 4096 Sep 23 2011 etc 7 drwxr-xr-x. 2 root 4096 Sep 23 2011 games 8 drwxr-xr-x. 2 root 4096 Sep 23 2011 include 9 lrwxrwxrwx. 1 root 30 Jan 31 0inotify-tools->/usr/local/inotify-tools-3.14/10 drwxr-xr-x. 6 root 4096 Jan 31 0:55 inotify-tools-3.1411 drwxr-xr-x. 2 root 4096 Sep 23 2011 lib12 drwxr-xr-x. 2 root 4096 Sep 23 2011 lib6413 drwxr-xr-x. 2 root 4096 Sep 23 2011 libexec14 drwxr-xr-x. 2 root 4096 Sep 23 2011 sbin15 drwxr-xr-x. 5 root 4096 Nov 25 listen 16 drwxr-xr-x. 2 root 4096 Sep 23 2011 src17 [root @ nfs-server local] # cd inotify-tools18 [root @ nfs-server inotify-tools] # ll19 total 16
After a message is displayed, four directories are generated:
1 [root @ nfs-server/] # cd/usr/local/inotify-tools2 [root @ nfs-server inotify-tools] # ll3 total 164 drwxr-xr-x. 2 root 4096 Jan 31 bin # inotify Execute Command (Binary) 5 drwxr-xr-x. 3 root 4096 Jan 31 include # header file 6 drwxr-xr-x required by the inotify program. 2 root 4096 Jan 31 lib # dynamically linked library file 7 drwxr-xr-x. 4 root 4096 Jan 31 share # Help document
Tool Set introduction:
Two tools (commands) are installed, namely inotifywait and inotifywatch.
Inotifywait: waits for a specific File System Event (open, close, delete, etc.) on the monitored file or directory and is blocked after execution. It is suitable for use in shell scripts.
Inotifywatch: collects statistics on the usage of monitored file systems and specifies the number of times a file system event occurs.
4) Detailed description of common parameters of inotify commands
1 [root @ nfs-server inotify-tools] #. /bin/inotifywait -- help 2 inotifywait 3.14 3 Wait for a special event on a file or set of files. 4 Usage: inotifywait [options] file1 [file2] [file3] [...] 5 Options: 6-r | -- recursive Watch directories recursively. # recursive query directory 7-q | -- quiet Print less (only print events) # Print a small amount of information, only the monitoring information is printed 8-m | -- monitor Keep listening for events forever. without 9 this option, inotifywait will exit after one10 event is already ed. # always keep the event listening status 11 -- excludei <pattern> 12 Like -- exclude but case insensitive. # excluding files or directories, Case Insensitive 13 -- timefmt <fmt> strftime-compatible format string for use with14 % T in -- format string. # output format of the specified time
5) test monitoring events
Open two windows
1. Test create
1 Step 1: 2 [root @ nfs-server inotify-tools] #/usr/local/inotify-tools/bin/inotifywait-mrq -- timefmt '% d/% m/% y % H: % m' -- format' % T % w % F'-e create/data # after the current command is executed, the interface is blocked. Only when another client is testing, the monitoring status is 3 31/01/16/data/ceshi.txt 4 31/01/16/data/what 5 31/01/16/data/a 6 31/01/16/data/ B 7 31/01/16/data/c 8 31/01/16/data/d 9 31/01/16/data/e10 31/01/16/data/f11 12 step 2: 13. Clone an NFS client and switch to the/datadirectory to create the ceshi.txt and what files, observe monitoring changes 14 [root @ nfs-server data] # touch ceshi.txt 15 [root @ nfs-server data] # mkdir/who16 [root @ nfs-server data] # touch what17 [root @ nfs-server data] #18 [root @ nfs-server data] # touch {.. f}
2. Test delete
1 [root @ nfs-server inotify-tools] #/usr/local/inotify-tools/bin/inotifywait-mrq -- timefmt '% d/% m/% y % H: % m' -- format' % T % w % F'-e delete/data # monitoring/data DIRECTORY deletion time 2 3 monitoring is as follows: 4 5 31/01/16/data/ree0000txt 6 31/01/16/data/test1.txt7 8 [root @ nfs-server data] # rm-f ree0000txt test1.txt # Delete these two files 9 [root @ nfs-server data] #
3,At the same timeTest create and delete
1 Step 1: 2 [root @ nfs-server inotify-tools] #/usr/local/inotify-tools/bin/inotifywait-mrq -- timefmt '% d/% m/% y % H: % m' -- format' % T % w % F'-e delete, create/data 3 monitoring output information: 4 31/01/16/data/hero. sh 5 31/01/16/data/a 6 31/01/16/data/f 7 31/01/16/data/B 8 31/01/16/data/c 9 31/01/ 16/data/d10 31/01/16/data/e11 Step 2: 12 [root @ nfs-server data] # touch hero. sh13 [root @ nfs-server data] # ll14 [root @ nfs-server data] # rm-f [.. f] 15 [root @ nfs-server data] # rm-f {.. f} 16 [root @ nfs-server data] #
Generally used in work: [root @ nfs-server inotify-tools] #/usr/local/inotify-tools/bin/inotifywait-mrq -- timefmt '% d/% m/% y % H: % m' -- format' % T % w % F'-e delete, create, close_write/data
Real-time synchronization using scripts in the work environment:
1 #!/bin/bash 2 inotify=/usr/local/inotify-tools/bin/inotifywait 3 $inotify -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data 4 \ 5 |while read file 6 do 7 cd / && 8 rsync -az /data --delete rsync_backup@192.168.1.17::backup \ 9 --password-file=/etc/rsync.password10 done
Test:
1. The script is Enabled: 2 [root @ nfs-server scripts] # sh-x inotify. sh 3 + inotify =/usr/local/inotify-tools/bin/inotifywait 4 + read file 5 +/usr/local/inotify-tools/bin/inotifywait-mrq -- timefmt' % d/% m/% y % H: % m' -- format' % T % w % F'-e delete, create, close_write/data 6 + cd/7 + rsync-az/data -- delete rsync_backup@192.168.1.17 :: backup -- password-file =/etc/rsync. password 8 + read file 9 + cd/10 + rsync-az/data-delete rsync_backup@192.168.1.17 :: backup -- password11 then create file 12 [root @ NFS-server data] # touch {.. z} 13 [root @ nfs-server data] # echo I am best> a # modify the content in, check whether the Hot Standby server synchronizes to the 14th 15 rsync Hot Standby server: 16 [root @ backup //] # cd/backup/17 [root @ backup] # cd data/18 [root @ backup data] # ll19 total 020-rw-r -- r --. 1 rsync 0 Mar 4 2016 a21-rw-r --. 1 rsync 0 Mar 4 2016 b22-rw-r --. 1 rsync 0 Mar 4 2016 c23-rw-r --. 1 rsync 0 Mar 4 2016 d24-rw-r --. 1 rsync 0 Mar 4 2016 e25-rw-r --. 1 rsync 0 Mar 4 2016 f26-rw-r --. 1 rsync 0 Mar 4 2016 g27-rw-r --. 1 rsync 0 Mar 4 2016 h28-rw-r --. 1 rsync 0 Mar 4 2016 i29-rw-r --. 1 rsync 0 Mar 4 2016 j30-rw-r --. 1 rsync 0 Mar 4 2016 k31-rw-r --. 1 rsync 0 Mar 4 2016 l32-rw-r --. 1 rsync 0 Mar 4 2016 m33-rw-r --. 1 rsync 0 Mar 4 2016 n34-rw-r --. 1 rsync 0 Mar 4 2016 o35-rw-r --. 1 rsync 0 Mar 4 2016 p36-rw-r --. 1 rsync 0 Mar 4 2016 q37-rw-r --. 1 rsync 0 Mar 4 2016 r38-rw-r --. 1 rsync 0 Mar 4 2016 s39-rw-r --. 1 rsync 0 Mar 4 2016 t40-rw-r --. 1 rsync 0 Mar 4 2016 u41-rw-r --. 1 rsync 0 Mar 4 2016 v42-rw-r --. 1 rsync 0 Mar 4 2016 w43-rw-r --. 1 rsync 0 Mar 4 2016x44-rw-r --. 1 rsync 0 Mar 4 2016 y45-rw-r --. 1 rsync 0 Mar 4 2016 z46 [root @ backup data] # cat a47 I am best # show that the data has been synchronized in real time 48 [root @ backup data] #
6) Optimization
1 [root@nfs-server scripts]# echo "50000000" >/proc/sys/fs/inotify/max_user_watches 2 [root@nfs-server scripts]# echo "50000000" >/proc/sys/fs/inotify/max_queued_events
Conclusion: after testing, 200 concurrent files per second, there is almost no latency in data synchronization (less than 1 second)