inotify+rsync将服务器CentOS文件定时增量备份到另外CentOS

背景

之前写了一篇关于从CentOS备份到Windows的文章:inotify+rsync将服务器CentOS文件定时增量备份到Windows,现在改为备份到CentOS

服务器配置

IP地址系统功能
192.168.1.100CentOS7.xrsync服务端
192.168.1.101CentOS7.xrsync客户端

rsync服务端

1. 安装rsync

yum install -y rsync

2. 配置/etc/rsyncd.conf

transfer logging = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
uid = nobody
gid = nobody
use chroot = no
ignore errors
read only = no

[web1]
path = /root/test
auth users = myuser
secrets file = /etc/rsyncd.secrets
host allow = *
hosts deny = *
list = false

3. 新建/etc/rsyncd.secrets

echo "myuser:mypassword" > /etc/rsyncd.secrets
chmod +x /etc/rsyncd.secrets

4. 启动服务

systemctl restart rsyncd

rsync客户端

1. 安装rsync

yum install rsync -y

2. 新建/etc/rsync.passwd,内容如下,注意客户端rsync只需要密码

mypassword

3. 更改权限

chmod 600 /etc/rsync.passwd

4. 安装inotify

inotify-tools工具监测文件增加、删除和修改,同时同步到备份服务器windows
yum install inotify-tools -y

5. 启动脚本inotify_start.sh

#!/bin/bash
host=192.168.1.101
src=/home/backup
des=web1
user=myuser
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

6. 测试

# 测试命令
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd /root/test [email protected]::web1

7. 后台运行启动脚本

inotify_start.sh &

转载请注明:溜爸 » inotify+rsync将服务器CentOS文件定时增量备份到另外CentOS

相关推荐