-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb-rebuild.sh
More file actions
executable file
·163 lines (152 loc) · 3.91 KB
/
db-rebuild.sh
File metadata and controls
executable file
·163 lines (152 loc) · 3.91 KB
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
157
158
159
160
161
162
163
#!/bin/bash
# Set the things
HOST=${DB_HOST:-${MYSQL_HOST:-127.0.0.1}}
USER=${DB_USER:-${MYSQL_USER:-root}}
PASSWORD=${DB_PASSWORD:-${MYSQL_PASSWORD:-}}
DATABASE=${DB_NAME:-${MYSQL_DATABASE:-database}}
PORT=${DB_PORT:-${MYSQL_TCP_PORT:-3306}}
WEBROOT=${LANDO_WEBROOT:-$(dirname $(dirname $(realpath $0)))}
DRUSHTASK=${DRUSH_TASK:-deploy}
TERMINUSENV=${TERMINUS_ENV:-dev}
# PARSE THE ARGZZ
# TODO: compress the mostly duplicate code below?
while (( "$#" )); do
case "$1" in
-h|--host|--host=*)
if [ "${1##--host=}" != "$1" ]; then
HOST="${1##--host=}"
shift
else
HOST=$2
shift 2
fi
;;
-u|--user|--user=*)
if [ "${1##--user=}" != "$1" ]; then
USER="${1##--user=}"
shift
else
USER=$2
shift 2
fi
;;
-p|--password|--password=*)
if [ "${1##--password=}" != "$1" ]; then
PASSWORD="${1##--password=}"
shift
else
PASSWORD=$2
shift 2
fi
;;
-d|--database|--database=*)
if [ "${1##--database=}" != "$1" ]; then
DATABASE="${1##--database=}"
shift
else
DATABASE=$2
shift 2
fi
;;
-P|--port|--port=*)
if [ "${1##--port=}" != "$1" ]; then
PORT="${1##--port=}"
shift
else
PORT=$2
shift 2
fi
;;
-D|--drush-task|--drush-task=*)
if [ "${1##--drush-task=}" != "$1" ]; then
DRUSHTASK="${1##--drush-task=}"
shift
else
DRUSHTASK=$2
shift 2
fi
;;
--)
shift
break
;;
-*|--*=)
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*)
FILE="$(pwd)/$1"
shift
;;
esac
done
echo "$FILE"
# Test if FILE is set if not download pantheon's production db to temp file.
if [ -z "$FILE" ]; then
# generate temp file
FILE=$(mktemp) || exit 1
# grab latest production db backup.
curl `terminus backup:get --element=db $TERMINUS_SITE.$TERMINUSENV` --output $FILE
function cleanup {
rm "$FILE"
}
trap cleanup EXIT
fi
# Set positional arguments in their proper place
eval set -- "$FILE"
CMD="$FILE"
PV=""
# Validate we have a file
if [ ! -f "$FILE" ]; then
echo "File $FILE not found!"
exit 1;
fi
# reset db back to default repo state
echo "Dropping and re-creating $DATABASE @ $HOST:$PORT as $USER..."
if [ ! -z "$PASSWORD" ]; then
eval "mysqladmin -h$HOST -u$USER -p$PASSWORD drop $DATABASE -f"
eval "mysqladmin -h$HOST -u$USER -p$PASSWORD create $DATABASE -f"
else
eval "mysqladmin -h$HOST -u$USER drop $DATABASE -f"
eval "mysqladmin -h$HOST -u$USER create $DATABASE -f"
fi
# Inform the user of things
echo "Preparing to import $FILE into $DATABASE on $HOST:$PORT as $USER..."
# Check to see if we have any unzipping options or GUI needs
if command -v gunzip >/dev/null 2>&1 && gunzip -t $FILE >/dev/null 2>&1; then
echo "Gunzipped file detected!"
if command -v pv >/dev/null 2>&1; then
CMD="pv $CMD"
else
CMD="cat $CMD"
fi
CMD="$CMD | gunzip"
elif command -v unzip >/dev/null 2>&1 && unzip -t $FILE >/dev/null 2>&1; then
echo "Zipped file detected!"
CMD="unzip -p $CMD"
if command -v pv >/dev/null 2>&1; then
CMD="$CMD | pv"
fi
else
if command -v pv >/dev/null 2>&1; then
CMD="pv $CMD"
else
CMD="cat $CMD"
fi
fi
# Put the pieces together
CMD="$CMD | mysql -h $HOST -P $PORT --protocol tcp -u $USER"
if [ ! -z "$PASSWORD" ]; then
CMD="$CMD -p$PASSWORD $DATABASE"
else
CMD="$CMD $DATABASE"
fi
# Import
echo "Importing $FILE..."
eval "$CMD"
echo "Import completed with status code $?"
# update db with latest code/config changes
echo "Running drush:$DRUSHTASK to update DB with latest configs and baseline migrations."
eval "cd $WEBROOT && drush $DRUSHTASK"
# In case the drush command generated a new log file as the current user, make sure httpd can write to it
chmod -R g+w web/sites/default/files/private/logs