一、rsync介绍
rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。
rsync软件支持跨平台,适用于unix/ linux/windows等多种操作系统平台。rsync是一个快速和非常方便的文件复制工具。它能本地复制,远程复制,或者远程守护进程方式复制,它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制,以其delta-transfer算法闻名。
- rsync监听端口:873
- rsync运行模式:C/S
rsync同步方式
- 完整备份:每次备份都是从备份源将所有的文件或目录备份到目的地。
- 差量备份:备份上次完全备份以后有变化的数据(针对上次的完全备份,备份过程中不清除存档属性) 。
- 增量备份:备份上次备份以后有变化的数据(不管是哪种类型的备份,有变化的数据就备份,且会清除存档属性)
rsync的特性
- 可以镜像保存整个目录和文件系统
- 可以很容易做到保持原文件的权限、时间、软硬连接等
- 无须特殊权限即可安装
二 安装
准备两台虚拟机
关闭防火墙都执行
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
安装让rsync
[root@localhost ~]# yum install -y rsync
编辑/etc/rsyncd.conf文件
[root@localhost conf.d]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode# See rsyncd.conf man page for more options.# configuration example:# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2# [ftp]
# path = /home/ftp
# comment = ftp export area
uid = root
gid = root
use chroot = yes
address = 192.168.58.156
port = 874
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.58.0/24[nginxconf]path = /etc/nginx/conf.dcomment = Nginx Configuration Filesread only = nolist = yesauth users = backupersecrets file = /etc/server.pass
编辑/etc/server.pass密码文件
[root@localhost conf.d]# cat /etc/server.pass
backuper:123123
chmod 600 /etc/server.pass
启动rsync服务
[root@localhost conf.d]# systemctl start rsyncd
[root@localhost conf.d]# systemctl status rsyncd
测试
rsync -az --delete --password-file=/etc/server.pass /etc/nginx/conf.d/ backuper@192.168.58.156::nginxconf
在目标主机上查看配置文件是否传输成功
如果你正在开发web系统界面,可以使用Flask将传输功能集成到web系统上
@app.route('/sync_configs', methods=['POST'])
def sync_configs():command = ["/usr/bin/rsync", # 替换为实际的 rsync 路径"-az","--delete","--password-file=/etc/server.pass","/etc/nginx/conf.d/","backuper@192.168.58.156::nginxconf"]try:result = subprocess.run(command, check=True, capture_output=True, text=True)return {"message": "同步成功", "output": result.stdout}except subprocess.CalledProcessError as e:return {"error": str(e)}, 500