-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwp-pull.sh
executable file
·177 lines (144 loc) · 4.95 KB
/
wp-pull.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
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
#!/bin/bash
set -o errexit
set -o errtrace
set -o nounset
trap '_es=${?};
printf "${0}: line ${LINENO}: \"${BASH_COMMAND}\"";
printf " exited with a status of ${_es}\n";
exit ${_es}' ERR
USAGE="\
Creative Commons Site Reliability Engineering WordPress Data Pull
[!] Destroys and replaces destination data
Arguments:
CONFIG_FILE
Configuration File Variables:
DEST_HOST Host on which to install WordPress data
DEST_DOMAIN Domain/Site URL of the destination WordPress
DEST_UPLOADS_DIR Absolute path of WordPress uploads directory on
destination host (will be destroyed and replaced)
DEST_WP_DIR Absolute path of WordPress directory (where to run
WP-CLI from)
SOURCE_HOST_REMOTE Host to pull WordPress data from
SOURCE_DOMAIN Domain/Site URL of the source WordPress
SOURCE_DB_FILE Absolute path of the database file (.sql.gz)
SOURCE_UPLOADS_FILE Absolute path of the uploads file (.tgz)
"
# Colors
DKG=$(printf '\e[90m')
INV=$(printf '\e[7m')
LTG=$(printf '\e[37m')
RED=$(printf '\e[91m')
RST=$(printf '\e[0m')
WHT=$(printf '\e[97m')
YLW=$(printf '\e[93m')
#### FUNCTIONS ################################################################
confirm_with_random_number() {
# Loop until user enters random number
_rand=${RANDOM}${RANDOM}${RANDOM}
_rand=${_rand:0:4}
_msg="Type the number, ${_rand}, to continue: "
_i=0
while read -p "${_msg}" _confirm
do
[[ "${_confirm}" == "${_rand}" ]] && return 0
(( _i > 1 )) && error_exit 'invalid confirmation number'
_i=$(( ++_i ))
done
}
create_dest_bin_dir () {
headerone 'DEST_HOST: Ensuring ~/bin/ directory exists'
ssh -q -t -F"${CONFIG_FILE}" wp-pull 'mkdir -p bin'
donezo
}
display_summary_and_confirm(){
local _confirm _msg _rand
local -i _i
headerone 'WordPress Data Pull Information'
echo "${DKG}DEST_DOMAIN:${RST} ${DEST_DOMAIN}"
echo "${DKG}DEST_UPLOADS_DIR:${RST} ${DEST_UPLOADS_DIR}"
echo "${DKG}DEST_WP_DIR:${RST} ${DEST_WP_DIR}"
echo
echo "${DKG}SOURCE_HOST_REMOTE:${RST} ${SOURCE_HOST_REMOTE}"
echo "${DKG}SOURCE_DOMAIN:${RST} ${SOURCE_DOMAIN}"
echo "${DKG}SOURCE_DB_FILE:${RST} ${SOURCE_DB_FILE}"
echo "${DKG}SOURCE_UPLOADS_FILE:${RST} ${SOURCE_UPLOADS_FILE}"
echo
echo
echo -n "${YLW}${INV}[!]${RST} ${YLW}Continuing will irrevocably destroy"
echo ' the WordPress data on the source host!'
echo -n ' Please review the information above to ensure it is correct.'
echo "${RST}"
echo
confirm_with_random_number
}
donezo() {
echo "${DKG}...done.${RST}"
echo
}
error_exit() {
# Display error message and exit
local _msg
local -i _es
_msg="${1}"
_es="${2:-1}"
echo -e "${RED}ERROR: ${_msg}${RST}" 1>&2
exit "${_es}"
}
execute_remote_script() {
headerone 'DEST_HOST: Executing remote script'
echo
ssh -q -t -F"${CONFIG_FILE}" wp-pull "
export DEST_DOMAIN=\"${DEST_DOMAIN}\"
export DEST_UPLOADS_DIR=\"${DEST_UPLOADS_DIR}\"
export DEST_TEMP_DIR=\"${DEST_TEMP_DIR}\"
export DEST_WP_DIR=\"${DEST_WP_DIR}\"
export SOURCE_HOST_REMOTE=\"${SOURCE_HOST_REMOTE}\"
export SOURCE_DOMAIN=\"${SOURCE_DOMAIN}\"
export SOURCE_DB_FILE=\"${SOURCE_DB_FILE}\"
export SOURCE_UPLOADS_FILE=\"${SOURCE_UPLOADS_FILE}\"
bin/wp-pull-remote.sh
"
}
extract_variables_from_config() {
local _f="${CONFIG_FILE}"
DEST_DOMAIN="$(awk '/^# DEST_DOMAIN:/ {print $3}' "${_f}")"
DEST_UPLOADS_DIR="$(awk '/^# DEST_UPLOADS_DIR:/ {print $3}' "${_f}")"
DEST_UPLOADS_DIR="${DEST_UPLOADS_DIR%/}"
DEST_TEMP_DIR="$(awk '/^# DEST_TEMP_DIR:/ {print $3}' "${_f}")"
DEST_TEMP_DIR="${DEST_TEMP_DIR%/}"
DEST_WP_DIR="$(awk '/^# DEST_WP_DIR:/ {print $3}' "${_f}")"
DEST_WP_DIR="${DEST_WP_DIR%/}"
SOURCE_HOST_REMOTE="$(awk '/^# SOURCE_HOST_REMOTE:/ {print $3}' "${_f}")"
SOURCE_DOMAIN="$(awk '/^# SOURCE_DOMAIN:/ {print $3}' "${_f}")"
SOURCE_DB_FILE="$(awk '/^# SOURCE_DB_FILE:/ {print $3}' "${_f}")"
SOURCE_UPLOADS_FILE="$(awk '/^# SOURCE_UPLOADS_FILE:/ {print $3}' "${_f}")"
}
headerone() {
printf '%s# %-77s%s' "${WHT}${INV}" "${1}" "${RST}"
echo
}
headertwo() {
printf '%s## %-76s%s' "${LTG}${INV}" "${1}" "${RST}"
echo
}
help_print() {
# Print help/usage, then exit (incorrect usage should exit 2)
local -i _es
_es="${1:-0}"
echo "${USAGE}"
exit "${_es}"
}
upload_remote_script() {
headerone 'DEST_HOST: Uploading remote script'
scp -F"${CONFIG_FILE}" bin/wp-pull-remote.sh wp-pull:bin/
echo
}
#### MAIN #####################################################################
#### Parse options
(( ${#} == 0 )) && help_print 2
CONFIG_FILE="${1}"
extract_variables_from_config
display_summary_and_confirm
create_dest_bin_dir
upload_remote_script
execute_remote_script