rdync快速增量备份工具
- Rsync简介
- 同步方式
- 同步过程
- Rsync常用命令
- 配置源的三种表达方式
- 配置服务端与客户端的实验(下载文件实验)
- 下载服务端备份源中的数据,使用配置源的的第二种方式进行备份(rsync -avz zhangsan@20.0.0.200::myhtml /opt/ )
- 增量数据备份
- 配置免交互密码
- 服务端与客户端同步数据
- 每天实时数据备份
- 使用配置源的的第二种方式进行备份(rsync -avz rsync://zhangsan@20.0.0.200/myhtml /opt/student/
- 使用配置源的的第三种方式进行备份(rsync -avz -e 'ssh -p 22' root@20.0.0.200:/var/www/html /opt/student/)
- 使用inotify工具进行实时同步(上传数据)
- 使用命令进行监控(inotifywait -mrq -e modify,create,move,delete )
- 使用Shell脚本进行实时监控
- 批量删除数据(rsync --delete-before -a -H -v --progress --stats /home/blank /data/)
- 总结
- 配置源的三种表达方式
Rsync简介
rsync ( Remote sync,远程同步)
是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。在同步过程中,同步源负责提供文件的原始位置,发起端应对该位置具有读取权限。
Rsync是Linux系统下的数据镜像备份工具,使用快速增量备份工具Remote sync 可以远程同步,可以在不同主机之间进行同步,可实现全量备份与增量备份,保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适合用于架构集中式备份或异地备份等应用。同时Rsync支持本地复制,或者与其他SSH、 rsync主机同步。
Rsync官方网站:https://rsync.samba.org/
同步方式
(1)完整备份:每次备份都是从备份源将所有的文件或目录备份到目的地。
(2)差量备份:备份上次完全备份以后有变化的数据(他针对的上次的完全备份,他备份过程中不清除存档属性)。
(3)增量备份:备份上次备份以后有变化的数据(他才不管是那种类型的备份,有变化的数据就备份,他会清除存档属性)
同步过程
1、发起端:负责发起rsync
同步操作的客户机叫做发起端,通知服务器我要备份你的数据。
2、备份原:负责响应来自客户机rsync
同步操作的服务器叫做备份原,需要备份的服务器
3、服务端:运行rsyncd服务,一般来说 需要备份的服务器
4、客户端:存放备份数据
Rsync常用命令
基本格式
rsync 【选项】 原始位置 目标位置
常用选项
选项 | 含义 |
-r | 递归模式,包含目录及子目录中的所有文件 |
-l | 对于符号链接文件仍然复制为符号链接文件 |
-v | 显示同步过程的详细(verbose)信息 |
-z | 在传输文件时进行压缩( compress ) |
-a | 归档模式,保留文件的权限、属性等信息,等同于组合选项"-r lptgoD" |
-p | 保留文件的权限标记 |
-t | 保留文件的时间标记 |
-g | 保留文件的属组标记(仅超级用户使用) |
-o | 保留文件的属主标记(仅超级用户使用) |
-H | 保留硬连接文件 |
-A | 保留ACL属性信息 |
-D | 保留设备文件及其他特殊文件 |
-delete | 删除目标位置有而原始位置没有的文件 |
-checksum | 根据校验和(而不是文件天小、修改时间)来决定是否跳过文件 |
配置源的三种表达方式
格式一:
用户名@主机名::共享模块名
rsync -avz backuper@20.0.0.200::wwwroot /opt/
格式二:
rsync://用户名@主机地址:/共享模块名
rsync -avz rsync://backuper@20.0.0.200/wwwroot /opt/
格式三:
rsync -avz -e 'ssh -p ssh端口号' 原始位置 目标位置
rsync -avz -e 'ssh -p 22' root@20.0.0.200:/var/www/html /opt/student/
配置服务端与客户端的实验(下载文件实验)
20.0.0.100为客户端,发起端
20.0.0.200为服务端,同步源
==================================================================================================
客户端:20.0.0.100
[root@100 ~]# systemctl stop firewalld.service && setenforce 0
[root@100 ~]# cd /opt/
[root@100 opt]# ls
rh
[root@100 opt]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[root@100 opt]# cd /home/
[root@100 home]# ls
lisi
[root@100 home]# ls
lisi student
[[root@100 home]# echo "this is school" > student/1.txt
[root@100 home]# cat student/1.txt
this is school
[root@100 home]# rsync -avz student/ /opt/
## student后面加了/表示是传输 stdent目录中的文件, student后面不加/表示是只传输传输 stdent目录
sending incremental file list
./
1.txt
sent 99 bytes received 34 bytes 266.00 bytes/sec
total size is 17 speedup is 0.13
[root@100 home]# cd /opt
[root@100 opt]# ls
1.txt rh
[root@100 opt]# cd /home/
[root@100 home]# cd /opt/
[root@100 opt]# ls
1.txt rh
[root@100 opt]# mkdir student
[root@100 opt]# ls
1.txt rh student
服务端:20.0.0.200
[root@200 ~]# systemctl stop firewalld.service && setenforce 0
[root@200 ~]# cd /opt/
[root@200 opt]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[root@200 opt]# rpm -ac rsync
RPM 版本 4.11.3
版权所有 (C) 1998-2002 - 红帽公司。
该程序可以在 GNU GPL 条款下自由分发
用法: rpm [-aKfgpqVcdLilsiv?] [-a|--all] [-f|--file] [-g|--group]
[-p|--package] [--pkgid] [--hdrid] [--triggeredby] [--whatrequires]
[--whatprovides] [--nomanifest] [-c|--configfiles] [-d|--docfiles]
[-L|--licensefiles] [--dump] [-l|--list] [--queryformat=QUERYFORMAT]
[-s|--state] [--nofiledigest] [--nofiles] [--nodeps] [--noscript]
[--allfiles] [--allmatches] [--badreloc] [-e|--erase <package>+]
[--excludedocs] [--excludepath=<path>] [--force]
[-F|--freshen <packagefile>+] [-h|--hash] [--ignorearch] [--ignoreos]
[--ignoresize] [-i|--install] [--justdb] [--nodeps] [--nofiledigest]
[--nocontexts] [--noorder] [--noscripts] [--notriggers]
[--nocollections] [--oldpackage] [--percent] [--prefix=<dir>]
[--relocate=<old>=<new>] [--replacefiles] [--replacepkgs] [--test]
[-U|--upgrade <packagefile>+] [-D|--define “MACRO EXPR”]
[--undefine=MACRO] [-E|--eval “EXPR”] [--macros=<FILE:…>]
[--noplugins] [--nodigest] [--nosignature] [--rcfile=<FILE:…>]
[-r|--root ROOT] [--dbpath=DIRECTORY] [--querytags] [--showrc]
[--quiet] [-v|--verbose] [--version] [-?|--help] [--usage]
[--scripts] [--setperms] [--setugids] [--conflicts] [--obsoletes]
[--provides] [--requires] [--info] [--changelog] [--xml]
[--triggers] [--last] [--dupes] [--filesbypkg] [--fileclass]
[--filecolor] [--fscontext] [--fileprovide] [--filerequire]
[--filecaps]
[root@localhost opt]# rpm -qc rsync
/etc/rsyncd.conf
/etc/sysconfig/rsyncd
[root@localhost opt]# cd
[root@localhost ~]# cp /etc/rsyncd.conf /etc/rsyncd.conf_bak
### 备份好方便回滚
[root@200 ~]# vim /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
uid = root
gid = root
use chroot = yes
address = 20.0.0.200
# max connections = 4
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
host allow = 20.0.0.0/24
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[myhtml]
path = /var/www/html
comment = this is rsync of www.school.com
read only = yes
auth users = zhangsan
secrets file = /etc/rsyncd_users.db
# [ftp]
# path = /home/ftp
# comment = ftp export area
[root@200 ~]# mkdir -p /var/www/html
[root@200 ~]# ls -ld /var/www/html
drwxr-xr-x. 2 root root 6 4月 3 16:13 /var/www/html
[root@200 ~]# chmod +r /var/www/html/
[root@200 ~]# vim /etc/rsyncd_users.db
zhangsan:123456
[root@200 ~]# chmod 600 /etc/rsyncd_users.db
##给个权限,只有自己的用户访问,别的用户访问不了
[root@200 ~]# rsync --daemon
[root@200 ~]# netstat -antp|grep rsync
tcp 0 0 20.0.0.200:873 0.0.0.0:* LISTEN 38857/rsync
[root@200 ~]# cd /var/www/html
[root@200 html]# cp /etc/passwd /etc/shadow ./
## 拷贝文件到当前的目录中
[root@200 html]# ls
passwd shadow
下载服务端备份源中的数据,使用配置源的的第二种方式进行备份(rsync -avz zhangsan@20.0.0.200::myhtml /opt/ )
客户端:20.0.0.100
[root@100 opt]# rsync -avz zhangsan@20.0.0.200::myhtml /opt/
## 下载,服务端备份源中的数据,密码为123456上面服务端中的创建的密码
使用配置源的的第一种方式进行登录
Password:
receiving incremental file list
./
passwd
shadow
sent 101 bytes received 1488 bytes 353.11 bytes/sec
total size is 3296 speedup is 2.07
[root@100 opt]# ls student/
1.txt passwd shadow
增量数据备份
服务端:20.0.0.200
[root@200 html]# vim passwd
## 进入到配置文件中,使用命令 yy加G 跳到行尾 ,使用yy加p 添加 tcpdump:x:72:72::/:/sbin/nologin 此行,并且把tcpdump修改为zhangsan
zhangsan:x:72:72::/:/sbin/nologin
客户端:20.0.0.100
[root@100 opt]# rsync -avz zhangsan@20.0.0.200::myhtml /opt/student/
## 此时服务端的数据已经变动了,更新了里面修改过后的文件,密码为123456上面服务端中的创建的密码
Password:
receiving incremental file list
./
passwd
sent 106 bytes received 466 bytes 163.43 bytes/sec
total size is 3330 speedup is 5.82
[root@100 opt]# vim /opt/student/passwd
配置免交互密码
客户端:20.0.0.100
[root@100 opt]# echo "123456" > /etc/server.pass
[root@100 opt]# chmod 600 /etc/server.pass
## 只给当前的用户使用,不给其他用户使用
服务端:20.0.0.200
[root@200 html]# vim /etc/yum.repos.d/local.repo
## 由于本地没有此文件需要先自行创建一个文件数据,里面的数据可以随便输入
[root@200 html]# cp /etc/yum.repos.d/local.repo ./
## 把创建好的数据拷贝进html目录中
[root@200 html]# ls
local.repo passwd shadow
## 备份源中已有新增的数据
客户端:20.0.0.100
[root@100 opt]# rsync -avz --password-file=/etc/server.pass zhangsan@20.0.0.200::myhtml /opt/student/
## 此时免交互密码设置成功,可直接下载文件
receiving incremental file list
./
local.repo
sent 82 bytes received 208 bytes 580.00 bytes/sec
total size is 3463 speedup is 11.94
[root@100 opt]# ls student/
## 此时文件中已经有新文件存入当中
1.txt local.repo passwd shadow
服务端与客户端同步数据
服务端:20.0.0.200
[root@200 html]# ls
local.repo passwd shadow
[root@200 html]# rm -rf shadow
[root@200 html]# ls
local.repo passwd
客户端:20.0.0.100
[root@100 opt]# ls student/
1.txt local.repo passwd shadow
[root@100 opt]# rsync -avz --delete --password-file=/etc/server.pass zhangsan@20.0.0.200::myhtml /opt/student/
## 当中加入--delete
receiving incremental file list
deleting shadow
deleting 1.txt
./
sent 63 bytes received 135 bytes 396.00 bytes/sec
total size is 2268 speedup is 11.45
[root@100 opt]# ls student/
## 此时服务端与客户端数据已经同步
local.repo passwd
每天实时数据备份
客户端:20.0.0.100
[root@100 opt]# crontab -e
30 22 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass zhangsan@20.0.0.200::myhtml /opt/student/
### 每天十点半进行数据备份
[root@100 opt]# crontab -l
## -l 查看
30 22 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass zhangsan@20.0.0.200::myhtml /opt/student/
[root@100 opt]# systemctl restart crond
## 重启crond ,crond是定时任务,配置完后需要重启
[root@100 opt]# systemctl enable crond
## 开机自启crond
[root@100 opt]# systemctl status crond
## 查看crond状态是否开启
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 三 2023-04-05 18:56:46 CST; 17s ago
Main PID: 40477 (crond)
CGroup: /system.slice/crond.service
└─40477 /usr/sbin/crond -n
4月 05 18:56:46 100 systemd[1]: Started Command Scheduler.
4月 05 18:56:46 100 systemd[1]: Starting Command Scheduler...
4月 05 18:56:46 100 crond[40477]: (CRON) INFO (RANDOM_DELAY will b...)
4月 05 18:56:46 100 crond[40477]: (CRON) INFO (running with inotif...)
4月 05 18:56:46 100 crond[40477]: (CRON) INFO (@reboot jobs will b...)
Hint: Some lines were ellipsized, use -l to show in full.
使用配置源的的第二种方式进行备份(rsync -avz rsync://zhangsan@20.0.0.200/myhtml /opt/student/
)
客户端:20.0.0.100
[root@100 opt]# rsync -avz rsync://zhangsan@20.0.0.200/myhtml /opt/student/
Password:
receiving incremental file list
sent 60 bytes received 132 bytes 42.67 bytes/sec
total size is 2268 speedup is 11.81
## 下载,服务端备份源中的数据,密码为123456上面服务端中的创建的密码
使用配置源的的第一种方式进行登录
使用配置源的的第三种方式进行备份(rsync -avz -e ‘ssh -p 22’ root@20.0.0.200:/var/www/html /opt/student/)
服务端:20.0.0.200
[root@200 html]# ls
local.repo passwd
[root@200 html]# echo "this is school" > 2.txt
## 新增一份数据文件
[root@200 html]# ls
2.txt local.repo passwd
客户端:20.0.0.100
[root@100 opt]# rsync -avz -e 'ssh -p 22' root@20.0.0.200:/var/www/html /opt/student/
## 此密码需要输入登录虚拟机的密码,不是创建的123456用户名的密码
root@20.0.0.200's password:
receiving incremental file list
html/
html/2.txt
html/local.repo
html/passwd
sent 72 bytes received 1072 bytes 254.22 bytes/sec
total size is 2283 speedup is 2.00
[root@100 opt]# ls student/
## 上面/var/www/html 后面没加/ 是把整个html目录都给拷贝进来的,加了/代表拷贝目录中的文件
html local.repo passwd
[root@100 opt]# ls student/html/
2.txt local.repo passwd
使用inotify工具进行实时同步(上传数据)
发起端(客户端)配置rsync+inotify
使用inotify通知接口,可以用来监控文什系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。
将inotify机制与rsync工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。
因为 inotify 通知机制出 Linux 内核提供,因此主要做木机监控,在触发式备份中应用时更适合上行同步。
服务端:20.0.0.200
[root@200 html]# vim /etc/rsyncd.conf
## yes改为no,表示可读写
read only = no
[root@200 html]# cat /var/run/rsyncd.pid
38857
[root@200 html]# kill $(cat /var/run/rsyncd.pid)
## 杀掉rsync进程
[root@200 html]# netstat -antp|grep rsync
[root@200 html]# rsync --daemon
## 重启
[root@200 html]# netstat -antp|grep rsync
tcp 0 0 20.0.0.200:873 0.0.0.0:* LISTEN 41286/rsync
[root@200 html]# chmod 777 /var/www/html/
[root@200 html]# ls
2.txt local.repo passwd
客户端:20.0.0.100
[root@100 opt]# cat /proc/sys/fs/inotify/max_queued_events
16384
## inotify 的默认内核参数
[root@100 opt]# cat /proc/sys/fs/inotify/max_user_instances
128
## 每个用户所能创建的Inotify实例的上限
[root@100 opt]# cat /proc/sys/fs/inotify/max_user_watches
8192
## 每个inotify实例相关联的watches的上限,即每个inotify实例可监控的最大目录、文件数量
[root@100 opt]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@100 opt]# sysctl -p
## 载入sysctl配置文件
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
把 inotify-tools-3.14.tar.gz 放入进 opt 目录中
[root@100 opt]# ls
inotify-tools-3.14.tar.gz rh student
[root@localhost opt]# yum install -y gcc gcc-c++ make
##安装 gcc gcc-c++ make 是为了编译和构建其他软件需要的工具链和依赖项
[root@100 opt]# tar zxvf inotify-tools-3.14.tar.gz
## 解压inotify-tools-3.14.tar.gz压缩包
[root@100 opt]# cd inotify-tools-3.14/
[root@100 inotify-tools-3.14]# ls
aclocal.m4 config.h.in COPYING libinotifytools man src
AUTHORS config.sub depcomp ltmain.sh missing
ChangeLog configure INSTALL Makefile.am NEWS
config.guess configure.ac install-sh Makefile.in README
[root@100 inotify-tools-3.14]# ./configure && make && make install
[root@100 inotify-tools-3.14]# ls
aclocal.m4 config.log depcomp Makefile README
AUTHORS config.status INSTALL Makefile.am src
ChangeLog config.sub install-sh Makefile.in stamp-h1
config.guess configure libinotifytools man
config.h configure.ac libtool missing
config.h.in COPYING ltmain.sh NEWS
[root@100 inotify-tools-3.14]# cd ..
[root@100 opt]# ls
inotify-tools-3.14 inotify-tools-3.14.tar.gz rh student
使用命令进行监控(inotifywait -mrq -e modify,create,move,delete )
选项 | 含义 |
-e | 用来指定要监控哪些事件 |
-m | 表示持续监控 |
-r | 表示递归整个目录 |
-q | 简化输出信息 |
客户端:20.0.0.200
[root@100 opt]# inotifywait -mrq -e modify,create,move,delete /opt/student/
[root@100 opt]# cd student/
[root@100 student]# ls
html local.repo passwd
[root@100 student]# rm -rf html/
使用Shell脚本进行实时监控
## 监控端在什么地方,脚本也要放在相同地方
[root@100 opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /opt/student/"RSYNC_CMD="rsync -avH --delete --password-file=/etc/server.pass zhangsan@20.0.0.200::myhtml /opt/student/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
##while判断是否接收到监控记录
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
[root@100 opt]# ls
inotify.sh inotify-tools-3.14 inotify-tools-3.14.tar.gz rh student
[root@100 opt]# chmod +x /opt/inotify.sh
[root@100 opt]# chmod 777 /opt/student/
[root@100 opt]# chmod +x /etc/rc.d/rc.local
[root@100 opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local
[root@100 opt]# cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/opt/inotify.sh
[root@100 opt]# ./inotify.sh
## 启动脚本
[root@100 opt]# cp /etc/yum.repos.d/* /opt/student/
## 拷贝文件到文件夹中,之后服务端也会收到相同数据
批量删除数据(rsync --delete-before -a -H -v --progress --stats /home/blank /data/)
1.创建两个空文件夹,data用来存放10000个数据
[root@localhost ~]# mkdir /home/blank
[root@localhost /]# mkdir data
## 批量创建10000个数据
[root@localhost data]# touch file {1..10000}
##删除data10000个数据
[root@localhost data]# rsync --delete-before -a -H -v --progress --stats /home/blank /data/
总结
配置源的三种表达方式
rsync -az 原始位置 目标位置
格式一:
用户名@主机名ip::共享模块名
rsync -avz backuper@20.0.0.200::mythml /opt/student/
格式二:
rsync://用户名@主机地址ip:/共享模块名
rsync -avz rsync://backuper@20.0.0.200/myhtml /opt/student
格式三:
rsync -avz -e 'ssh -p ssh端口号' 原始位置 目标位置
rsync -avz -e 'ssh -p 22' root@20.0.0.200:/var/www/html /opt/student/