rsync远程同步

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地址安装包
Mastercentos7192.136.132.60rsync
Slavecentos7192.168.132.50rsync / 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

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值