rsync远程同步

本文介绍了rsync作为快速增量备份工具的使用,包括配置rsync源、命令选项、免交互处理。同时,讲解了Linux内核的inotify机制,以及如何结合rsync实现文件实时同步,包括调整内核参数、安装inotify-tools和利用inotifywait触发rsync同步操作。
摘要由CSDN通过智能技术生成

rsync远程同步

rsync

一款快速增量备份工具
  • Remote Sync,远程同步
  • 支持本地复制,或者与其他SSH、rsync主机同步
  • 官方网站: httop://rsync.samba.org

配置rsync源

[root@localhost ~]# rpm -q rsync         //查看rsync软件包是否存在
基本思路
  • 建立rsyncd.conf配置文件、独立的账号文件
  • 启用rsync的–daemon模式
配置文件rsyncd.conf
  • 需要手动建立、语法类似于Samba配置
  • 认证配置auth users、secrets file,不加则为匿名
配置rsync源服务器
[root@localhost ~]#  vim /etc/rsyncd.conf
uid = nobody
gid = nobody            //匿名用户
use chroot = yes     //禁止进入家目录
address = 192.168.45.133
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.45.0/24
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[wwwroot]
path = /var/www/html        //站点同步目录
comment = www.kgc.cn     //描述信息
read only = yes            //权限
auth users = backuper        //指定来访用户
secrets file = /etc/rsyncd_users.db      //密码文件
rsync账号文件
  • 采用“用户名:密码”的记录格式,每行一个用户记录
  • 独立的账号数据,不宜懒于系统账号
#设置账号登录密码
[root@localhost ~]# vim /etc/rsyncd_users.db
  backuper:abc123
#修改文件权限
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db
启用rsync服务
  • 通过–deamon服务独自提供服务
  • 执行kill $(cat /var/run/rsyncd.pid)关闭rsync服务
#启动服务
[root@localhost ~]# rsync -deamon
#查看文件是否启动成功
[root@localhost ~]# netstat -ntap | grep rsync
tcp        0      0 192.168.45.133:873      0.0.0.0:*               LISTEN      100732/rsync  
配置网站文件
主服务器
[root@localhost ~]#systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0 
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is test web" > index.html
[root@localhost html]# cd ..
[root@localhost www]# chmod 777 html/
客户机
root@localhost ~]#systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0 
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# cd /var/www/
[root@localhost www]# chmod 777 html/

rsync命令的用法

常用选项
  • -a:归档模式,递归并保留对象属性,等同于-rlptgoD
  • -v:显示同步过程的详细(verbose)信息
  • -z:在传递文件时进行压缩(compress)
  • -H:保留硬链接文件
  • -A: 保留ACL属性信息
  • –delete:删除目标位置有而原始位置没有的文件
  • –checksum:根据对着的校验和来决定是否跳过文件
配置源的两种方法
格式1:用户名@主机地址::共享模块名
格式2:rsync://用户名@主机地址/共享模块名
[root@localhost ~]# rsync -avz
backuper@192.168.45.132::wwwroot/root

[root@localhost ~]# rsync -avz
rsync://backuper@192.168.45.132/wwwroot/root
第一种方式
[root@localhost www]# rsync -avz backuper@192.168.45.133::wwwroot /var/www/html/
Password: 
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  14.57 bytes/sec
total size is 17  speedup is 0.07
第二种方式
[root@localhost html]# rm -rf index.html 
[root@localhost html]# ls
[root@localhost html]# rsync -avz rsync://backuper@192.168.45.133/wwwroot /var/www/html/
Password: 
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  72.86 bytes/sec
total size is 17  speedup is 0.07
[root@localhost html]# ls
index.html
rsync同步操作示例
  • 下行rsync源:wwwroot共享——>myweb
rsync源的免交互处理
  • 使用 --password-file=密码文件
#删除刚才同步过来的文件
[root@localhost html]# rm -rf index.html 
[root@localhost html]# ls
#创建密码文件
[root@localhost html]# vim /etc/services.pass
abc123
[root@localhost html]#  rsync -avz --delete --password-file=/etc/services.pass backuper@192.168.45.133::wwwroot /var/www/html/
receiving incremental file list
./
index.html
sent 83 bytes  received 172 bytes  24.29 bytes/sec
total size is 17  speedup is 0.07
[root@localhost html]# 
[root@localhost html]# ls
index.html

rsync实时同步

定期同步的不足
  • 执行备份的时间固定,延迟明显,实时性差
  • 当同步源长期不变化时,密集的定期任务是不必要的
实时同步的优点
  • 一旦同步源出现变化,立即启动备份
  • 只要同步源无变化,则不执行备份

inotify

linux内核的inotify机制

  • 从版本2.6.13开始提供
  • 可以监控文件系统的变动情况,并作出通知响应
  • 辅助软件:inotify-tools
    在这里插入图片描述

rsync+inotify实时同步

调整inotify内核参数

  • max_queue_events:监控队列大小
  • max_user_instances:最多监控实例数
  • max_user_watches:每个实例最多监控文件数
[root@localhost html]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost html]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

安装inotify-tools辅助工具

  • inotifywait:用于持续监控,实时输出结果
  • inotifywatch:用于短期监控,任务完成后再除结果
#挂载软件包
[root@localhost html]# mount.cifs //192.168.100.3/GUN /mnt
Password for root@//192.168.100.3/GUN:  
[root@localhost html]# cd /mnt
#解压软件包
[root@localhost mnt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt
[root@localhost mnt]# cd /opt/inotify-tools-3.14/
#安装必要软件包+
[root@localhost inotify-tools-3.14]# yum install gcc gcc-c++ make -y
#编译安装
[root@localhost inotify-tools-3.14]# ./configure
[root@localhost inotify-tools-3.14]# make && make install

#监控命令
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/

通过inotifywait触发rsync同步操作

[root@localhost ~]# vim /opt/inotify_rsync.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete  /var/www/html/"
RSYNC_CMD="rsync-azH --delete --password-file=/etc/services.pass /var/www/html/ backuper@192.168.45.130::wwwroot/"
#读取输出的监控记录
$INOTIFY_CMD | while read DIRECTORY EVENT FILE           
do
# 如果rsync未在执行,则立即启动
  if [ $(pgrep rsync | wc -l) -le 0 ];then
      $RSYNC_CMD
   fi
done
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值