shell脚本,对MySQL数据库进行分库加分表备份

Posted 王月波

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本,对MySQL数据库进行分库加分表备份相关的知识,希望对你有一定的参考价值。

[[email protected] wyb]# cat table_backup.sh 
#!/bin/bash

flag=0
user=root
pass=test

mysql -u$user -p"$pass" -e "show databases;" &>/dev/null
[ $? -ne 0  ] && read -p "Mysql do not running,start it?(`echo -e "\033[32myes/no\033[0m"`):" choice && flag=1
[[ "choice" -eq "yes" ]] && service mysqld start &>/dev/null && flag=0
[ $flag -eq 1 ] && exit 2
database=`mysql -u$user -p$pass  -e "show databases;"|sed 1d|grep -v schema`

echo -e "\033[32m==================backup start=====================\033[0m"
for i in $database
do
  tables=`mysql -u$user -p"$pass" -e "use $i;show tables;"|sed 1d`
  for j in $tables
  do
    mysqldump -u$user -p"$pass"   -B --databases $i --tables $j > /tmp/${i}-${j}-`date +%F`.sql
   [ $? -eq 0 ] && echo $i $j ok >>/tmp/table.log||echo $i $j failed >>/tmp/table.log
   [ $? -eq 0 ] && echo -e "$i $j \033[32mok\033[0m" ||echo -e "$i $j \033[31mfailed\033[0m"
  done

done
echo -e "\033[32m===================backup stop=======================\033[0m"
[[email protected] wyb]# bash table_backup.sh 
==================backup start=====================
mysql columns_priv ok
mysql db ok
mysql func ok
mysql help_category ok
mysql help_keyword ok
mysql help_relation ok
mysql help_topic ok
mysql host ok
mysql proc ok
mysql procs_priv ok
mysql tables_priv ok
mysql time_zone ok
mysql time_zone_leap_second ok
mysql time_zone_name ok
mysql time_zone_transition ok
mysql time_zone_transition_type ok
mysql user ok
===================backup stop=======================
[[email protected] wyb]# cd tmp
-bash: cd: tmp: No such file or directory
[[email protected] wyb]# cd /tmp
[[email protected] tmp]# ls
a.log                              mysql-help_category-2016-09-07.sql  mysql-tables_priv-2016-09-07.sql                table.log
dashazi123.sh                      mysql-help_keyword-2016-09-07.sql   mysql-time_zone-2016-09-07.sql                  VMwareDnD
keyring-cmzNiY                     mysql-help_relation-2016-09-07.sql  mysql-time_zone_leap_second-2016-09-07.sql      vmware-root
mapping-root                       mysql-help_topic-2016-09-07.sql     mysql-time_zone_name-2016-09-07.sql             vmware-root-1723735141
mysql-columns_priv-2016-09-07.sql  mysql-host-2016-09-07.sql           mysql-time_zone_transition-2016-09-07.sql       VMwareTools-9.6.2-1688356.tar.gz
mysql-db-2016-09-07.sql            mysql-proc-2016-09-07.sql           mysql-time_zone_transition_type-2016-09-07.sql  vmware-tools-distrib
mysql-func-2016-09-07.sql          mysql-procs_priv-2016-09-07.sql     mysql-user-2016-09-07.sql
[[email protected] tmp]# ls mysql*
mysql-columns_priv-2016-09-07.sql   mysql-help_topic-2016-09-07.sql   mysql-time_zone_leap_second-2016-09-07.sql
mysql-db-2016-09-07.sql             mysql-host-2016-09-07.sql         mysql-time_zone_name-2016-09-07.sql
mysql-func-2016-09-07.sql           mysql-proc-2016-09-07.sql         mysql-time_zone_transition-2016-09-07.sql
mysql-help_category-2016-09-07.sql  mysql-procs_priv-2016-09-07.sql   mysql-time_zone_transition_type-2016-09-07.sql
mysql-help_keyword-2016-09-07.sql   mysql-tables_priv-2016-09-07.sql  mysql-user-2016-09-07.sql
mysql-help_relation-2016-09-07.sql  mysql-time_zone-2016-09-07.sql
[[email protected] tmp]# cat table.log 
mysql columns_priv ok
mysql db ok
mysql func ok
mysql help_category ok
mysql help_keyword ok
mysql help_relation ok
mysql help_topic ok
mysql host ok
mysql proc ok
mysql procs_priv ok
mysql tables_priv ok
mysql time_zone ok
mysql time_zone_leap_second ok
mysql time_zone_name ok
mysql time_zone_transition ok
mysql time_zone_transition_type ok
mysql user ok
[[email protected] tmp]# 

 

以上是关于shell脚本,对MySQL数据库进行分库加分表备份的主要内容,如果未能解决你的问题,请参考以下文章

CentOS 7 MySQL5.6分库分表-全备-shell脚本

Shell脚步之MySql分库分表备份

合格linux运维人员必会的30道shell编程面试题及讲解-09

分库分表MySQL分库分表方案

MySQL 备份脚本(单库/分库分表)

MySQL单机优化---分表分区分库