rsync远程同步(理论+实践篇)
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,关于rsync一款快速增量备份工具Remote Sync,远程同步支持本地复制,或者与其他SsH、rsync 主机同步官方网站: http://rsync.samba.org配置rsync源服务器rs
千家信息网最后更新 2025年12月02日rsync远程同步(理论+实践篇)
关于rsync
一款快速增量备份工具
- Remote Sync,远程同步
- 支持本地复制,或者与其他SsH、rsync 主机同步
- 官方网站: http://rsync.samba.org
配置rsync源服务器
rsync同步源
- 指备份操作的远程服务器,也称为备份源
配置rsync源
- 基本思路
- 建立rsyncd.conf配置文件、独立的账号文件
- 启用rsync的-daemon模式
- 应用示例
- 用户backuper, 允许下行同步
- 操作的目录为/var/www/html/
- 配置文件rsyncd.conf
- 需手动建立,语法类似于Samba配置
- 认证配置auth users、secrets file, 不加则为匿名
- rsync账号文件
- 采用"用户名:密码"的记录格式,每行一个用户记录
- 独立的账号数据,不依赖于系统账号
- 启用rsync服务
- 通过--daemon独自提供服务
- 执行kill $(cat /var/run/rsyncd.pid)关闭rsync服务
使用rsync备份工具
rsync命令用法
rsync [选项] 原始位置 目标位置- 常用选项
- -a:归档模式,递归并保留对象属性,等同于-rlptgoD
- -v:示同步过程的详细(verbose)信息
- -z:在传输文件时进行压缩(compress)
- -H:保留硬连接文件
- -A:保留ACL属性信息
- --delete:删除目标位置有而原始位置没有的文件
- --checksum:根据对象的校验和来决定是否跳过文件
- 配置源的两种表示方法
- 格式1: 用户名@主机地址::共享模块名
- 格式2: rsync://用户名@主机地址/共享模块名
rsync实时同步
定期同步的不足
- 执行备份的时间固定,延迟明显、实时性差
- 当同步源长期不变化时,密集的定期任务是不必要的
实时同步的优点
- 一旦同步源出现变化,立即启动备份
- 只要同步源无变化,则不执行备份
关于inotify
Linux内核的inotify机制
- 从版本2.6.144开始提供
- 可以监控文件系统的变动情况,并作出通知响应
- 辅助软件: inotify-tools
rsync+inotify实时同步
- 调整inotify内核参数
- max_queue_events: 监控队列大小
- max_user_instances: 最多监控实例数
- max_user_watches:每个实例最多监控文件数,配置时应大于监控目标的总文件数
- 安装inotify-tools辅助工具
- inotifywait:用于持续监控,实时输出结果
- 常用选项
- -m:持续进行监控
- -r:递归监控所有子对象
- -q:简化输出信息
- -e:指定要监控哪些事件类型
- inotifwatch:用于短期监控,任务完成后再出结果
- 通过inotifywait触发rsync同步操作
- 使用while、read持续获取监控结果
- 根据结果可以作进一 步判断,决定执行何种操作
实践配置
实验环境
- rsyncd服务器IP地址:192.168.144.128
- client客户端IP地址:192.168.144.129
在rsyncd服务器上修改配置文件
[root@rsyncd ~]# rpm -q rsyncrsync-3.0.9-18.el7.x86_64[root@rsyncd ~]# vim /etc/rsyncd.confuid = nobody //匿名用户gid = nobodyuse chroot = yes //禁锢家目录pid file = /var/run/rsyncd.pid //pid文件路径address = 192.168.144.128 //配置监听地址port = 873 //端口号log file = /var/log/rsyncd.log //日志文件路径hosts allow = 192.168.144.0/24 //允许地址段访问dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 //不需要压缩的类型[wwwroot] //共享模块名path = /var/www/html //共享文件路径comment = www.kgc.com //定义名称read only = yes //只读权限auth users = backuper //身份验证用户名secrets file = /etc/rsyncd_users.db //密码文件:wq[root@rsyncd ~]# vim /etc/rsyncd_users.db //创建密码文件backuper:123123 //编辑用户名:密码:wq[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db //更改权限[root@rsyncd ~]# rsync --daemon //开启rsync服务[root@rsyncd ~]# netstat -ntap | grep rsync //查看端口tcp 0 0 192.168.144.128:873 0.0.0.0:* LISTEN 36346/rsync [root@rsyncd ~]# systemctl stop firewalld.service //关闭防火墙[root@rsyncd ~]# setenforce 0[root@rsyncd ~]# yum install httpd -y //安装httpd服务[root@rsyncd ~]# cd /var/www/html/[root@rsyncd html]# echo "this is test web" > index.html //编辑网页信息[root@rsyncd html]# cd ../[root@rsyncd www]# chmod 777 html/ //放开目录权限,方便用户操作在客户端服务器上,拉取同步源rsyncd
[root@client ~]# systemctl stop firewalld.service //关闭防火墙[root@client ~]# setenforce 0 //关闭selinux[root@client ~]# rpm -q rsync //检查是否安装rsync服务rsync-3.0.9-18.el7.x86_64[root@client ~]# yum install httpd -y //安装httpd服务[root@client ~]# cd /var/www/[root@client www]# chmod 777 html/ //放开目录权限[root@client www]# rsync -avz backuper@192.168.144.128::wwwroot /var/www/html //拉取共享模块Password: //输入密码 [root@client www]# cat html/index.html //查看是否同步信息this is test web[root@client www]# rm -rf html/index.html [root@client www]# vim /etc/server.pass //创建本地的密码文件123123[root@client www]# chmod 600 /etc/server.pass //更改权限[root@client www]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.144.128::wwwroot /var/www/html/ //指定本地密码文件,删除目标位置有而原始位置没有的文件,实现免交互在client客户端上安装inotify(监控)
[root@client www]# vim /etc/sysctl.conf //修改内核参数文件fs.inotify.max_queued_events = 16384 //队列fs.inotify.max_user_instances = 1024 //每个队列中的实例数fs.inotify.max_user_watches = 1048576 //每个实例中的文件数[root@client www]# sysctl -p ##加载[root@client www]# mount.cifs //192.168.100.8/LNMP-C7 /mnt/ //挂载Password for root@//192.168.100.3/LNMP-C7: [root@client www]# cd /mnt/[root@client mnt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/ //解压inotify到/opt下[root@client mnt]# cd /opt/[root@client opt]# cd inotify-tools-3.14/[root@client inotify-tools-3.14]# yum install gcc gcc-c++ make -y //安装环境[root@client inotify-tools-3.14]# ./configure //配置[root@client inotify-tools-3.14]# make && make install //编译安装[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ //启动监控重新开启一个客户机的终端
[root@client ~]# cd /var/www/html/[root@client html]# touch abc[root@client html]# rm -rf abc回到开启监控的终端查看
/var/www/html/ CREATE abc/var/www/html/ DELETE abc //显示监控信息在client客户机创建脚本,通过inotifywait触发rsync同步操作脚本
[root@client inotify-tools-3.14]# cd /opt/[root@client opt]# vim inotify.sh#!/bin/bashINOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /var/www/html/"RSYNC_CMD="rsync -avz --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.144.128::wwwroot/"$INOTIFY_CMD | while read DIRECTORY EVENT FILE do if [ $(pgrep rsync | wc -l) -le 0 ]; then $RSYNC_CMD fidone[root@client opt]# chmod +x inotify.sh //添加执行权限在rsyncd服务器上修改配置文件
[root@rsyncd www]# vim /etc/rsyncd.confread only = no //关闭只读权限[root@rsyncd www]# netstat -natp | grep rsynctcp 0 0 192.168.144.128:873 0.0.0.0:* LISTEN 36346/rsync [root@rsyncd www]# kill -9 36346 //关闭服务[root@rsyncd www]# netstat -natp | grep rsync[root@rsyncd www]# rm -rf /var/run/rsyncd.pid //删除pid文件[root@rsyncd www]# rsync --daemon //重新开启rsync服务在client客户机上执行inotify脚本文件
[root@client opt]# ./inotify.sh重新开启一个client客户机终端
[root@client html]# echo "this is test" > test.txt //添加文本查看开启监控服务终端信息
[root@client opt]# ./inotify.sh sending incremental file list./rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)test.txtsent 121 bytes received 30 bytes 302.00 bytes/sectotal size is 30 speedup is 0.20rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]sending incremental file listsent 66 bytes received 8 bytes 148.00 bytes/sectotal size is 30 speedup is 0.41在rsync服务器上查看
[root@rsyncd www]# cd html/[root@rsyncd html]# lsindex.html test.txt //实现同步
文件
同步
服务
监控
配置
用户
信息
备份
客户
密码
服务器
权限
位置
地址
实时
用户名
实例
模块
目录
目标
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
虚拟现实展示系统软件开发
株洲数据库地址
杭州在线学习软件开发
逻辑服务器
小学六年级网络安全
福建省网络安全周
数据库连接断开会释放锁
武汉亘知网络技术有限公司
智能软件开发平台
香港服务器托管
维宏软件开发商密码
2019年网络安全的主体是
数据库连接池是在哪里
圣安地列斯线下服务器在哪里
多cpu服务器组装图片
数据库人民
松江区海航数据库服务制品价格
5g网络技术带来的影响
易语言查找数据库中的数据
乡镇网络安全等级保护制度
贵州微丰软件开发公司电话
软件开发中哪一部最赚钱
中航锂电怎么样 软件开发
租用国外服务器搭建网页
机器人服务器分配
数据库 用户好友关系
计算机软件开发工程师就业前景
电子商务与网络安全 说课稿
华中科技大学互联网教授
关于网络安全的班会主持词