-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmysqldumpp.sh
executable file
·72 lines (60 loc) · 1.74 KB
/
mysqldumpp.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
#!/usr/bin/env bash
USER=$1
DATABASE=$2
HOST=$3
PORT=$4
# Validate our arugments and ensure that GNU parallel is available.
if [[ -z $DATABASE ]]
then
echo "Usage: mysqldumpp.sh <user> <database> [host] [port]"
exit 1
fi
if [[ -z $HOST ]]
then
HOST='localhost'
fi
if [[ -z $PORT ]]
then
PORT=3306
fi
PARALLEL=`type -P parallel`
if [[ -z $PARALLEL ]]
then
echo "GNU Parallel is required. Install it from your package manager or from"
echo "https://savannah.gnu.org/projects/parallel/."
exit 1
fi
BZIP2=`type -P lbzip2`
if [[ -z $BZIP2 ]]
then
echo "lbzip2 was not found. Falling back to bzip2. Consider installing lbzip2 for improved"
echo "performance."
BZIP2=`type -P bzip2`
fi
echo -n "Please enter your mysql password for $USER: "
read -s PASS
echo ""
# Fetch all of the tables in the database.
if [ -z "$PASS" ]
then
TABLES=`mysql --batch --skip-column-names -u $USER -h$HOST -P$PORT -e 'SHOW TABLES;' $DATABASE`
else
TABLES=`mysql --batch --skip-column-names -u $USER --password="$PASS" -h$HOST -P$PORT -e 'SHOW TABLES;' $DATABASE`
fi
if [[ -z $TABLES ]]
then
echo "Unable to read tables from $DATABASE. Check your connection options"
exit 1
fi
DATE=`date "+%Y-%m-%d-%H%M"`
DESTINATION="$DATABASE-$DATE"
mkdir -p "$DESTINATION"
# Run one job for each table we are dumping.
if [ -z "$PASS" ]
then
time echo $TABLES |
$PARALLEL -d ' ' --trim=rl -I , echo "Dumping table ,." \&\& mysqldump -C -u$USER -h$HOST -P$PORT --skip-lock-tables --add-drop-table $DATABASE , \| $BZIP2 \> $DESTINATION/,.sql.bz2
else
time echo $TABLES |
$PARALLEL -d ' ' --trim=rl -I , echo "Dumping table ,." \&\& mysqldump -C -u$USER -p"'$PASS'" -h$HOST -P$PORT --skip-lock-tables --add-drop-table $DATABASE , \| $BZIP2 \> $DESTINATION/,.sql.bz2
fi