rsync远程同步(实例!!!)
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,rsync 同步概述:Remote Sync ----- 远程同步,支持本地复制,或者与其他SSH 、rsync主机同步,功能类似于scp,但是要比scp丰富。官方网站:http://rsync.sa
千家信息网最后更新 2025年12月02日rsync远程同步(实例!!!)
rsync 同步概述:
Remote Sync ----- 远程同步,支持本地复制,或者与其他SSH 、rsync主机同步,功能类似于scp,但是要比scp丰富。官方网站:http://rsync.samba.orgrsync 同步特点:
1、可以镜像保存整个目录树和文件系统。2、可以很容易做到保持原来文件的权限、时间、软硬链接等等,无须特殊权限即可安装。3、快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。4、安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。5、支持匿名传输,以方便进行网站镜像。实例演示
第一步:配置rsync源服务器
rsync 是系统内核自带的,rpm - q rsync查看包 ,无需额外安装.如果是最小化安装的话,使用 yum安装一下即可1.修改rsyncd.conf配置文件
[root@server ~]# vim /etc/rsyncd.conf #7、8、9行,uid = nobodygid = nobodyuse chroot = yes#11行,pid file = /var/run/rsyncd.pid#16行,dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2#追加端口号port 873#追加日志文件路径log file = /var/log/rsyncd.log#追加授权访问地址段hosts allow = 192.168.142.0/24#添加共享模块#模块名称[wwwroot]#源共享目录路径path = /var/www/html#网站说明comment = www.bdqn.cn#是否只读read only = yes#认证用户名auth users = backuper#认证用户密码文件路径secrets file = /etc/rsyncd_users.db2.创建认证用户密码文件
[root@server ~]# vim /etc/rsyncd_users.db#写入认证用户名与密码backuper:abc1233.授权仅属主的最大权限
[root@server ~]# chmod 600 /etc/rsyncd_users.db4.安装HTTP的服务
[root@server ~]# yum install httpd -y5.创建共享内容
[root@server ~]# cd /var/www/html[root@server html]# echo "this is test web" > index.html6.开启服务
[root@server html]# rsync --daemon7.查看服务状态
[root@server html]# netstat -ntap | grep rsynctcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 60268/rsync tcp6 0 0 :::873 :::* LISTEN 60268/rsync 8.关闭防火墙及安全功能
[root@server html]# systemctl stop firewalld.service [root@server html]# setenforce 0第二步:配置客户发起端
1.关闭防火墙及安全功能
[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 02.安装http服务
[root@localhost ~]# yum install httpd -y3.客户发起端配置方式
#配置源方式一,用户名@主机地址::共享模块名[root@localhost ~]# rsync -avz backuper@192.168.142.153::wwwroot /var/www/htmlPassword: #输入用户密码receiving incremental file list./index.htmlsent 83 bytes received 172 bytes 72.86 bytes/sectotal size is 17 speedup is 0.07#查看共享到的内容[root@localhost ~]# cat /var/www/html/index.html this is test web#配置源方式二,rsync://用户名@主机地址/共享模块名[root@localhost ~]# rsync -avz rsync://backuper@192.168.142.153/wwwroot /var/www/htmlPassword: receiving incremental file list./index.htmlsent 83 bytes received 172 bytes 56.67 bytes/sectotal size is 17 speedup is 0.07#查看共享到的内容[root@localhost ~]# cat /var/www/html/index.html this is test web4.创建免交互密码文件
[root@localhost ~]# vim /etc/server.passabc123[root@localhost ~]# chmod 600 /etc/server.pass#免交互配置源方式[root@localhost ~]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.142.153::wwwroot /var/www/htmlreceiving incremental file list./index.htmlsent 83 bytes received 172 bytes 510.00 bytes/sectotal size is 17 speedup is 0.07#查看共享到的内容[root@localhost ~]# cat /var/www/html/index.html this is test web配合inotify工具使用,实现rsync实时同步
配置rsync实时同步:
1.定期同步的不足:执行备份的时间固定,延迟明细,实时性差;当同步源长期不变化时,密集的定期任务是不必要的2.实时同步的优点:一旦同步源出现变化,立即启用备份;只要同步源不变化,则不执行备份关于 inotify:
Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。从版本 2.6.13 开始提供;可以监控文件系统的变化情况,并作出通知响应;辅助软件:inotify-tools第一步: 配置rsync+inotify实时同步
1.配置rsync源服务器,修改rsyncd.conf配置文件
[root@server ~]# vim /etc/rsyncd.conf #关闭只读read only = no2.调整客户端的inotify内核参数
[root@client ~]# vim /etc/sysctl.conf#监控队列大小fs.inotify.max_queued_events = 16384#最多监控实例数fs.inotify.max_user_instances = 1024#每个实例最多监控文件数fs.inotify.max_user_watches = 10485763.生效内核参数
[root@client ~]# sysctl -pfs.inotify.max_queued_events = 16384fs.inotify.max_user_instances = 1024fs.inotify.max_user_watches = 10485764.安装编译环境
[root@client ~]# yum install -y gcc gcc-c++ make5.远程获取资源包
[root@client ~]# mount.cifs //192.168.142.1/inotify /mnt[root@sclient ~]# cd /mnt[root@client mnt]# lsinotify-tools-3.14.tar.gz6.解压资源包
[root@client mnt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt7.配置inotify
[root@client mnt]# cd /opt/inotify-tools-3.14/[root@client inotify-tools-3.14]# ./configure8.编译安装
[root@client inotify-tools-3.14]# make && make install9.安装inotify-tools辅助工具
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/#-m表示持续进行监控,-r表示递归监控所有子对象,-q表示简化输出信息,-e表示要监控哪些时间类型10.重开一个终端登录,增删文件
#创建文件[root@client html]# touch abc[root@client html]# lsabc index.html#删除文件[root@client html]# rm -rf abc[root@client html]# lsindex.html11.返回监控端,验证同步效果
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html//var/www/html/ CREATE abc #创建记录/var/www/html/ DELETE abc #删除记录12.通过inotifywait触发rsync同步操作脚本
vim inotify.sh#!/bin/bashINOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"RSYNCLCMD="rsyne -azH --delete --password-file=/etc/server.pass /var/www/htm1/ backuper@192.168.142.153::wwwroot/"$INOTIFY_CMD | while read DIRECTORY EVENT FILE#读取输出的监控记录doif [ $(pgrep rsync | wc -l) -le 0 ] ; then#若rsync为执行,则立即启动$RSYNC_CMDfidone13.源端于客户端都需要html目录最高授权
[root@server www]# chmod 777 html/[root@client www]# chmod 777 html/14.执行脚本
[root@client opt]# source inotify.sh15.重开终端,并切入共享目录
[root@client opt]# cd /var/www/html/16.写入新的内容
[root@client html]# echo "this is my update" > test.txt第二步:验证实时同步
**1.回到源端查看同步数据包**[root@server html]# lsindex.html test.txt2.查看同步数据
[root@server html]# cat test.txt this is my update谢谢阅读!!!
同步
文件
配置
监控
用户
内容
实时
服务
密码
方式
客户
模块
用户名
目录
系统
传输
变化
认证
实例
安全
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器网络的特点是什么
坦克世界闪击战一直服务器繁忙
阿里云管理控制台服务器搭建
重新查询数据库中一样的数据
如何管理svn服务器
数据库删除数据的方法
济南安卓软件开发公司哪家比较好
网络安全专业工作方向
服务器上如何安装win7系统
阿里云服务器备份失败
服务器tls安全设置
国产数据库有哪些优缺点
微信泄密和网络安全专项整顿会
校园网络安全示范区申报理由
计算机网络安全管理的方法
山东网络技术产品介绍
国内数据库类型
网络安全最多以处罚
告诉我网络安全的手抄报的字
放置奇兵绿色服务器
sql数据库条件更改
软件开发中tag是什么意思啊
免费ip数据库接口
怎么查看魔兽世界服务器
5g网络技术参数
gis数据库删除的数据怎么恢复
丽水软件开发费用
唱歌评分器软件开发
服务器安全狗去验证页面
服务器管理器启动不了