Rsync介绍
①rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。
②rsync软件适用于unix/linux/windows等多种操作系统平台
③rsync是一个快速和非常方便的文件复制工具。它能本地复制,远程复制,或者远程守护进程方式复制,它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制
④以其delta-transfer算法闻名。
⑤rsync监听端口: 873
⑥rsync运行模式:c/s
同步方式
①全量备份:
②原有的数据全部传送
③把原来的文件和新的文件一起统一传送
④全量复制,效率低
增量备份
在传输数据之前通过一些算法通过你有的数据和我有的数据进行对比,把不一样的数据通过网络传输22增量复制,效率高
rsync命令
常用选项 说明
-r 递归模式,包含目录及子目录中的所有文件
-l 对于符号链接文件仍然复制为符号链接文件
-v 显示同步过程的详细信息
-z 在传输文件时进行压缩
-a 档模式,递归并保留对象属性,等同于-rlptgoD
-p 保留文件的权限标记
-t 保留文件的时间标记
-g 保留文件的属组标记(仅超级用户使用)
-o 保留文件的属主标记(仅超级用户使用)
-H 保留硬链接文件
-A 保留ACL属性信息
-D 保留设备文件及其他特殊文件
--delete 删除目标位置有而原始位置没有的文件
--checksum 根据对象的校验和来决定是否跳过文件
本地复制
[root@rsync bak]# cd /
[root@rsync /]# rsync -avz /bak /opt/
sending incremental file list
bak/
bak/1.txt
bak/2.txt
[root@rsync /]# rsync -avz /bak/ /opt/
sending incremental file list
./
1.txt
2.txt
配置rsync源服务器
[root@rsync ~]# rpm -q rsync
[root@rsync ~]# yum -y install rsync
修改/etc/rsyncd.conf配置文件
[root@rsync ~]# vim /etc/rsyncd.conf
uid = nobody #root
gid = nobody #root
use chroot = yes #禁锢在源目录
address = 192.168.235.179 #监听地址
port 873 #监听端口 tcp/udp 873,可通过 cat /etc/services | grep rsync 查看
log file = /var/log/rsyncd.log #日志文件位置
pid file = /var/run/rsyncd.pid #存放进程ID的文件位置
hosts allow = 192.168.235.0/24 #允许访问的客户机地址
[wwwroot] ##第一个共享模块
path = /var/www/html #源目录的实际路径
comment = Document Root of www.ljm.com
read only = yes #是否为只读
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z #同步时不再压缩的文件类型
auth users = backuper #授权账户,多个账号以空格分隔
secrets file = /etc/rsyncd_users.db #存放账户信息的数据文件
为备份账户创建数据文件
[root@rsync ~]# vim /etc/user.db
[root@rsync ~]# chmod 600 /etc/user.db
保证所有用户对源目录/var/www/html 都有读的权限
[root@rsync ~]# mkdir -p /var/www/html
[root@rsync ~]# chmod +r /var/www/html/
[root@rsync ~]# ls -ld /var/www/html/
启动rsync服务
[root@rsync ~]# rsync --daemon
[root@rsync ~]# netstat -natp | grep rsync
服务器
[root@rsync ~]# cd /var/www/html/
[root@rsync html]# echo "hello world" >> 1.txt
客户端
[root@client ~]# mkdir /bak
[root@client ~]# rsync -avz backuper@192.168.235.179::wwwroot /bak/
客户端:免交互格式配置
[root@client /]# echo "123123" > /etc/server.pass
[root@client /]# cat /etc/server.pass
123123
[root@client /]# chmod 600 /etc/server.pass
[root@client /]# cd /bak
[root@client bak]# ls
1.txt
[root@client bak]# rm -rf 1.txt
[root@client bak]# rsync -avz --password-file=/etc/server.pass backuper@192.168.235.179::wwwroot /bak
[root@rsync html]# rm -rf 1.txt
[root@client bak]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.235.179::wwwroot /bak
rsync买时同步
定期同步的不足
执行备份的时间固定,延迟明显实时性差
当同步源长期不变化时,密集的定期任务是不必要的
实时同步的优点
—旦同步源出现变化,立即启动备份
只要同步源无变化,则不执行备份
inotify
简介
可以监控文件系统的变动情况,并做出通知响应
调整inotify内核参数(优化)
/etc/sysctl.conf(内核参数配置文件)
inotifywait: #用于持续监控,实时输出结果
inotifywatch: #用于短期监控,任务完成后再输出结果
max_queue_events #监控事件队列大小
max_user_instances #最多监控实例数
max_user_watches #每个实例最多监控文件数
2、inotifywait(持续监控并实时输出监控结果的命令)
格式:inotifywait [参数]
常见参数 说明
-m 持续进行监控
-r 递归监控所有子对象
-q 简化输出信息
-e 指定要监控哪些事件类型
实验
[root@rsync ~]# vim /etc/rsyncd.conf
uid = root
gid = root
read only = no #关闭只读,上行同步需要可写权限
[root@rsync ~]# kill `cat /var/run/rsyncd.pid`
[root@rsync ~]# netstat -natp | grep rsync
[root@rsync ~]# rsync --daemon
[root@rsync ~]# netstat -natp | grep rsync
客户端:inotify内核参数
[root@client ~]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 32768 #监控时间队列,默认为16384
fs.inotify.max_user_instances = 1024 #最多监控实例数,默认为128
fs.inotify.max_user_watches = 1048576 #每个实例最多监控文件数,默认为8192
[root@client ~]# sysctl -p
client安装inotify-tools
[root@client ~]# yum -y install gcc gcc-c++
[root@client opt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt!
[root@client inotify-tools-3.14]# ./configure
[root@client inotify-tools-3.14]# make && make install
[root@client ~]# inotifywait -mrq -e modify,create,move,delete /bak
client编写触发同步脚本
[root@client opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /bak/"
RSYNC_CMD="rsync -apzH --delete --password-file=/etc/server.pass /bak/ backuper@192.168.235.179::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
[root@client opt]# chmod +x /opt/inotify.sh
[root@client opt]# chmod +x /etc/rc.d/rc.local
[root@client opt]# echo "/opt/inotify.sh" >> /etc/rc.d/rc.local
测试
[root@client opt]# sh -x inotify.sh