一、rsync介绍
Remote Sync,远程同步,它是一个开源的快速增量备份工具,可以在不同主机之间镜像同步整个目录树。
支持增量备份、保持连接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用
支持本地复制,或者与其他SSH、rsync主机同步
rsync 与scp FTP等工具备份的机制的优越性在于rsync 同步备份是先比较在拷贝变化过的数据,这样更节省资源,如有1T的数据只有1K的数据改变,则rsync 基本上备份只要同步1k 的数据而 scp 是个傻瓜式的拷贝,全部拷贝。
1.1、rsync 服务的模式
1、ssh方式进行同步
2、C/S 方式,rsync 有服务器端daemon模块 和rsync 客户端
1.2、rsync服务原理
在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责相应来自客户机的rsync同步操作的服务器称为同步源。
在同步过程中,同步源负责提供文档的原始位置,发起端应对该位置有读取权限。
如下图:
rsync是一款快速增量备份工具,支持:
(1)本地复制;
(2)与其他SSH同步;
(3)与rsync主机同步。
稍后从这三个方面来演示怎么使用rsync备份工具。
1.3 rsync 常用操作
1.4、配置rsync源思路
配置rsync源服务器大致分为三步:
(1)建立rsync配置文件;
(2)为备份账户创建数据文件;
(3)启动rsync服务。
二、搭建rsync服务
准备两台虚拟机,一台作为同步源,一台用于客户机发起同步。
2.1、建立rsync配置文件
同步源服务器:
[root@server-9~]# yum -y install httpd
//如果虚拟机是最小化安装,还需要rsync包安装。
[root@localhost ~]# yum -y install rsync
[root@server-9 ~]# vi /etc/rsyncd.conf ####在同步源服务器上配置
uid = nobody
gid = nobody
use chroot = yes ####禁锢在源目录
#address = XXXXXXXX ####监听地址
port 873 ####监听端口号
log file = /var/log/rsyncd.log ####日志文件位置
pid file = /var/run/rsyncd.pid ####存放进程ID的文件位置
hosts allow = 192.168.100.0/24 ####允许访问的客户机地址
[wwwroot] ####共享模块名称
path = /var/www/html ####源目录的实际路径
comment = Document Root of www.51xit.top
read only =yes #####是否只读(手动同步为yes ,做自动同步需改为no)
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z ####同步时不在压缩的文件类型
auth users =backuper #####授权账户
secrets file = /etc/rsyncd_users.db ####存放账户信息的数据文件
基于安全性考虑,同步源最好仅允许以只读方式做同步,另外,同步也可以采用匿名的方式,只要将其中的“auth users”"secrets file"配置注释掉即可
2.2、为备份账户创建数据文件
//创建rsync账号文件
采用“用户名:密码”的记录格式,每行一个用户记录,独立的账号数据,不依赖于系统账号
[root@server-9~]# vi /etc/rsyncd_users.db
backuper:pwd123
//由于账号信息采用明文存放,因此需要调整文件权限,避免账号信息泄露
[root@server-9 ~]# chmod 600 /etc/rsyncd_users.db
2.3 重启rsync 服务,使之生效
rsync --dameon
//此处如果想要停止这个服务 ,有两个方式:
方式一:
[root@server-9 ~]# kill $(cat /var/run/rsyncd.pid) #使用这个停止服务必须删除存放rsync服务进程的文件
[root@lserver-9 ~]# rm -rf /var/run/rsyncd.pid
方式二:直接使用“netstat -anpt | grep rsync”命令查出进程号,使用“kill 进程号”。
三 . rsync 同步测试(三种方法)
3.1 首先写入测试文件
[root@server-9 ~]# cd /var/www/html/
[root@server-9 html]# ls
[root@server-9 html]# echo "this is one" > /var/www/html/index.html
[root@server-9 html]# echo "this is web" > /var/www/html/web.html
[root@server-9 html]# ls
index.html web.html
3.2 在其它节点端进行操作同步命令
3.2.1 第一种同步方法
[root@SERVER 10 ~]# rsync -avz backuper@192.168.100.9::wwwroot /opt
Password:
receiving incremental file list
./
index.html
web.html
sent 65 bytes received 199 bytes 40.62 bytes/sec
total size is 24 speedup is 0.09
3.2.2 第二种同步方法
[root@SERVER 10 ~]# rsync -avz rsync://backuper@192.168.100.9/wwwroot /opt
Password:
receiving incremental file list
sent 20 bytes received 90 bytes 12.94 bytes/sec
total size is 24 speedup is 0.22
3.2.3 第三种同步方法,免交互方式
[root@SERVER 10 opt]# rm -rf *.html
创建密码文件
[root@SERVER 10 opt]# vim server.pass
abc123
~
~增加权限
[root@SERVER 10 opt]# chmod 600 server.pass
[root@SERVER 10 opt]# rsync -az --delete --password-file=/opt/server.pass backuper@192.168.100.9::wwwroot /opt
查看同步情况,同步成功
[root@SERVER 10 opt]# ls
index.html web.html
3.2.4 、设置定期同步
[root@SERVER 10 wwwroot]# crontab -e ####每天晚上10点半对服务器网站目录更新一次
30 22 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass backuper@192.168.100.41::wwwroot /opt/myweb/
[root@SERVER 10 wwwroot]# systemctl restart crond
[root@SERVER 10wwwroot]# systemctl enable crond
四. rsync + inotify 监控工具 实现自动同步
实时同步介绍
1、定期同步存在一些不足之处,比如:
执行备份的时间固定,延迟明显、实时性差
当同步源长期不变化时,密集的定期任务是不必要的
2、实时同步的优点
一旦同步源出现变化,立即启动备份
只要同步源无变化,则不执行备份
3、inotify介绍
Linux内核从2.6.13开始,引入了inotify机制。
它是一种文件系统的变化通知机制
可以监控文件,也可以监控目录。
当监控目录时,它可以同时监控目录及目录中的各子目录及文件的。
可以协助rsync,监控到数据的改变,触发 rsync 进行数据的同步。
4.1在服务端配置
vim /etc/rsync.conf
uid = nobody
gid = nobody
port 873
log file = /var/log/rsyncd.log
# use chroot = yes
# max connections = 4
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.100.0/24
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[wwwroot]
path = /var/www/html
comment = www
read only = no (自动同步改NO ,手动同步改为yes )
auth users = backuper
secrets file = /etc/rsyncd_users.db
4.2 创建密码文件文本
vim /etc/rsync_users.db
backuper:abc123
chmod 600 /etc/rsyncd_users.db //设置权限
4.3 重启服务
kill 16360
netstat -ntap | grep rsync
rsync --daemon
netstat -ntap | grep rsync
安装httpd ,生成 、var/www/html 文件夹,并赋予权限
yum install httpd -y
cd /var/www
chmod 777 html
ll
总用量 0
drwxr-xr-x 2 root root 6 4月 2 21:14 cgi-bin
drwxrwxrwx 2 root root 6 4月 2 21:14 html
4.4 节点端设置 inotify-tools
4.4.1 安装监控软件
tar -zxvf inotify-tools-3.14.tar.gz
yum install make gcc gcc-c++ httpd -y
cd 进入目录
./configure
make && make install
4.4.2 设置内核文件
当要监控的目录、文件数量较多或者变化较频繁时,建议加大这三个参数的值。
可以直接修改/etc/sysctl.conf的配置文件,将管理队列、实例数、监控数进行设置。
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p // 立即生效
4.4.3 创建密码文本
vim /etc/server.pass
写入 abc123
[root@SERVER 10 /]# chmod 600 /etc/server.pass
4.4.4 给目录增加权限
[root@SERVER 10 /]# cd /var/www
[root@SERVER 10 www]# chmod 777 html
4.4.5 监控测试
[root@SERVER 10 ~]# cd /var/www/html/
[root@SERVER 10 html]# touch abc
[root@SERVER 10 html]# rm -rf abc
[root@SERVER 10 www]# inotifywait -mrq -e create,delete,move,modify,attrib,move,delete /var/www/html/
/var/www/html/ CREATE abc
/var/www/html/ ATTRIB abc
/var/www/html/ DELETE abc
4.4.6 创建脚本文件,实现自动化监控同步
vim /opt/intoify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.100.9::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
# echo "${FILE} was rsynced" >>/opt/inotify_rsync.log
fi
done
增加执行权限,并执行
chmod +x intoify.sh
./inotify.sh
4.4.7 在 目录端创建文件,并到另一服务端进行查看测试
[root@SERVER 10 html]# echo "this is test" > test.html
[root@SERVER 10 html]# echo "this is test" > test2.html
[root@SERVER 10 html]# echo "this is test3" > test3.html
[root@SERVER 10 html]#
[root@SERVER 10 opt]# ./inotify.sh
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/.test.html.CMUx1o" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.test2.html.QsPEBC" (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]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/.test.html.WlvSvG" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.test2.html.uKf5IU" (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]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/.test.html.T8HIwo" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.test2.html.QPx7rz" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.test3.html.ZmVwnK" (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]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/test3.html" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.test.html.VG7wIt" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/.test2.html.xuuoiF" (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]
4.4.8 查看服务端 ,发现已经同步成功
[root@server-9 html]# ls
test2.html test3.html test.html
[root@server-9 html]# cat test3.html
this is test3