Home >Operation and Maintenance >Linux Operation and Maintenance >Share rsync+inotify real-time synchronization example tutorial
1.rsync
Compared with the traditional cp, tar backup methods, rsync has the advantages of high security, fast backup, and supports incremental backup. rsync can solve data backup needs that do not require high real-time performance, such as Regularly back up file server data to remote servers, regularly perform data mirroring on local disks, etc.
As the scale of application systems continues to expand, higher requirements are put forward for data security and reliability, rsync is also gradually exposed in high-end businesses There are many shortcomings. First of all, during rsync real-time synchronization, all files need to be scanned for comparison and differential transmission. If the number of files reaches millions or even tens of millions, scanning all files is very time-consuming, and only a small part of them are changing, which is a very inefficient way. Secondly, rsync cannot monitor and synchronize data in real time. Although it can perform start synchronization through the linux daemon, there will be a time difference between the two start actions, which will lead to inconsistencies between the server and the client, making it impossible to completely recover data in the event of an application failure. . Based on the above reasons, rsync+inotify appeared!
2.inotify (monitoring)
inotify is a powerful, fine-grained, asynchronous file System event monitoring mechanism. Through inotify, you can monitor various subtle events such as addition, deletion, modification, and movement in the file system. Using this kernel interface, third-party software can monitor various events under the file system. The situation changes, and inotify-tools is such a third-party software.
1. Server configuration (only need to install rsync)
Share to /tmp/:
1.Add virtual useruseradd rsync -s /sbin/nologin
chown -R rsync.rsync /tmp/
2.Configurationrsyncd.confConfiguration file
vim rsyncd.conf
#rsync_config_config_start
#rsyncd.conf start
##uid = rsync(user)
gid = rsync(user)
use chroot = no (to prevent security issues)
max connections = 200 (how many clients can connect to my backup server)
timeout = 300 (timeout, disconnect the connection after no action for a long time)
pid file = /var/run/rsyncd.pid (process number, Put the process number in this file)
lock file = /var/run/rsync.lock (equivalent to the concept of "lock", the concept of locking the door in the toilet)
log file = /var/log/rsyncd.log (an error occurred, you can view the log file)
[tmp](module)
path = /tmp/(path)
ignore errors
read only = false (read-only means false, readable and writable)
list = false (not allowed list)
hosts allow = 10.0.0.0/24 (allowed hosts)
hosts deny = 0.0.0.0/32 (denied hosts)
auth users = rsync_backup (support virtual users)
secrets file = /etc/rsync.password (user’s corresponding password file)
#rsync_config_config_______________end
3. Create a password file
echo “rsync_backup:123456” >/etc/rsync.password
All password files600Permissionschmod 600 /etc/rsync.password
4 .rsync --daemon daemonmode startup
5.Addrsync --daemon/etc/rc.local
echo “/ usr/bin/rsync --daemom” >>/etc/rc.local
2. Client configuration
Installationrsyncandinotify
1.Installation rsync(yumJust install it)
2. Create a password authentication file
echo “123456” >/etc/rsync.password Only password required
Set permissions600 chmod 600 /etc/rsync.password
3.Installationinotify:
cd /home/cai/tools/
wget
54 tar xf inotify-tools-3.14.tar.gz
55 ls
56 cd inotify-tools-3.14
57 ./configure --prefix=/usr/local/inotify-tools-3.14
58 make && make install
59 yum install -y gcc
60 ./configure --prefix=/usr/local/inotify-tools-3.14
61 make && make install
62 cd /usr/local/inotify-tools-3.14/
63 ls
64 ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify
4.
Scripts are placed under/server/scripts
vim /server/scripts /rsync.sh
#!/bin/sh
host=192.168.76.129
src=/tmp/
des=tmp
user=rsync_backup
/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 -avz --delete $src $user@$host::$des --password-file=/etc/rsync.password
echo "${files} was rsynced" >> ;/var/log/rsyncd.log 2>&1
done
exit o
~
and give
764 permission
Test script:sh -x /server/scripts/rsync.sh
Run the script:sh /server/scripts/rsync.sh &
Put thersync.sh script into the boot entry: echo “/tmp/rsync.sh” >>/ etc/rc.local
The above is the detailed content of Share rsync+inotify real-time synchronization example tutorial. For more information, please follow other related articles on the PHP Chinese website!