近期公司有一个数据库需要迁移,但是里面创建的数据库比较多,我们采取脚本将库单独备份,然后上传到另一台主机,新建数据库,然后恢复数据库。将自己写的几个小脚本粘贴到此,
分库压缩备份
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#/bin/sh
#version 0.1
MYUSER=mysqlback
MYPASS=databack@162.com
#SOCKET=/data/3306/mysql.sock
MYLOGIN=
"mysql -u$MYUSER -p$MYPASS "
MYDUMP=
"mysqldump -u$MYUSER -p$MYPASS -B"
DATABASE
=
"$($MYLOGIN -e "
show databases;
"|egrep -vi "
Data|_schema|mysql
")"
#正则
for
dbname
in
$
DATABASE
do
MYDIR=/data/backup/$dbname
[ ! -d $MYDIR ] && mkdir -p $MYDIR
$MYDUMP $dbname|gzip >$MYDIR/${dbname}_$(
date
+%F).sql.gz
done
|
批量解压缩
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#/bin/bash
for
i
in
`
ls
*.gz`
do
gunzip $i
done
解压结果:
wl_smsgate_2017-04-12.sql
wl_smsgate_hx_2017-04-12.sql
wl_smsgate_ldkj_2017-04-12.sql
wl_smsgate_tt_2017-04-12.sql
wl_smsgate_dg_2017-04-12.sql
wl_smsgate_jjbs_2017-04-12.sql
wl_smsgate_pc_2017-04-12.sql
wl_smsgate_tt_lt_2017-04-12.sql
wl_smsgate_dxfj_2017-04-12.sql
wl_smsgate_jl_2017-04-12.sql
wl_smsgate_sb_2017-04-12.sql
|
批量创建数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#/bin/bash
#fun create database
mysqluser=root
mysqlpass=winnerlook123
mysqlcent=
"mysql -u $mysqluser -p$mysqlpass"
dabasename=`
cat
databasename`
date2=
"_2017-04-12.sql"
for
data
in
$dabasename
do
$mysqlcent -e
"create database if not exists $data default character set utf8"
done
|
恢复数据
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#/bin/bash
#fun create database
mysqluser=root
mysqlpass=winnerlook123
mysqlcent=
"mysql -u $mysqluser -p$mysqlpass"
dabasename=`
cat
databasename`
date2=
"_2017-04-12.sql"
for
data
in
$dabasename
do
$mysqlcent -e
"use $data "
&& $mysqlcent -e
" source /tmp/backup/$data$date2 "
#恢复数据
done
|
然后检查数据库的数据字符集以及其数据条数。
本文转自 tianya1993 51CTO博客,原文链接:http://blog.51cto.com/dreamlinux/1915152,如需转载请自行联系原作者