Skip to content

Commit 98b2a09

Browse files
committed
initial commit. works, but needs moar docs and multisite support
1 parent cf47406 commit 98b2a09

File tree

3 files changed

+421
-0
lines changed

3 files changed

+421
-0
lines changed

bin/wp-pull-remote.sh

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
#!/bin/bash
2+
#
3+
# Creative Commons WordPress Data Pull Remote Script
4+
#
5+
# This script is installed and invoked by wp-pull.sh. It should *not* be
6+
# invoked directly.
7+
#
8+
# For additional information, see:
9+
# TODO: add link
10+
set -o errexit
11+
set -o errtrace
12+
set -o nounset
13+
14+
trap '_es=${?};
15+
printf "${0}: line ${LINENO}: \"${BASH_COMMAND}\"";
16+
printf " exited with a status of ${_es}\n";
17+
exit ${_es}' ERR
18+
19+
# Colors
20+
DKG=$(printf '\e[90m')
21+
INV=$(printf '\e[7m')
22+
LTG=$(printf '\e[37m')
23+
RED=$(printf '\e[91m')
24+
RST=$(printf '\e[0m')
25+
WHT=$(printf '\e[97m')
26+
YLW=$(printf '\e[93m')
27+
28+
29+
#### FUNCTIONS ################################################################
30+
31+
32+
create_temp_dir() {
33+
headerone 'DEST_HOST: Creating temporary directory (TEMP_DIR)'
34+
TEMP_DIR=$(mktemp -d -p"${DEST_TEMP_DIR}")
35+
echo "${DKG}TEMP_DIR:${RST} ${TEMP_DIR}"
36+
echo
37+
}
38+
39+
40+
display_dest_host_info() {
41+
headerone 'DEST_HOST: Info / Connection Validation'
42+
echo -n "${DKG}LABEL:${RST} "
43+
if [[ -f /etc/salt/minion_id ]]
44+
then
45+
cat /etc/salt/minion_id
46+
else
47+
hostname -f
48+
fi
49+
echo -n "${DKG}SSH AGENT KEYS:${RST} "
50+
ssh-add -L
51+
echo
52+
}
53+
54+
55+
display_source_host_info() {
56+
headerone 'SOURCE_HOST: Info / Connection Validation'
57+
echo -n "${DKG}LABEL:${RST} "
58+
ssh -t -q -oProxyJump=${BASTION_HOST_REMOTE} ${SOURCE_HOST_REMOTE} '
59+
if [[ -f /etc/salt/minion_id ]]
60+
then
61+
cat /etc/salt/minion_id
62+
else
63+
hostname -f
64+
fi
65+
'
66+
echo
67+
}
68+
69+
70+
donezo() {
71+
echo "${DKG}... done.${RST}"
72+
echo
73+
}
74+
75+
76+
error_exit() {
77+
# Display error message and exit
78+
local _msg
79+
local -i _es
80+
_msg="${1}"
81+
_es="${2:-1}"
82+
echo -e "${RED}ERROR: ${_msg}${RST}" 1>&2
83+
exit "${_es}"
84+
}
85+
86+
87+
headerone() {
88+
printf '%s# %-77s%s' "${WHT}${INV}" "${1}" "${RST}"
89+
echo
90+
}
91+
92+
93+
headertwo() {
94+
printf '%s## %-76s%s' "${LTG}${INV}" "${1}" "${RST}"
95+
echo
96+
}
97+
98+
99+
import_database() {
100+
headerone "DEST_HOST: Importing database from PULLED_DB_FILE"
101+
echo "${DKG}PULLED_DB_FILE:${RST} ${PULLED_DB_FILE}"
102+
pushd ${DEST_WP_DIR} >/dev/null
103+
echo 'Decompressing PULLED_DB_FILE to stdout and piping to wp db import'
104+
zcat ${TEMP_DIR}/${SOURCE_DB_FILE##*/} | wp db import -
105+
popd >/dev/null
106+
echo
107+
}
108+
109+
110+
pull_data() {
111+
headerone 'DEST_HOST: Pulling data from SOURCE_HOST_REMOTE'
112+
scp -oProxyJump=${BASTION_HOST_REMOTE} \
113+
${SOURCE_HOST_REMOTE}:${SOURCE_DB_FILE} \
114+
${SOURCE_HOST_REMOTE}:${SOURCE_UPLOADS_FILE} \
115+
${TEMP_DIR}/
116+
PULLED_DB_FILE="${TEMP_DIR}/${SOURCE_DB_FILE##*/}"
117+
PULLED_UPLOADS_FILE="${TEMP_DIR}/${SOURCE_UPLOADS_FILE##*/}"
118+
echo
119+
}
120+
121+
122+
remove_temp_dir() {
123+
headerone 'DEST_HOST: Removing temporary directory (TEMP_DIR)'
124+
rm -rf "${TEMP_DIR}"
125+
donezo
126+
}
127+
128+
129+
replace_uploads_dir() {
130+
local _msg1 _msg2
131+
_msg1='DEST_HOST: Replacing DEST_UPLOADS_DIR with contents of'
132+
_msg2=' PULLED_UPLOADS_FILE'
133+
headerone "${_msg1}${_msg2}"
134+
echo "${DKG}PULLED_UPLOADS_FILE:${RST} ${PULLED_UPLOADS_FILE}"
135+
headertwo 'Removing old/existing upload data from DEST_UPLOADS_DIR'
136+
rm -rf "${DEST_UPLOADS_DIR}/"*
137+
donezo
138+
headertwo 'Extracting new upload data'
139+
tar -x -f "${PULLED_UPLOADS_FILE}" \
140+
--no-same-owner \
141+
--no-same-permissions \
142+
--directory="${DEST_UPLOADS_DIR}" \
143+
--strip-components=1
144+
donezo
145+
headertwo 'Correcting group ownership and permissions on new upload data'
146+
chgrp -R www-data "${DEST_UPLOADS_DIR}"/*
147+
find "${DEST_UPLOADS_DIR}"/* \( -type d -exec chmod 2775 {} + \) -o \
148+
\( -type f -exec chmod 0664 {} + \)
149+
donezo
150+
}
151+
152+
153+
update_urls_in_database() {
154+
headerone 'DEST_HOST: Updating URLs in database'
155+
echo "${DKG}SOURCE_URL:${RST} ${SOURCE_URL}"
156+
echo "${DKG}DEST_URL:${RST} ${DEST_URL}"
157+
echo
158+
pushd ${DEST_WP_DIR} >/dev/null
159+
headertwo 'Updating URLs in wp_options table'
160+
wp db query "
161+
UPDATE \`wp_options\`
162+
SET \`option_value\` = REPLACE( \`option_value\`, '${SOURCE_URL}',
163+
'${DEST_URL}' )
164+
WHERE \`option_name\` IN ('siteurl', 'home')
165+
;"
166+
donezo
167+
headertwo 'Updating URLs in wp_posts table'
168+
wp db query "
169+
UPDATE \`wp_posts\`
170+
SET \`post_content\` = REPLACE( \`post_content\`, '${SOURCE_URL}',
171+
'${DEST_URL}' )
172+
;"
173+
popd >/dev/null
174+
donezo
175+
}
176+
177+
178+
#### MAIN #####################################################################
179+
180+
display_dest_host_info
181+
display_source_host_info
182+
create_temp_dir
183+
pull_data
184+
import_database
185+
update_urls_in_database
186+
replace_uploads_dir
187+
remove_temp_dir
188+
189+
exit
190+
#### NOTES ####################################################################
191+
# TODO: update for multisite. note query below:
192+
cd /var/www/chapters/wp; wp db query 'SELECT * FROM wp_domain_mapping'
193+
+----+---------+-----------------------------+--------+
194+
| id | blog_id | domain | active |
195+
+----+---------+-----------------------------+--------+
196+
| 2 | 2 | au-beta.creativecommons.net | 1 |
197+
| 3 | 3 | ca-beta.creativecommons.net | 1 |
198+
| 4 | 4 | ke-beta.creativecommons.net | 1 |
199+
| 5 | 5 | mx-beta.creativecommons.net | 1 |
200+
| 6 | 6 | nl-beta.creativecommons.net | 1 |
201+
+----+---------+-----------------------------+--------+

config_examples/chapters__stage

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# vim: ft=sshconfig
2+
#
3+
# The following are parsed by wp-pull.sh:
4+
#
5+
# DEST_URL: stage.creativecommons.net
6+
# DEST_UPLOADS_DIR: /var/www/chapters/wp-content/uploads
7+
# DEST_TEMP_DIR: /var/www/chapters
8+
# DEST_WP_DIR: /var/www/chapters/wp
9+
# BASTION_HOST_REMOTE: 10.22.10.10
10+
# SOURCE_HOST_REMOTE: 10.22.10.14
11+
# SOURCE_URL: creativecommons.net
12+
# SOURCE_DB_FILE: /var/www/chapters/backup/now/db.sql.gz
13+
# SOURCE_UPLOADS_FILE: /var/www/chapters/backup/now/uploads.tgz
14+
15+
16+
#pushconfig
17+
Host bastion-us-east-2
18+
HostName bastion-us-east-2.creativecommons.org
19+
20+
21+
# The wp-pull HostName is the DEST_HOST
22+
Host wp-pull
23+
AddKeysToAgent yes
24+
ForwardAgent yes
25+
HostName 10.22.10.18
26+
ProxyJump bastion-us-east-2
27+
28+
29+
Host *
30+
ControlMaster auto
31+
ControlPath ~/.ssh/wp-pull_%r_%h
32+
ControlPersist 5m
33+
IdentityFile FILEPATH
34+
ServerAliveCountMax 60
35+
ServerAliveInterval 30
36+
TCPKeepAlive no
37+
User USERNAME

0 commit comments

Comments
 (0)