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