Rsync + inotify for Real-Time Synchronization
Rsync + inotify for Real-Time Synchronization
1. Install rsync
You can use the yum source to install the rsync service.
[Root @ dg test] # yum install rsync
You can also install the source code package
Wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
[Root @ dg test] # tar zxvf rsync-3.0.9.tar.gz
[Root @ dg test] # rsync-3.0.9 cd
[Root @ dg test] #./configure -- prefix =/usr/local/rsync
[Root @ dg test] # make
[Root @ dg test] # make install
Make-j 4 # make Compilation: the source code is compiled into binary. executable file-j 4 is compiled using four processes at the same time. The number of CPUs should not exceed the number of CPUs.
For example, if there is a directory/tmp/test on the source end and there are files under the directory/tmp/test, the target end will be synchronized under 10.10.6.82:/data. However, this method can only be manually synchronized and cannot be synchronized in real time.
[Root @ dg test] # rsync-azP -- delete/tmp/test root@10.10.6.82:/data
Note: rsync is based on the ssh protocol and requires the root and password of the server.
-A, archive (archive) archive mode, indicating that the file is transmitted recursively and maintains a stable attribute. It is equivalent to the parameter-riptgoD.
-Z -- compress indicates Compression
-P: Display transmission speed
-- Delete: delete objects with the target location but not the original location.
Add an ssh key service. You do not need to enter a password next time.
[Root @ dg bin] # 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:
1e: 5f: 6e: fe: 15: a6: c1: 12: d7: 1d: 4c: 79: c5: 9f: a3: 9d root @ dg
Copy trust is related to the target end
[Root @ dg. ssh] # ssh-copy-id root@10.10.6.82
Check that the target segment key has passed
[Root @ rac2. ssh] # ls
Authorized_keys known_hosts
You do not need to enter a password to log on to 10.10.6.82.
[Root @ dg. ssh] # ssh root@10.10.6.82
Last login: Wed Jan 20 10:42:22 2016 from 10.10.8.54
[Root @ rac2 ~] #
2. Install inotify
[Root @ dg test] # tar zxvf inotify-tools-3.14.tar.gz
[Root @ dg test] # inotify-tools-3.14 cd
[Root @ dg test] #./configure -- prefix =/usr/local/inotify
[Root @ dg test] # make
[Root @ dg test] # make install
Inotifywait-mrq-e create, move, delete, modify/tmp/test
-E is used to specify which events you want to monitor. These events include create, create, move, delete, and modify attributes of attrib.
-M indicates continuous monitoring
-R indicates recursive monitoring
-Q: simplified output information
Another window is opened:
Root @ dg test] # cp/etc/group/tmp/test
[Root @ dg ~] # Inotifywait-mrq-e create, move, delete, modify/tmp/test
/Tmp/test/CREATE group
/Tmp/test/MODIFY group
Inotifywait can capture the file creation message.
3. Create an rsync copy script
This function is mainly used to monitor the content in the directory/tmp on the server. If the content is modified (whether it is to add, modify, or delete files) through inotify, and Real-Time Synchronization Through rsync to the client's/tmp, the following is achieved through shell script.
[Root @ dg test] # cat rsync. sh
#! /Bin/bash
Host = 10.10.6.82
Src =/data
Des =/data2
User = root
/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 $ src $ user @ $ host: $ des
Echo "$ {files} was rsynced">/rsync. log 2> & 1
Done
The host is the ip address of the client, src is the directory to be monitored in real time by the server, des is the name of the authenticated module, and must be consistent with the client. The user is the authenticated user in the password file.
Name this script rsync. sh: Put it in the monitored directory. For example, put my file under/tmp and grant 764 permissions. We recommend that you store rsync logs in other directories (non-backup directories ).
[Root @ dg test] # chmod 764 rsync. sh
Then run the script.
[Root @ dg test] # sh/tmp/rsync. sh &
We can also add the rsync. sh script to the startup Item.
Echo "/tmp/rsync. sh">/etc/rc. local
Add or delete files in the source directory, and the target folder will also change.
RSync for file backup Synchronization
Monitor host files and directories using inotifywait
Using inotify + rsync for Linux File batch update
Inotify-tools + rsync real-time file synchronization installation and configuration
Complete rsync synchronization Configuration
Remote synchronization of Rsync in CentOS 6.5
Rsync details: click here
Rsync: click here
This article permanently updates the link address: