rsync远程同步和配置inotify+rsync远程同步

一.rsync远程同步

  1. 服务的启动停止和安装
    安装(默认不需要安装)
    [root@centos01 ~]# yum -y install rsync
    在这里插入图片描述
    启动(rsync默认端口号为873)
    [root@centos01 ~]# rsync --daemon
    [root@centos01 ~]# netstat -anptu | grep rsync
    在这里插入图片描述
    停止
    [root@centos01 ~]# killall rsync
    [root@centos01 ~]# netstat -anptu | grep rsync
    在这里插入图片描述
  2. 备份和配置rsync主配置文件
    1) 备份主配置文件
    [root@centos01 ~]# cp /etc/rsyncd.conf /etc/rsyncd.conf.bak
    在这里插入图片描述
    2) 配置主配置文件
    [root@centos01 ~]# vim /etc/rsyncd.conf 进入主配置文件中
    7 uid = nobody 管理rsync用户
    8 gid = nobody 管理rsync组
    9 use chroot = yes 禁锢rsync同步源根目录
    10 address = 192.168.100.10 监听IP地址
    11 port = 873 监听端口号
    12 hosts allow = 192.168.100.0/24 允许100网段访问
    24 [bdqn] 共享模块名字
    25 path = /bdqn 存储同步源数据根目录
    26 omment = test bdqn 同步源说明
    27 read only = yes 设置为只读
    28 dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 指定文件类型不再进行压缩
    29 auth users = bob 验证账户
    30 secrets file = /etc/rsyncd_user.db 设置验证数据库
    在这里插入图片描述
    在这里插入图片描述
    3) 创建同步源根目录
    [root@centos01 ~]# mkdir /bdqn
    在这里插入图片描述
    4) 配置验证账户密码数据库
    [root@centos01 ~]# vim /etc/rsyncd_user.db
    bob:pwd@123
    在这里插入图片描述
    5) 配置权限
    [root@centos01 ~]# chmod 600 /etc/rsyncd_user.db
    在这里插入图片描述
    6) 重启服务
    [root@centos01 ~]# rsync --daemon
    在这里插入图片描述
  3. 使用rsync客户端同步rsync源数据(使用02测试)
    1) 创建目录benet和accp和在01上的/bdqn下创建测试文件
    [root@centos02 ~]# mkdir /benet
    [root@centos02 ~]# mkdir /accp
    在这里插入图片描述
    [root@centos01 ~]# cd /bdqn
    [root@centos01 bdqn]# touch 1.txt
    [root@centos01 bdqn]# touch 2.txt
    [root@centos01 bdqn]# touch 2.mp3
    在这里插入图片描述
    2) 方式一全部备份
    [root@centos02 ~]# rsync -avz bob@192.168.100.10::bdqn /benet/
    共享模块为bdqn 同步到/benet下
    在这里插入图片描述
    验证
    [root@centos02 ~]# cd /benet
    [root@centos02 ~]# ls
    在这里插入图片描述
    3) 方式二备份没有的
    [root@centos02 ~]# rsync -avz rsync://bob@192.168.100.10/bdqn /accp
    在这里插入图片描述
    验证
    [root@centos02 ~]# cd /accp
    [root@centos02 ~]# ls
    在这里插入图片描述
  4. Rsync的格式和常见选项
    1) 格式
    rsync [选项] [同步源] [同步目标]
    2) 常见选项
    -r:递归模式同步目录使用
    -l:复制符号连接数据(小写L)
    -v:显示同步详细信息
    -a:归档模式保留文件的属性
    -z:传输过程中对数据进行压缩
    -p:保留源文件的权限标记
    -t:保留源文件的时间标记
    -g:保留源文件的属组标记
    -o:保留源文件的属主标记
    -H:保存硬连接文件
    -A:保存AVL属性信息
    -D:保存设别文件以及其他的特殊文件
    –delete:删除目标位置有而原始位置没有的文件
    –checksum:根据校验和(而不是文件大小,修改时间)来决定是否跳过文件配置rsync客户端定期同步rsync数据
    1) 创建存储密码文件并授权600
    [root@centos02 ~]# vim /root/password.pass
    pwd@123
    在这里插入图片描述
    2) 创建计划任务定期同步数据
    [root@centos02 ~]# crontab -e 进入计划任务配置文件
    */2 * * * * /usr/bin/rsync -az --delete --password-file=/root/password.pass bob@192.168.100.10::bdqn /accp
    在这里插入图片描述
    3) 启动计划任务服务并设置开机自启
    [root@centos02 ~]# systemctl start crond
    [root@centos02 ~]# systemctl enable crond
    在这里插入图片描述
    4) 查看是否计划任务备份成功
    在01上添加新的测试文件
    在这里插入图片描述
    验证2分钟后是否同步了4.txt
    在这里插入图片描述
    crontab -l 查看配置的计划任务列表
  5. 配置本地数据同步
    1) 创建一个benet目录
    在这里插入图片描述
    2) 同步bdqn目录数据及子目录数据到benet目录
    [root@centos01 ~]# rsync -rl /bdqn/ /benet
    在这里插入图片描述
    3) 同步bdqn目录到benet显示详细信息
    [root@centos01 ~]# rsync -rlv /bdqn/ /benet
    在这里插入图片描述
    二.配置inotify
  6. 切换光盘解压inotify软件包
    [root@centos01 ~]# mount /dev/cdrom /mnt
    [root@centos01 ~]# tar zxvf /mnt/inotify-tools-3.14.tar.gz -C /usr/src
    在这里插入图片描述
  7. 配置inotify服务
    1)进入目录
    [root@centos01 ~]# cd /usr/src/inotify-tools-3.14/
    在这里插入图片描述
    2)直接配置
    [root@centos01 inotify-tools-3.14]# ./configure
    在这里插入图片描述
    3)安装
    [root@centos01 inotify-tools-3.14]# make && make install
    在这里插入图片描述
    4)验证安装是否成功(支持这俩命令的使用说明安装成功)
    [root@centos01 ~]# inotifywa
    Inotifywait(动态监控系统文件的变化信息) inotifywatch(安装的是服务工具)
    在这里插入图片描述
    5)调整内核参数
    [root@centos01 ~]# vim /etc/sysctl.conf
    fs.inotify.max_queued_events = 16384 监控队列
    fs.inotify.max_user_instances = 1024 监控实例
    fs.inotify.max_user_watches = 1048576 一次性文件同步数量大于监控目标总数量
    在这里插入图片描述
    6)更新内核参数
    [root@centos01 ~]# sysctl -p
    在这里插入图片描述
  8. Inotifywait的作用和常见选项和监控事件类型
    1)作用
    监控创建文件,修改文件,修改文件属性,移动删除数据信息
    2)常见选项
    -m:持续监控
    -r:递归同步目录数据
    -q:输出信息
    -e:指定监控事件
    3)监控事件类型
    create:创建文件或目录
    move:移动文件或目录
    delete:删除文件或目录
    modify:修改文件目录数据
    attrib:修改文件目录属性
    4)使用inotifywait开始监控(mv修改 rm -rf 删除 echo修改数据 )
    [root@centos01 ~]# inotifywait -mrq -e create,move,delete,modify,attrib /bdqn/
    在这里插入图片描述
    在另一个同系统窗口上创建文件或目录验证监控 在这里插入图片描述
    在这里插入图片描述
    在第一个窗口查看是否监控成功
    在这里插入图片描述
    5)配置ssh秘钥对实现无身份验证scp数据
    获取rsync服务器的密钥对
    [root@centos01 ~]# ssh-keygen -t rsa
    在这里插入图片描述
    上传秘钥(公钥)到远程同步目标主机(公加私解)输入上传密码:pwd@123
    [root@centos01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.20
    在这里插入图片描述
    验证
    [root@centos01 ~]# scp /bdqn/1.txt root@192.168.100.20:/benet
    在这里插入图片描述
    6)编辑数据同步脚本自动同步
    [root@centos01 ~]# vim /inotify_rsync.sh
    #!/bin/bash
    INOTIFY_CMD="/usr/local/bin/inotifywait -mrq -e create,move,delete,modify,attrib /bdqn/"
    RSYNC_CMD=“rsync -azH --delete /bdqn/ root@192.168.100.20:/benet”
    $INOTIFY_CMD | while read DIRECTORY EVENT FILE 一旦INOTIFY_CMD读取到信息执行do后面的命令变量
    do
    $RSYNC_CMD
    done
    在这里插入图片描述
    配置执行权限
    [root@centos01 ~]# chmod +x /inotify_rsync.sh
    在这里插入图片描述
    验证
    在01创建自动同步到02上
    在这里插入图片描述
    在这里插入图片描述
    转到后台运行并设置开机自动运行脚本并给rc.local执行权限
    转到后台’
    [root@centos01 ~]# sh /inotify_rsync.sh &
    在这里插入图片描述
    设置开机自动启动脚本
    [root@centos01 bdqn]# vim /etc/rc.d/rc.local
    14 /usr/bin/sh /inotify_rsync.sh &
    在这里插入图片描述
    设置权限
    [root@centos01 bdqn]# chmod +x /etc/rc.d/rc.local
    在这里插入图片描述
    重启验证是否还可以同步数据在01上创建查看02是否同步
    在这里插入图片描述
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值