/
wp.sh
executable file
·203 lines (164 loc) · 4.44 KB
/
wp.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
#!/bin/bash
host=""
project_path=""
user=""
remote_hostname=""
local_hostname=""
remote_wpcli=""
db_filename="wp-db.sql"
dry_run="--dry-run"
set -e
function say_red {
echo -e "\033[31m>>> $@\033[0m"
}
function say_green {
echo -e "\033[32m>>> $@\033[0m"
}
function say_yellow {
echo -e "\033[33m>>> $@\033[0m"
}
function protect {
if [ -z "$dry_run" ]
then
$@
else
say_red "[dry run] $@"
fi
}
function run_local {
say_yellow "[local] $@"
$@
}
function run {
say_yellow "[remote] $@"
ssh ${user}@${host} "cd ${project_path} && $@"
}
function usage {
echo "Usage: ${0} cmd [--force]"
echo -e "\t cmd can be:"
echo -e "\t\t deploy"
echo -e "\t\t deploy_db"
echo -e "\t\t fetch"
echo -e "\t\t fetch_db"
echo -e "\t\t export_db"
echo -e "\t\t export_db_for_prod"
echo -e "\t\t import_db_from_prod dump.sql"
echo -e "\t\t sync_from_local"
exit 1
}
# parsing first argument (command)
if [ -z "$1" ]
then
usage
fi
CMD=$1
if [ "$2" == "--force" ]
then
dry_run=""
fi
source "`pwd`/wp.sh.config"
function deploy_files {
say_yellow "Rsyncing to remote"
exclude="--exclude=.git --exclude=wp-config.php --exclude=wp.sh.config --exclude=node_modules --exclude=.idea"
options="--no-owner --no-group --progress -crDpLt --force"
rsync ${dry_run} ${exclude} ${options} ./ ${user}@${host}:${project_path}
}
function sync_from_local {
say_yellow "Rsyncing to remote"
exclude="--exclude=.git --exclude=wp-config.php"
options="--no-owner --delete --no-group --progress -crDpLt --force"
rsync ${dry_run} ${exclude} ${options} ./ ${user}@${host}:${project_path}
}
function import_db_from_prod() {
say_yellow "Exporting database for production use"
echo $!
run_local wp db import $1
run_local wp search-replace ${remote_hostname} ${local_hostname}
run_local wp search-replace ${project_path} `pwd`
say_green "Database has been imported"
}
function export_db_for_prod {
say_yellow "Exporting database for production use"
run_local wp search-replace ${local_hostname} ${remote_hostname}
run_local wp search-replace `pwd` ${project_path}
run_local wp db export ${db_filename}
run_local wp db export ${db_filename}
sed -i '' 's/utf8mb4_unicode_520_ci/utf8mb4_unicode_ci/g' ${db_filename}
run_local wp search-replace ${remote_hostname} ${local_hostname}
run_local wp search-replace ${project_path} `pwd`
say_green "Database has been exported to ${db_filename}"
}
function deploy_db {
export_db_for_prod
say_yellow "Deploying database to remote server"
run_local scp ${db_filename} ${user}@${host}:${project_path}/${db_filename}
run php ${remote_wpcli} db import ${db_filename}
run_local wp search-replace ${remote_hostname} ${local_hostname}
run_local wp search-replace ${project_path} `pwd`
rm ${db_filename}
run rm ${db_filename}
}
function fetch_files {
say_yellow "Rsyncing from remote"
exclude="--exclude=.git --exclude=wp-config.php --exclude=wp.sh.config"
options="--no-owner --no-group --progress -crDpLt --force"
rsync ${dry_run} ${exclude} ${options} ${user}@${host}:${project_path}/ ./
}
function fetch_db {
say_yellow "Fetching database..."
run php ${remote_wpcli} db export ${db_filename}
run_local scp ${user}@${host}:$project_path/${db_filename} ${db_filename}
import_db_from_prod ${db_filename}
run rm ${db_filename}
}
function export_db {
say_yellow "Exporting database to ${db_filename}"
run_local wp search-replace ${local_hostname} ${remote_hostname}
run_local wp search-replace `pwd` ${project_path}
run_local wp db export ${db_filename}
sed -i '' 's/utf8mb4_unicode_520_ci/utf8mb4_unicode_ci/g' ${db_filename}
run_local wp search-replace ${remote_hostname} ${local_hostname}
run_local wp search-replace ${project_path} `pwd`
}
if [ "deploy" == "$CMD" ]
then
deploy_files
exit $?
fi
if [ "deploy_db" == "$CMD" ]
then
deploy_db
exit $?
fi
if [ "export_db" == "$CMD" ]
then
export_db
exit $?
fi
if [ "export_db_for_prod" == "$CMD" ]
then
export_db_for_prod
exit $?
fi
if [ "import_db_from_prod" == "$CMD" ]
then
import_db_from_prod "$2"
exit $?
fi
if [ "fetch" == "$CMD" ]
then
fetch_files
exit $?
fi
if [ "fetch_db" == "$CMD" ]
then
fetch_db
exit $?
fi
if [ "sync_from_local" == "$CMD" ]
then
sync_from_local
exit $?
fi
say_red "Unknown command"
exit 1