文章目录
一、rsync同步简介
1.1 概述
一款快速增量备份工具
- Remote Sync,远程同步
- 支持本地复制,或者与其他SSH、rsync主机同步
- 官方网站:http://rsync.samba.org
1.2 配置rsync源
基本思路
- 建立rsyncd.conf配置文件、独立的账号文件
- 启用rsync的–daemon模式
应用示例
- 用户backuper,允许下行同步
- 操作的目录为 /var/www/html
配置文件rsyncd.conf
- 需手动建立,语法类似于Samba配置
- 认证配置auth users、secrets file、不加则为匿名
rsync账号文件
- 采用 “用户名:密码” 的记录格式,每行一个用户记录
- 独立的账号数据,不依赖于系统账号
启用rsync服务
- 通过–daemon独自提供服务
注: 执行kill $(cat /var/run/rsyncd.pid)关闭rsync服务
1.3 rsync基本命令
rsync命令的用法
rsync 【选项】 原始位置 目标位置
常用选项:
- -a:归档模式,递归并保留对象属性,等同于-rlptgoD
- -v:显示同步过程的详细(verbose)信息
- -z:在传递文件时进行压缩(compress)
- -H:保留硬链接文件
- -A: 保留ACL属性信息
- –delete:删除目标位置有而原始位置没有的文件
- –checksum:根据对着的校验和来决定是否跳过文件
配置源的两种方法
格式1:用户名@主机地址::共享模块名
格式2:rsync://用户名@主机地址/共享模块名
二、实验
2.1 实验环境
名称 | IP | 安装服务 |
---|---|---|
源端 | 20.0.0.21 | rysnc |
发起端 | 20.0.0.22 | rysnc,inotify |
2.2 实验步骤
2.2.1 源端修改配置文件
vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes ### 禁锢家目录
address = 20.0.0.21 ### 监听地址
port 873 ### 监听端口号
log file = /var/log/rsyncd.log ### 日志文件位置
pid file = /var/run/rsyncd.pid ### 进程文件位置
hosts allow = 20.0.0.0/24 ### 允许访问的客户机地址
[wwwroot] ### 共享模块名称
path = /var/www/html
comment = Document Root of www.51xit.com
read only = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper
secrets file = /etc/rsyncd_users.db
2.2.2 创建于用户密码文件
vi /etc/rsyncd_users.db
backuper:abc123
chmod 600 /etc/rsyncd_users.db
2.2.3 启动rsync 服务
rsync --daemon
netstat -anpt |grep rsync
tcp 0 0 20.0.0.21:873 0.0.0.0:* LISTEN 20917/rsync
2.3.4 在 /var/www/html目录下新建文件
echo “this is 123” > /var/www/html/1.txt
2.3.5 发起端配置
###测试有密码拉取文件
方法1:
rsync -avz backuper@20.0.0.21::wwwroot /opt
方法2:
rsync -avz rsync://backuper@20.0.0.21/wwwroot /opt
###查看拉取的文件
cat /opt/1.txt
this is 123
免交互
###源端:
cd /opt
rm -rf 1.txt
vi /etc/server.pass
abc123
chmod 600 /etc/server.pass
###发起端:
rsync -az --delete --password-file=/etc/server.pass backuper@20.0.0.21::wwwroot /opt/
2.3.6 设置周期性计划任务
mkdir /webbak
crontab -e
30 22 * * * /usr/bin/rsync -avz --delete --password-file=/etc/server.pass backuper@20.0.0.10::wwwroot /webbak
systemctl restart crond
systemctl enable crond
2.3.7 rsync实时同步
■ 定期同步的不足之处
- 执行备份的时间固定,延迟明显、实时性差
- 当同步源长期不变化时,密集的定期任务是不必要的
■ 实时同步的优点
- 一旦同步源出现变化,立即启动备份
- 只要同步源无变化,则不执行备份
实时同步:稳定,带宽大,负载大
三、inotify
3.1 inotify 概述
■ Linux内核的inotify机制
- 从版本2.6.13开始提供
- 可以监控文件系统的变动情况,并作出通知响应
- 辅助软件:inotify-tools
3.2 rsync + inotify实时同步
3.2.1 源端
修改inotify内核参数
vi /etc/sysctl.conf
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
3.2.2 安装http服务
yum -y install httpd
systemctl start httpd
systemctl enable httpd
3.2.3 安装inotify-tools辅助工具
yum -y install gcc gcc-c++
上传inotify-tools压缩包到/opt目录下
cd /opt
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14/
./configure
make && make install
cd ~
mkdir /web
inotifywait -mrq -e modify,create,move,delete /web
新开启一个发起端的终端
cd /web
touch 1.txt
返回在发起端查看
/web CREATE 1.txt ###会看到有此信息更新
3.2.4 通过inotifywai t触发rsync 同步操作
cd /opt
vi inotify.sh
#!/bin/bash
INOTIFY_CMD=“inotifywait -mrq -e create,delete,move,modify,attrib /web”
RSYNC_CMD=“rsync -azH --delete --password-file=/etc/server.pass /web backuper@20.0.0.21::wwwroot”
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
chmod +x inotify.sh
接收端
vi /etc/rsyncd.conf
read only = no ###设置只读模式
netstat -anpt | grep rsync
tcp 0 0 20.0.0.21:873 0.0.0.0:* LISTEN 20917/rsync
kill -9 20917
netstat -anpt | grep rsync ###已经关闭服务
rsync --daemon ###重启服务,报错
failed to create pid file /var/run/rsyncd.pid: File exists
cd /run/
rm -rf rsyncd.pid
rsync --daemon ###启动成功
netstat -anpt | grep rsync
tcp 0 0 20.0.0.21:873 0.0.0.0:* LISTEN 26021/rsync
'***注:*** 发起端的/web和接收端的/var/www/html权限设置为777
接收端:chmod 777 /var/www/html/
发起端:chmod 777 /web'
发起端
./inotify.sh
**新开启终端发起端**
cd /web
echo “this is t” >text.txt
ll
-rw-r–r-- 1 root root 13 Oct 23 03:12 text.txt
在接收端上查看
cd /var/www/html/web
ll
-rw-r–r-- 1 root root 13 Oct 23 15:08 text.txt
在新开启的发起端上查看
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
rsync: chgrp “/web” (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp “/web/.text.txt.veXjy3” (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
测试,发现可以将发起端创建的数据文件同步到接收端,但是发起端会有报错
解决方法:将接收的uid和gid修改为root
vi /etc/rsyncd.conf
uid = root
gid = root
cd /run
rm -rf rsyncd.pid
rsync --daemon
netstat -anpt | grep rsync
tcp 0 0 20.0.0.21:873 0.0.0.0:* LISTEN 20987/rsync
返回发起端
###重新运行脚本
./inotify.sh &
//可以放到后台运行
重新打开新开启的发起端
cd /web
touch aa
到接收端
cd /var/www/html/web
ll
aa