-
Notifications
You must be signed in to change notification settings - Fork 3
/
pantheon-db-dump
executable file
·65 lines (53 loc) · 3.03 KB
/
pantheon-db-dump
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
#!/bin/bash
# pantheon-db-dump
# Dump a DB from a Pantheon environment.
# Check if mysql is installed locally.
command -v mysqldump >/dev/null 2>&1 || { echo >&2 "FAILURE. MySQL is required. Please install mysql locally and run the command again."; exit 1; }
if [[ $# -lt 2 ]] ; then
>&2 echo -e 'Usage:'
>&2 echo -e " $(basename $0) [site] [environment] | drush sql-cli"
>&2 echo -e ' [site] - The name of a Pantheon site. E.g. aclu-d7'
>&2 echo -e ' [environment] - An environment for that site. E.g. dev'
>&2 echo -e ' The drush command may require `@something.local` or `-l foo.dev` or whatever you usually use to help it connect to the right database.'
exit 0
fi
connection_info=`terminus connection:info $1.$2 --fields=mysql_port,git_port,sftp_username,mysql_host,mysql_username,mysql_password,mysql_database --format=print-r`
if [ $? -ne 0 ]; then
>&2 echo -e 'FAILURE. Please ensure that you can run this command successfully:'
>&2 echo -e ' terminus site connection-info'
exit 1;
fi
mysql_port=`echo -e "$connection_info" | grep mysql_port | awk '{print $3}'`
git_port=`echo -e "$connection_info" | grep git_port | awk '{print $3}'`
if [ -z "$git_port" ]; then
git_port=2222
fi
sftp_username=`echo -e "$connection_info" | grep sftp_username | awk '{print $3}'`
mysql_host=`echo -e "$connection_info" | grep mysql_host | awk '{print $3}'`
mysql_username=`echo -e "$connection_info" | grep mysql_username | awk '{print $3}'`
mysql_password=`echo -e "$connection_info" | grep mysql_password | awk '{print $3}'`
mysql_database=`echo -e "$connection_info" | grep mysql_database | awk '{print $3}'`
if [ "$mysql_database" == '' ]; then
mysql_database="pantheon"
fi
# First "Wake up" the server.
>&2 echo -e "--Now waking up the server.--"
terminus env:wake $1.$2
# Open the SSH connection.
>&2 echo -e "--Now opening the SSH connection.--"
#echo "ssh -o 'StrictHostKeyChecking accept-new' -f -N -L $mysql_port:localhost:$mysql_port -p $git_port $sftp_username@$mysql_host"
ssh -o 'StrictHostKeyChecking accept-new' -f -N -L $mysql_port:localhost:$mysql_port -p $git_port $sftp_username@$mysql_host
PV=''
if command -v pv >/dev/null 2>&1; then
SIZE_BYTES=$(mysql --skip-column-names --user=$mysql_username --host=127.0.0.1 --port=$mysql_port --password=$mysql_password $mysql_database <<< 'SELECT ROUND(SUM(data_length) * 0.8) AS "size_bytes" FROM information_schema.TABLES;')
PV="pv --progress --timer --rate --size $SIZE_BYTES"
>&2 echo -e "Approximate database size: $SIZE_BYTES"
>&2 echo -e "This progressbar is only an estimate. If the transfer completes at less than 100% without errors, then it was successful."
fi
#Transfer the Database.
>&2 echo -e "--Now transferring the database.--"
mysqldump --no-autocommit --single-transaction -B --opt --quote-names --user=$mysql_username --host=127.0.0.1 --port=$mysql_port --password=$mysql_password $mysql_database | $PV
# Kill the SSH session.
>&2 echo -e "--Now cleaning up the SSH connection.--"
ps -fU `whoami` | grep "ssh -o" | grep "@$mysql_host" | awk '{print $2}' | xargs kill
>&2 echo -e "All done"