inotify+rsync real-time backup
Environmental preparation
Two centos7, close the firewall and selinux
A backup end——192.168.112.153 [server side]
B backup source——192.168.112.172 [client side]
Introduction
- Inotify is a powerful, fine-grained, asynchronous file system event monitoring mechanism. The Linux kernel has added Inotify support since 2.6.13. Inotify can monitor the addition, deletion, modification, and movement of the file system. Event, using this kernel interface, third-party software can monitor various changes of files under the file system, and inotify-tools is such a third-party software
- Rsync has the advantages of high security, fast backup, and support for incremental backup. Through rsync, data backup requirements that do not require real-time performance can be solved, but rsync cannot monitor and synchronize data in real time.
principle
- Inotify only needs to be deployed in the synchronization client, when the monitored file changes, the rsync script is triggered to synchronize
Operation on A
1. Install EPEL
yum -y install epel-release
2. Install inotify and rsync services
yum -y install inotify-tools rsync
3. Modify the rsync configuration file
vim /etc/rsyncd.conf
uid=root
gid=root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[mysqldata]
path = /data/mysqldata
comment = mysql data
ignore errors
read only = no
write only = no
hosts allow = 192.168.112.172
list = false
auth users = rsync_user
secrets file = /etc/rsync.password
- Note: ip writes the ip of B [that is, the ip of the client],
and the path of path needs to be created! ! !
4. Create a directory
mkdir -p /data/mysqldata
5. Create rsync user name and password files and add permissions
echo "rsync_user:rsync_user_pwd" > /etc/rsync.password
chmod -R 600 /etc/rsync.password
6. Start the service and verify the port
rsync --daemon
netstat -nlpt|grep 873
Operation on B
7. Install EPEL
yum -y install epel-release
8. Install inotify and rsync services
yum -y install inotify-tools rsync
9. Set the client password file and modify permissions
echo "rsync_user_pwd" > /etc/rsync.password
chmod -R 600 /etc/rsync.password
10. Scripting
vim bf.sh
#!/bin/bash
ip=192.168.112.153
src=/data/mysqldata_src/
dst=mysqldata
user=rsync_user
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${
src} \
| while read file
do
rsync -vzrtopg --delete --progress $src $user@$ip::$dst --password-file=/etc/rsync.password > /dev/null && echo "$src was rsyncd"
done
- Note: The path on line 6, yum is installed, the
/usr/bin/inotifywait
source is installed,/usr/local/bin/inotifywait
and the src path needs to be created! ! ! - Path verification
11. Create a directory
mkdir -p /data/mysqldata_src
12. Execute the script
sh bf.sh
13. Enter the corresponding directory and create a file
cd /data/mysqldata_src/
touch aa.txt
14. Switch back to A and check
cd /data/mysqldata/
ls