远程同步(remote sync),是linux系统下的镜像备份工具。
可以镜像保存整个目录树和文件系统
支持同步背备份大数量的小文件
保持源文件的权限时间链接等
无需特殊权限(普通用户也能同步)
第一次同步会复制全部内容,下一次只会传输修改过的文件(增量备份),传输过程会进行解压缩,节省带宽(远程备份,异地灾备)
支持匿名传输
同步系统文件需要远程主机remote认证,可选择的协议有:
和scp原理一致,通过分发key来实现免密通信
使用该协议时,源服务器端不需要启动守护进程和配置rsync配置文件,直接获取远程用户密码。
//-a宿主变化时间不变 -z压缩传输 -v显示详情
[root@DR ~]# rsync -avz /root/anaconda-ks.cfg -e ssh root@node2:/root
sending incremental file list
anaconda-ks.cfg
sent 96 bytes received 47 bytes 95.33 bytes/sec
total size is 1,179 speedup is 8.24
//node2端验证
[root@node2 /]# ll tmp/
total 4
-rw-------. 1 root root 1179 Apr 22 10:39 anaconda-ks.cfg
Usage: rsync [OPTION]... SRC [SRC]... DEST #本地备份
or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST #远程备份
or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST #远程备份(rsync协议)
or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
or rsync [OPTION]... [USER@]HOST:SRC [DEST]
or rsync [OPTION]... [USER@]HOST::SRC [DEST]
or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
//rsync常用选项:
-a, --archive //归档
-v, --verbose //啰嗦模式
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
--delete //在源服务器上做的删除操作也会在目标服务器上同步
//同步文件
[root@DR ~]# rsync -avz /root/anaconda-ks.cfg /tmp/
[root@DR ~]# ll /tmp/|grep ana*
-rw------- 1 root root 1179 Apr 22 10:39 anaconda-ks.cf
//同步目录
[root@DR ~]# tree a
a
├── b
└── c
[root@DR ~]# rsync -avz /root/a /tmp/A
/tmp/A
└── a
├── b
└── c
[root@DR ~]# rsync -avz /root/anaconda-ks.cfg root@node2:/tmp
[root@node2 /]# ll tmp/|grep ana.*
-rw-------. 1 root root 1179 Apr 22 10:39 anaconda-ks.cfg
相比传统cp ;tar
备份,rsync
具有安全性高备份迅速支持增量备份等优点。但随着系统规模深入,数据量增大,rsync
每次备份需要扫描所有文件进行对比,再进行差量传输,但往往发生变化的部分只占了很小一部分,导致效率低下。同时不能实时同步,这时inotify
的出现则可以有效解决该问题。
linux内核从2.5.13起加入了Inotify支持,通过该接口,第三方软件就能监控文件系统上下文的各种变化情况。
环境:
类型 | ip | 应用 | 系统 |
---|---|---|---|
源服务器(source) | 192.168.94.141 | rsync&inotify&脚本 | rhel8 |
目标服务器(dest) | 192.168.94.143 | rsync | rhel8 |
需求:
[root@dest ~]# systemctl stop firewalld.service
[root@dest ~]# setenforce 0
[root@dest ~]# systemctl stop firewalld.service
[root@dest ~]# yum -y install rsync
[root@dest ~]# 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
[etc_from_client]
path = /tmp/
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.94.143
[root@dest ~]# echo ‘admin:123456‘>/etc/rsync.pass
[root@dest ~]# cat /etc/rsync.pass
admin:123456
[root@dest ~]# rsync --daemon
[root@dest ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
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@source ~]# yum -y install inotify-tools rsync
[root@source ~]# echo ‘123456‘> /etc/rsync.pass
[root@source ~]# chmod 600 /etc/rsync.pass
[root@source ~]# mkdir -pv /root/etc/test
mkdir: created directory ‘/root/etc‘
mkdir: created directory ‘/root/etc/test‘
[root@source ~]# echo ‘a‘ >etc/test/a
[root@source ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.94.143::etc_from_client --password-file=/etc/rsync.pass
...
./
test/
sent 77 bytes received 3,330 bytes 6,814.00 bytes/sec
total size is 0 speedup is 0.00
//目标服务器验证
[root@dest ~]# tree /tmp/test/
/tmp/test/
└── a
0 directories, 1 file
原文:https://www.cnblogs.com/fangxinxin/p/14753610.html