目录
一、rsync (Remote Sync,远程同步)
- 是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
- 在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。在同步过程中,同步源负责提供文件的原始位置,发起端应对该位置具有读取权限。
二、部署rsync源服务器
rpm -q rsync #一般系统已默认安装rsync
#建立/etc/rsyncd.conf 配置文件
vim /etc/rsyncd.conf #添加以下配置项
uid = nobody #也可以为root
gid = nobody #也可以为root
use chroot = yes #禁锢在源目录
address = 192.168.200.40 #监听地址,监听本机地址
port 873 #监听端口 tcp/udp 873,可通过cat /etc/services | grep rsync查看
log file = /var/log/rsyncd.log #日志文件位置
pid file = /var/run/rsyncd.pid #存放进程 ID 的文件位置
hosts allow = 192.168.200.0/24 #允许同步的客户机网段
[wwwroot] #共享模块名称
path = /var/www/html #源目录的实际路径(同步的目录)
comment = Document Root of www.gcc.com
read only = yes #是否为只读
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z #同步时不再压缩的文件类型
auth users = backuper #授权账户,多个账号以空格分隔
secrets file = /etc/rsyncd_users.db #存放账户信息的数据文件
-------------------------------------------------------------------------------
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.200.40
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.200.0/24
[wwwroot]
path = /var/www/html
comment = Document Root of www.gcc.com
read only = yes
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = backuper
secrets file = /etc/rsyncd_users.db
----------------------------------------------------------------------------------
#如采用匿名的方式,只要将其中的“auth users”和“secrets file”配置项去掉即可。
#为备份账户创建数据文件
vim /etc/rsyncd_users.db
backuper:abc123 #无须建立同名系统用户
chmod 600 /etc/rsyncd_users.db
#补充:SSH -i 密钥文件位置 root@192.168.200.1 #授权远程登录
#密钥文件的权限需要是600
#保证所有用户对源目录/var/www/html 都有读取权限
#安装http服务
yum -y install httpd
systemctl start httpd
systemctl enable httpd
mkdir -p /var/www/html
echo “this is gcc” > /var/www/html/gcc.txt
chmod +r /var/www/html/
ls -ld /var/www/html/ #以长格式显示文件目录权限
drwxr-xr-x. 2 root root 6 2月 28 09:01 /var/www/html/
#启动 rsync 服务程序
rsync --daemon #启动 rsync 服务,以独立监听服务的方式(守护进程)运行
netstat -anpt | grep rsync
#关闭 rsync 服务的方法
kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid
三、发起端
基本格式:
rsync [选项] 原始位置 目标位置
常用选项:
-r; 递归模式,包含目录及子目录中的所有文件。
-l: 对于符号链接文件仍然复制为符号链接文件。.
-V: 显示同步过程的详细(verbose) 信息。
-z: 在传输文件时进行压缩( compress) 。
-a: 归档模式,保留文件的权限、属性等信息,等同于组合选项"-rlptgoD"。
-p: 保留文件的权限标记。
-t: 保留文件的时间标记。
-g: 保留文件的属组标记(仅超级用户使用)。
-o: 保留文件的属主标记(仅超级用户使用)。
-H: 保留硬连接文件。
-A: 保留ACL属性信息。
-D: 保留设备文件及其他特殊文件。
--delete: 删除目标位置有而原始位置没有的文件。
--checksum:根据校验和(而不是文件大小、修改时间)来决定是否跳过文件。
#将指定的资源下载到本地/opt 目录下进行备份。
格式一:
rsync -avz backuper@192.168.200.40::wwwroot /opt/ #密码abc123
格式二:
rsync -avz rsync://backuper@192.168.200.40/wwwroot /opt/
#查看同步获取的文件
cat /opt/gcc.txt
this is gcc
#免交互格式配置:
cd /opt
rm -rf gcc.txt
echo "abc123" > /etc/server.pass
chmod 600 /etc/server.pass
crontab -e
30 22 * * * /usr/bin/rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.200.40::wwwroot /opt/
systemctl restart crond
systemctl enable crond
Slave(192.168.184.20)
四、发起端配置 rsync+inotify
- 使用inotify通知接口,可以用来监控文件系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应
- 将 inotify 机制与 rsync 工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态
- 因为 inotify 通知机制由 Linux 内核提供,因此主要做本机监控,在触发式备份中应用时更适合上行同步
1、Master(192.168.184.10)
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(192.168.184.20)
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/haha/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/haha/ kiki@192.168.184.10::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.184.10)验证
但是,之前上图slave同步时会出现报错,原因是我们以匿名用户身份登陆的