shell脚本,对MySQL数据库进行分库加分表备份
时间:2021-07-01 10:21:17
帮助过:2人阅读
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"
[root@localhost 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=======================
[root@localhost wyb]# cd tmp
-bash: cd: tmp: No such
file or directory
[root@localhost wyb]# cd /
tmp
[root@localhost 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
[root@localhost 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
[root@localhost 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
[root@localhost tmp]#
shell脚本,对MySQL数据库进行分库加分表备份
标签: