rsync
rsync
rsync简介
一款快速增量备份工具
Remote Sync,远程同步
支持本地复制,或者与其他SSH、rsync主机同步
官网
rsync服务器
rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。
rsync软件适用于unix/linux/windows等多种操作系统平台
rsync是一个快速和非常同样的文件复制工具。它能本能复制,远程复制,或者远程守护进程方式复制,它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制
以其delta-transfer算法闻名。
rsync监听端口:873
rsync运行模式:C/S
同步方式
全量备份
原有的数据全部传送
把原来的文件和新的文件一起统一传送
全量复制,效率低
增量备份
在传输数据之前通过一些算法通过你有的数据和我有的数据进行对比,把不一样的数据通过网络传输
增量复制,效率高
rsync命令
rsync [选项] 原始位置 目标位置
| 常用选项 | 说明 |
|---|---|
| -r | 递归模式,包含目录及子目录中的所有文件 |
| -l | 对于符号链接文件仍然复制为符号链接文件 |
| -v | 显示同步过程的详细信息 |
| -z | 在传输文件时进行压缩 |
| -a | 归档模式,递归并保留对象属性,等同于-rlptgoD |
| -p | 保留文件的权限标记 |
| -t | 保留文件的时间标记 |
| -g | 保留文件的属组标记(仅超级用户使用) |
| -o | 保留文件的属主标记(仅超级用户使用) |
| -H | 保留硬链接文件 |
| -A | 保留ACL属性信息 |
| -D | 保留设备文件及其他特殊文件 |
- -delete |删除目标位置有而原始位置没有的文件
- -checksum |根据对象的校验和来决定是否跳过文件
配置源的两种方法
格式一: 用户名@主机地址::共享模块名
格式二: rsync://用户名@主机地址/共享模块名
inotify介绍
inotify是一种文件系统的变化通知机制。 如文件增加、删除等事件可以立刻让用户态得知 。
利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。
将inotify机制与rsync相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。
| inotifywait常用选项 | 说明 |
|---|---|
| -e | 用来指定要监控哪些事情 |
| -m | 表示持续监控 |
| -r | 表示递归整个目录 |
| -q | 简化输出信息 |
环境说明:
| 服务器类型 | ip地址 | 应用 | 操作系统 |
|---|---|---|---|
| master | 192.168.170.129 | inotify-tools脚本 | rsync |
| client | 192.168.170.132 | rsyncrsync-daemon | centos8 |
需求:
部署rsync+inotify同步/runtime目录至目标服务器的/NAME/下。这里的NAME是指你的名字,比如你叫tom,则要把/runtime目录同步至目标服务器的/tom/下。
部署master服务器
//安装rsync、epel
[root@master ~]# dnf -y install rsync
[root@master ~]# dnf -y install epel-release
//创建认证密码文件
[root@master ~]# echo '123456' > /etc/.srcpasswd
[root@master ~]# cat /etc/.srcpasswd
123456
[root@master ~]# chmod +600 /etc/.srcpasswd
//关闭防火墙、selinux
[root@master ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# setenforce 0
[root@master ~]# getenforce
Permissive
[root@master ~]# vim /etc/selinux/config
SELINUX=disabled //此行修改为disabled
//创建测试目录#安装rsync服务器和守护进程
[root@master ~]# mkdir -v /runtime
mkdir: 已创建目录 '/runtime'
[root@master ~]# rsync -avh --port 873 --progress --delete /runtime admin@192.168.170.132::lyy --password-file=/etc/.srcpasswd
sending incremental file list
runtime/
sent 69 bytes received 28 bytes 194.00 bytes/sec
total size is 0 speedup is 0.00
//到client终端上查看
[root@client ~]# ls /lzx/
runtime
//安装inotify-tools,实时触发rsync进行同步
[root@master ~]# ll /proc/sys/fs/inotify/
//创建脚本文件
[root@master ~]# mkdir /scripts
[root@master ~]# touch /scripts/inotify.sh
[root@master ~]# chmod 755 /scripts/inotify.sh
[root@master ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 8月 10 01:21 /scripts/inotify.sh
[root@master ~]# vim /scripts/inotify.sh
host=192.168.170.132
src=/runtime
des=lyy
password=/etc/.srcpasswd
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@master ~]# nohup bash /scripts/inotify.sh &
[1] 333926
[root@master ~]# nohup: 忽略输入并把输出追加到'nohup.out'
[root@master ~]# ps -ef|grep inotify
root 333926 7669 0 01:27 pts/0 00:00:00 bash /scripts/inotify.sh
root 333927 333926 0 01: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 333928 333926 0 01:27 pts/0 00:00:00 bash /scripts/inotify.sh
root 336349 7669 0 01:27 pts/0 00:00:00 grep --color=auto inotify
//在源服务器上生成一个新文件
[root@master ~]# cd /runtime/
[root@master runtime]# touch zy
//查看inotify生成的日志
[root@master ~]# tail /tmp/rsync.log
20220810 01:29 /runtime/zy CREATE was rsynced
20220810 01:29 /runtime/zy ATTRIB was rsynced
//设置脚本开机自启
[root@master ~]# vim /etc/rc.d/rc.local
nohup bash /scripts/inotify.sh & //在脚本中添加此行
[root@master ~]# chmod +x /etc/rc.d/rc.local
reboot //重启虚拟机
[root@master ~]# ps -ef|grep inotify // //重启后脚本已经在自动运行
root 957 1 0 01:41 ? 00:00:00 bash /scripts/inotify.sh
root 965 957 0 01:41 ? 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 966 957 0 01:41 ? 00:00:00 bash /scripts/inotify.sh
root 2559 2016 0 01:42 pts/0 00:00:00 grep --color=auto inotify
[root@master ~]# cd /runtime/
[root@master runtime]# echo 'hello' > aaa
部署client服务器
//关闭防火墙和selinux
[root@client ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@client ~]# setenforce 0
[root@client ~]# getenforce
Permissive
[root@client ~]# vim /etc/selinux/config
SELINUX=disabled //此行修改为disabled
//安装rsync服务器和守护进程
[root@client ~]# dnf -y install rsync
[root@client ~]# dnf install -y rsync-daemon
//配置rsyncd.conf文件
[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/.passwd
[lyy]
path = /lzx/
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
//创建用户认证文件
[root@client ~]# cat /etc/.passwd
admin:123456
//设置权限
[root@client ~]# chmod 600 /etc/rsyncd.conf
[root@client ~]# chmod 600 /etc/.passwd
//开启服务并设置开机自启
[root@client ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@client ~]# systemctl status rsyncd.service
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-08-10 00:42:26 CST; 9s ago
Main PID: 130292 (rsync)
Tasks: 1 (limit: 4612)
Memory: 900.0K
CGroup: /system.slice/rsyncd.service
└─130292 /usr/bin/rsync --daemon --no-detach
//创建同步数据存储目录
[root@client ~]# mkdir /lzx/
到client终端中查看有源服务器创建的文件
[root@client runtime]# ls
aaa zy
[root@client runtime]# cat aaa
hello

浙公网安备 33010602011771号