使用rsync+inotify实现/www目录实时同步
inotify:对同步数据目录信息的监控 rsync:完成对数据的同步
环境准备:
IP | 操作系统及版本 | 描述 |
---|
10.0.0.7 | CentOS-7.9 | data-server,inotify | 10.0.0.17 | CentOS-7.9 | backup-server,rsync |
[root@data ~]
total 0
-rw-r--r-- 1 root root 0 Mar 21 09:01 max_queued_events
-rw-r--r-- 1 root root 0 Mar 21 09:01 max_user_instances
-rw-r--r-- 1 root root 0 Mar 21 09:01 max_user_watches
[root@data ~]
16384
[root@data ~]
128
[root@data ~]
8192
inotify 内核参数说明:
- max_queued_events:inotify 事件队列最大长度,如值太小会出现 Event Queue Overflow 错误,默认值:16384, 生产环境建议调大,比如:327679
- max_user_instances:每个用户创建inotify实例最大值,默认值:128
- max_user_watches:可以监视的文件的总数量(inotifywait 单进程),默认值:8192,建议调大
[root@data ~]
fs.inotify.max_queued_events=66666
fs.inotify.max_user_watches=100000
[root@data ~]
fs.inotify.max_queued_events = 66666
fs.inotify.max_user_watches = 100000
[root@data ~]
66666
128
100000
1、安装inotify-tools:基于epel源
[root@data ~]
[root@data ~]
/usr/bin/inotifywait
/usr/bin/inotifywatch
/usr/lib64/libinotifytools.so.0
/usr/lib64/libinotifytools.so.0.4.1
/usr/share/doc/inotify-tools-3.14
/usr/share/doc/inotify-tools-3.14/AUTHORS
/usr/share/doc/inotify-tools-3.14/COPYING
/usr/share/doc/inotify-tools-3.14/ChangeLog
/usr/share/doc/inotify-tools-3.14/NEWS
/usr/share/doc/inotify-tools-3.14/README
/usr/share/man/man1/inotifywait.1.gz
/usr/share/man/man1/inotifywatch.1.gz
inotify-tools包主要工具:
- inotifywait: 在被监控的文件或目录上等待特定文件系统事件(open ,close,delete等)发生, 常用于实时同步的目录监控
- inotifywatch:收集被监控的文件系统使用的统计数据,指文件系统事件发生的次数统计
2、创建rsync服务器的配置文件
[root@backup ~]
rsync-3.1.2-10.el7.x86_64
[root@backup ~]
uid = root
gid = root
max connections = 0
ignore errors
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
exclude = lost+found/
lock file = /var/run/rsyncd.lock
reverse lookup = no
[backup]
path = /data/backup/
comment = backup dir
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.pas
3、准备备份目录
[root@backup ~]
4、生成验证文件
[root@backup ~]
[root@backup ~]
5、启动rsyncd服务
[root@backup ~]
[root@backup ~]
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 5 *:873 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 5 [::]:873 [::]:*
6、数据服务器配置密码文件
[root@data ~]
[root@data ~]
7、查看远程rsync服务器的模块信息
[root@data ~]
backup backup dir
8、交互式验证查看具体模块内的文件
[root@data ~]
Password:
9、非交互式查看共享目录
[root@data ~]
10、客户端测试同步数据
[root@backup ~]
[root@data ~]
[root@data ~]
total 0
drwxr-xr-x 2 root root 6 Mar 21 10:26 www
[root@backup ~]
[root@data ~]
[root@data ~]
total 0
drwxr-xr-x 2 root root 6 Mar 21 10:26 a.txt
[root@backup ~]
[root@data ~]
[root@data ~]
total 0
drwxr-xr-x 2 root root 6 Mar 21 10:26 a.txt
drwxr-xr-x 2 root root 6 Mar 21 10:26 test.txt
11、shell 脚本实现实时数据同步
注意: 此脚本执行前先确保两主机初始数据处于同步状态,此脚本实现后续的数据同步
[root@data ~]
SRC='/data/www/'
DEST='rsyncuser@10.0.0.17::backup'
rpm -q rsync &> /dev/null || yum -y install rsync
inotifywait -mrq --exclude=".*\.swp" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pas $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done
[root@data ~]
[root@data ~]
a.txt test.txt
[root@data ~]
[root@data www]
a.txt test.txt
[root@data www]
[root@data www]
[root@data www]
[root@data www]
a.txt b.txt
[root@data www]
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 65.0319 s, 16.1 MB/s
[root@data www]
-rw-r--r-- 1 root root 1048576000 Mar 21 12:42 f1.img
[root@data www]
[root@data www]
-rw-r--r-- 1 chen root 1048576000 Mar 21 12:42 f1.img
[root@data www]
[root@data www]
-rw-r--r-- 1 chen root 1048576000 Mar 21 12:42 f2.img
[root@backup ~]
Every 0.5s: ls -l /data/backup/ Mon Mar 21 12:53:42 2022
total 1024000
-rw-r--r-- 1 root root 0 Mar 21 12:32 a.txt
-rw-r--r-- 1 root root 0 Mar 21 12:31 b.txt
-rw-r--r-- 1 chen root 1048576000 Mar 21 12:42 f2.img
[root@data www]
At 12:31:59 on 2022-03-21, file /data/www/b.txt was backuped up via rsync
At 12:31:59 on 2022-03-21, file /data/www/b.txt was backuped up via rsync
At 12:31:59 on 2022-03-21, file /data/www/b.txt was backuped up via rsync
At 12:32:52 on 2022-03-21, file /data/www/a.txt was backuped up via rsync
At 12:32:52 on 2022-03-21, file /data/www/a.txt was backuped up via rsync
At 12:32:52 on 2022-03-21, file /data/www/a.txt was backuped up via rsync
At 12:41:19 on 2022-03-21, file /data/www/f1.img was backuped up via rsync
At 12:42:24 on 2022-03-21, file /data/www/f1.img was backuped up via rsync
At 12:48:39 on 2022-03-21, file /data/www/f1.img was backuped up via rsync
At 12:51:24 on 2022-03-21, file /data/www/f2.img was backuped up via rsync
|