rsync远程同步
一、rsync同步简介
1-1 、关于rsync
■ 一款增量备份工具
- Remote Sync,远程同步
- 支持本地复制,或者与其他SSH、rsync主机同步
- 官方网站: http://rsync.samba.org
二、 配置rsync备份源
2-1、 配置rsync源
■ rsync同步源
■ 基本思路
- 建立rsyncd.conf配置文件、独立的帐号文件
- 启用rsync的 --daemon模式
■ 应用示例
- 用户backuper,允许下行同步
- 操作目录为 /var/www/html(安装HTTP)
■ 配置文件rsyncd.conf
- 需手动建立,语法类似于Samba配置
- 认证配置auth users、secrets file,不加则为匿名
■ rsync帐号文件
- 采用 “用户名:密码” 的记录格式,每行一个用户记录
- 独立的账号数据,不依赖于系统账号
■ 启用rsync服务
- 通过 --daemin独自提供服务
- 执行kill $(cat /var/run/rsyncd.pid)关闭rsync服务
三、rsync命令基本用法
3-1、 使用rsync备份工具
■ rsync命令的用法
rsync [选项] 原始位置 目标位置
■ 常用选项
- -a:归档模式,递归并保留对象属性
- -v:显示同步过程的详细信息
- -z:在传输文件时进行压缩
- -H保留硬链接文件
- -A:保留ACL属性信息
- –delete:删除目标位置而原始位置没有的文件
- –checksum:根据对象的校验和来决定是否跳过文件
■ 配置源的两种表示方法
格式1:用户名@主机地址::共享模块名
格式2:rsync://用户名@主机地址/共享模块名
格式一:
[root@localhost etc]# rsync backuper@20.0.0.11::wwwroot /opt/
格式二:
[root@localhost etc]# rsync -avz rsync://backuper@20.0.0.11::wwwroot /opt/
四、 rsync+inotify结合使用
4-1、 rsync远程同步
要先关闭防火墙和核心防护
[root@localhost ~]# systemctl stop firewalld ###关闭防火墙
[root@localhost ~]# vim /etc/selinux/config ###关闭核心防护
SELINUX=disabled
[root@localhost ~]# iptables -F ###清空防火墙规则
[root@localhost ~]# setenforce 0 ###临时关闭核心防护
配置rsync源服务器
[root@localhost ]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
address = 20.0.0.11
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 = www.jb.cn
read only = no
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper
secrets file = /etc/reyncd_users.db
上面配置文件解释
uid = nobody
gid = nobody
use chroot = yes ###禁锢家目录
address = 20.0.0.11 ###监听地址
port 873 ###端口号
log file = /var/log/rsyncd.log ###指定日志文件
pid file = /var/run/rsyncd.pid ###开启pid
hosts allow = 20.0.0.0/24 ###运行哪些网段可以访问(白名单)
[wwwroot] ###共享模块名(自定义的)
path = /var/www/html ###共享站点目录
comment = www.jb.cn ###描述性信息,可以随便写
read only = no ###只读模式
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 ###里面这些格式不进行压缩(因为格式本来就是压缩格式)
auth users = backuper ###创建一个身份验证的账户
secrets file = /etc/reyncd_users.db ###配置了身份验证账户,那要有密码,去这里配置密码
[root@localhost ~]#vim /etc/reyncd_users.db ###添加密码
backuper:abc123
[root@localhost ~]# chmod 600 /etc/reyncd_users.db ###给读的权限
[root@localhost ~]# yum -y install httpd ###安装一下apache,因为需要/var/www/html文件
[root@localhost ~]# reync --daemon ###启动服务
[root@localhost ~]# rsync -antp |grep 873 ###过滤一下端口号看看起来了没
发起端配置
```bash
发起端:
格式一:
[root@localhost etc]# rsync backuper@20.0.0.11::wwwroot /opt/ ###进行同步
格式二:
[root@localhost etc]# rsync -avz rsync://backuper@20.0.0.11::wwwroot /opt/ ###进行同步
免交互:
[root@localhost ]#cd /etc/
[root@localhost etc]# touch server.pass ###创建存密码文件
[root@localhost etc]# vim server.pass ###往里面写密码
abc123
[root@localhost etc]# chmod 600 /etc/server.pass ###给读的权限
[root@localhost etc]# rsync -avz --delete --password-file=/etc/server.pass backuper@20.0.0.11::wwwroot /opt/ ###免交互式同步
- 发起端可以做计划任务定期同步
[root@localhost ]# crontab -e ###可以用计划任务定期同步
4-2、 rsync+inotify
发起端配置
[root@localhost ~]# vim /etc/sysctl.conf ###调整inotify内核参数,在最后添加这三段
fs.inotify.max_queued_events = 16384 ###监控事件队列大小
fs.inotify.max_user_instances = 1024 ###最多监控实例数
fs.inotify.max_user_watches = 1048576 ###每个实例最多监控文件数,监控数应大于监控目标的总文件数
[root@localhost ~]# sysctl -p ###刷新生效一下
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
将inotify-tools-3.14.tar工具上传上来
[root@localhost abc]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/ ###将工具解压到/opt目录下
[root@localhost inotify-tools-3.14]# yum -y install gcc gcc-c++ make ###安装一下gcc编译器
[root@localhost ~]# cd /opt/inotify-tools-3.14/ ###到这个目录下
[root@localhost inotify-tools-3.14]# ./configure ###执行
[root@localhost inotify-tools-3.14]# make && make install ###编译安装
[root@localhost ~]# yum -y install httpd ###安装一下apache
[root@localhost ]# chmod 777 /var/www/html/ ###目录给个777权限
inotifywait -mrq -e modify,create,move,delete /var/www/html ### -mrq:持续性监控,-e:操作,modify:修改,create:创建,move:移动,delete:删除 ,监控本地的/var/www/html
敲了上面的监控命令就会持续监控不能操作,需要在开一个发起端操作字符页面
进入到另开的发起端操作字符界面
[root@localhost ~]# cd /var/www/html/ ###进入到html目录
[root@localhost html]# touch abc ###创建一个abc
然后刚刚敲监控那边就会出现操作提示
/var/www/html/ CREATE abc
没问题的话,我们就回刚刚监控的发起端,ctrl+c或ctrl+z结束掉监控
[root@localhost ~]# cd /opt/
[root@localhost opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html backuper@20.0.0.11::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
[root@localhost opt]# chmod +x inotify.sh ###给个执行权限
[root@localhost ]# ls -l /var/www/ ###用这条命令查看两边都查看一下html权限给足了没
在源端在调一个数据
前面配置的时候已经是no了,就不用操作这几步了,直接跳到下面的发起端启动脚本就好
[root@localhost ~]# vim /etc/rsyncd.conf
read only = no ###需要把wwwroot模块的只读模式改成no
[root@localhost ~]# netstat -ntap | grep rsync ###虽然改了配置,但是还没重启,查看一下进程
tcp 0 0 20.0.0.11:873 0.0.0.0:* LISTEN 20491/rsync
[root@localhost ~]# kill -9 20491 ###杀死进程
[root@localhost ~]# netstat -ntap | grep rsync ###在查看一下应该就没有了
[root@localhost ~]# cd /var/run/ ###去这个目录下
[root@localhost run]# rm -rf rsyncd.pid ###删掉这个pid
[root@localhost run]# rsync --daemon ###启动一下就行
[root@localhost run]# netstat -ntap | grep rsync ###在看一下就又有了
tcp 0 0 20.0.0.11:873 0.0.0.0:* LISTEN 70903/rsync
发起端启动脚本
[root@localhost opt]# ./inotify.sh
需要在开一个发起端操作字符页面
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is test" > test.txt
监控那边的发起端就会出现提示
[root@localhost opt]# ./inotify.sh ###这个报错不影响同步
rsync: chgrp "/html" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/html/.test.txt.at3wRj" (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]
源端查看一下是否同步过去了
[root@localhost run]# cd /var/www/html/
[root@localhost html]# cd html
[root@localhost html]# ls
test.txt
rsync的同步操作和rsync+inotify结合使用操作完成
中途遇到的报错
解决:防火墙没关闭