数据服务器:192.168.1.239
备份服务器:192.168.1.238
备份服务器安装rsync
tar -zxvf rsync-3.0.9.tar.gz
cd rsync-3.0.9
./configure
make && make install
备份服务器rsync配置
mkdir /etc/rsyncd
touch /etc/rsyncd/rsyncd.conf
##创建软链接
ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
#可以空格,允许多个
hosts allow = 192.168.1.239
#默认开启端口
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
#认证模块名
[inotify]
#监控目录
path = /opt/efile/
#此为注释信息,便于识别,可自定义
comment = Hello My Dear!
ignore errors
read only = no
write only = no
#此为数据服务器(client)IP地址
hosts allow = 192.168.1.239
hosts deny = *
list = false
uid = root
gid = root
#赋予权限的用户,此处我用的是root用户
auth users = root
#密码文件
secrets file = /etc/rsyncd/rsync.password
生成密钥文件
echo "root:root123"> /etc/rsyncd/rsync.password
chmod 600 /etc/rsyncd/rsync.password
启动rsync
rsync --daemon
数据服务器
重复备份服务器配置rsyncd
注意秘钥生成,数据服务器只需要设置跟备份服务器的一样的密码即可,不需要设置用户名
echo "root123"> /etc/rsyncd/rsync.password
chmod 600 /etc/rsyncd/rsync.password
安装inotify-tools
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make
make install
编写inotify同步脚本
#!/bin/bash
step=10 #间隔的秒数,不能大于60
for (( i = 0; i < 60; i = 1 )); do
rm -rf /opt/efile/nohup.out
rm -rf /tmp/rsync.log
host=192.168.1.238
#server的ip(备份服务器)
src=/opt/efile/
#所要监控的备份目录(此处可以自定义,但是要保证存在)
des=inotify
#自定义的模块名,需要与client端定义的一致
password=/etc/rsyncd/rsync.password
#密码文件
user=root
#用户名(一个实际存在的账号)
inotify=/usr/local/inotify
#inotify的安装目录
${inotify}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src while read files do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done
done
exit 0
在后台运行inotify脚本
nohup ./inotify.sh -log=stdout &