rsync数据实时同步
rsync数据实时同步

  • 案例一:同步整个文件夹同步
[[email protected] ~]# ls /abc
fstab  group  passwd
[[email protected] ~]# ls /test/
[[email protected] ~]# rsync -avz /abc /test            #同步没有加/,整个文件夹同步
[[email protected] ~]# ls /test/
abc

  • 案例二:只同步文件内容
[[email protected] ~]# rsync -avz /abc/ /test        #加/只同步文件内容
[[email protected] ~]# ls /test/
abc  fstab  group  passwd

[[email protected] ~]# echo 123 >> /abc/group          #写入内容
[[email protected] ~]# rsync -acz /abc/ /test/         #同步变化的数据

  • 案例三:同步文件夹多余删除,保持和服务文件一致
[[email protected] ~]# touch /test/ahha.txt       #创建一个文件
[[email protected] ~]# ls /test/
ahha.txt  fstab  group  passwd
[[email protected] ~]# rsync -acz --delete /abc/ /test/       #同步 ,多余删除
[[email protected] ~]# ls /test/                                    
fstab  group  passwd
  • 案例四:虚假机B文件同步到虚假机A
[[email protected] ~]# rsync -avz --delete /abc/ [email protected]192.168.4.100:/opt/   #同步到
[email protected]192.168.4.100's password: 

rsync数据实时同步

  • 案例五:实时同步
  • A主机安装notify-tools工具服务

rsync数据实时同步
rsync数据实时同步

[[email protected] ~]# rpm -ivh inotify-tools-3.14-8.el7.x86_64.rpm 
警告:inotify-tools-3.14-8.el7.x86_64.rpm:V3 RSA/SHA256 Signature, ** ID 352c64e5: NOKEY
准备中...                          ################################# [100%]
	软件包 inotify-tools-3.14-8.el7.x86_64 已经安装

[[email protected] ~]# inotifywait                       #运行命令
No files specified to watch!                  #没有监控文件
  • shell脚本实现实时监控
[[email protected] init.d]# vim rsync.sh             #创建脚本
#! /bin/bash
while inotifywait -rqq /abc                #
do 
rsync -az --delete /abc/ [email protected]192.168.4.200:/opt/           #监控A主机/abc/文件 同步到主机 /opt/
done
[[email protected] init.d]# /etc/init.d/rsync.sh &                              #后台运行脚本
[1] 8857

相关文章: