-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstaff_migrate.sh
More file actions
executable file
·634 lines (554 loc) · 17.9 KB
/
staff_migrate.sh
File metadata and controls
executable file
·634 lines (554 loc) · 17.9 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/usr/bin/env bash
#
# Notes:
# - This script can only be run by Creative Commons (CC) staff--it requires
# shell access to the production server
# - If you modify this file, please re-check it with shellcheck
# - '| xargs' is used to trim whitespace
set -o errexit
set -o errtrace
set -o nounset
# shellcheck disable=SC2154
trap '_es=${?};
printf "${0}: line ${LINENO}: \"${BASH_COMMAND}\"";
printf " exited with a status of ${_es}\n";
exit ${_es}' ERR
DOCKER_WP_DIR='/var/www/index'
DOCKER_WP_URL='http://localhost:8080'
# https://en.wikipedia.org/wiki/ANSI_escape_code
E0="$(printf "\e[0m")" # reset
E1="$(printf "\e[1m")" # bold
E30="$(printf "\e[30m")" # foreground: black
E31="$(printf "\e[31m")" # foreground: red
E33="$(printf "\e[33m")" # foreground: yellow
E90="$(printf "\e[90m")" # foreground: bright black (gray)
E92="$(printf "\e[92m")" # foreground: bright green
E93="$(printf "\e[93m")" # foreground: bright yellow
E97="$(printf "\e[97m")" # foreground: bright white
E43="$(printf "\e[43m")" # background: yellow
E47="$(printf "\e[47m")" # background: white
E100="$(printf "\e[100m")" # background: bright black (gray)
E107="$(printf "\e[107m")" # background: bright white
NOTICE_STAFF="\
⚠️ This script's pull command can only be run by Creative Commons (CC) team
members--it requires shell access to the legacy production server."
PROD_SERVER='index__prod'
PROD_UPLOADS_DIR='/var/www/index/wp-content/uploads'
PROD_WP_DIR='/var/www/index/wp'
PROD_WP_HOST='creativecommons.org'
PROD_WP_URL="https://${PROD_WP_HOST}"
declare -i RSYNC_PROT_VER_MIN=31
# NOTE: wordfence does not play nice with Docker. Enabling it results in WP-CLI
# commands taking approximately 13 times longer (ex. 10.8 seconds
# instead of 0.8 seconds)
PLUGINS_DEACTIVATE='
google-analytics-for-wordpress
wordfence'
SCRIPT_NAME="${0##*/}"
# The configure_environment() function sets the following global variables:
CACHE_SQL=''
CACHE_DIR=''
CACHE_UPLOADS_DIR=''
DOCKER_SQL=''
DOCKER_WP_UPLOADS_DIR=''
DOCKER_WP_UPLOADS_TEMP_DIR=''
SCRIPT_DIR="$(cd -P -- "${0%/*}" && pwd -P)"
SCRIPT_ENV=''
# Linux server unimplemented
#SERVER_DOCROOT=''
#SERVER_POD=''
SERVER_WP_DIR=''
SERVER_WP_UPLOADS_DIR=''
SERVER_WP_URL=''
# The parse_command() function sets the following global variables:
COMMAND=''
# The rsync_version() function sets the following global variables:
declare -i RSYNC_PROT_VER=0
#### FUNCTIONS ################################################################
bold() {
printf "${E1}%s${E0}\n" "${@}"
}
danger_confirm() {
local _confirm _i _prompt _rand
if [[ "${DANGER_BYPASS:-}" == 'i will be careful' ]] \
&& [[ "${SCRIPT_ENV}" == 'docker' ]]
then
return
fi
printf "${E43}${E30} %-71s$(date '+%T') ${E0}\n" \
'Confirmation required'
echo -e "${E33}WARNING:${E0} the '${COMMAND}' command is destructive"
# Loop until user enters random number
_rand=${RANDOM}${RANDOM}${RANDOM}
_rand=${_rand:0:4}
_prompt="Type the number, ${_rand}, to continue: "
_i=0
while read -p "${_prompt}" -r _confirm
do
if [[ "${_confirm}" == "${_rand}" ]]
then
echo
return
fi
(( _i > 1 )) && error_exit 'invalid confirmation number'
_i=$(( ++_i ))
done
if [[ "${SCRIPT_ENV}" == 'linux' ]] && [[ "${COMMAND}" == 'import' ]]
then
sudo_auth
fi
}
deactivate_plugins() {
local _bold _plugin _reset
print_header1 'Deactivate plugins'
for _plugin in ${PLUGINS_DEACTIVATE}
do
if wpcli --no-color --quiet plugin is-active "${_plugin}" &> /dev/null
then
wpcli plugin deactivate "${_plugin}"
else
no_op "${_plugin} is already inactive"
fi
done
echo
}
delete_wordpress_uploads() {
local _count
print_header1 'Delete WordPress uploads'
if [[ "${SCRIPT_ENV}" == 'docker' ]]
then
print_var DOCKER_WP_UPLOADS_DIR
# (xargs is used to trim whitespace)
_count=$(docker compose exec index-web \
sh -c "rm -frv ${DOCKER_WP_UPLOADS_DIR}/* | wc -l | xargs")
elif [[ "${SCRIPT_ENV}" == 'server' ]]
then
print_var SERVER_WP_UPLOADS_DIR
sudo_auth
# (xargs is used to trim whitespace)
_count=$(sudo rm -frv "${SERVER_WP_UPLOADS_DIR}/"* | wc -l | xargs)
fi
success "Directories/files removed: ${_count}"
echo
}
error_exit() {
# Echo error message and exit with error
echo -e "${E31}ERROR:${E0} ${*}" 1>&2
exit 1
}
import_database() {
local _sql
print_header1 'Import database'
if [[ "${SCRIPT_ENV}" == 'docker' ]]
then
print_var DOCKER_SQL
_sql="${DOCKER_SQL}"
elif [[ "${SCRIPT_ENV}" == 'server' ]]
then
print_var CACHE_SQL
_sql="${CACHE_SQL}"
fi
# https://developer.wordpress.org/cli/commands/db/import/
time wpcli db import "${_sql}"
echo
}
import_uploads() {
print_header1 'Import uploads'
print_var CACHE_UPLOADS_DIR
if [[ "${SCRIPT_ENV}" == 'docker' ]]
then
print_var DOCKER_WP_UPLOADS_DIR
echo 'Copy cache uploads to docker temp uploads dir'
docker compose cp "${CACHE_UPLOADS_DIR}" \
"index-web:${DOCKER_WP_UPLOADS_DIR}.temp" 2>/dev/null
echo 'Set ownership of temp uploads dir to www-data:wwww-data'
docker compose exec --user root index-web chown -R www-data:www-data \
"${DOCKER_WP_UPLOADS_TEMP_DIR}"
echo 'Replace uploads dir with temp uploads dir'
docker compose exec index-web mv "${DOCKER_WP_UPLOADS_DIR}" \
"${DOCKER_WP_UPLOADS_DIR}.old"
docker compose exec index-web mv "${DOCKER_WP_UPLOADS_DIR}.temp" \
"${DOCKER_WP_UPLOADS_DIR}"
docker compose exec index-web rm -rf "${DOCKER_WP_UPLOADS_DIR}.old"
elif [[ "${SCRIPT_ENV}" == 'server' ]]
then
print_var SERVER_WP_UPLOADS_DIR
sudo_auth
# (xargs is used to trim whitespace)
#_count=$(sudo rm -frv "${SERVER_WP_UPLOADS_DIR}/"* | wc -l | xargs)
fi
echo
}
no_op() {
# Print no-op message"
printf "${E90}no-op: %s${E0}\n" "${@}"
}
optimize_tables() {
print_header1 'Optimize WordPress database tables'
wpcli db optimize --color \
| sed -e'/Table does not support optimize/d' -e'/^status : OK/d'
echo
}
parse_command() {
if [[ -z "${1}" ]]
then
error_exit 'a COMMAND is required'
elif [[ -n "${2:-}" ]]
then
error_exit 'only a single COMMAND is allowed'
fi
case "${1:-}" in
-h*|--h*|h*|'-?'|'--?'|'?') COMMAND=help;;
info) COMMAND=info;;
pull) COMMAND=pull;;
import) COMMAND=import;;
*) error_exit "invalid COMMAND: ${1}";;
esac
}
post_import_db_update_site_url() {
local _new_url
print_header1 'Post-import: Database update: Site URL'
print_var PROD_WP_URL
if [[ "${SCRIPT_ENV}" == 'docker' ]]
then
_new_url="${DOCKER_WP_URL}"
print_var DOCKER_WP_URL
elif [[ "${SCRIPT_ENV}" == 'server' ]]
then
_new_url="${SERVER_WP_URL}"
print_var SERVER_WP_URL
fi
_site_url_replacements='
wp_options:option_value
wp_postmeta:meta_value
wp_posts:post_content
wp_posts:post_excerpt
wp_posts:guid
'
for _replacement in ${_site_url_replacements}
do
_table=${_replacement%:*}
_column=${_replacement#*:}
echo "${_table}: ${_column}"
# https://mariadb.com/kb/en/replace-function/
wpcli db query "
UPDATE ${_table}
SET ${_column} = REPLACE(
${_column}, '${PROD_WP_URL}', '${_new_url}'
)
WHERE ${_column} LIKE '%${PROD_WP_URL}%'
"
done
echo
}
print_key_val() {
printf "${E97}${E100}%24s${E0} %s\n" "${1}:" "${2}"
}
print_header1() {
# Print 80 character wide black on bright white heading
printf "${E30}${E107}# %-69s$(date '+%T') ${E0}\n" "${@}"
}
print_header2() {
# Print 80 character wide black on white heading
printf "${E30}${E47} %-78s ${E0}\n" "${@}"
}
print_var() {
print_key_val "${1}" "${!1}"
}
pull_database() {
print_header1 'Pull WordPress database'
print_var PROD_SERVER
print_var PROD_WP_DIR
print_var CACHE_SQL
# https://developer.wordpress.org/cli/commands/db/export/
# https://mariadb.com/kb/en/mariadb-dump/
ssh "${PROD_SERVER}" \
wp --path="${PROD_WP_DIR}" --url="${PROD_WP_URL}" db export \
--exclude_tables=wp_users,wp_usermeta --skip-lock-tables \
--single-transaction - \
> "${CACHE_SQL}.tmp"
mv "${CACHE_SQL}.tmp" "${CACHE_SQL}"
echo
}
pull_uploads() {
print_header1 'Pull WordPress uploads files from legacy production server'
print_var PROD_SERVER
print_var PROD_UPLOADS_DIR
print_var CACHE_DIR
# The rsync options below are ordered to match `man rsync`
rsync \
--recursive \
--links \
--delete \
--delete-excluded \
--partial \
--prune-empty-dirs \
--times \
--exclude .svn \
--exclude uploads/gravity_forms \
--exclude uploads/pum \
--stats \
--human-readable \
"${PROD_SERVER}:${PROD_UPLOADS_DIR}" \
"${CACHE_DIR}/"
echo
}
rsync_version() {
local _rsync_version
_rsync_version="$(rsync --version 2>&1 \
| awk '/version/ {print $3", "$4" "$5" "$6}')"
RSYNC_PROT_VER=${_rsync_version##* }
print_key_val 'rsync version' "${_rsync_version}"
}
script_setup() {
local _cache_dir_filesystem _err _rsync_ver _service _var
# Determine whether we working in a local macOS dev environment or on a
# linux server
case $(uname) in
Darwin)
print_header1 'Setup environment: local docker development'
CACHE_DIR=./cache
DOCKER_SQL="${DOCKER_WP_DIR}/cache/${PROD_WP_HOST}_export.sql"
DOCKER_WP_UPLOADS_DIR="${DOCKER_WP_DIR}/wp-content/uploads"
_var="${DOCKER_WP_DIR}/wp-content/uploads.temp"
DOCKER_WP_UPLOADS_TEMP_DIR="${_var}"
SCRIPT_ENV=docker
# Check execution environment
if [[ "${PWD##*/}" != 'index-dev-env' ]]
then
_err='this script must be executed from a clone of the'
_err="${_err} index-dev-env repository (this check requires"
_err="${_err} the current directory to me named"
_err="${_err} 'index-dev-env')"
error_exit "${_err}"
fi
mkdir -p "${CACHE_DIR}"
print_var COMMAND
print_var SCRIPT_ENV
echo
print_header2 'localhost'
print_key_val "$(sw_vers --productName) version" \
"$(sw_vers --productVersion)"
rsync_version
# Check rsync version
if (( RSYNC_PROT_VER < 31 ))
then
_err="rsync protocol version ${RSYNC_PROT_VER} is less than"
_err="${_err} ${RSYNC_PROT_VER_MIN}--please install via"
_err="${_err} \`brew install rsync\` (you may need to open a"
_err="${_err} new terminal to see new the rsync)"
error_exit "${_err}"
fi
echo
verify_docker_services
;;
Linux)
print_header1 'Setup environment: linux server'
#######################
no_op 'unimplemented' #
exit ##################
#######################
# SCRIPT_ENV=server
# print_var COMMAND
# print_var SCRIPT_ENV
# print_key_val 'Debian version' \
# "$(cat /etc/debian_version) ($(lsb_release -cs))"
# rsync_version
# print_key_val 'PHP version' \
# "$(php --version | awk '/^PHP/ {print $2}')"
#
#
# SERVER_POD="$(cat /etc/salt/minion_id)"
# SERVER_POD="${SERVER_POD#*__}"
# SERVER_POD="${SERVER_POD%__*}"
#
# SERVER_DOCROOT=/var/www/index
#
# case "${SERVER_POD}" in
# 'prod')
# SERVER_WP_URL=https://creativecommons.org
# ;;
# 'stage')
# SERVER_WP_URL=https://stage.creativecommons.org
# ;;
# esac
# SERVER_WP_DIR="${SERVER_DOCROOT}/wp"
# SERVER_WP_UPLOADS_DIR="${SERVER_DOCROOT}/wp-content/uploads"
# print_key_val 'WordPress version' "$(wpcli core version)"
# print_key_val 'WP-CLI version' \
# "$(wpcli cli version | cut -d' ' -f2)"
# echo
#
# CACHE_DIR="/var/www/cache-${USER}"
# _cache_dir_filesystem=$(df "${CACHE_DIR}" | awk 'END {print $1}')
# if [[ "${COMMAND}" != 'clean' ]] && [[ ! -d "${CACHE_DIR}" ]]
# then
# echo "CACHE_DIR: ${CACHE_DIR}"
# echo "CACHE_DIR filesystem: ${_cache_dir_filesystem}"
# echo
# sudo_auth
# echo 'Creating CACHE_DIR...'
# sudo mkdir "${CACHE_DIR}"
# sudo chown "${USER}:webdev" "${CACHE_DIR}"
# echo ' done.'
# fi
;;
esac
CACHE_UPLOADS_DIR="${CACHE_DIR}/uploads"
CACHE_SQL="${CACHE_DIR}/${PROD_WP_HOST}_export.sql"
staff_only_notice
}
show_help() {
print_header1 'Usage'
echo "${SCRIPT_NAME} COMMAND"
echo
bold 'Commands'
# help
echo 'help print this help message and exit'
echo 'info print setup information'
echo
# pull
echo -n 'pull pull WordPress database and uploads files from'
echo ' production server'
echo
# import
echo -n 'import import WordPress database and uploads files'
echo
exit
}
staff_only_notice() {
echo "${E93}${NOTICE_STAFF}${E0}"
echo
}
success() {
printf "${E92}Success:${E0} %s\n" "${@}"
}
sudo_auth() {
if ! sudo -nv 2>/dev/null
then
echo "Authorize sudo (update user's cached credentials)"
sudo -v
echo
fi
sudo -v
}
test_ssh_to_prod() {
print_header1 'Test SSH connection to production server'
print_var PROD_SERVER
if ! ssh "${PROD_SERVER}" true
then
error_exit 'unable to connect--verify config and public key'
else
success 'connection verified'
echo
fi
}
test_wordpress_installed() {
if ! wpcli --no-color --quiet core is-installed &> /dev/null
then
error_exit 'initial WordPress install has not been completed'
fi
}
verify_docker_services() {
local _msg
# Ensure docker daemon is running
if [[ ! -S /var/run/docker.sock ]]
then
error_exit 'docker daemon is not running'
fi
# Ensure db service is running
if ! docker compose exec index-db true 2>/dev/null
then
error_exit 'docker service is not running: index-db'
fi
print_header2 'index-db container - Database server for WordPress'
print_key_val 'Debian version' \
"$(docker compose exec --no-tty 'index-db' \
cat /etc/debian_version)"
print_key_val 'MariaDB version' \
"$(docker compose exec --no-tty 'index-db' \
mariadb --version \
| awk -F',' '{print $1}')"
echo
# Ensure web service are running
if ! docker compose exec index-web true 2>/dev/null
then
error_exit "docker service is not running: index-web"
fi
_msg='index-web container - Web server (WordPress and static HTML'
_msg="${_msg} components)"
print_header2 "${_msg}"
print_key_val 'Debian version' \
"$(docker compose exec --no-tty 'index-web' \
cat /etc/debian_version)"
print_key_val 'PHP version' \
"$(docker compose exec --no-tty 'index-web' \
/usr/bin/php --version \
| awk '/^PHP/ {print $2}')"
test_wordpress_installed
print_key_val 'WordPress version' "$(wpcli core version)"
print_key_val 'WP-CLI version' "$(wpcli cli version | cut -d' ' -f2)"
echo
}
wordpress_status() {
print_header1 'Show maintenance mode status to expose any PHP Warnings'
wpcli_loud maintenance-mode status
echo
}
wpcli() {
case "${SCRIPT_ENV}" in
'docker')
# Call WP-CLI with appropriate site arguments via Docker and
# silence warings
docker compose exec --no-tty index-web \
/usr/local/bin/wp \
--path="${DOCKER_WP_DIR}" \
--url="${DOCKER_WP_URL}" \
"${@}" 2> >(sed -e'/^PHP Warning:/d' -e'/^Warning:/d')
;;
'server')
# Call WP-CLI with appropriate site arguments
/usr/local/bin/wp \
--path="${SERVER_WP_DIR}" \
--url="${SERVER_WP_URL}" \
"${@}"
;;
esac
}
wpcli_loud() {
# Call WP-CLI with appropriate site arguments via Docker and colorize
# warnings
docker compose exec --no-tty index-web \
/usr/local/bin/wp \
--path="${DOCKER_WP_DIR}" \
--url="${DOCKER_WP_URL}" \
"${@}" 2> >(
sed -e"s/PHP Warning:/${E33}PHP Warning:${E0}/" \
-e"s/Warning:/${E33}Warning:${E0}/"
)
}
#### MAIN #####################################################################
cd "${SCRIPT_DIR}"
parse_command "${@:-}"
case "${COMMAND}" in
# the following are sorted by order of operations then lexicographically
'help') show_help;;
'info') script_setup;;
'pull')
script_setup
test_ssh_to_prod
pull_uploads
pull_database
;;
'import')
script_setup
danger_confirm
delete_wordpress_uploads
import_uploads
import_database
deactivate_plugins
post_import_db_update_site_url
optimize_tables
wordpress_status
;;
esac