【理论+实操】rsync远程同步---------太详细了

一、简介

■remote sync ,远程同步,它是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树
■支持本地复制,或者与其他ssh、rsync主机同步
■官方网站:http://rsync.samba.org
■支持增量备份、保持连接和权限,且采用优化的同步算法,传输前执行压缩,适用于异地备份、镜像服务器等应用

二、配置rsync源服务器

2.1 rsync同步源

■指备份操作的远程服务器,也称为备份源
在这里插入图片描述

2.2 配置rsync源

■基本思路
●建立rsyncd.conf配置文件、独立的账号文件
●启用rsync——daemon模式

■应用示例
●用户gsybk,允许下行同步
●操作的目录为/var/www/html

■配置文件
●需要手动建立,语法类似samba配置
●认证配置auth users、secrets file ,不加则默认为匿名

■rsync账号文件
●采用“用户名:密码”的格式记录,每行一个用户记录
●独立的账号数据,不依赖系统账号

■启动rsync服务
●通过–daemon独自提供服务
●执行 kill $(cat /var/run/rsyncd.pid)关闭rsync服务

三、备份工具

3.1 命令基本用法

■用法:
rsync [选项] 原始位置 目标位置

●-a:归档模式,递归并保留对象属性,等同于 -rlptgoD
●-v:显示同步过程的详细(verbose)信息
●-z:在传输文件时进行压缩(compress)
●-H:保留硬连接文件
●-A:保留ACL属性信息
●–delete:删除目标位置有而原始位置没有的文件
●–checksum:根据对象的校验和来决定是否跳过文件

3.2 配置源表示方法

■格式1:用户名@主机地址::共享模块名
■格式2:rsync://用户名@主机地址/共享模块名

四、备份操作

4.1 实验环境

■用户backuper,允许下行同步
■操作的目录为/var/www/html

4.2 基本思路

■建立rsyncd.conf配置文件
■建立独立的账号文件
■启用rsync——daemon模式
■服务端IP: 20.0.0.10
■客户端IP: 20.0.0.11

4.3 检查是否安装rsync服务

■rpm -q rsync
rsync-3.1.2-4.el7.x86_64

■若没安装 yum -y install rsync

■rpm -q rsyncd
package rsyncd is not installed //服务没有启用

4.4 关闭防火墙、核心防护

■iptables -F
■setenforce 0

或者■sed -i ‘s/^SELINUX=.*/SELINUX=disabled/’ /etc/selinux/config 永久

4.5 修改/etc/rsyncd.conf

vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
address = 20.0.0.10
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

##参数解释##
uid = nobody
gid = nobody
use chroot = yes ####禁锢在源目录
address = 20.0.0.10 ####监听地址
port 873 ####监听端口号
log file = /var/log/rsyncd.log ####日志文件位置
pid file = /var/run/rsyncd.pid ###存放进程ID的文件位置
hosts allow = 20.0.0.0/24 ####允许访问的客户机地址
[wwwroot] ####共享模块名称
path = /var/www/html ####源目录的实际路径
comment = Document Root of www.gsy.com
read only =yes #####是否只读
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 ####同步时不再压缩的文件类型
auth users =backuper #####授权账户

4.6 编辑账户文件/etc/rsyncd_users.db

vi /etc/rsyncd_users.db
backuper:abc123

chmod 600 /etc/rsyncd_users.db

4.7 启动rsync服务

rsync --daemon
netstat -anpt |grep rsync
tcp 0 0 20.0.0.10:873 0.0.0.0:* LISTEN 20917/rsync

4.8 在/var/www.httml/下创建文件

echo “this is abc” > /var/www/html/1.txt

4.9 客户端配置

4.9.1 检测rsync是否安装

rpm -q rsync ###若没安装 yum -y install rsync

4.9.2 关闭防火墙、核心防护

systemctl stop firewalld
setenforce 0
sed -i ‘s/^SELINUX=.*/SELINUX=disabled/’ /etc/selinux/config

4.9.3 测试有密码拉取文件

方法1:
rsync -avz [email protected]::wwwroot /opt

方法2:
rsync -avz rsync://[email protected]/wwwroot /opt

4.9.4 查看拉取的文件

cat /opt/1.txt
this is abc

4.9.5 免交互

■在客户端上:
cd /opt
rm -rf 1.txt

vi /etc/server.pass
abc123

chmod 600 /etc/server.pass
rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/

4.9.6 设置周期性计划任务

■每天晚上10点半对服务器网站目录更新一次
mkdir /webbak
crontab -e

30 22 * * * /usr/bin/rsync -avz --delete --password-file=/etc/server.pass [email protected]::wwwroot /webbak

systemctl restart crond
systemctl enable crond

五、rsync+inotify

5.1 rsync实时同步

■定期同步的不足之处:
●执行备份的时间固定,延迟明显、实时性差
●当同步源长期不变化时,密集的定期任务是不必要的

■实时同步的优点:
●一旦同步源出现变化,立即启动备份
●只要同步源无变化,则不执行备份

5.2 inotify简介

■Linux内核的inotify机制
●从版本2.6.13开始提供
●可以监控文件系统的变动情况,并作出通知响应
●辅助软件:inotify-tools

5.3 rsync+inotify实时同步

5.3.1 调整inotify内核参数

●max_queued_events:监控事件队列的大小
●max_user_instances:最多监控实例数,默认128
●max_user_watches:每个实例最多监控文件数(watch一般是针对目录,决定了同时同一用户可以监控的目录数量)

5.3.2 安装inotify-tools辅助工具

●inotifywait:用于持续监控,实时输出结果
●inotifywatch:用于短期监控,任务完成后再出结果

在这里插入图片描述

5.3.3 通过inotifywait触发rsync同步操作

●使用while、read持续获取监控结果
●根据结果可以作进一步判断,决定执行何种操作

在这里插入图片描述

5.4 实验操作

5.4.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

5.4.2 安装inotify-tools辅助工具

■上传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

■新开启一个client(1)的终端
cd /web
touch 1.txt

■返回在client查看
/web CREATE 1.txt 会看到有此信息更新

5.4.3 通过inotifywait触发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 [email protected]::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

5.4.4

■在rsyncd上:
vi /etc/rsyncd.conf

read only = no
//设置只读模式
netstat -anpt | grep rsync
tcp 0 0 20.0.0.10: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.10:873 0.0.0.0:* LISTEN 26021/rsync

注意client的/web和rsyncd的/var/www/html权限设置为777
rsyncd:chmod 777 /var/www/html/
client:chmod 777 /web

■在client上:
./inotify.sh

■新开启终端client(1):
cd /web
echo “this is text” >text.txt
ll

-rw-r–r-- 1 root root 13 Oct 23 03:12 text.txt

■在rsyncd上查看:
cd /var/www/html/web
ll

-rw-r–r-- 1 root root 13 Oct 23 15:08 text.txt

■在client(1)上查看:
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.10:873 0.0.0.0:* LISTEN 20987/rsync

■返回client端:
重新yunxing脚本
./inotify.sh &
//可以放到后台运行

■重新打开client(1):
cd /web
touch aa

■到rsync服务器端: cd /var/www/html/web ll aa