-
Notifications
You must be signed in to change notification settings - Fork 4
/
wordget.sh
executable file
·320 lines (302 loc) · 14.4 KB
/
wordget.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/bash
#------------------
# Download a Wordpress site + db into a virtualhost in MAMP
# George Tsarouchas
# Created December 2019 - updated July 2020
# IMPORTANT NOTE BEFORE USING
# add to your BIN path with
#ln -s /path_to_wordget/wordget.sh /usr/local/bin/wordget
# ------------------
#INITILIAZE THESE 2 variables
local_db_user='wp'
local_db_password='wp'
local_dev_env='default'
rsync_options='-arpz'
show_instructions(){
echo "WordGet v1.6.6"
echo "--------------------------------"
echo "(C) 2020-2021 Hellenic Technologies"
echo "https://hellenictechnologies.com"
echo ""
echo "Downloads all Wordpress website files and database and imports them into your local development enviroment"
echo ""
echo "USAGE:"
echo "wordget -h website_ipaddress -u website_username -s source_directory -t target_directory -d local_database_name -o exclude-uploads"
echo ""
echo "REQUIREMENTS:"
echo "Make sure that your SSH PUBLIC key is installed on the source server. "
echo "IMPORTANT: If the option -o localwp is going to be used, then wp-cli MUST be installed on the source server and wordget NEEDS to be run through 'Open Site Shell' inside LocalWP"
echo ""
echo "EXAMPLES:"
echo "1) Download the whole project into a LocalWP site"
echo "wordget -h 88.99.242.152 -u electropop -s /home/electropop/dev.electropop.gr/ -t ~/Sites/electropop/htdocs/ -o localwp,exclude-uploads"
echo ""
echo "2) Download files only without the database or the uploads folder"
echo "wordget -h 88.99.242.152 -u electropop -s /home/electropop/dev.electropop.gr/ -t ~/Sites/electropop/htdocs/ -o exclude-uploads"
echo ""
echo "3) Download all files and database in current folder"
echo "wordget -h 88.99.242.152 -u electropop -s /home/electropop/dev.electropop.gr/ -d mylocaldbname"
echo ""
echo "PARAMETERS:"
echo ""
echo -e "\t-h [WEBSITE HOST/IP ADDRESS]"
echo -e "\t-u [WEBSITE USERNAME]"
echo -e "\t-s [REMOTE DIRECTORY]"
echo -e "\t-t [LOCAL DIRECTORY]"
echo -e "\t-d [LOCAL DATABASE NAME]"
echo -e "\t-p [OPTIONAL SSH PORT NUMBER]"
echo -e "\t-o [OPTIONAL PARAMETERS - exclude-uploads (COMMA SEPARATED)]"
echo ""
exit 1
}
while getopts "h:u:s:t:d:p:o:" opt
do
case "$opt" in
h ) website_ipaddress=$OPTARG ;;
u ) website_username=$OPTARG ;;
s ) source_directory=$OPTARG ;;
t ) target_directory=$OPTARG ;;
d ) database_name=$OPTARG ;;
o ) extra_options=$OPTARG ;;
p ) port_number=$OPTARG ;;
? ) echo OPTARG; show_instructions ;;
esac
done
#Check if all parameters are given by user
if [ -z $website_ipaddress ] || [ -z $website_username ] || [ -z $source_directory ]
then
show_instructions
echo "You need to fill in all parameters";
fi
# Check for extra parameters
IFS=',' read -r -a array <<< "$extra_options"
for cmd_option in "${array[@]}"
do
if [ "$cmd_option" == "exclude-uploads" ]
then
exclude_uploads=1
fi
#use LocalWP as local development environment
if [ "$cmd_option" == "localwp" ] #TODO: needs to check SHELL env variables to detect it automatically
then
local_dev_env="localwp"
fi
#use LocalWP as LocalMode development environment
if [ "$cmd_option" == "localmode" ] #TODO: needs to check SHELL env variables to detect it automatically
then
local_dev_env="localmode"
#be quiet when in localmode
rsync_options="-qarpz"
fi
#Check if we are in a Vagrant VVV host
host_hostname="$(hostname)";
if [[ "$cmd_option" == "vvv" || "$host_hostname" == "vvv" ]] #TODO: needs to check SHELL env variables to detect it automatically
then
local_dev_env="vvv"
fi
done
#Confirmation prompt
echo ""
echo ""
if [ -z $target_directory ]
then
target_directory=$(pwd);
fi
if [ "$local_dev_env" == "localwp" ]
then
echo "LocalWP detected!";
echo "";
fi
if [ "$local_dev_env" == "vvv" ]
then
echo "VVV detected!";
echo "";
fi
#localmode runs without a prompt
if [ "$local_dev_env" != "localmode" ]
then
echo "From: ${website_username}@${website_ipaddress}."
echo "Remote Directory: ${source_directory}"
echo "into Local Directory: ${target_directory}"
if [ $exclude_uploads ]
then
echo "The uploads/ folder will not be downloaded. ";
fi
if [ $database_name ]
then
echo "The remote database will be downloaded and imported into your local database: $database_name.";
else
echo "The remote database will not be downloaded.";
fi
echo ""
read -p "Are you sure you want to continue? <y/N> " prompt
if [[ $prompt != "y" && $prompt != "Y" && $prompt != "yes" && $prompt != "Yes" ]]
then
exit 0
fi
fi
if [ -z $port_number ]
then
port_number=22;
fi
# What type of OS are we on?
host_uname="$(uname -s)"
case "${host_uname}" in
Linux*) host_os=Linux;;
Darwin*) host_os=Mac;;
CYGWIN*) host_os=Windows;;
MINGW*) host_os=Windows;;
*) host_os="UNKNOWN:${host_uname}"
esac
#Begin the process - We know enough
if [ "$local_dev_env" == "localwp" ]
then
#Get the env variables that the specific site has.
echo "Preparing Import for LocalWP";
#get the local site domain name
local_domain_url=$(wp option get siteurl)
if [ $database_name ]
then
#Find out the MYSQL Socket that LocalWP is using
mysql_socket=$(echo ${MYSQL_HOME//conf\//})"/mysqld.sock"
#echo "Mysql socket is: $mysql_socket";
#Get the remote site domain name
remote_domain_url=$(ssh $website_username@$website_ipaddress -p $port_number "cd $source_directory && wp option get siteurl")
echo "Remote URL is: $remote_domain_url";
echo "Local URL is: $local_domain_url";
echo "Fetching remote Database";
#ssh to server and wp export db local.sql
ssh $website_username@$website_ipaddress -p $port_number "cd $source_directory && wp db export local.sql --quiet --no-tablespaces=1 && gzip -c local.sql > local.sql.gz"
echo "Fetching Database";
#rsync the database
rsync -e "ssh -i ~/.ssh/id_rsa -q -p $port_number -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no" $rsync_options --progress $website_username@$website_ipaddress:$source_directory/local.sql.gz $target_directory/local.sql.gz
echo "Importing remote Database to LocalWP";
gzip -d local.sql.gz
#Import the remote DB to local DB
if [ "$host_os" == 'Windows' ];
then
#On Windows we need the mysql port
mysql_port=$(grep port $MYSQL_HOME/my.cnf | tail -c6)
wp db import local.sql --quiet --force --skip-optimization --port=$mysql_port
else
#On Linux/MacOS we need the socket
wp db import local.sql --quiet --force --skip-optimization --socket="$mysql_socket"
fi
wp search-replace "$remote_domain_url" "$local_domain_url" --quiet
# Cleaning up from Database fetch
#delete remote db download file
ssh $website_username@$website_ipaddress -p $port_number "cd $source_directory && rm local.sql.gz && rm local.sql"
#delete local db download file
rm local.sql
fi
#Get the remote files
echo "Downloading Website files..."
if [ $exclude_uploads ]
then
rsync -e "ssh -i ~/.ssh/id_rsa -q -p $port_number -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no" $rsync_options --exclude 'wp-config.php' --exclude 'wp-content/cache/*' --exclude 'wp-content/uploads/*' --progress $website_username@$website_ipaddress:$source_directory $target_directory
else
rsync -e "ssh -i ~/.ssh/id_rsa -q -p $port_number -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no" $rsync_options --exclude 'wp-config.php' --exclude 'wp-content/cache/*' --progress $website_username@$website_ipaddress:$source_directory $target_directory
fi
#if we are on Linux make the certificate is trusted and if the command mkcert exists in the PATH
if [ -x "$(command -v mkcert)" ] && [ "$host_os" == 'Linux' ];
then
local_domain_url_stripped=$(echo ${local_domain_url//https\:\/\//})
local_domain_url_stripped=$(echo ${local_domain_url_stripped//http\:\/\//})
mkcert $local_domain_url_stripped 2> /dev/null
mv $local_domain_url_stripped.pem ~/.config/Local/run/router/nginx/certs/$local_domain_url_stripped.crt
mv $local_domain_url_stripped-key.pem ~/.config/Local/run/router/nginx/certs/$local_domain_url_stripped.key
fi
#finalizations
#Import the remote DB to local DB
if [ "$host_os" == 'Windows' ];
then
#On Windows we need the mysql port
wp cache flush && wp rewrite flush && wp transient delete --all && wp db optimize --port=$mysql_port
wp db import local.sql --quiet --force --skip-optimization --port=$mysql_port
else
#On Linux/MacOS we need the socket
wp db import local.sql --quiet --force --skip-optimization --socket="$mysql_socket"
fi
#TODO: have a unique id so that mutliple users can download from the same site concurrently
elif [[ "$local_dev_env" == "vvv" || "$local_dev_env" == "localmode" ]]
then
#Get the env variables that the specific site has.
echo "Preparing Import for $local_dev_env";
#get the local site domain name
local_domain_url=$(wp option get siteurl)
if [ $database_name ]
then
#Get the remote site domain name
remote_domain_url=$(ssh $website_username@$website_ipaddress -p $port_number "cd $source_directory && wp option get siteurl")
echo "Remote URL is: $remote_domain_url";
echo "Local URL is: $local_domain_url";
echo "Fetching remote Database";
#ssh to server and wp export db local.sql
ssh $website_username@$website_ipaddress -p $port_number "cd $source_directory && wp db export local.sql --quiet --no-tablespaces=1 && gzip -c local.sql > local.sql.gz"
echo "Fetching Database";
#rsync the database
rsync -e "ssh -i ~/.ssh/id_rsa -q -p $port_number -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no" $rsync_options --progress $website_username@$website_ipaddress:$source_directory/local.sql.gz $target_directory/local.sql.gz
echo "Importing remote Database to $local_dev_env";
gzip -d local.sql.gz
#Import the remote DB to local DB
wp db import local.sql --quiet --force --skip-optimization
wp search-replace "$remote_domain_url" "$local_domain_url" --quiet
# Cleaning up from Database fetch
#delete remote db download file
ssh $website_username@$website_ipaddress -p $port_number "cd $source_directory && rm local.sql.gz && rm local.sql"
#delete local db download file
rm local.sql
fi
#Get the remote files
echo "Downloading Website files..."
if [ $exclude_uploads ]
then
rsync -e "ssh -i ~/.ssh/id_rsa -q -p $port_number -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no" $rsync_options --exclude 'wp-config.php' --exclude 'wp-content/cache/*' --exclude 'wp-content/uploads/*' --progress $website_username@$website_ipaddress:$source_directory $target_directory
else
rsync -e "ssh -i ~/.ssh/id_rsa -q -p $port_number -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no" $rsync_options --exclude 'wp-config.php' --exclude 'wp-content/cache/*' --progress $website_username@$website_ipaddress:$source_directory $target_directory
fi
#if we are on Linux make the certificate is trusted and if the command mkcert exists in the PATH
if [ -x "$(command -v mkcert)" ] && [ "$host_os" == 'Linux' ];
then
local_domain_url_stripped=$(echo ${local_domain_url//https\:\/\//})
local_domain_url_stripped=$(echo ${local_domain_url_stripped//http\:\/\//})
mkcert $local_domain_url_stripped 2> /dev/null
mv $local_domain_url_stripped.pem ~/.config/Local/run/router/nginx/certs/$local_domain_url_stripped.crt
mv $local_domain_url_stripped-key.pem ~/.config/Local/run/router/nginx/certs/$local_domain_url_stripped.key
fi
wp cache flush && wp rewrite flush && wp transient delete --all && wp db optimize
else
#Default Local development environment
echo "Downloading website files..."
#check if they want the uploads folder or not
if [ $exclude_uploads ]
then
rsync -e "ssh -i ~/.ssh/id_rsa -q -p $port_number -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no" $rsync_options --exclude 'wp-content/cache/*' --exclude 'wp-content/uploads/*' --progress $website_username@$website_ipaddress:$source_directory $target_directory
else
rsync -e "ssh -i ~/.ssh/id_rsa -q -p $port_number -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no" $rsync_options --exclude 'wp-content/cache/*' --progress $website_username@$website_ipaddress:$source_directory $target_directory
fi
if [ $database_name ]
then
echo "Downloading Database and Importing to local database $database_name"
WPDBNAME=`cat ${target_directory}/wp-config.php | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat ${target_directory}/wp-config.php | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat ${target_directory}/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
echo "remote DB credentials"
echo "D: $WPDBNAME";
echo "U: $WPDBUSER";
echo "P: $WPDBPASS";
#import the new one into $database_name
#TODO --compress and gzip
mysqldump --column-statistics=0 --no-tablespaces --quiet --add-drop-database -P 3306 --host=$website_ipaddress --user=$WPDBUSER --password=$WPDBPASS $WPDBNAME 2> /dev/null > ${database_name}_temp_db.sql && \
mysql --user=$local_db_user --password=$local_db_password --host=localhost -e "\
CREATE DATABASE IF NOT EXISTS ${database_name}; \
USE ${database_name}; \
source ${database_name}_temp_db.sql;" 2> /dev/null \
&& rm ${database_name}_temp_db.sql
#replace the wp-config password to connect to the database
sed -i -e "s|${WPDBNAME}|${database_name}|g" ${target_directory}/wp-config.php
sed -i -e "s|${WPDBUSER}|$local_db_user|g" ${target_directory}/wp-config.php
sed -i -e "s|${WPDBPASS}|${local_db_password}|g" ${target_directory}/wp-config.php
fi
fi