rsync远程同步
文章目录
一、关于rsync
一款快速增量备份工具
Remote Sync,远程同步
支持本地复制,或者与其他SSH,rsync主机同步
二、配置rsync源
1、基本思路
建立rsyncd.conf配置文件、独立的账号文件
启用rsync的 --daemon模式
2、配置文件rsyncd.conf
认证配置auth users、secrets file,不加则为匿名
3、独立的账号文件
用户名:密码、每行一个用户记录、独立的账号数据,不依赖系统账号
4、启用rsync服务
通过 --daemon独自提供服务,rsync --daemon
执行kill $(cat /var/run/rsyncd.pid)关闭服务
三、rsync命令
命令使用语法
rsync 【选项】原始位置 目标位置
常用选项
-r | 递归模式,包含目录及子目录中的所有文件 |
---|---|
-l | 对于符号链接文件仍然复制为符号链接文件 |
-v | 显示同步过程的详细(verbose)信息 |
-z | 在传输文件时进行压缩(compress) |
-a | 归档模式,保留文件的权限、属性等信息,等同于组合选项“-rlptgoD” |
-p | 保留文件的权限标记 |
-t | 保留文件的时间标记 |
-g | 保留文件的属组标记(仅超级用户使用) |
-o | 保留文件的属主标记(仅超级用户使用) |
-H | 保留硬连接文件 |
-A | 保留 ACL 属性信息 |
-D | 保留设备文件及其他特殊文件 |
–delete | 删除目标位置有而原始位置没有的文件 |
–checksum | 根据校验和(而不是文件大小、修改时间)来决定是否跳过文件 |
1、配置源的两种表示方法
格式一:用户名@主机地址::共享模块名
格式二:rsync://用户名@主机地址/共享模块名
2、rsync实时同步
1)、定期同步的不足
1、执行备份的时间固定,延迟明显、实时性差
2、当同步源长期不变化时,密集的定期任务是不必要的
2)、实时同步的优点
1、一旦同步源出现变化,立即启动备份
2、只要同步源无变化,则不执行备份
四、关于inotify
可以监控文件系统的变动情况,并做出通知响应
调整inotify内核参数(优化)
/etc/sysctl.conf(内核参数配置文件)
max_queue_events #监控事件队列大小
max_user_instances #最多监控实例数
max_user_watches #每个实例最多监控文件数
inotifywait:用于持续监控,实时输出结果
inotifywatch:用于短期监控,任务完成后再输出结果
五、配置rsync下行同步
环境配置
主机 | 操作系统 | IP地址 | 安装包 |
---|---|---|---|
Master | centos7 | 192.136.132.60 | rsync |
Slave | centos7 | 192.168.132.50 | rsync / inotify-tools-3.14.tar.gz |
1、master
systemctl stop firewalld.service
setenforce 0
yum -y install httpd rsync
vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.132.60
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.132.0/24
[wwwroot]
path = /var/www/html
comment = Document Root of www.kgc.com
read only = yes
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = haha
secrets file = /etc/rsyncd_users.db
vim /etc/rsyncd_users.db
haha:123123
chmod 600 /etc/rsyncd_users.db
rsync --daemon
netstat -natp | grep rsync
cd /var/www/html
touch aaa.html bbb.html
ls
2、slave
systemctl stop firewalld.service
setenforce 0
yum -y install rsync
cd /opt
mkdir 222
chmod 777 haha
vim /etc/server.pass
123123
chmod 600 /etc/server.pass
rsync -az --delete --password-file=/etc/server.pass kiki@192.168.132.60::wwwroot /opt/222
ls 222
六、rsync+inotify实时同步
1、Master
vim /etc/rsyncd.conf
read only = no
kill `cat /var/run/rsyncd.pid`
netstat -natp | grep rsync
rsync --daemon
netstat -natp | grep rsync
chmod 777 /var/www/html
2、Slave
cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p
yum -y install gcc gcc-c++
#放入安装包
tar zxvf inotify-tools-3.14.tar.gz -C /opt
cd /opt/inotify-tools-3.14/
./configure
make && make install
vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /opt/222/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/222/ haha@192.168.132.60::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
cd /opt/
chmod +x inotify.sh
./inotify.sh
cd /opt/haha
touch ccc.html
rm -rf aaa.html
Master(192.168.132.60)验证
cd /var/www/html
ls