工作服务器为A
备份服务器为B
1、在B服务器上安装rsync包
yum -y install rsync
2、配置rsync
vi /etc/rsyncd.conf
uid = root
gid = root
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 = 192.168.8.0/24
[backup]
path = /backup/
comment = backup
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.pass
3、创建用户验证文件
echo "rsyncuser:centos" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
4、创建备份目录
mkdir /backup
5、启动rsync服务
rsync --daemon
6、在A服务器上安装inotify-tools 及rsync
yum -y install inotify-tools rsync
7、在A服务器上创建rsync的密码文件
echo "centos" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
8、创建脚本实现自动备份
vi inotify_rsync.sh
#!/bin/bash
SRC='/data/'
DEST='rsyncuser@192.168.34.17::backup'
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write ${SRC} |while read DATE TIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done
9、将脚本放在后台运行