shell脚本实战案例---使用第三方工具备份数据库

案例:基于Innobackupex的MySQL备份脚本

具体要求:

1、周日全备
2、周一至周六增量备份
3、备份使用backup用户

目录

1.创建用户并进行授权

2.创建数据库,创建表,并插入数据

3.备份命令

4.进行分析:周六至周六增量备份、周日全备

5.参考代码展示

6.进行测试

7.扩展:


1.创建用户并进行授权

[root@node11 ~]# mysql -uroot -p'MySQL@123'
mysql> grant SELECT,RELOAD,SHOW DATABASES,LOCK TABLES,SUPER,PROCESS,REPLICATION CLIENT on *.* to backup@'localhost' identified by 'MySQL#123';

2.创建数据库,创建表,并插入数据

这里为了做案例,就随便写一点数据就好

mysql> create database db3;
mysql> use db3;
mysql> create table t1 (id int(11));
mysql> insert into t1 values(1),(2),(3);
mysql> select * from t1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

3.备份命令

【1】首先下载备份工具

下载路径:
https://downloads.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.8/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.8-1.el7.x86_64.rpm?_gl=1*48a0o*_gcl_au*NjYwMjQ0MDQyLjE2ODYxMjU0NjM.

【2】安装备份工具

我这个电脑需要安装依赖,你们视情况而定,也可以先安装工具,不行了,在安装依赖

1.先安装依赖
[root@node11 ~]# yum install -y mysql-community-devel-5.7.14-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.14-1.el7.x86_64.rpm
2.安装工具
[root@node11 ~]# yum install -y percona-xtrabackup-24-2.4.8-1.el7.x86_64.rpm

【3】写备份命令

由于每天都需要进行备份,所有我们这里使用函数来封装备份:FULL、INCR

FULL () {
    innobackupex -u backup -p 'MySQL#123' --no-timestamp /backup/mysql/full_$(date +%F)
}

INCR () {
    innobackupex --incremental -u backup -p 'MySQL#123' --no-timestamp /backup/mysql/incre_$(date +%F) --incremental-basedir=上一次备份的路径
}

4.进行分析:周六至周六增量备份、周日全备

如何判断今天是周几:

[root@node11 scripts]# date +%u

 前一天的日期如何表示?

[root@node11 scripts]# date -d '-1 day' +%F

5.参考代码展示

#!/bin/bash

# define var
today=$(date +%F)
yesterday=$(date -d '-1 day' +%F)
cmd_opt='-u backup -p 'MySQL#123' --no-timestamp'
bak_dir=/backup/mysql

# functions
FULL () {
    innobackupex ${cmd_opt} ${bak_dir}/full_${today}
}

INCR () {
    innobackupex --incremental ${cmd_opt} ${bak_dir}/incre_${today} --incremental-basedir=$1
}

# main program
[ -d ${bak_dir} ] || mkdir -p ${bak_dir}

if [ $(date +%u) -eq 7 ]
then
    FULL
elif [ $(date +%u) -eq 1 ]
then
    INCR ${bak_dir}/full_${yesterday}
else
    INCR ${bak_dir}/incre_${yesterday}
fi

6.进行测试

1.将时间改到周天的日期,进行测试
[root@node11 scripts]# date -s $(date -d '-2 day' +%F)
[root@node11 scripts]# sh inno.sh 
[root@node11 scripts]# ll /backup/mysql/
total 0
drwxr-x--- 8 root root 226 Oct 29 00:01 full_2023-10-29

2.我们插入数据,将时间改为周一,进行测试
mysql> insert into t1 values(4),(5);
[root@node11 scripts]# date -s $(date -d '+1 day' +%F)
[root@node11 scripts]# sh inno.sh
[root@node11 scripts]# ll /backup/mysql/
total 0
drwxr-x--- 8 root root 226 Oct 29 00:01 full_2023-10-29
drwxr-x--- 8 root root 252 Oct 30 00:03 incre_2023-10-30

3.我们插入数据,将时间改为周二,进行测试
mysql> insert into t1 values(6),(7);
[root@node11 scripts]# date -s $(date -d '+1 day' +%F)
[root@node11 scripts]# sh inno.sh
[root@node11 scripts]# ll /backup/mysql/
total 0
drwxr-x--- 8 root root 226 Oct 29 00:01 full_2023-10-29
drwxr-x--- 8 root root 252 Oct 30 00:03 incre_2023-10-30
drwxr-x--- 8 root root 252 Oct 31 00:00 incre_2023-10-31

7.扩展:

具体要求:

周日完全备份
周1-2增量备份
周3差异备份
周4-6增量备份

 我们只需要把代码稍微修改一下,就可以,代码如下:

#!/bin/bash

# define var
today=$(date +%F)
yesterday=$(date -d '-1 day' +%F)
cmd_opt='-u backup -p 'MySQL#123' --no-timestamp'
bak_dir=/backup/mysql

# functions
FULL () {
    innobackupex ${cmd_opt} ${bak_dir}/full_${today}
}

INCR () {
    innobackupex --incremental ${cmd_opt} ${bak_dir}/incre_${today} --incremental-basedir=$1
}

# main program
[ -d ${bak_dir} ] || mkdir -p ${bak_dir}

case $(date +%u) in
    7)
        FULL
        ;;
    1)
        INCR ${bak_dir}/full_${yesterday}
        ;;
    3)
        INCR ${bak_dir}/full_$(date -d '-3 day' +%F)
        ;;
    *)
        INCR ${bak_dir}/incre_${yesterday}
esac

进行测试:

1.插入数据,把日期改为周三,进行测试
mysql> insert into t1 values(8);
[root@node11 scripts]# date -s $(date -d '+1 day' +%F)
[root@node11 scripts]# sh inno.sh 
[root@node11 scripts]# ll /backup/mysql/
total 0
drwxr-x--- 8 root root 226 Oct 29 00:01 full_2023-10-29
drwxr-x--- 8 root root 252 Oct 30 00:03 incre_2023-10-30
drwxr-x--- 8 root root 252 Oct 31 00:00 incre_2023-10-31
drwxr-x--- 8 root root 252 Nov  1 00:00 incre_2023-11-01
2.插入数据,把日期改为周四,进行测试
mysql> insert into t1 values(9);
[root@node11 scripts]# date -s $(date -d '+1 day' +%F)
[root@node11 scripts]# sh inno.sh 
[root@node11 scripts]# ll /backup/mysql/
total 0
drwxr-x--- 8 root root 226 Oct 29 00:01 full_2023-10-29
drwxr-x--- 8 root root 252 Oct 30 00:03 incre_2023-10-30
drwxr-x--- 8 root root 252 Oct 31 00:00 incre_2023-10-31
drwxr-x--- 8 root root 252 Nov  1 00:00 incre_2023-11-01
drwxr-x--- 8 root root 252 Nov  2 00:00 incre_2023-11-02

这次的实战案例,到此结束。

由于是测试,很多东西都不是很严谨,希望大家多多包涵

你可能感兴趣的:(shell,数据库,linux,运维,adb,mysql)