目录
rsync介绍
在 Linux 系统下,数据备份的工具很多,除了tar、cpio、dd 命令之外,还有一个用来备份数据的命令,就是 rsync。
从字面意思上,rsync 可以理解为 remote sync(远程同步),但它不仅可以远程同步数据(类似于 scp 命令),还可以本地同步数据(类似于 cp 命令)。不同于 cp 或 scp 的一点是,使用 rsync 命令备份数据时,不会直接覆盖以前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。
rsync是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。在同步过程中,同步源负责提供文件的原始位置,发起端应对该位置具有读取权限
rsync的一些选项
rsync一般安装图像化界面都有安装包,如果没有yum install -y rsync命令安装即可
-r | 递归模式,包含目录及子目录中的所有文件。 |
-l | 对于符号链接文件仍然复制为符号链接文件。 |
-V | 显示同步过程的详细(verbose)信息。 |
-z | 在传输文件时进行压缩(compress) |
-a | 归档模式,递归并保留对象属性,等同于 -rlptgoD |
-v | 表示打印一些信息,比如文件列表、文件数量等。 |
-p | 保留文件的权限标记。 |
-t | 保留文件的时间标记 |
-g | 保留文件的属组标记(仅超级用户使用)。 |
-o | 保留文件的属主标记(仅超级用户使用) |
-H | 保留硬连接文件 |
-A | 保留ACL属性信息 |
-D | 保留设备文件及其他特殊文件。 |
--delete | 删除目标位置有而原始位置没有的文件 |
--checksum | 根据对象的校验和来决定是否跳过文件 |
差不多只需记住最常用的几个即可,比如 -a、-v、-z、--delete
rsync-a
比如创建两个文件
[root@localhost ~]# cd rsync/
[root@localhost ~/rsync]# mkdir test1
[root@localhost ~/rsync]# mkdir test2
[root@localhost ~/rsync/test1]# touch 1 2 3
开始备份
[root@localhost ~/rsync]# rsync -a test1 test2/
[root@localhost ~/rsync]# ls test2
test1
[root@localhost ~/rsync]# ls test1
1 2 3
[root@localhost ~/rsync]# cd test2/
[root@localhost ~/rsync/test2]# ls
test1
[root@localhost ~/rsync/test2]# cd test1/
[root@localhost ~/rsync/test2/test1]# ls
1 2 3
如果想要实现将 test1 目录中的内容直接备份到 test2 目录中,需修改上面的命令为:
[root@localhost ~/rsync]# rsync -a test1/ test2
[root@localhost ~/rsync]# ls
test1 test2
[root@localhost ~/rsync]# ls test2/
1 2 3
rsync远程同步实现
master:192.168.37.106
client:192.168.37.100
--------------------------------------------------master------------------------------------------------------------------
初始化环境
systemctl stop firewalld
setenforce 0
修改配置文件
[root@localhost ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
pid file = /var/run/rsyncd.pid
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
address = 192.168.37.106
port = 873
log file = /var/log/rsycd.log
hosts allow = 192.168.37.0/24
[ftp]
path = /var/www/html
comment = web service
read only = yes
auth users = backuper
secrets file = /root/rsyncd_users.db
如果采用匿名的方式,只要将其中的“auth users” 和“secrets file” 配置项去掉即可
为备份账户创建数据文件
[root@localhost ~]# vim /root/rsyncd_users.db
backuper:123123
###建立同步用户
设置权限,一定要600权限,不然会报错
[root@localhost ~]# chmod 600 /root/rsyncd_users.db
给站点文件添加可读权限
[root@localhost /var/www]# chmod +r /var/www/html/
[root@localhost /var/www]# ls -ld html/
drwxr-xr-x. 2 root root 6 Dec 14 01:00 html/
开启服务
[root@localhost /var/www]# rsync --daemon
[root@localhost /var/www]# ss -natp |grep rsync
LISTEN 0 5 192.168.37.106:873 *:* users:(("rsync",pid=4554,fd=4))
在站点文件下创建2个文件,测试用
[root@localhost /var/www/html]# ls
1.txt 2.txt
-----------------------------------------------------客户机----------------------------------------------------------
在/opt目录下创建xb目录,并给全部权限
[root@localhost /opt]# mkdir xb
[root@localhost /opt]# chmod 777 xb
编辑密码文件
[root@localhost /opt]# vim /etc/server.pwd
123123
给密码文件设置权限
[root@localhost /opt]# chmod 600 /etc/server.pwd
在客户机上拉取源rsync文件
[root@localhost /opt/xb]# rsync -avz backuper@192.168.37.106::ftp /opt/xb
Password:
receiving incremental file list
./
1.txt
2.txt
sent 98 bytes received 193 bytes 30.63 bytes/sec
total size is 0 speedup is 0.00
[root@localhost /opt/xb]# ls
1.txt 2.txt
如果删除下面文件,再恢复
[root@localhost /opt/xb]# rm -rf *
[root@localhost /opt/xb]# rsync -az --password-file=/etc/server.pwd backuper@192.168.37.106::ftp /opt/xb
[root@localhost /opt/xb]# ls
1.txt 2.txt
rsync实时同步(inotify)
使用inotify通知接口,可以用来监控文件系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。
将inotify机制与rsync相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。
因为inotify通知机制由 Linux内核提供,因此主要做本机监控,在触发式备份中,应用时更适合上行同步。
实际操作
---------------------------------------------------------------master---------------------------------------------:
[root@localhost /var/www/html]# vim /etc/rsyncd.conf
调整inotify内核参数
max_queue_events:监控队列大小 默认16384
max_user_instances:最多监控实例数 默认 128
max_user_watches:每个实例最多监控文件数 默认8192
[root@localhost /var/www/html]# vim /etc/sysctl.conf
[root@localhost /var/www/html]# sysctl -p
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
------------------------------------------------------------client-------------------------------------------------------
安装inotify
将软件包拉进来,
安装inotify-tools辅助工具
inotifywait:用于持续监控,实时输出结果(增删改属性修改)
inotifywatch:用于短期监控,任务完成后再出汇总结果
解压并编译安装
[root@localhost opt]# tar xf inotify-tools-3.14.tar.gz
[root@localhost opt]# cd inotify-tools-3.14/
[root@localhost /opt/inotify-tools-3.14]# ./configure
[root@localhost /opt/inotify-tools-3.14]# make && make install
编写触发式同步脚本
[root@localhost /opt/inotify-tools-3.14]# vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /var/www/html"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pwd /var/www/html/ backuper@192.168.37.106::ftp"
$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
执行脚本
[root@localhost /var/www/html]# chmod +x /opt/inotify.sh
[root@localhost /var/www/html]# /opt/inotify.sh &
[1] 107802
另外开启两个客户端
一个用户创建文件,一个用来监控信息
[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html
-e 用来指定要监控哪些事情
-m 表示持续监控
-r 表示递归整个目录
-q 简化输出信息
测试
创建文件、删除文件
去看监控
再去master查看,截取一部分
rsync快速删除大量文件
在主节点上 创建文件
[root@localhost data]#touch file{1..10}.txt
[root@localhost /data]# ls
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
找个空文件
[root@localhost /data]# mkdir bin
删除文件
[root@localhost /data]# rsync --delete-before -aHv --progress -stats /data/bin/ /data/
building file list ...
1 file to consider
deleting bin/
deleting 9.txt
deleting 8.txt
deleting 7.txt
deleting 6.txt
deleting 5.txt
deleting 4.txt
deleting 3.txt
deleting 2.txt
deleting 10.txt
deleting 1.txt
./
sent 29 bytes received 15 bytes 88.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost /data]# ls
[root@localhost /data]#
文件就全部删除了,其实就是同步到空文件状态
总结
rsync 是一个常用的 Linux 应用程序,用于文件同步。 它可以在本地计算机与远程计算机之间,或者两个本地目录之间同步文件(但不支持两台远程计算机之间的同步)。它也可以当作文件复制工具,替代cp和mv命令。 它名称里面的r指的是 remote,rsync 其实就是"远程同步"(remote sync)的意思。与其他文件传输工具(如 FTP 或 scp)不同,rsync 的最大特点是会检查发送方和接收方已有的文件,仅传输有变动的部分(默认规则是文件大小或修改时间有变动)