server IP
rsync_server:192.168.195.44 master.lab.example.com
rsync_client:192.168.195.45 node.lab.example.com
[root@master&node ~]# vi /etc/hosts
192.168.195.44 master.lab.example.com master
192.168.195.45 node.lab.example.com node
[root@master&node]# systemctl stop firewalld
[root@master&node]# systemctl disable firewalld
[root@master ~]# ssh-keygen
[root@master ~]# ssh-copy-id root@node
[root@master ~]# ssh-copy-id root@master
[root@master ~]# mkdir -p /app/rsync_server
[root@node ~]# mkdir /app/rsync_client -p
[client]:
[root@node ~]# yum -y install rsync xinetd
[root@node ~]# vi /etc/xinetd.d/rsync
service rsync
{
disable = no #修改为no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[root@node ~]# vi /etc/rsyncd.conf
logfile = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid #pid文件的存放位置
lockfile = /var/run/rsync.lock #支持max connections参数的锁文件
secretsfile = /etc/rsync.pass #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motdfile = /etc/rsyncd.Motd #rsync启动时欢迎信息页面文件位置(文件内容自定义)
[app_rsync_client] #自定义名称
path = /app/rsync_client/ #rsync服务端数据目录路径
comment = app_rsync_client #模块名称与[app_rsync_client]自定义名称相同
uid = root #设置rsync运行权限为root
gid = root #设置rsync运行权限为root
port =873
use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份
read only = no 设置rsync服务端文件为读写权限
list = no #不显示rsync服务端资源列表
mac connections = 200
timeout = 600
auth users = rsync #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.195.45 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.195.46,192.168.195.47 #禁止数据同步的客户端IP地址,可以设置多个
[root@node ~]# vi /etc/rsync.pass
rsync:redhat
[root@node rsync_client]# systemctl start rsyncd
[root@node rsync_client]# systemctl enable rsyncd
[root@node ~]# systemctl start xinetd
[root@node ~]# systemctl enable xinetd
[server]:
[root@master ~]# yum install -y rsync xinetd
[root@master ~]# vi /etc/xinetd.d/rsync
service rsync
{
disable = no #修改为no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[root@master ~]# vi /etc/passwd.txt ---set rsync's password
redhat
[root@master ~]# chmod 600 /etc/passwd.txt
[root@master&node rsync_client]# systemctl restart rsyncd xinetd
[root@master&node rsync_client]# systemctl enable rsyncd xinetd
[root@master app]# rsync -avH --port=873 --progress --delete /app/rsync_server/ [email protected]::app_rsync_client --password-file=/etc/passwd.txt
[root@master app]# ll /proc/sys/fs/inotify ---view 3 max_* file suppost inotify
[root@master inotify-tools-3.14]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.backup
[root@master yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@master yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@master yum.repos.d]# yum clean all
[root@master yum.repos.d]# yum makecache
[root@master yum.repos.d]# yum install -y inotify-tools
sysctl -a|egrep -i "max_queued_events|max_user_watches|max_user_instances"
default volue is very min need modify
[root@master yum.repos.d]# vi /etc/sysctl.conf
fs.inotify.max_queued_events = 99999999
fs.inotify.max_user_watches = 99999999
fs.inotify.max_user_instances = 65535
[root@master yum.repos.d]# sysctl -p
[root@master yum.repos.d]# cat /proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events}
[root@master yum.repos.d]# mkdir /usr/local/inotify
[root@master yum.repos.d]# vi /usr/local/inotify/rsync.sh
#!/bin/bash
src_dir="/app/rsync_server/"
dst_dir="app_rsync_client"
exclude_dir="/usr/local/inotify/exclude.list"
rsync_user="rsync"
rsync_passwd="/etc/passwd.txt"
dst_ip="192.168.195.45"
rsync_command(){
rsync -avH --port=873 --progress --delete --exclude-from=$exclude_dir $src_dir $rsync_user@$dst_ip::$dst_dir --password-file=$rsync_passwd
}
for ip in $dst_ip;do
rsync_command
done
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $src_dir \
| while read file;do
for ip in $dst_ip;do
rsync_command
echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
done
done
注释:
src_dir="/app/rsync_server/" #源服务器同步目录
dst_dir="app_rsync_client" #目标服务器rsync同步目录模块名称
exclude_dir="/usr/local/inotify/exclude.list" #不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;
例如:不需要同步/app/rsync_server/"目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写
a/
b/b1/rsync_user="rsync" #目标服务器rsync同步用户名
rsync_passwd="/etc/passwd.txt" #目标服务器rsync同步用户的密码在源服务器的存放路径
dst_ip="192.168.195.45" #目标服务器ip,多个ip用空格分开
[root@master yum.repos.d]# chmod +x /usr/local/inotify/rsync.sh
[root@master yum.repos.d]# touch /usr/local/inotify/exclude.list
[root@master yum.repos.d]# vi /etc/rc.d/rc.local
nohup /bin/sh /usr/local/inotify/rsync.sh &
[root@master rsync_server]# sh /usr/local/inotify/rsync.sh
[root@master app]# cd rsync_server/
[root@master rsync_server]# touch test{1..9}
[root@node rsync_client]# pwd
/app/rsync_client