-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-backup
executable file
·318 lines (281 loc) · 8.04 KB
/
wp-backup
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
#!/usr/bin/env bash
# TO DO:
# USE FUNCTIONS BETTER
# OPTION TO USE ZIP FORMAT
set -e
set -o pipefail
# output all commands ran to console
# set -x
# Example usage:
# echo -e ${Red}This text will be red!${Reset}
Colors() {
Escape="\033";
# Reset
Reset="${Escape}[0m" # Text Reset
# Regular Colors
Black="${Escape}[0;30m" # Black
Red="${Escape}[0;31m" # Red
Green="${Escape}[0;32m" # Green
Yellow="${Escape}[0;33m" # Yellow
Blue="${Escape}[0;34m" # Blue
Purple="${Escape}[0;35m" # Purple
Cyan="${Escape}[0;36m" # Cyan
White="${Escape}[0;37m" # White
# Bold
BBlack="${Escape}[1;30m" # Black
BRed="${Escape}[1;31m" # Red
BGreen="${Escape}[1;32m" # Green
BYellow="${Escape}[1;33m" # Yellow
BBlue="${Escape}[1;34m" # Blue
BPurple="${Escape}[1;35m" # Purple
BCyan="${Escape}[1;36m" # Cyan
BWhite="${Escape}[1;37m" # White
# Underline
Underline="${Escape}[4m"
UBlack="${Escape}[4;30m" # Black
URed="${Escape}[4;31m" # Red
UGreen="${Escape}[4;32m" # Green
UYellow="${Escape}[4;33m" # Yellow
UBlue="${Escape}[4;34m" # Blue
UPurple="${Escape}[4;35m" # Purple
UCyan="${Escape}[4;36m" # Cyan
UWhite="${Escape}[4;37m" # White
# Background
On_Black="${Escape}[40m" # Black
On_Red="${Escape}[41m" # Red
On_Green="${Escape}[42m" # Green
On_Yellow="${Escape}[43m" # Yellow
On_Blue="${Escape}[44m" # Blue
On_Purple="${Escape}[45m" # Purple
On_Cyan="${Escape}[46m" # Cyan
On_White="${Escape}[47m" # White
# High Intensity
IBlack="${Escape}[0;90m" # Black
IRed="${Escape}[0;91m" # Red
IGreen="${Escape}[0;92m" # Green
IYellow="${Escape}[0;93m" # Yellow
IBlue="${Escape}[0;94m" # Blue
IPurple="${Escape}[0;95m" # Purple
ICyan="${Escape}[0;96m" # Cyan
IWhite="${Escape}[0;97m" # White
# Bold High Intensity
BIBlack="${Escape}[1;90m" # Black
BIRed="${Escape}[1;91m" # Red
BIGreen="${Escape}[1;92m" # Green
BIYellow="${Escape}[1;93m" # Yellow
BIBlue="${Escape}[1;94m" # Blue
BIPurple="${Escape}[1;95m" # Purple
BICyan="${Escape}[1;96m" # Cyan
BIWhite="${Escape}[1;97m" # White
# High Intensity backgrounds
On_IBlack="${Escape}[0;100m" # Black
On_IRed="${Escape}[0;101m" # Red
On_IGreen="${Escape}[0;102m" # Green
On_IYellow="${Escape}[0;103m" # Yellow
On_IBlue="${Escape}[0;104m" # Blue
On_IPurple="${Escape}[0;105m" # Purple
On_ICyan="${Escape}[0;106m" # Cyan
On_IWhite="${Escape}[0;107m" # White
}
Colors
function print_usage() {
echo
echo -e ${Purple}"NAME"${Reset}
echo
echo " wp-backup"
echo
echo -e ${Purple}"DESCRIPTION"${Reset}
echo
echo " Selectively backup WordPress host depending on option flag used."
echo
echo -e ${Purple}"SYNOPSYS"${Reset}
echo
echo " wp-backup --host <remote host> <flag(s)>"
echo
echo -e ${Purple}"FLAGS:"${Reset}
echo " --help Print this help message"
echo " --full, -f All of WordPress (minus uploads, cache and other wp-content directories), including database"
echo " --database, -d Database"
echo " --plugins, -p Plugins"
echo " --themes, -t Themes"
echo " --uploads, -u Uploads"
echo " --content, -c wp-content"
echo -e " --host, -h Remote host to backup from. Must (${RedF}both${Reset}) exist in ~/.wpcli/config.yml as an alias && ~/.ssh/config as a Host"
echo " --directory, -D Local directory to create back up files onto. If not set, will default to current directory."
echo
exit 1
}
[ $# -eq 0 ] && echo "This command requires an argument." >&2 && print_usage && exit 1
while [ ! $# -eq 0 ]
do
case "$1" in
--help )
print_usage
exit
;;
--full | -f)
wp_flag="true"
break
;;
--database | -d)
db_flag="true"
;;
--plugins | -p)
plugins_flag="true"
;;
--themes | -t)
themes_flag="true"
;;
--uploads | -u)
uploads_flag="true"
;;
--content | -c)
content_flag="true"
;;
--host | -h)
host=$2
;;
--directory | -D)
directory=$2
# Directory exists or exit
if [ ! -d "$directory" ]
then
echo -e ${BRed}"Directory $directory does not exists!"${Reset}
echo
while true
do
read -r -p "Would you like to create it? [Y/n] " input
echo
case $input in
[yY][eE][sS]|[yY]|"")
mkdir --parents $directory
echo -e ${Underline}"Created:${Reset} $directory"
break
;;
[nN][oO]|[nN])
echo "Suit yourself... Exiting!"
exit
;;
*)
echo "Invalid input..."
;;
esac
done
fi
# Verify trailing slash exists, if not add it.
[[ $directory != */ ]] && directory="$directory/"
;;
-*)
echo
echo -e ${Red}"$1${Reset} is not a valid argument!"
print_usage
exit 1
;;
esac
shift
done
[[ -z "$host" ]] && echo "This command must have at least one option flag followed by the remote host's name!" && print_usage
[[ -z "$directory" ]] && directory="$PWD/"
date=`date +%Y-%m-%d`
plugins="wp-content/plugins"
themes="wp-content/themes"
uploads="wp-content/uploads"
content="wp-content"
# Get blog's name
wp_blogname=$(wp @$host option get blogname)
# Remove spaces
blogname=${wp_blogname//[[:space:]]/}
# wp_abspath=/var/www/wp-config.php
wp_abspath="$(wp @$host config path)"
# wp_path=/var/www/
[[ $wp_abspath != *\wp-config.php ]] && echo ${Red}"Path looks bad: $wp_abspath"${Reset} && exit 1
wp_path=${wp_abspath//wp-config.php}
function maintenance_in {
echo
echo -e ${UYellow}"*** Entering maintenance mode ***"${Reset}
start=`date +%s`
# create .maintenance file in WordPress' root directory to put in maintenance mode
maintenance='<?php \n\$upgrading = time();'
ssh $host " echo -e \"$maintenance\" > \"$wp_path.maintenance\" "
}
function maintenance_out {
ssh $host rm -f $wp_path.maintenance
end=`date +%s`
echo
echo
echo -e ${UYellow}"*** Exiting maintenance mode after $((end-start)) seconds ***"${Reset}
echo
}
function backup_db {
echo -n "Database..."
wp @$host db export - > $directory$blogname-db-$date.sql
echo -e ${Green}"done!"${Reset}
}
function backup_plugins {
echo -n "Plugins..."
ssh $host -T "cd $wp_path; tar --ignore-failed-read \
-zcf $blogname-plugins-$date.tar.gz $plugins"
echo -e ${Green}"done!"${Reset}
}
function backup_themes {
echo -n "Themes..."
ssh $host -T "cd $wp_path; tar --ignore-failed-read \
-zcf $blogname-themes-$date.tar.gz $themes"
echo -e ${Green}"done!"${Reset}
}
function backup_uploads {
echo -n "Uploads..."
ssh $host -T "cd $wp_path; tar --ignore-failed-read \
-zcf $blogname-uploads-$date.tar.gz $uploads"
echo -e ${Green}"done!"${Reset}
}
function backup_content {
echo -n "Content..."
ssh $host -T "cd $wp_path; tar --ignore-failed-read \
-zcf $blogname-content-$date.tar.gz $content"
echo -e ${Green}"done!"${Reset}
}
function backup_wp {
echo -n "WordPress..."
ssh $host tar --ignore-failed-read \
--exclude="wp-snapshots" \
--exclude="wp-content/*backup*" \
--exclude="wp-content/cache" \
--exclude="wp-content/upgrade" \
--exclude="wp-content/uploads" \
--exclude="wp-content/updraft" \
-zcf $blogname-all-$date.tar.gz $wp_path
echo -e ${Green}"done!"${Reset}
}
function download {
echo -n "Downloading backups..."
rsync -az --info=progress2 --remove-source-files $host:$wp_path$blogname-*-$date.tar.gz $directory
}
function finish_message {
echo
echo -e ${Underline}"Finished backing up $wp_blogname to:"${Reset}
echo "$directory"
}
maintenance_in
echo
echo
echo -e ${Underline}"Backing up $wp_blogname:"${Reset}
echo
if [[ $wp_flag == "true" ]]; then
backup_wp
backup_db
maintenance_out
download
finish_message
exit
fi
[[ $db_flag == "true" ]] && backup_db
[[ $plugins_flag == "true" ]] && backup_plugins
[[ $themes_flag == "true" ]] && backup_themes
[[ $uploads_flag == "true" ]] && backup_uploads
[[ $content_flag == "true" ]] && backup_content
maintenance_out
# Check there is something to download
[[ -n "$db_flag" ]] || [[ -n "$plugins_flag" ]] || [[ -n "$themes_flag" ]] || [[ -n "$uploads_flag" ]] || [[ -n "$content_flag" ]] && download
finish_message
exit