-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync-kinsta.sh
executable file
·182 lines (157 loc) · 5.74 KB
/
sync-kinsta.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
#!/bin/bash
# Syncing Trellis & Bedrock-based WordPress environments with WP-CLI aliases (Kinsta version)
# Version 1.2.0
# Copyright (c) Ben Word
DEVDIR="web/app/uploads/"
DEVSITE="https://example.test"
DEVPORT="22"
REMOTEDIR="example@1.2.3.4:/www/example_123/public/shared/uploads/"
PRODPORT="12345"
PRODSITE="https://example.com"
STAGPORT="54321"
STAGSITE="https://staging-example.kinsta.cloud"
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=$PRODSITE; FROMDIR=$REMOTEDIR; FROMPORT=$PRODPORT; TOPORT=$DEVPORT; TOSITE=$DEVSITE; TODIR=$DEVDIR; ;;
staging-development) DIR="down ⬇️ " FROMSITE=$STAGSITE; FROMDIR=$REMOTEDIR; FROMPORT=$STAGPORT; TOPORT=$DEVPORT; TOSITE=$DEVSITE; TODIR=$DEVDIR; ;;
development-production) DIR="up ⬆️ " FROMSITE=$DEVSITE; FROMDIR=$DEVDIR; FROMPORT=$DEVPORT; TOPORT=$PRODPORT; TOSITE=$PRODSITE; TODIR=$REMOTEDIR; ;;
development-staging) DIR="up ⬆️ " FROMSITE=$DEVSITE; FROMDIR=$DEVDIR; FROMPORT=$DEVPORT; TOPORT=$STAGPORT; TOSITE=$STAGSITE; TODIR=$REMOTEDIR; ;;
production-staging) DIR="horizontally ↔️ "; FROMSITE=$PRODSITE; FROMDIR=$REMOTEDIR; FROMPORT=$PRODPORT; TOPORT=$STAGPORT; TOSITE=$STAGSITE; TODIR=$REMOTEDIR; ;;
staging-production) DIR="horizontally ↔️ "; FROMSITE=$STAGSITE; FROMDIR=$REMOTEDIR; FROMPORT=$STAGPORT; TOPORT=$PRODPORT; TOSITE=$PRODSITE; TODIR=$REMOTEDIR; ;;
*) 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 - &&
wp search-replace "$FROMSITE" "$TOSITE" --all-tables-with-prefix
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 - &&
wp "@$TO" search-replace "$FROMSITE" "$TOSITE" --all-tables-with-prefix
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 - &&
wp "@$TO" search-replace "$FROMSITE" "$TOSITE" --all-tables-with-prefix
fi
fi
if [ "$SKIP_ASSETS" = false ]
then
echo "Syncing assets..."
# Sync uploads directory
chmod -R 755 web/app/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 -p $FROMPORT -o ForwardAgent=yes $FROMHOST "rsync -aze 'ssh -o StrictHostKeyChecking=no -p $TOPORT' --progress $FROMDIR $TOHOST:$TODIR"
elif [[ $DIR == "down"* ]]; then
rsync -chavzP -e "ssh -p $FROMPORT" --progress "$FROMDIR" "$TODIR"
else
rsync -az -e "ssh -p $TOPORT" --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