Shell编写关于mysqldump实现分库分表备份

编写脚本,使用mysqldump实现分库分表备份。

#编辑脚本文件
[root@localhost scripts]# vim bak_tb1.sh

#脚本内容:
#设置变量,减少代码冗余
mysql_cmd='-uroot -p123'
exclude_db='Database|information_schema|-S|mysql|performance_schema|sys'
bak_path=/backup/db
#将要分出来的数据库名写在dbname文件缓存中
mysql ${mysql_cmd} -e 'show databases' | egrep -v "${exclude_db}" > dbname

while read line #获取所需数据库的备份
do
  [ -d ${bak_path}/$line ] || mkdir -p ${bak_path}/$line #创建文件路径,若有则在该目录下创建目录;反之,创建符合条件的相关目录
  mysqldump ${mysql_cmd} -B $line | gzip > ${bak_path}/${line}/${line}_$(date +%F).sql.gz #通过循环将需要备份的数据库一个个压缩,传到之前创建的文件目录下

  mysql -uroot -p123 -N -e "show tables from $line" > tbname #查询数据库中的表

  while read tb #获取数据库中表的备份
  do
    mysqldump ${mysql_cmd}  $line $tb | gzip > ${bak_path}/${line}/${line}_${tb}_$(date +%F).sql.gz #将数据库中的表压缩并在相应的数据库路径下
  done < tbname     #读取tbname文件内容
done < dbname       #读取dbname文件内容
rm -f dbname tbname  删除缓存的数据库,数据表文件

#在对应路径下查看生成的文件:
[root@localhost scripts]# tree /backup/db
/backup/db
├── db
│   ├── db_2023-08-03.sql.gz
│   ├── db_score_2023-08-03.sql.gz
│   └── db_student_2023-08-03.sql.gz
├── db1
│   └── db1_2023-08-03.sql.gz
├── db2
│   └── db2_2023-08-03.sql.gz
├── db3
│   └── db3_2023-08-03.sql.gz
└── db4
    └── db4_2023-08-03.sql.gz

5 directories, 7 files
#检验文件是否能成功还原:
[root@localhost scripts]# mysql -uroot -p123 -e 'select * from db.student'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 902 | 张老二    ||  1986 | 中文系       | 北京市昌平区       |
| 903 | 张三      ||  1990 | 中文系       | 湖南省永州市       |
| 904 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+

#删除db数据库中的student表
[root@localhost scripts]# mysql -uroot -p123 -e 'drop table db.student'
mysql: [Warning] Using a password on the command line interface can be insecure.

#检查是否成功删除
[root@localhost scripts]# mysql -uroot -p123 -e 'select * from db.student'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1146 (42S02) at line 1: Table 'db.student' doesn't exist

#将/backup/db/db中的 db_student_2023-08-03.sql.gz表压缩包还原到数据库db中

[root@localhost scripts]# zcat /backup/db/db/db_student_2023-08-03.sql.gz | mysql -uroot -p123 db
mysql: [Warning] Using a password on the command line interface can be insecure.

#再次查看student表的情况:
[root@localhost scripts]# mysql -uroot -p123 -e 'select * from db.student'      mysql: [Warning] Using a password on the command line interface can be insecure.
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 902 | 张老二    ||  1986 | 中文系       | 北京市昌平区       |
| 903 | 张三      ||  1990 | 中文系       | 湖南省永州市       |
| 904 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值