shell中编写备份数据库脚本(使用mysqldump工具)

本文主要是介绍shell中编写备份数据库脚本(使用mysqldump工具),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

mysqldump备份

目录

mysqldump备份

分库备份

分表备份


利用自带工具mysqldump 实现数据库分库分表备份。

要想知道需要备份哪些数据库,就得先列出来

mysql -uroot -p'Openlab123!' -N -e 'show databases' | egrep -on_schema|mysql|performance_schema|sys" 
mysql: [Warning] Using a password on the command line interfa
MySchool_db
MyScl_db
WORK
mysl
  • -N: 或者写作 --no-column-names,是一个选项,告诉mysql客户端在输出查询结果时不包含列名行。这对于脚本或自动化任务特别有用,因为它使得输出更易于解析。

  • -e 'show databases': -e 后面跟的是执行的SQL命令,这里是要执行的命令是 show databases,该命令用于列出MySQL服务器上所有的数据库。

整个命令的作用是:以root用户身份,使用密码登录MySQL服务器,并且在登录后执行show databases命令来显示服务器上的所有数据库列表,同时在输出时不包含列标题。

命令会列出所有数据库中名称匹配除去 information_schema, mysql, performance_schema, 或 sys 的数据库的行

mysqldump进行数据库备份

mysqldump -uroot -pOpenlab123! -B MySchool_db > MySchool_db.sql

备份肯定是不能只备份一条 --> 使用for循环进行备份

分库备份

思路:将除去系统自带的数据库以外的数据库名赋值给DBS,再使用for循环遍历DBS变量循环内部就对每一个数据库进行备份

[root@localhost script] DBS=$(mysql -uroot -p'Openlab123!' -N -e "show databases" | egrep -v "information_schema|mysql|performance_schema|sys")
mysql: [Warning] Using a password on the command line interface can be insecure.[root@localhost script] echo $DBS
MySchool_db MyScl_db WORK mysl[root@localhost script] for db in $DBS
> do
> echo 备份$db
> done
备份MySchool_db
备份MyScl_db
备份WORK
备份mysl

编写shell备份服务器脚本

#!/bin/bashDBS=$(mysql -uroot -p'Openlab123!' -N -e "show databases" | egrep -v "information_schema|mysql|performance_schema|sys")for db in $DBS
domysqldump -uroot -pOpenlab123! -B $db > ${db}_$(date +%F).sql
done

启动脚本

[root@localhost script] ls
db_back.sh
#以下警告是因为使用mysqldump命令的时候直接在命令行输入了密码,要解决这个问题可以重定向到/dev/null 下
[root@localhost script] bash db_back.sh 
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost script] ll
总用量 52
-rw-r--r--. 1 root root   221  5月 27 21:10 db_back.sh
-rw-r--r--. 1 root root 22792  5月 27 21:10 MySchool_db_2024-05-27.sql
-rw-r--r--. 1 root root 10939  5月 27 21:10 MyScl_db_2024-05-27.sql
-rw-r--r--. 1 root root  2413  5月 27 21:10 mysl_2024-05-27.sql
-rw-r--r--. 1 root root  5644  5月 27 21:10 WORK_2024-05-27.sql

此时一个初步的脚本已经完成了,接下来就是优化

  • 将路径修改为变量,将四个基本的数据库修改为变量的形式,将用户密码修改为变量的形式---都是常用修改的设为变量以便往后修改
  • 将主程序加入死循环,并将备份脚本放到特定的目录下。
  • 如果存在这个目录就直接创建,如果不存在就创建并跳过本次循环再生成脚本
#!/bin/bashBAK_DIR=/backup/db
DB_BASE="information_schema|mysql|performance_schema|sys"
DBS=$(mysql -uroot -p'Openlab123!' -N -e "show databases" | egrep -v ${DB_BASE})
DB_USER_PW="-uroot -pOpenlab123!"# main program
while true;doif [ -d ${BAK_DIR} ];thenfor db in $DBSdomysqldump ${DB_USER_PW} -B $db > ${BAK_DIR}/${db}_$(date +%F).sqldonebreakelif [ ! -d ${BAK_DIR} ];thenmkdir -p ${BAK_DIR}continuefi
done

分表备份

一样的思路,将每一个库里面每一个表做循环备份

#!/bin/bashBAK_DIR=/backup/db
DB_USER_PW="-uroot -pOpenlab123!"
DB_BASE="information_schema|mysql|performance_schema|sys"
DBS=$(mysql ${DB_USER_PW} -N -e "show databases" | egrep -v ${DB_BASE})# main program
while true;doif [ -d ${BAK_DIR} ];thenfor db in $DBSdoTABS=$(mysql ${DB_USER_PW} -N -e "show tables from $db")for tab in $TABSdomysqldump ${DB_USER_PW} $db $tab > ${BAK_DIR}/${db}_${tab}_$(date +%F).sqldonedonebreakelif [ ! -d ${BAK_DIR} ];thenmkdir -p ${BAK_DIR}continuefi
done

实现效果

[root@localhost script] bash  db_back_tables.sh 
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost script] ls /backup/db/
MySchool_db_grade_2024-05-28.sql            MySchool_db_subject_2024-05-28.sql  MyScl_db_index_4_2024-05-28.sql        MyScl_db_tab22_2024-05-28.sql  WORK_test_2024-05-28.sql
MySchool_db_result_2024-05-28.sql           MySchool_db_t1_2024-05-28.sql       MyScl_db_index5_2024-05-28.sql         mysl_t1_2024-05-28.sql
MySchool_db_student_2024-05-28.sql          MySchool_db_user_2024-05-28.sql     MyScl_db_student_2024-05-28.sql        WORK_college_2024-05-28.sql
MySchool_db_Student_V_1_2024-05-28.sql      MyScl_db_index1_2024-05-28.sql      MyScl_db_student_count_2024-05-28.sql  WORK_dept_2024-05-28.sql
MySchool_db_Student_V_grade_2024-05-28.sql  MyScl_db_index2_2024-05-28.sql      MyScl_db_tab11_2024-05-28.sql          WORK_emp_2024-05-28.sql

其实代码还可以继续优化,因为没有达成将每一个表备份到固定的目录之下

#!/bin/bashDB_USER_PW="-uroot -pOpenlab123!"
DB_BASE="information_schema|mysql|performance_schema|sys"
BAK_ROOT=/backup# 创建根备份目录
mkdir -p ${BAK_ROOT}# 获取所有数据库列表,排除特定系统库
DBS=$(mysql ${DB_USER_PW} -N -e "show databases" | egrep -v ${DB_BASE})# 遍历数据库
for db in $DBS
doBAK_DIR=${BAK_ROOT}/${db}# 检查备份目录是否存在,使用if-elif结构if [ ! -d "${BAK_DIR}" ]; then# 如果目录不存在,则创建mkdir -p "${BAK_DIR}"fi# 获取数据库内的所有表TABS=$(mysql ${DB_USER_PW} -N -e "show tables from ${db}")# 遍历表并执行备份for tab in $TABS; domysqldump ${DB_USER_PW} ${db} ${tab} > ${BAK_DIR}/${db}_${tab}_$(date +%F).sqldone
done

这个修订版脚本做了以下改动:

  • 移除了不必要的 while true 循环,因为它可能导致无限循环或不期望的退出。
  • 仅创建一次备份根目录,并为每个数据库动态创建子目录。
  • 为每个数据库单独备份其所有表,而不是在每个目录内重新遍历所有数据库。
  • 修正了备份文件路径,确保它们被正确地保存在每个数据库对应的备份目录下。

实现效果

[root@localhost script] bash  db_back_tables.sh 
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.[root@localhost script] tree /backup/
/backup/
├── MySchool_db
│   ├── MySchool_db_grade_2024-05-28.sql
│   ├── MySchool_db_result_2024-05-28.sql
│   ├── MySchool_db_student_2024-05-28.sql
│   ├── MySchool_db_Student_V_1_2024-05-28.sql
│   ├── MySchool_db_Student_V_grade_2024-05-28.sql
│   ├── MySchool_db_subject_2024-05-28.sql
│   ├── MySchool_db_t1_2024-05-28.sql
│   └── MySchool_db_user_2024-05-28.sql
├── MyScl_db
│   ├── MyScl_db_index1_2024-05-28.sql
│   ├── MyScl_db_index2_2024-05-28.sql
│   ├── MyScl_db_index_4_2024-05-28.sql
│   ├── MyScl_db_index5_2024-05-28.sql
│   ├── MyScl_db_student_2024-05-28.sql
│   ├── MyScl_db_student_count_2024-05-28.sql
│   ├── MyScl_db_tab11_2024-05-28.sql
│   └── MyScl_db_tab22_2024-05-28.sql
├── mysl
│   └── mysl_t1_2024-05-28.sql
└── WORK├── WORK_college_2024-05-28.sql├── WORK_dept_2024-05-28.sql├── WORK_emp_2024-05-28.sql└── WORK_test_2024-05-28.sql4 directories, 21 files

这篇关于shell中编写备份数据库脚本(使用mysqldump工具)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1012480

相关文章

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

MySQL中的交叉连接、自然连接和内连接查询详解

《MySQL中的交叉连接、自然连接和内连接查询详解》:本文主要介绍MySQL中的交叉连接、自然连接和内连接查询,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、引入二、交php叉连接(cross join)三、自然连接(naturalandroid join)四

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA

Mysql表如何按照日期字段的年月分区

《Mysql表如何按照日期字段的年月分区》:本文主要介绍Mysql表如何按照日期字段的年月分区的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、创键表时直接设置分区二、已有表分区1、分区的前置条件2、分区操作三、验证四、注意总结一、创键表时直接设置分区