-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti-sync.sh
225 lines (195 loc) · 6.64 KB
/
multi-sync.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
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
#!/bin/bash
# Syncing Trellis & Bedrock-based WordPress environments with WP-CLI aliases
# Version 1.2.0
# Copyright (c) Ben Word
# Requires: `jq` and new-ish bash (> 4)
# MacOS users:
# > `brew install jq bash`
# > `source ~/.zshrc`
basedir="$(dirname $(realpath ../composer.json))"
jsonpath="$basedir/scripts/sync.json"
uploads="$basedir/$(jq -r '.uploads' $jsonpath)"
function connection() {
echo $(jq -r ".connection | .$1" $jsonpath)
}
function site() {
echo $(jq -r ".sites | .[0] | .$1" $jsonpath)
}
function env() {
echo $(jq -r ".sites | .[$1] | .$2 | .$3" $jsonpath)
}
function protocol() {
result="$(jq -r ".secure | .$1" $jsonpath)"
if [[ "$result" == "true" ]]; then
echo "https://"
else
echo "http://"
fi
}
function replace() {
from=$1
to=$2
local=$3
id=0
for _x in $(jq -c ".sites | .[]" $jsonpath); do
echo "[@${to}] $(env $id $from) => $(env $id $to)";
echo "[@${to}] $(protocol $from)$(env $id $to) => $(protocol $to)$(env $id $to)";
if [[ "$local" == "true" ]]; then
wp search-replace "$(env $id $from)" "$(env $id $to)" --network;
if [[ "$(protocol $from)$(env $id $to)" != "$(protocol $to)$(env $id $to)" ]]
then
wp search-replace "$(protocol $from)$(env $id $to)" "$(protocol $to)$(env $id $to)" --network;
fi
else
wp "@${to}" search-replace "$(env $id $from)" "$(env $id $to)" --network;
if [[ "$(protocol $from)$(env $id $to)" != "$(protocol $to)$(env $id $to)" ]]
then
wp "@${to}" search-replace "$(protocol $from)$(env $id $to)" "$(protocol $to)$(env $id $to)" --network;
fi
fi;
((id++));
done
}
LOCAL=false
SKIP_DB=false
SKIP_ASSETS=false
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--skip-db)
SKIP_DB=true
shift
;;
--skip-assets)
SKIP_ASSETS=true
shift
;;
--local)
LOCAL=true
shift
;;
--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
if [ $# != 2 ]
then
echo "Usage: $0 [[--skip-db] [--skip-assets] [--local]] [ENV_FROM] [ENV_TO]"
exit;
fi
FROM=$1
TO=$2
bold=$(tput bold)
normal=$(tput sgr0)
case "$1-$2" in
production-development) DIR="down ⬇️ " FROMSITE=$(site production); FROMDIR=$(connection production); TOSITE=$(site development); TODIR=$uploads; ;;
staging-development) DIR="down ⬇️ " FROMSITE=$(site staging); FROMDIR=$(connection staging); TOSITE=$(site development); TODIR=$uploads; ;;
development-production) DIR="up ⬆️ " FROMSITE=$(site development); FROMDIR=$uploads; TOSITE=$(site production); TODIR=$(connection production); ;;
development-staging) DIR="up ⬆️ " FROMSITE=$(site development); FROMDIR=$uploads; TOSITE=$(site staging); TODIR=$(connection staging); ;;
production-staging) DIR="horizontally ↔️ "; FROMSITE=$(site production); FROMDIR=$(connection production); TOSITE=$(site staging); TODIR=$(connection staging); ;;
staging-production) DIR="horizontally ↔️ "; FROMSITE=$(site staging); FROMDIR=$(connection staging); TOSITE=$(site production); TODIR=$(connection production); ;;
*) echo "usage: $0 [[--skip-db] [--skip-assets] [--local]] production development | staging development | development staging | development production | staging production | production staging" && exit 1 ;;
esac
if [ "$SKIP_DB" = false ]
then
DB_MESSAGE=" - ${bold}reset the $TO database${normal} ($TOSITE)"
fi
if [ "$SKIP_ASSETS" = false ]
then
ASSETS_MESSAGE=" - sync ${bold}$DIR${normal} from $FROM ($FROMSITE)?"
fi
if [ "$SKIP_DB" = true ] && [ "$SKIP_ASSETS" = true ]
then
echo "Nothing to synchronize."
exit;
fi
echo
echo "Would you really like to "
echo $DB_MESSAGE
echo $ASSETS_MESSAGE
read -r -p " [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
# Change to site directory
cd ../ &&
echo
# Make sure both environments are available before we continue
availfrom() {
local AVAILFROM
if [[ "$LOCAL" = true && $FROM == "development" ]]; then
AVAILFROM=$(wp option get home 2>&1)
else
AVAILFROM=$(wp "@$FROM" option get home 2>&1)
fi
if [[ $AVAILFROM == *"Error"* ]]; then
echo "❌ Unable to connect to $FROM"
exit 1
else
echo "✅ Able to connect to $FROM"
fi
};
availfrom
availto() {
local AVAILTO
if [[ "$LOCAL" = true && $TO == "development" ]]; then
AVAILTO=$(wp option get home 2>&1)
else
AVAILTO=$(wp "@$TO" option get home 2>&1)
fi
if [[ $AVAILTO == *"Error"* ]]; then
echo "❌ Unable to connect to $TO $AVAILTO"
exit 1
else
echo "✅ Able to connect to $TO"
fi
};
availto
if [ "$SKIP_DB" = false ]
then
echo "Syncing database..."
# Export/import database, run search & replace
if [[ "$LOCAL" = true && $TO == "development" ]]; then
wp db export --default-character-set=utf8mb4 &&
wp db reset --yes &&
wp "@$FROM" db export --default-character-set=utf8mb4 - | wp db import -
replace $FROM $TO true
elif [[ "$LOCAL" = true && $FROM == "development" ]]; then
wp "@$TO" db export --default-character-set=utf8mb4 &&
wp "@$TO" db reset --yes &&
wp db export --default-character-set=utf8mb4 - | wp "@$TO" db import - &&
replace $FROM $TO
else
wp "@$TO" db export --default-character-set=utf8mb4 &&
wp "@$TO" db reset --yes &&
wp "@$FROM" db export --default-character-set=utf8mb4 - | wp "@$TO" db import - &&
replace $FROM $TO
fi
fi
if [ "$SKIP_ASSETS" = false ]
then
echo "Syncing assets..."
# Sync uploads directory
$(chmod -R 755 $uploads) &&
if [[ $DIR == "horizontally"* ]]; then
[[ $FROMDIR =~ ^(.*): ]] && FROMHOST=${BASH_REMATCH[1]}
[[ $FROMDIR =~ ^(.*):(.*)$ ]] && FROMDIR=${BASH_REMATCH[2]}
[[ $TODIR =~ ^(.*): ]] && TOHOST=${BASH_REMATCH[1]}
[[ $TODIR =~ ^(.*):(.*)$ ]] && TODIR=${BASH_REMATCH[2]}
ssh -o ForwardAgent=yes $FROMHOST "rsync -aze 'ssh -o StrictHostKeyChecking=no' --progress $FROMDIR $TOHOST:$TODIR"
else
rsync -az --progress "$FROMDIR" "$TODIR"
fi
fi
# Slack notification when sync direction is up or horizontal
# if [[ $DIR != "down"* ]]; then
# USER="$(git config user.name)"
# curl -X POST -H "Content-type: application/json" --data "{\"attachments\":[{\"fallback\": \"\",\"color\":\"#36a64f\",\"text\":\"🔄 Sync from ${FROMSITE} to ${TOSITE} by ${USER} complete \"}],\"channel\":\"#site\"}" https://hooks.slack.com/services/xx/xx/xx
# fi
echo -e "\n🔄 Sync from $FROM to $TO complete.\n\n ${bold}$TOSITE${normal}\n"
fi