1.环境说明
服务器类型 | IP地址 | 应用 | 操作系统 |
---|---|---|---|
源服务器 | 192.168.159.135 | rsync inotify-tools 脚本 | centos7/redhat7 |
目标服务器 | 192.168.159.132 | rsync | centos7/redhat7 |
需求:
- 把源服务器上/runtime目录实时同步到目标服务器的/longnian下
2.配置前的准备工作
配置yum源、安装所需的安装包
[root@longnian ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
//修改源文件
:%s/$Releaserver/7/g
//源服务器上安装的包
[root@longnian ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@longnian ~]# yum -y install epel-release inotify-tools rsync
//目标服务器上安装的包
[root@client ~]# yum -y install rsync
//在源服务器上创建/runtime
[root@longnian runtime]# tree
.
├── abc
├── edf
├── game
├── haha
│ └── hello
├── mount
│ └── unmont
└── xixi
└── index.html
3 directories, 6 files
2.1配置目标服务器
//关闭防火墙和SELINUX
[root@client ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
[root@client ~]# getenforce
Disabled
//安装rsync服务端软件
[root@client ~]# yum -y install rsync
//设置rsyncd.conf配置文件
[root@client ~]# cat >> /etc/rsyncd.conf <<EOF
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
[runtime]
path = /longnian/
comment = sync etc 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 = admin
hosts allow = 192.168.159.135
//创建用户认证文件
[root@client ~]# echo 'admin:123long' > /etc/rsync.pass
[root@client ~]# cat /etc/rsync.pass
admin:123long
//设置文件权限
[root@client ~]# chmod 600 /etc/rsync*
[root@client ~]# ll /etc/rsync*
-rw------- 1 root root 432 7月 29 05:11 /etc/rsyncd.conf
-rw------- 1 root root 14 7月 29 05:54 /etc/rsync.pass
//启动rsync服务并设置开机自启动
[root@client ~]# systemctl start rsyncd
[root@client ~]# systemctl enable --now rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@client ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 *:873 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:20048 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 64 *:46303 *:*
LISTEN 0 128 *:42944 *:*
LISTEN 0 64 *:2049 *:*
LISTEN 0 5 :::873 :::*
LISTEN 0 128 :::56524 :::*
LISTEN 0 64 :::46255 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::20048 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 64 :::2049 :::*
2.2配置源服务器
//关闭防火墙和SELINUX
[root@server ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
[root@server ~]# getenforce
Disabled
//创建认证密码文件
[root@server ~]# echo "123long" > /etc/rsync.pass
[root@server ~]# cat /etc/rsyncd.pass
123long
//设置文件权限
[root@server ~]# chmod 600 /etc/rsync.pass
[root@server ~]# ll /etc/rsync.pass
-rw------- 1 root root 8 7月 29 06:10 /etc/rsync.pass
//在源服务器上创建测试目录
[root@server ~]# mkdir -pv /root/etc/test
mkdir: 已创建目录 "/root/etc"
mkdir: 已创建目录 "/root/etc/test"
[root@server ~]# rsync -avH --port 873 --progress --delete /runtime admin@192.168.159.132::runtime --password-file=/etc/rsync.pass
sending incremental file list
runtime/
runtime/abc
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=8/10)
runtime/edf
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=7/10)
runtime/game
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=6/10)
runtime/haha/
runtime/haha/hello
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=2/10)
runtime/mount/
runtime/mount/unmont
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=1/10)
runtime/xixi/
runtime/xixi/index.html
12 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=0/10)
sent 543 bytes received 154 bytes 1,394.00 bytes/sec
total size is 12 speedup is 0.02
//查看服务器内核是否支持inotify
[root@server ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 7月 29 06:14 max_queued_events
-rw-r--r-- 1 root root 0 7月 29 06:14 max_user_instances
-rw-r--r-- 1 root root 0 7月 29 06:14 max_user_watches
//写同步脚本
[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 7月 29 06:14 /scripts/inotify.sh
[root@server ~]# vim /scripts/inotify.sh
[root@server ~]# cat /scripts/inotify.sh
host=102.168.159.132
src=/runtime
des=runtime
password=/etc/rsync.pass
user=admin
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
//启动脚本
[root@server ~]# nohup bash /scripts/inotify.sh &
[1] 3029
[root@server ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@server ~]# ps -ef|grep inotify
root 3029 2809 0 06:17 pts/0 00:00:00 bash /scripts/inotify.sh
root 3030 3029 0 06:17 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /runtime
root 3031 3029 0 06:17 pts/0 00:00:00 bash /scripts/inotify.sh
root 3033 2809 0 06:18 pts/0 00:00:00 grep --color=auto inotif
//在源服务器上生成一个新文件
[root@server ~]# ls /etc/httpd/
conf conf.d conf.modules.d logs modules run ssl
[root@server ~]# echo "hello world" > /etc/httpd/test
//查看inotify生成的日志
[root@server ~]# tail /longnian/rsync.log
20200728 22:27 /etc/httpd/testCREATE was rsynced
20200728 22:27 /etc/httpd/testMODIFY was rsynced
2.3设置脚本开机自动启动
[root@server ~]# chmod +x /etc/rc.d/rc.local
[root@server ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 4月 7 22:38 /etc/rc.d/rc.local
[root@server ~]# echo 'nohup /bin/bash /scripts/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 /scripts/inotify.sh
[root@server ~]# touch /var/lock/subsys/local
[root@server ~]# nohup /bin/bash /scripts/inotify.sh
2.4验证
[root@client longnian]# ls
runtime
[root@client longnian]# tree
.
└── runtime
├── abc
├── edf
├── game
├── haha
│ └── hello
├── mount
│ └── unmont
└── xixi
└── index.html
4 directories, 6 files
[root@client longnian]# ls runtime/
abc edf game haha mount xixi