Repository navigation
Expand file tree
/
Copy pathexodus
More file actions
executable file
·281 lines (232 loc) · 6.33 KB
/
Copy pathexodus
File metadata and controls
executable file
·281 lines (232 loc) · 6.33 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
#
# Writes a script to export your site to some Linux (e.g. Digital Ocean) context.
#
# DANGER: PROTECT RESULTING SCRIPT. Sensitive information such as the password
# for the Pantheon database are written in plain text in the script.
#
# REQUIREMENTS FOR SOURCE SCRIPT (running "exodus" script):
#
# - PHP
# - Terminus
#
#
# Usage:
#
# exodus PANTHEON_SITE [PANTHEON_SITE ...]
#
# Options:
#
# --base Base directory where site is located on target system
#
# --base-domain Base domain where to serve site from.
#
# --no-database Omit portion of output script that copies database
#
# --no-files Omit portion of output script that copies files
#
# --db-pw Include code in output script to write a settings.local.php file
#
# --output-file Filename to use to store output script. Defaults to 'export_script.sh'.
# Note that regardless of the valueo of this option, the ouput
# script is always printed to stdout AND written to a file.
#
SITES=""
BASE_DIR="/var/www"
BASE_DOMAIN="example.com"
HTTP_PORT=80
APACHE_LOG_DIR=/var/log/apache2
OUTPUT_FILE=export_script.sh
FILE_STORE="$HOME/tmp/rsync-files"
verbose=false
help=false
do_database=true
do_files=true
do_local_files_backup=false
do_apache=true
while [ $# -gt 0 ] ; do
option=$1
shift
case "$option" in
-v | --verbose )
verbose=true
;;
-h | --help )
help=true
;;
--base )
BASE_DIR="$1"
shift
;;
--base-domain )
BASE_DOMAIN="$1"
shift
;;
--no-database )
do_database=false
;;
--no-files )
do_files=false
;;
--local-files-backup )
do_local_files_backup=true
;;
--db-pw )
DB_PASSWORD="$1"
shift
;;
--output-file )
OUTPUT_FILE="$1"
shift
;;
-* )
echo "Unknown option $option"
exit 1
;;
*)
SITES="$SITES $option"
;;
esac
done
if $help ; then
cat $0 | sed -e '1,2d' -e '/^ *$/,$d' | sed -e 's/^#//' -e 's/^ //'
exit 0
fi
(
cat << __EOF__
#!/bin/bash
#
# This script was generated via:
#
# $0 $@
#
# See https://github.com/greg-1-anderson/utiliscripts/blob/master/exodus
#
__EOF__
if $do_database ; then
cat << __EOF__
#!/bin/bash
# IMPORTANT NOTE: Database info writting into this script are sensitive;
# do not distribute to untrusted parties.
#
# Also, Pantheon might migrate the database server at any time, which will
# invalidate the credentials cached here. Re-run the exodus script to update
# if that happens.
#
__EOF__
fi
echo
echo 'set -x'
echo
for site in $SITES ; do
shortname="$(echo $site | sed -e 's/-//g')"
env_id=live
PROJECT_DIR="$BASE_DIR/$shortname"
git_url="$(terminus connection:info $site.dev --field=git_url)"
mysql_command="$(terminus connection:info $site.$env_id --field=mysql_command)"
mysqldump_command="${mysql_command/mysql /mysqldump }"
site_id="$(terminus site:info $site --field=id)"
site_address="$env_id.$site_id@appserver.$env_id.$site_id.drush.in"
echo
echo "##"
echo "## $site"
echo "##"
# Database
if $do_database ; then
echo
echo "# Database"
echo "sudo mariadb -e 'create database if not exists $shortname'"
echo "$mysqldump_command > $shortname.sql"
echo "sudo bash -c 'mariadb $shortname < $shortname.sql'"
fi
# Code
echo
echo "# Code"
echo "if [[ ! -d $PROJECT_DIR/.git ]] ; then git clone $git_url '$PROJECT_DIR' ; else ( cd $PROJECT_DIR && git pull ) ; fi"
echo "if [[ ! -f $PROJECT_DIR/index.php ]] && [[ ! -f $PROJECT_DIR/web/index.php ]] ; then ( cd $PROJECT_DIR && composer install ) ; fi"
# Docroot
echo
echo "if [[ -d '$PROJECT_DIR/web' ]] ; then WEB_ROOT='$PROJECT_DIR/web'; else WEB_ROOT='$PROJECT_DIR'; fi"
# Files
if $do_files ; then
echo
echo "# Files"
echo "# Note that you might need to change the location of the files. Make sure the web server can write to this directory."
echo "rsync -rlIpzv $site_address:/files \$WEB_ROOT/sites/default"
fi
# Local files backup
if $do_local_files_backup ; then
echo "Backing up files for $shortname locally..."
mkdir -p $FILE_STORE/$shortname
terminus rsync $site.$env_id:/files $FILE_STORE/$shortname
fi
# Apache vhost
if $do_apache ; then
echo
echo "# Apache vhost (tbd)"
# Write an Apache http.conf HEREDOC into our generated script
cat << __EOF__
cat > /tmp/$shortname.conf << EOF
<VirtualHost *:$HTTP_PORT>
ServerAdmin webmaster@localhost
ServerName $site.$BASE_DOMAIN
ServerAlias www.$site.$BASE_DOMAIN
DocumentRoot \$WEB_ROOT
ErrorLog $APACHE_LOG_DIR/$shortname-error.log
CustomLog $APACHE_LOG_DIR/$shortname-access.log combined
<Directory \$WEB_ROOT>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
sudo mv -f /tmp/$shortname.conf /etc/apache2/sites-available/$shortname.conf
sudo a2ensite $shortname.conf
__EOF__
fi
# Drupal settings.local.php
if [[ -n "$DB_PASSWORD" ]] ; then
echo
echo "# settings.local.php"
# Write a Drupal local settings.php HEREDOC into our generated script
HASH_SALT="$(LC_ALL=C tr -dc "AHJ-NP-Za-km-np-z2-9" </dev/urandom | head -c 64)"
cat << __EOF__
cat > /tmp/$shortname.settings.php << 'EOF'
<?php
\$databases['default']['default'] = [
'database' => '$shortname',
'username' => 'www',
'password' => '$DB_PASSWORD',
'host' => 'localhost',
'port' => '3306',
'driver' => 'mysql',
'prefix' => '',
'collation' => 'utf8mb4_general_ci',
];
\$settings['hash_salt'] = '$HASH_SALT';
EOF
sudo mv -f /tmp/$shortname.settings.php \$WEB_ROOT/sites/default/settings.local.php
__EOF__
cat << __EOF__
if ! \$(grep -q settings.local \$WEB_ROOT/sites/default/settings.php) ; then
cat > /tmp/$shortname.include-local.php << 'EOF'
/**
* Inclue local settings file, if available.
*/
\$local_settings = __DIR__ . "/settings.local.php";
if (file_exists(\$local_settings)) {
include \$local_settings;
}
EOF
cat /tmp/$shortname.include-local.php | sudo tee -a \$WEB_ROOT/sites/default/settings.php
rm /tmp/$shortname.include-local.php
fi
__EOF__
fi
done
# Restart apache
echo
echo "# Resart apache"
echo "sudo systemctl reload apache2"
) | tee $OUTPUT_FILE