-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpxb2mysqldump.sh
executable file
·156 lines (141 loc) · 3.93 KB
/
pxb2mysqldump.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# A script to turn Percona Xtrabackup to seperate or one large database dump.
set -e
## Print basic usage
usage() {
me=`basename $0`
echo "$me [-u username] [-A|-d database [-t table]] [-h] [directory]"
exit 1
}
## Check I am root
if [ "$EUID" -ne "0" ]; then
echo "you are not running as root" >&2
usage
fi
## Set some defaults
backupdir=`pwd`
runasuser='mysql'
alldatabases=FALSE
## Grab the options, and setup variables
while getopts ":u:hAd:t:" opt; do
case $opt in
u)
if [ -z "${OPTARG}" ]; then
usage
fi
runasuser=${OPTARG}
;;
d)
# dump just this database
if [ -z "${OPTARG}" ]; then
usage
fi
databasetodump=${OPTARG}
;;
t)
# requires database option
if [ -z "${databasetodump}" ]; then
usage
fi
if [ -z "${OPTARG}" ]; then
usage
fi
tabletodump=${OPTARG}
;;
A)
if [ -n "${databasetodump}" ]; then
usage
fi
alldatabases=TRUE
;;
h)
usage
;;
\?)
usage
;;
:)
usage
;;
esac
done
## Set the backupdir if supplied
shift $(($OPTIND - 1))
if [ -n "${1}" ]; then
backupdir=${1}
fi
## Safe guard
## TODO: Use a special override incase this is what people want to do.
if [ "${backupdir}" = "/var/lib/mysql" ]; then
echo "You probably don't want to run this in /var/lib/mysql." >&2
exit 1
fi
## Does the supplied directory exist?
if [ ! -d ${backupdir} ]; then
echo "The provided dir: ${backupdir}, doesn't exist" >&2
exit 1
fi
## Dump all the logs, config, etc, in a unique directory
temp=`mktemp -d`
chown ${runasuser} ${temp}
## Create a minimal my.cnf
cat > $temp/my.cnf << EOF
[mysqld]
skip-networking
datadir=$backupdir
socket=$temp/mysql.sock
innodb_data_home_dir=$backupdir
innodb_data_file_path=ibdata1:10M:autoextend
innodb_log_group_home_dir=$temp
innodb_log_files_in_group=2
skip-grant-tables
[mysql.server]
user=$runasuser
basedir=$backupdir
[mysqld_safe]
log-error=$temp/mysqld.log
pid-file=$temp/mysqld.pid
EOF
## Make all the files owned by the user we are going to run as.
chown -R ${runasuser} ${backupdir}
## Start mysql
mysqld_safe --defaults-file=${temp}/my.cnf &
sleep 20
## TODO: This could be varible time, but really it should be able to determine when mysqld is ready....
## Check that mysql has innodb
have_innodb=$(mysql -S ${temp}/mysql.sock -NB -e "show variables like 'have_innodb'"|awk '{print $2}')
if [ "$have_innodb" != "YES" ]; then
echo "InnoDB was not detected, exiting" >&2
exit 1
fi
if [ "${alldatabases}" == "TRUE" ]; then
## Full database dump
echo "Dumping all databases..."
mysqldump -S ${temp}/mysql.sock --all-databases --quick --single-transaction | gzip > ${backupdir}/alldatabases.sql.gz
echo "Done"
echo "You can import this into an empty or alternative host with something like this:"
echo "# Example:"
echo "# zcat ${backupdir}/alldatabases.sql.gz | mysql -uroot -p "
elif [ -z "${databasetodump}" ]; then
database=${databasetodump}
echo -n "Dumping $database to: $database.sql..."
mysqldump -S ${temp}/mysql.sock $database | gzip > ${backupdir}/${database}.sql.gz
echo "Done"
echo "You can import this into an instance with something like this:"
echo "# Example:"
echo "# zcat ${backupdir}/${database}.sql.gz | mysql -uroot -p $database"
else
## Just dump useful databases
for database in $(mysql -S ${temp}/mysql.sock -BN -e "show databases"|grep -Ev 'mysql|test|information_schema'); do
echo "Found: $database"
echo -n "Dumping $database to: $database.sql..."
mysqldump -S ${temp}/mysql.sock $database | gzip > ${backupdir}/${database}.sql.gz
echo "Done"
echo "You can import this into an instance with something like this:"
echo "# Example:"
echo "# zcat ${backupdir}/${database}.sql.gz | mysql -uroot -p $database"
done
fi
## Destory the mysql running
kill $(ps -fC mysqld|grep "${backupdir}"|awk '{print $2}')
# vim:syntax=bash