rsync简介
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
rsync | rsync与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。 |
---|---|
Inotify | notify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。 |
环境说明:
服务器类型 | IP地址 |
---|---|
源服务器 | 192.168.149.137 |
目标服务器 | 192.168.149.140 |
需求
部署rsync+inotify同步/runtime目录至目标服务器的/NAME/下。这里的NAME是指你的名字,比如你叫tom,则要把/runtime目录同步至目标服务器的/tom/下。
目标服务器
关闭防火墙
[root@host ~]# systemctl stop firewalld
[root@host ~]# setenforce 0
[root@host ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
安装rsync服务端软件
[root@host ~]# yum -y install rsync
配置文件
[root@host ~]# vim /etc/rsyncd.conf
[root@host ~]# cat /etc/rsyncd.conf
[root@host ~]# cat /etc/rsyncd.conf
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
[runtime]
path = /xkq/
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.149.137
创建用户认证文件
[root@host ~]# echo 'admin:123456' > /etc/rsync.pass
[root@host ~]# cat /etc/rsync.pass
admin:123456
设置文件权限
[root@host ~]# chmod 600 /etc/rsync*
[root@host ~]# ll /etc/rsync*
-rw-------. 1 root root 377 6月 7 06:44 /etc/rsyncd.conf
-rw-------. 1 root root 13 6月 7 06:46 /etc/rsync.pass
配置rsync服务并设置开机自启动
[root@host ~]# echo 'OPTIONS=""' > /etc/sysconfig/rsyncd
[root@host ~]# vim /lib/systemd/system/rsyncd.service
[root@host ~]# cat /lib/systemd/system/rsyncd.service
[Unit]
Description=fast remote file copy program daemon
[Service]
User=root
Group=root
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --config=/etc/rsyncd.conf --no-detach
ExecReload=/bin/kill -HUP
KillMode=process
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
开启服务并设置开机自启
[root@host ~]# systemctl start rsyncd.service
[root@host ~]# systemctl enable rsyncd.service
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@host ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 5 0.0.0.0:873 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 [::]:873 [::]:*
LISTEN 0 128 [::]:22 [::]:*
测试
[root@host ~]# mkdir /xkq
[root@host1 ~]# mkdir /runtime
[root@host1 ~]# cd /runtime/
[root@host1 runtime]# touch xx
[root@host1 runtime]# rsync -avH --port 873 --progress --delete /runtime/ admin@192.168.149.140::runtime --password-file=/etc/rsync.pass
sending incremental file list
./
xx
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2)
sent 109 bytes received 46 bytes 62.00 bytes/sec
total size is 0 speedup is 0.00
[root@host ~]# ls /xkq/
xx
源服务器
关闭防火墙和selinux并安装rsync和inotify-tools
[root@host1 ~]# systemctl stop firewalld
[root@host1 ~]# setenforce 0
[root@host1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
[root@host1 ~]# yum -y install epel-release rsync
[root@host1 ~]# yum -y install inotify-tools
创建认证密码文件并设置权限
[root@host1 ~]# echo '123456' > /etc/rsync.pass
[root@host1 ~]# chmod 600 /etc/rsync.pass
编写监控runtime目录并且发生改变时执行rsync的脚本
[root@host1 ~]# mkdir /scripts
[root@host1 ~]# cd /scripts/
[root@host1 scripts]# touch inotify.sh
[root@host1 scripts]# cd ..
[root@host1 /]# cd
[root@host1 ~]# chmod 755 /scripts/inotify.sh
[root@host1 ~]# ll /scripts/
总用量 0
-rwxr-xr-x. 1 root root 0 6月 7 10:37 inotify.sh
[root@host1 ~]# vim /scripts/inotify.sh
[root@host1 ~]# cat /scripts/inotify.sh
host=192.168.149.140 目标服务器的ip(备份服务器)
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
使用nohup执行脚本
[root@host1 ~]# nohup bash /scripts/inotify.sh &
[1] 132104
[root@host1 ~]# nohup: 忽略输入并把输出追加到'nohup.out'
[root@host1 ~]# ps -ef|grep inotify
root 132104 24183 0 11:27 pts/0 00:00:00 bash /scripts/inotify.sh
root 132105 132104 0 11:27 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 132106 132104 0 11:27 pts/0 00:00:00 bash /scripts/inotify.sh
root 137308 24183 0 11:30 pts/0 00:00:00 grep --color=auto inotify
[root@host1 ~]# ls /runtime/
xx
[root@host1 ~]# touch /runtime/ff
查看日志
[root@host1 ~]# tail /tmp/rsync.log
20210607 11:31 /runtime/ffCREATE was rsynced
20210607 11:31 /runtime/ffATTRIB was rsynced
设置监控runtime目录脚本开机自启
[root@host1 ~]# chmod +x /etc/rc.d/rc.local
[root@host1 ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@host1 ~]# cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# 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@host ~]# ls /xkq/
runtime xx