#rsync 常用于做为 linux系统下的数据镜像备份工具,实现远程同步,支持本地复制,或者与其他SSH、rsync主机同步数据,支持增量备份,配合任务计划,rsync能实现定时或间隔同步,配合inotify或sersync,可以实现触发式的实时数据同步
官方网站: http://rsync.samba.org/
软件包:rsync,rsync-daemon(CentOS 8)
服务文件:/usr/lib/systemd/system/rsyncd.service
配置文件:/etc/rsyncd.conf
端口:873/tcp
backup服务器 10.0.0.150
client服务器 10.0.0.152
[root@backup ~]#dnf -y install rsync-daemon
[root@backup ~]#vim /etc/rsyncd.conf
uid = root
gid = root
#port = 874
#use chroot = no
max connections = 0
ignore errors
exclude = lost+found/
pid file = /var/run/rsyncd.pid
log file = /var/run/rsyncd.log
lock file = /var/run/rsyncd.lock
reverse lookup = no
#hosts allow = 10.0.0.0/24
[bkup] #每个模块名对应一个不同的path目录,如果同名后面模块生效
path = /data/bkup/
comment = backup dir
read only = no #默认为yes,即只读
auth users = rsyncuser #默认anonymous可以访问rsync服务器
secrets file = /etc/rsync.pas
[root@backup ~]#mkdir /data/bkup -p
[root@backup ~]#echo "rsyncuser:123456" > /etc/rsync.pas
[root@backup ~]#chmod 600 /etc/rsync.pas
[root@backup ~]#systemctl start rsyncd
#客户端配置
[root@client ~]# yum install -y inotify-tools
[root@client ~]# yum install rsync -y
[root@client ~]# echo "123456" >/etc/rsync.pas
[root@client ~]# chmod 600 /etc/rsync.pas
[root@client ~]# rsync --password-file=/etc/rsync.pas [email protected]::
bkup backup dir
[root@client ~]# rsync --password-file=/etc/rsync.pas [email protected]::bkup
drwxr-xr-x 6 2021/11/18 21:13:32 .
#同步测试
#推数据
[root@client ~]# mkdir /data/www
[root@client ~]# echo test2 >/data/www/dd.txt
[root@client ~]# rsync --password-file=/etc/rsync.pas [email protected]::bkup
drwxr-xr-x 23 2021/11/18 21:46:28 .
-rw-r--r-- 5 2021/11/18 21:46:28 index.txt
[root@client ~]# rsync -avz --delete --password-file=/etc/rsync.pas /data/www/ [email protected]::bkup
sending incremental file list
deleting index.txt
./
dd.txt
sent 118 bytes received 51 bytes 338.00 bytes/sec
total size is 6 speedup is 0.04
#拉数据
[root@backup ~]#echo test > /data/bkup/index.txt
[root@backup ~]#cat /data/bkup/index.txt
test
[root@client ~]# rsync -avz --delete --password-file=/etc/rsync.pas [email protected]::bkup /data/www/
receiving incremental file list
./
index.txt
sent 46 bytes received 141 bytes 374.00 bytes/sec
total size is 11 speedup is 0.06
[root@client ~]# ll /data/www
total 8
-rw-r--r-- 1 root root 6 Nov 18 21:47 dd.txt
-rw-r--r-- 1 root root 5 Nov 18 21:50 index.txt
#在数据服务器上创建inotify_rsync.sh脚本
说明:此脚本执行前先确保两主机初始数据处于同步状态,此脚本实现后续的数据同步
[root@client ~]# vim inotify_rsync.sh
f#!/bin/bash
SRC='/data/www/'
DEST='[email protected]::backup'
rpm -q rsync &> /dev/null || yum -y install rsync
inotifywait -mrq --exclude=".*\.swp" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATETIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pas $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done
[root@client ~]# bash inotify_rsync.sh
[root@client www]# touch 11
[root@backup bkup]#ll
total 8
-rw-r--r-- 1 root root 0 Nov 18 22:06 11
-rw-r--r-- 1 root root 6 Nov 18 21:47 dd.txt
-rw-r--r-- 1 root root 5 Nov 18 21:50 index.txt
[root@client ~]# tail -f /var/log/changelist.log
At on , file 22:06:32/data/www/ 11 was backuped up via rsync
At on , file 22:06:32/data/www/ 11 was backuped up via rsync
At on , file 22:06:32/data/www/ 11 was backuped up via rsync
#在客户端/data/www目录下创建文件11,在backup服务器上查看同步成功