环境说明:
服务类型 | IP地址 | 应用 | 操作系统 |
---|---|---|---|
源服务器server | 192.168.102.128 | rsync;inotify-tools;脚本 | redhat7 |
目标服务器client | 192.168.102.129 | rsync | redhat7 |
需求:
- 部署rsync+inotify同步/lishuai目录至目标服务器/tmp/lishuai下
在目标服务器做以下操作:
[root@client ~]# yum -y install rsync.x86_64
安装过程略...
[root@client ~]# vim /etc/rsyncd.conf #添加如下配置
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.lishuai
[lishuai_from_client]
path = /tmp/lishuai
comment = sync lishuai from client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = lishuai
----------
[root@client ~]# echo 'lishuai:123456' > /etc/rsync.lishuai
[root@client ~]# chmod 600 /etc/rsync*
[root@client ~]# ll /etc/rsync*
-rw-------. 1 root root 800 9月 12 11:00 /etc/rsyncd.conf
-rw-------. 1 root root 15 9月 12 11:12 /etc/rsync.lishuai
[root@client ~]# systemctl start rsyncd
[root@client ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@client ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 5 *:873 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 5 :::873 :::*
在源服务器上做以下操作:
配置yum源
[root@server yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@server yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@server yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
安装epel-release、rsync
[root@server yum.repos.d]# yum -y install epel-release
安装过程略...
[root@server yum.repos.d]# yum -y install rsync
安装过程略··· #rsync安装完成后不需要启动不需要配置
创建认证密码文件
[root@server ~]# echo '123456' > /etc/rsync.lishuai
[root@server ~]# chmod 600 /etc/rsync.lishuai
----------
测试
[root@server ~]# mkdir -pv /root/etc/test
rsync -avH --port 873 --progress --delete /lishuai lishuai@192.168.102.129::lishuai_from_client --password-file=/etc/rsync.lishuai
目标验证
[root@client ~]# ll /tmp/
总用量 0
drwxr-xr-x. 2 root root 6 9月 17 2018 test #数据同步成功
安装inotify-tools工具,实时触发rsync进行同步
查看服务器内核是否支持inotify
[root@server ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r--. 1 root root 0 9月 17 16:40 max_queued_events
-rw-r--r--. 1 root root 0 9月 17 16:40 max_user_instances
-rw-r--r--. 1 root root 0 9月 17 16:40 max_user_watches
有三个max开头文件说明内核支持inotify
安装inotify-tools
[root@server ~]# yum -y install inotify-tools
安装过程略...
写同步脚本
[root@server ~]# mkdir /scripts
[root@server ~]# touch /scripts/inotify.sh
[root@server ~]# chmod 755 /scripts/inotify.sh
[root@server ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 9月 17 16:52 /scripts/inotify.sh
[root@server ~]# vim /scripts/inotify.sh #编辑如下内容
host=192.168.102.129
src=/lishuai
des=lishuai_from_client
password=/etc/rsync.lishuai
user=lishuai
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,d
elete,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
启动脚本
[root@server ~]# nohup bash /scripts/inotify.sh &
[1] 12989
[root@server ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[1]+ 完成 nohup bash /scripts/inotify.sh
[root@server ~]# ps -ef|grep inotify
root 12979 12883 0 17:14 pts/1 00:00:00 vim /scripts/inotify.sh
root 12997 12229 0 17:19 pts/0 00:00:00 grep --color=auto inotif
在源服务器上生成一个新文件
[root@server ~]# ls /lishuai/
[root@server ~]# echo 'warframe' > /lishuai/tonne
目标验证
[root@client lishuai]# ls
tonne
[root@client lishuai]# cat tonne
warframe
[root@client lishuai]# pwd
/tmp/lishuai
设置脚本开机自启
[root@server ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 6月 27 2017 /etc/rc.d/rc.local
[root@server ~]# echo 'nohup /bin/bash /tmp/inotify.sh' >> /etc/rc.d/rc.local
[root@server ~]# tail /etc/rc.d/rc.local
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup /bin/bash /tmp/inotify.sh
[root@server ~]# reboot
----------
验证
[root@server ~]# cd /lishuai/
[root@server lishuai]# ls
aa tonne wf
[root@server lishuai]# mkdir 11
目标验证
[root@client lishuai]# ls
11 aa tonne wf
[root@client lishuai]# pwd
/tmp/lishuai