Configure Rsync first, as mentioned in the previous section.
Inotify
Install and configure the inotify service
Build the inotify service on 8.8.8.110
1. Download and install
| The code is as follows: |
Copy code |
Wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz Tar xzvf inotify-tools-3.13.tar.gz Cd inotify-tools-3.13 ./Configure -- prefix =/usr/local/inotify Make Make install |
2. Create an inotify_rsync.sh script
(It is best to create a file directly on the server. The. sh file created on windows may not be executed and the conversion format needs to be modified.
Solution:
Vi filename
Run the following command:
: Set ff? # You can see the dos or unix words. If it is in dos format
Then use
: Set ff = unix # force it to unix format, and then save the disk and exit.
Run the script again .)
| The code is as follows: |
Copy code |
Vi inotify_rsync.sh The following is the script content --------------------------------------- #! /Bin/sh # Date: # Function: rysnc 8.8.8.110 to 8.8.8.111 If [! -F/etc/passwd.txt]; then Echo "mypassword">/etc/passwd.txt /Bin/chmod 600/etc/passwd.txt Fi Log =/opt/logs/rsync. log Src = "/home/DemoRoot/Uploads /" Host = "8.8.8.111" Module = "home_www" /Usr/local/inotify/bin/inotifywait-mr -- timefmt '% d/% m/% y % H: % M' -- format' % T % w % F'-e close_write, modify, delete, create, attrib $ src | while read date dir file; do FILECHANGE =$ {DIR }$ {FILE} /Usr/bin/rsync-avH -- delete -- progress -- password-file =/etc/passwd.txt $ src root @ $ host ::$ module & Echo "At $ {TIME} on $ {DATE}, file $ FILECHANGE was backed up via rsync" >>$ log Done |
Script Ended ------------------------------------------------
The related annotations are as follows:
/Usr/local/bin/inotifywait-mrq-e modify, delete, create, attrib $ {src}
-M: Keep Listening
-R is used to recursively View directories.
-Q indicates the output event.
-E close_write, modify, delete, create, and attrib are the "listening for creating a mobile delete write permission" event.
/Usr/bin/rsync-avH -- delete -- progress -- password-file
-A Archive mode
-H save the hard connection
-Delete: delete unnecessary files.
-- Password-file: password file
The parameter can be man rsync
When synchronizing a directory, add the -- exculde = PATTERN parameter to rsync. Note that the path is relative. For details, see man rsync.
To exclude the processing of event listening in a directory, add the -- exclude or -- excludei parameter for inotifywait. For details, see man inotifywait.
-- Exclude-from = "/usr/local/inotify/logs/rules.txt:
For example, exclude files including. svn:
# Cat/usr/local/inotify/logs/rules.txt
-*. Svn *
The inotifywait command generates three return values: "date, time, and file", which are passed as parameters to read, therefore, the "while read d e f" statement in the script details the return value.
Grant the script executable permission
Chmod-R 755 inotify_rsync.sh
Run
Nohup/opt/inotify_rsync.sh &
Display process List
Ps-ef | grep inotify_rsync
Show Logs
Tail-f/opt/logs/rsync. log