rsync远程同步——(实战!)

发布时间:2024-06-01 点击:86
系统运维
关于rsync
一款快速增量备份工具
remote sync,远程同步支持本地复制,或者与其他ssh,rsync主机同步配置rsync源服务器
rsync同步源
指备份操作的远程服务器,也称为备份源
配置rsync源
基本思路
建立rsync.conf配置文件,独立的账号文件启用rsync的--daemon模式应用示例
用户backuper,允许下行同步操作的目录为/var/www/html配置文件rsyncd.conf
需手动建立,语法类似于samba配置认证配置auth users,secrets file,不加则为匿名rsync账号文件
采用“用户名:密码”的记录格式,每行一个用户记录独立的账号数据,不依赖于系统账号启用rsync服务
通过--daemon独自提供服务执行kill $(cat /var/run/rsync.pid)关闭rsync服务使用rsync备份工具
rsync命令的用法
rsync [选项] 原始位置 目标位置常用选项
-a:归档模式,递归并保留对象属性,等用于-rlptgod-v:显示同步过程的详细信息-z:在传输文件时进行压缩-h:保留硬连接文件-a:保留acl属性信息--delete:删除目标位置有而原始位置没有的文件--checksum:根据对象的校验和来决定是否跳过文件配置源的两种表示方法
格式1:用户名@主机地址::共享模块名格式2:rsync://用户名@主机地址/共享模块名rsync实时同步
定期同步的不足
执行备份的时间固定,延迟明显,实时性差当同步源长期不变化时,密集的定期任务是不必要的实时同步的优点
一旦同步源出现变化,立即启动备份只要同步源无变化,则不执行备份关于 inotify(安装在发起端的)
inotify 是一个 linux特性,它监控文件系统操作,比如读取、写入和创建。inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。可以监控文件系统的变化情况,并作出通知响应;辅助软件:inotify-tools
实验环境
rsyncd服务器 192.168.13.128client服务器 192.168.13.1291,在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.13.128 ##监听地址port = 873 ##端口号log file = /var/log/rsyncd.log ##日志文件路径hosts allow = 192.168.13.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 ##密码文件[root@rsyncd ~]# vim /etc/rsyncd_users.db ##创建密码文件backuper:123123 ##用户名:密码[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db ##给root用户读写权限[root@rsyncd ~]# rsync --daemon ##开启rsync服务[root@rsyncd ~]# netstat -ntap | grep rsync ##查看端口tcp 0 0 192.168.13.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/ ##给最大权限,方便任意用户操作2,在客户端服务器上,拉取同步源rsyncd
[root@client ~]# systemctl stop firewalld.service ##关闭防火墙[root@client ~]# setenforce 0[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.13.128::wwwroot /var/www/html/ ##拉取共享模块##输入密码 [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.13.128::wwwroot /var/www/html/ ##指定本地密码文件,删除目标位置有而原始位置没有的文件,实现免交互3,在客户机上安装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.3/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

阿里云云服务器怎么搭建网站
金融业制作网站建设应该怎样做,请看!
怎么做友情链接?做友情链接需要注意什么?
买腾讯云服务器选什么镜像
设计网站要注意什么 如何选择网站制作设计公司
我的域名访问不了-虚拟主机/数据库问题
百度云怎么进入服务器
门户网站建设公司案例的建设类型有哪些?