rsync备份案例

1. 需求


客户端需求
1.客户端提前准备存放的备份的目录,目录规则如下:/backup/nfs_172.16.1.31_2018-09-02 
2.客户端在本地打包备份(系统配置文件、应用配置等)拷贝至/backup/nfs_172.16.1.31_2018-09-02
3.客户端最后将备份的数据进行推送至备份服务器
4.客户端每天凌晨1点定时执行该脚本
5.客户端服务器本地保留最近7天的数据, 避免浪费磁盘空间

服务端需求
1.服务端部署rsync,用于接收客户端推送过来的备份数据
2.服务端需要每天校验客户端推送过来的数据是否完整
3.服务端需要每天校验的结果通知给管理员
4.服务端仅保留6个月的备份数据,其余的全部删除

2. 准备服务器

主机内网IP
web01172.16.1.7
backup172.16.1.41

3. 客户端

1) 创建备份目录

#备份目录格式
/backup/nfs_172.16.1.31_2018-09-02

#取主机名
[root@web01 ~]# hostname
web01

#取ip
[root@web01 ~]# ifconfig eth1 | awk 'NR==2{print $2}'
172.16.1.7
[root@web01 ~]# hostname -I | awk '{print $2}'
172.16.1.7

#取时间
[root@web01 ~]# date +%F
2020-08-13

#创建目录
[root@web01 ~]# mkdir -p /backup/`hostname`_`hostname -I | awk '{print $2}'`_`date +%F`

2) 打包


[root@web01 ~]# cd /backup/web01_172.16.1.7_2020-08-23

[root@web01 /backup/web01_172.16.1.7_2020-08-12]# tar zcf conf.tar.gz /etc/passwd /var/log/messages &> /dev/null

3) 对打包文件生成验证文件

[root@web01 ~]# md5sum /backup/web01_172.16.1.7_2020-08-23/conf.tar.gz > /backup/web01_172.16.1.7_2020-08-23/auth.txt

[root@web01 ~]# cat /backup/web01_172.16.1.7_2020-08-23/auth.txt
05ee3b6231317d0f5b70c63afae6afb8  /backup/web01_172.16.1.7_2020-08-23/conf.tar.gz

4) 推送数据

[root@web01 ~]# rsync -avz /backup/web01_172.16.1.7_2020-08-23 rsync_backup@172.16.1.41::backup
Password: ****
sending incremental file list
web01_172.16.1.7_2020-08-23/
web01_172.16.1.7_2020-08-23/auth.txt

sent 255 bytes  received 47 bytes  67.11 bytes/sec
total size is 50,999  speedup is 168.87


5) 清理数据

#模拟生成一个月数据
[root@web01 ~]# for i in {1..30};do date -s 2020/08/$i;sh /scripts/backup.sh;done

#只保留七天的文件
[root@web01 ~]# find /backup -mtime +7 | xargs rm -rf
[root@web01 ~]# ll /backup
total 0
drwxr-xr-x 2 root root 25 Aug 23 00:00 web01_172.16.1.7_2020-08-23
drwxr-xr-x 2 root root 25 Aug 24 00:00 web01_172.16.1.7_2020-08-24
drwxr-xr-x 2 root root 25 Aug 25 00:00 web01_172.16.1.7_2020-08-25
drwxr-xr-x 2 root root 25 Aug 26 00:00 web01_172.16.1.7_2020-08-26
drwxr-xr-x 2 root root 25 Aug 27 00:00 web01_172.16.1.7_2020-08-27
drwxr-xr-x 2 root root 25 Aug 28 00:00 web01_172.16.1.7_2020-08-28
drwxr-xr-x 2 root root 25 Aug 29 00:00 web01_172.16.1.7_2020-08-29
drwxr-xr-x 2 root root 25 Aug 30 00:00 web01_172.16.1.7_2020-08-30

6) 将以上内容写成脚本


[root@web01 ~]# vim /scripts/backup.sh

#!/bin/bash
#1.定义变量
SRC=/backup
HOST=`hostname`
IP=`hostname -I | awk '{print $2}'`
DATE=`date +%F`
DIR=${SRC}/${HOST}_${IP}_${DATE}

#2.创建目录
mkdir -p $DIR

#3.进到目录下并打包文件
cd $DIR
tar czf conf.tar.gz /etc/passwd /var/log/messages &>/dev/null

#4.对打包文件生成验证文件
md5sum $DIR/conf.tar.gz > $DIR/auth.txt

#5.推送
export RSYNC_PASSWORD=123456
rsync -avz $DIR rsync_backup@172.16.1.41::backup

#6.清理数据
find $SRC -type d -mtime +7 | xargs rm -rf

6) 写入定时任务

[root@web01 ~]# crontab -e
#每天凌晨1点进行备份
00 1 * * * /bin/bash /scripts/backup.sh &> /dev/null

5. 服务端

1) 安装邮件系统

[root@backup ~]# yum install -y mailx

#编辑邮件系统
[root@backup ~]# vim /etc/mail.rc 
set from=*******@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=*******@qq.com
set smtp-auth-password=邮箱授权码
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/

2) 脚本

[root@backup ~]# vim /scripts/auth.sh

#!/bin/bash
#1.定义变量
SRC=/backup
HOST='web01'
IP='172.16.1.7'
DATE=`date +%F`
DIR=${SRC}/${HOST}_${IP}_${DATE}

#2.验证文件完整性
md5sum -c $DIR/auth.txt > $SRC/result.txt

#3.把验证结果发送到邮箱
mail -s "备份数据完整性" 1255986946@qq.com < $SRC/result.txt

#4.清理数据
find $SRC -type d -mtime +180 | xargs rm -rf
~                                     

3) 写入定时任务

[root@backup ~]# crontab -e
#服务端定时发送验证结果
30 1 * * * /bin/bash /scripts/auth.sh &> /dev/null

6.rsync结合inotifywait实现实时备份

1) inotifywait

 inotifywait    #用于等待文件或文件集上的一个待定事件,可以监控任何文件和目录设置,并且可以递归地监控整个目录树

# 安装命令
yum -y install inotify-tools

选项:
     -m          #持续监控
     -r          #递归
     -q          #静默,仅打印时间信息
      
     --timefmt       #指定输出时间格式
     --format        #指定事件输出格式
            %Xe       #事件
            %w        #目录
            %f        #文件
      
       -e      #指定监控的事件
         access       #访问
         modify       #内容修改
         attrib       #属性修改
         close_write  #修改真实文件内容
         open         #打开
         create       #创建
         delete       #删除
         umount       #卸载
   

2) 实时监控脚本

#粗糙版
[root@web01 /backup]# vim /scripts/rsync_inotify.sh 
#!/bin/bash
dir=/backup
/usr/bin/inotifywait -mrq --format '%w %f' -e create,delete,attrib,close_write $dir | while read line;do
    cd $dir && rsync -az -R --delete . rsync_backup@172.16.1.41::backup --password-file=/etc/rsyncd.password >/dev/null 2>&1
done  &


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值