Rsync+inotify数据同步
安装环境
备份服务器端:CentOS7,IP:192.168.1.100
备份客户端:CentOS7,IP:192.168.1.200
服务器端Rsync服务部署
1、安装程序包
# yum –y install rsync
2、创建服务端配置文件
# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
ignore errors = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
lock file = /var/run/rsyncd.lock
[backup]
path = /backup/data #服务器端的存储目录
read only = no
write only = no
hosts allow = 192.168.0.0/16
hosts deny = *
list = false
auth users = server1-200
secrets file = /etc/rsync.password
3、创建rsync用户名
# useradd -M -s /sbin/nologin rsync
4、创建备份目录,修改目录权限
# mkdir /backup
# chown -R rsync /backup
5、创建密码文件,并修改权限
# echo "server1-200:123456" >/etc/rsync.password #注意:server1-200是用户名,123456是密码
# chmod 600 /etc/rsync.password
6、启动服务
# systemctl start rsyncd
注意:启动服务后打开tcp:873端口
客户端配置inotify
1、安装程序包
# yum –y install rsync
2、编译安装inotify
1、下载源码包
inotify-tools-3.14.tar.gz
2、解压:
# tar zxf inotify-tools-3.14.tar.gz
3、编译安装(需要安装gcc)
# cd inotify-tools-3.14
# ./configure --prefix=/usr/local/inotify-tools-3.14
# make & make install
4、创建软连接
# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools
5、修改环境变量
# echo 'PATH=/usr/local/inotify-tools/bin:$PATH' > /etc/profile.d/inotify.sh
# source /etc/profile.d/inotify.sh
3、客户端自动检测脚本
# vim /script/backup.sh
##################################
#!/bin/bash
SRC='/data/' #客户端需要备份内容的原始路径
DEST='server1-200@192.168.1.100::backup' # 备份到服务器端的位置server1-200是用户名,backup是服务器配置文件中的的模块名
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --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.password $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done
4、修改参数
sysctl -w fs.inotify.max_queued_events="200000000" sysctl -w fs.inotify.max_user_watches="2000000000" sysctl -w fs.inotify.max_user_instances="65535"
参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
inotifywait命令可以监视文件数量,要同步的文件包含多少目录,可以用:find /home/test -type d | wc -l 统计,必须保证max_user_watches值大于统计结果
max_user_instances:
每个用户创建inotify实例最大值
5、启动服务脚本
# chmod +x /script/backup.sh
# nohup /script/backup.sh &> /dev/null & #放到后台执行