Rsync 简介
Inotify 简介
配置过程
# Linux 下支持 inotify 的最小内核为 2.6.13
# 可使用 ll /proc/sys/fs/inotify 命令检查是否支持
# 出现以下三个文件表示支持
~]# ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Apr 18 15:46 max_queued_events
-rw-r--r-- 1 root root 0 Apr 18 15:46 max_user_instances
-rw-r--r-- 1 root root 0 Apr 18 15:46 max_user_watches
# max_queued_events:inotify事件队列最大长度,如值太小会出现 Event Queue Overflow 错误,默认值:16384
# max_user_watches:可以监视的文件数量(单进程),默认值:8192
# max_user_instances:每个用户创建inotify实例最大值,默认值:128
~]# yum install -y rsync inotify-tools
~]# echo "rookie" > /etc/rsync.pass # 非交互式
~]# chmod 600 /etc/rsync.pass
~]# vim /data/scripts/rsync_inotify.sh
#!/bin/bash
#
SRC='/data/'
DEST='rsyncuser@172.18.33.105::backup # backup 是服务端配置的共享名称
inotifywait -mrq --timefmt '%T %F' --format '%T %w %f -e create,delete,moved_to,close_write,attrib ${SRC} | while read DATE TIME DIR FILE; do
FILEPATH=${DIR}${FILE}
rsync -avlopg --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped via rsync" >> /var/log/change_list.log # 记录日志可酌情添加
done
~]# bash /data/scripts/rsync_inotify.sh
# 可使用 screen 命令跑此脚本实现终端没有断开亦可执行
~]# yum install -y rsync
~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot= no
max connections = 0
ignore errors exclude = lost+found/
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
reverse lookup = no
hosts allow = 172.18.33.104
[backup] # 服务共享名
path = /data/nfs/
comment = backup
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.pass
~]# echo "rsyncuser:rookie" > /etc/rsync.pass
~]# chmod 600 /etc/rsync.pass
~]# mkdir -pv /data/nfs
~]# rsync --daemon # 后台运行
# 可加入 /rc.d/rc.local 实现开机启动(需加执行权限)