rsync概述
一款快速增量备份工具
可以实现远程同步
支持本地复制,或者与其他SSH,rsync主机同步
配置操作
rsync
服务端:20.0.0.185
客户端:20.0.0.186
安装rsync
yum -y install rsync
systemctl disable firewalld
vim /etc/selinux/config ###关闭核心防护
SELINUX=disabled
setenforce 0
yum -y install httpd
配置
vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes #禁锢家目录
address = 20.0.0.185 #服务端地址
port 873 #端口号
log file = /var/log/rsyncd.log #日志文件
pid file = /var/run/rsyncd.pid #pid进程文件
hosts allow = 20.0.0.0/24 #白名单
[wwwroot] #共享同步名,客户端访问时需要提供此名
path = /var/www/html #共享文件路径
comment = ftp export area #说明,没啥意义
read only = yes #只读
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #传输过程中对这些格式的文件不压缩
auth users = backuper #程序账户名,客户端访问时需要
secrets file = /etc/rsyncd_users.db #程序账户信息的文件路径
设置程序账号和密码
vim /etc/rsyncd_users.db
backuper:abc123 #账户:密码,远程同步需要的
chmod 600 /etc/rsyncd_users.db #给读的权限
启动服务
rsync --daemon #开启进程守护、开启服务
netstat -anutp | grep rsync #查看服务启动成功
在客户端访问20.0.0.185
rsync -avz backuper@20.0.0.185::wwwroot /opt #输入密码即可
免交互同步
echo 'abc123' > /etc/server.pass
chmod 600 /etc/server.pass #必须设置其它用户不能访问,否则会报错
rsync -az --delete --password-file=/etc/server.pass backuper@20.0.0.186::wwwroot /media
inotify
安装rsync
yum -y install rsync
systemctl disable firewalld
vim /etc/selinux/config ###关闭核心防护
SELINUX=disabled
setenforce 0
vim /etc/sysctl.conf #修改内核参数
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p #刷新内核参数
tar zxvf inotify-tools-3.14.tar.gz -C /opt/
cd inotify-tools-3.14
./configure
make
make install
yum -y install httpd
inotifywait -mrq -e modify,create,move,delete /var/www/html #监控/var/www/html目录
此时重开终端,可以在/var/www/html收到创建文件来验证监控
做一个同步脚本
cd /opt
vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html backuper@20.0.0.185::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
在服务器端20.0.0.185
chmod 777 /var/www/html/
vim /etc/rsyncd.conf
read only = no ###需要把wwwroot模块的只读模式改成no,不能注释,因为默认是yes。
重新启动rsyncd服务
pkill -9 rsync #结束进程
rm -rf /var/run/rsyncd.pid #删除残留pid文件
rsync --daemon #启动服务
在客户端20.0.0.186
cd /opt
./inotify.sh #执行脚本
测试
重开终端,在/var/www/html目录新建文件,在客户端查看是否同步