Rsync&Inotify

rsync

rsync是Unix系统的文件传输程序。可以更新整个目录树和文件系统,可选择保留符号链接,硬链接,文件所有权,权限,设备和时间。

命令行方式运行

[root@han rpm]# rsync -avz --delete /root/rpm/ root@192.168.2.160:/root/backup
root@192.168.2.160's password: 
sending incremental file list
./
123.txt
a.txt
autoconf-2.69-11.el7.noarch.rpm
dep.sh
index.html
m4-1.4.16-10.el7.x86_64.rpm
net-tools-2.0-0.25.20131004git.el7.x86_64.rpm
perl-5.16.3-294.el7_6.x86_64.rpm
perl-Data-Dumper-2.145-3.el7.x86_64.rpm
perl-libs-5.16.3-294.el7_6.x86_64.rpm
test/
test/gdisk-0.8.10-3.el7.x86_64.rpm
test/test

sent 10,302,533 bytes  received 259 bytes  1,585,044.92 bytes/sec
total size is 10,603,322  speedup is 1.03
[root@han rpm]# rm -rf a.txt 
[root@han rpm]# rsync -avz --delete /root/rpm/ root@192.168.2.160:/root/backup
root@192.168.2.160's password: 
sending incremental file list
deleting a.txt
./
sent 510 bytes  received 25 bytes  118.89 bytes/sec
total size is 10,603,322  speedup is 19,819.29
[root@han rpm]# 
[root@han]# rsync -avPW -e "ssh -T -c aes256-gcm@openssh.com -o Compression=no -x -p22" /mnt/mongo root@192.168.2.160:/nas		#提升传输效率,大约提升10倍的速度;
  • 说明:
    1、源端路径是目录,末尾是否加 “/”是有区别的,不加会把该目录复制到目的端,加上表示只复制此目录下的文件。
    2、rsync选项
    -a 保留权限属性等
    -v 显示详细信息
    -z选项也可能会降低性能,如果不通过慢速链接传输数据,则不应该使用它;–delete 源端删除的文件在目标端同步删除
    –progress 显示传输文件过程中的详细信息
    3、ssh选项
    -c aes128-ctr:选择一个较弱但较快的SSH加密。其他人指定arcfour,这将需要在目标主机上手动修改ssh_config。这并非总是可能的,并且这种加密对我来说效果很好;
    -T:在目标上的可行伪tty分配
    -c aes128-ctr:选择一个较弱但较快的SSH加密。其他人指定arcfour,这将需要在目标主机上手动修改ssh_config。这并非总是可能的,并且这种加密对我来说效果很好。
    -x:禁用X11转发
    -o Compression = no:禁用上述SSH压缩瓶颈

守护进程方式运行

  • 服务端配置
#全局配置
uid = root  
gid = root  
use chroot = no
max connections = 0
ignore errors
exclude = lost+found/
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
reverse lookup = no
#模块配置
[backup]  #模块名
path = /root/backup/  #目标端路径
comment = backup
read only = no    #客户端是否能够上传文件,no为可以上传    
auth users = rsyncuser  #传输文件使用的用户,不需要在系统中创建
secrets file = /root/rsync.user  #传输文件使用的用户密码文件,该文件需要手动创建,并且权限为600
[root@console ~]# cat rsync.user
rsyncuser:Han@5tgb
[root@console ~]# ll rsync.user
-rw------- 1 root root 22 Jun 13 13:08 rsync.user
[root@console ~]#
  • 客户端配置
[root@han ~]# ll rsync.user #客户端也需要创建密码文件,权限为600
-rw------- 1 root root 12 Jun 13 13:34 rsync.user
[root@han ~]# cat rsync.user  #只需要写入密码即可
Huawei@5tgb
[root@han ~]# rsync -avz --delete --password-file=/root/rsync.user /root/rpm/ rsyncuser@192.168.2.160::backup #backup为服务端的模块名,服务端和模块名之间为两个冒号,用户名为服务端配置文件中的认证用户,--password-file参数只有在守护进程时候才能使用,rsync默认端口为873。
sending incremental file list
./
123.txt
autoconf-2.69-11.el7.noarch.rpm
dep.sh
index.html
m4-1.4.16-10.el7.x86_64.rpm
net-tools-2.0-0.25.20131004git.el7.x86_64.rpm
perl-5.16.3-294.el7_6.x86_64.rpm
perl-Data-Dumper-2.145-3.el7.x86_64.rpm
perl-libs-5.16.3-294.el7_6.x86_64.rpm
test/
test/gdisk-0.8.10-3.el7.x86_64.rpm
test/test

sent 10,302,477 bytes  received 240 bytes  4,121,086.80 bytes/sec
total size is 10,603,322  speedup is 1.03
[root@han ~]# 

Inotify

对同步目录信息进行实时监控,记录并输出变化等相关信息,配合rsync进行对文件进行实时同步。
linux下支持inotify的最下内核版本为2.6.13。
相关内核参数:
/proc/sys/fs/inotify/max_queued_events inotify时间队列最大长度,如果太小会出现Event Queue Overflow错误,默认值是16834
/proc/sys/fs/inotify/max_user_watches 单进程可以监视文件的数量,默认值:8192
/proc/sys/fs/inotify/max_user_instances 每个用户创建inotify实例的最大值,默认:128
安装源:
epel源 yum install inotify-tools.
inotify-tools包主要文件:
inotifywait:在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,常用于实时同步的目录监控。
inotifywatch:收集被监控文件系统使用的统计数据,指文件系统事件发生的次数统计。

inotify常见用法

inotifywait常见选项
-m,–monitor 始终保持监听状态,不加此参数监听一次后退出。
-d,–daemon 以守护进程方式执行,和-m相似,配合-o使用
-r,–recursive 递归监控目录数据信息变化
-q,–quiet 输出少量事件信息
–exclude 指定排除文件或目录,使用扩展的正则表达式匹配的模式实现
–excludei 和exclude相似,不区分大小写
-o,–outfile 打印事件到文件中,想象与标准正确输出
-s,–syslogOutput 发送错误到syslog相当于标准错误输出
–timefmt 指定时间输出格式
–format 指定的输出格式:即实际监控输出内容
-e 指定监听指定的事件,多个事件之间用逗号隔开;如果省略,表示所有事件都进行监听。
–timefmt 时间格式,参考man 3 strftime
%Y 年份信息,包含世纪信息
%y 年份信息,不包含世纪信息
%m 显示月份,范围01-12
%d 每月的第几天,范围是01-31
%H 小时信息,使用24小时制,范围00-23
%M 分钟,范围00-59
%S 秒,范围00-59
%F 等价于 %Y-%m-%d
示例:–timefmt “%Y-%m-%d %H:%M:%S”
–format 格式定义
%T输出时间格式中定义的时间格式信息,通过–timefmt option语法格式指定
%w事件出现时,监控文件或目录的名称信息
%f时间发生时,将显示监控目录下触发时间的而文件或目录信息,否则为空。
%e显示发生的事件信息,不同的事件默认用逗号分隔
%Xe显示发生的事件信息,不同的事件指定用X进行分隔
示例:–format “%T %w%f event:%;e”
-e选项指定的时间类型
create 文件或目录创建
delete 文件或目录被删除
modify 文件或目录内容被写入
attrib 文件或目录属性被修改,一般指权限和所属用户或组
close_write 文件或目录关闭,在写入模式打开之后关闭的
close_nowrite 文件或目录关闭,在只读模式打开之后关闭的
close 文件或目录关闭,不管读或是写模式
open 文件或目录被打开
moved_to 文件或目录被移动到监控的目录中
move_from 文件或目录从监控的目录中被移动
move 文件或目录不管移动到或是移出监控目录都触发事件
access 文件或目录内容被读取
delete_self 文件或目录内容被读取
unmount 取消挂载
示例:-e create,delete,moved_to,close_write,attrib

inotify 最佳实践

示例1:监控一次性事件
intifywait /backup
示例2:持续监控
inotify -mrq /backup
示例3:持续后台监控指定事件
inotifywait -mrq /backup --timefmt “%F %H:%M:%S” --format “%T %w%f event:%;e” -e create,delete,moved_to,close_write,attrib

inotify&rsync实现数据实时同步

  • 服务端配置
    在目标服务器上生成验证文件
echo "rsyncuser:123456" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
在目标服务器上开启rsync服务
rsync --daemon 			CentOS 6
systemctl start rsyncd 	CentOS 7
  • 客户端配置
    客户端配置密码文件
echo "123456" > /etc/rsync.pass
chmod 600 /etc/rsync.pass

客户端测试同步数据

rsync -avz --password-file=/etc/rsync.pass /data/ rsyncuser@rsync服务器
IP::backup
  • 客户端上创建实时同步脚本
 #!/bin/bash
SRC='/data/'
DEST='rsyncuser@rsync服务器IP::backup'
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync">> /var/log/changelist.log
done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩华盛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值