Note: This is the introduction of one-way sync, file synchronization that is A to B, but B is not synchronized to the file A, two-way synchronization of the next article.
rsync and inotify not go directly into combat.
0, background
Two servers IP addresses are:
Source server: 192.168.43.159
The target server: 192.168.43.231
Synchronization direction: down from all the additions and deletions to files in the / root / test directory of the source server (192.168.43.159) real-time synchronization of changes to the target server (192.168.43.231) of / root / www_bak / directory, but the reverse is not synchronized.
Under the source server to install rsync and inotify, just install rsync target server, the source server as a server-side, real-time data is sent to the target server client-side
A source server configuration
1、
First check whether the machine has been installed rsync
View command rpm -qa | grep rsync
After downloading the installation package better into the installation package and increase the installation suffix, mounted to the / usr / local / rsync path
[root@nginx rsync-3.0.9]# ./configure --prefix=/usr/local/rsync [root@nginx rsync-3.0.9]# make [root@nginx rsync-3.0.9]# make install
Note: The installation path is very important, the follow-up to start the rsync service must use the files in that directory
2, password files
[root@nginx rsync-3.0.9]# cd /usr/local/rsync/ [root@nginx rsync]# echo "rsyncpwd" >/usr/local/rsync/rsync1.passwd
Of course, you can also manually edit the file by vim, specify a password for rsyncpwd
600 give permission, or will be error
[root@nginx rsync]# chmod 600 rsync1.passwd
3, install inotify
First check whether the server supports inotify
ll /proc/sys/fs/inotify
If the following three files, which is to support the installation inotify
# Install inotify cd / usr / src /
wget HTTP: // cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz tar zxvf inotify-Tools- 3.14 .tar.gz cd inotify -tools- 3.14 . / the configure --prefix = / usr / local / inotify ## of the mounting location is important because of the need to call the next after installing inotify file path inotifywait file
the make
the make the make install
4, create monitoring and synchronization scripts
Its function is: from / root / test directory of the source server (192.168.43.159) of all files, whether to add, modify, delete files, can be monitored by inotify, and real-time synchronization via rsync to a target server (192.168.43.231) the up / down root / www_bak / directory.
The script to be placed in the path to be monitored, as this is the synchronization path / root / test /
vim /root/test/rsync1.sh
#!/bin/bash host=192.168.43.231 src=/root/test/ des=web user=webuser /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib $src | while read files do /usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync1.passwd $src $user@$host::$des echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done
host: the target server IP; src: local path is synchronized; des: synchronization module name, the target server must be consistent; user: authentication user name, password file must be the target server
Precautions: wherein the / usr / local / inotify / bin / inotifywait path found in the above inotify installation path; /usr/local/rsync/rsync1.passwd path found in said password file path.
Modify the permissions, permission to give 764
[root@nginx tmp]# chmod 764 rsync1.sh
Second, the target server installation configuration
1, the target server also need to install rsync, and the source server.
2, create a password file:
[root@nginx rsync-3.0.9]# cd /usr/local/rsync/ [root@nginx rsync]# echo "webuser:rsyncpwd" >/usr/local/rsync/rsync2.passwd
Also give this file permissions of a 600
chmod 600 /etc/rsync2.passwd
Note: The password file created on the source server, only the password, no user name; established in the password file on the target server, user name and password there.
3, monitor file edit rsync
The files in the mounted monitor the rsync path / usr / local / rsync
vim /usr/local/rsync/rsync2.conf
uid = root gid = root use chroot = no max connections = 10 strict modes = yes pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [web] path = /root/www_bak/ comment = web file ignore errors read only = no write only = no hosts allow = 192.168.43.159 hosts deny = * list = false uid = root gid = root auth users = webuser secrets file = /usr/local/rsync/rsync2.passwd
Third, start the service
a, the source server to start monitoring and synchronization:
sh /root/test/rsync1.sh &
b, start rsync target server monitoring
/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync2.conf
Fourth, the test synchronization