查看系统状态
Server端 172.25.254.141 redhat7.0 rsync+inotify
Client端 172.25.254.241 redhat7.0 rsync
内核版本
3.10.0-123.el7.x86_64
Inotify内核支持要求
[root@server ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Jul 16 23:22 max_queued_events
-rw-r--r-- 1 root root 0 Jul 16 23:22 max_user_instances
-rw-r--r-- 1 root root 0 Jul 16 23:22 max_user_watches
软件安装
服务器端
配置内核支持参数
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
参数生效
sysctl -p
配置好网络使得sever端可以连接上外网
获取epol源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
下载软件inotify-tools
yum install inotify-tools –y
yum install rsync -y
关闭防火墙
客户端
yum install rsync -y
关闭防火墙
文件配置(先配置客户端,再配置服务器端)
服务器端
编写密码认证文件,只需要写入密码即可
vim /etc/rsync.passwd
octopus
修改权限
chmod 600 /etc/rsync.passwd
编写同步脚本/root/rsync_backup.sh如下
# 目标服务器的ip(备份服务器)
host=172.25.254.241
# 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
src=/test
# 自定义的模块名,需要与目标服务器上定义的同步名称一致
des=student
# 执行数据同步的密码文件
password=/etc/rsync.passwd
user=rsync
# 执行数据同步的用户名
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %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
客户端
因为系统redhat7.0,安装管理服务xinetd,同时添加文件/etc/xinetd.d/rsync
编辑内容如下所示
service rsync
{
disable = no
flags = IPv4
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
额!刚去试了一下,好像不需要xinetd,直接rsync --daemon 就可以,尴了个尬
编写rsync配置文件(注释和配置参数一定要分行写)
uid = nobody
gid = nobody
use chroot = yes
address = 172.25.254.241
#本机地址
port 873
#监听的端口
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 172.25.254.0/24
#允许访问的客户机地址
[student]
#项目名称
path = /share
#同步文件到的目录
comment = student's home
#注释
read only = no
#只读
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
#同步时不再压缩的文件
auth users = rsync
#使用用户,即配置文件/etc/rsync.password中写的用户名即可
secrets file = /etc/rsync.password
#用户认证信息存放的路径
编写需要的用户以及其密码文件/etc/rsync.password
格式----用户:密码
rsync:octopus
www:asdsd
修改权限
chmod 600 /etc/rsync.password
创建被推送的目录
mkdir /share
修改读写权限
chmod 777 /share
启动服务
服务器端
启动inotify的监视功能
sh /root/rsync_back.sh >/dev/null &
客户端
启动rsync服务以及打开防火墙端口
systemctl start xinetd.service
firewall-cmd --add-port=873/tcp --permanent
systemctl restart firewalld