-
Notifications
You must be signed in to change notification settings - Fork 6
/
mgo-backup.sh
executable file
·384 lines (320 loc) · 8.62 KB
/
mgo-backup.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
#
# Script to backup Magento2 Codebase + Database
#
# @author Raj KB <magepsycho@gmail.com>
# @website http://www.magepsycho.com
# @version 0.1.0
# UnComment it if bash is lower than 4.x version
shopt -s extglob
################################################################################
# CORE FUNCTIONS - Do not edit
################################################################################
#
# VARIABLES
#
_bold=$(tput bold)
_underline=$(tput sgr 0 1)
_reset=$(tput sgr0)
_purple=$(tput setaf 171)
_red=$(tput setaf 1)
_green=$(tput setaf 76)
_tan=$(tput setaf 3)
_blue=$(tput setaf 38)
#
# HEADERS & LOGGING
#
function _debug()
{
if [[ "$DEBUG" = 1 ]]; then
"$@"
fi
}
function _header()
{
printf '\n%s%s========== %s ==========%s\n' "$_bold" "$_purple" "$@" "$_reset"
}
function _arrow()
{
printf '➜ %s\n' "$@"
}
function _success()
{
printf '%s✔ %s%s\n' "$_green" "$@" "$_reset"
}
function _error() {
printf '%s✖ %s%s\n' "$_red" "$@" "$_reset"
}
function _warning()
{
printf '%s➜ %s%s\n' "$_tan" "$@" "$_reset"
}
function _underline()
{
printf '%s%s%s%s\n' "$_underline" "$_bold" "$@" "$_reset"
}
function _bold()
{
printf '%s%s%s\n' "$_bold" "$@" "$_reset"
}
function _note()
{
printf '%s%s%sNote:%s %s%s%s\n' "$_underline" "$_bold" "$_blue" "$_reset" "$_blue" "$@" "$_reset"
}
function _die()
{
_error "$@"
exit 1
}
function _safeExit()
{
exit 0
}
#
# UTILITY HELPER
#
function _seekConfirmation()
{
printf '\n%s%s%s' "$_bold" "$@" "$_reset"
read -p " (y/n) " -n 1
printf '\n'
}
# Test whether the result of an 'ask' is a confirmation
function _isConfirmed()
{
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
return 0
fi
return 1
}
function _typeExists()
{
if type "$1" >/dev/null; then
return 0
fi
return 1
}
function _isOs()
{
if [[ "${OSTYPE}" == $1* ]]; then
return 0
fi
return 1
}
function _checkRootUser()
{
#if [ "$(id -u)" != "0" ]; then
if [ "$(whoami)" != 'root' ]; then
echo "You have no permission to run $0 as non-root user. Use sudo"
exit 1;
fi
}
function _printPoweredBy()
{
local mp_ascii
mp_ascii='
__ ___ ___ __
/ |/ /__ ____ ____ / _ \___ __ ______/ / ___
/ /|_/ / _ `/ _ `/ -_) ___(_-</ // / __/ _ \/ _ \
/_/ /_/\_,_/\_, /\__/_/ /___/\_, /\__/_//_/\___/
/___/ /___/
'
cat <<EOF
${_green}
Powered By:
$mp_ascii
>> Store: ${_reset}${_underline}${_blue}http://www.magepsycho.com${_reset}${_reset}${_green}
>> Blog: ${_reset}${_underline}${_blue}http://www.blog.magepsycho.com${_reset}${_reset}${_green}
################################################################
${_reset}
EOF
}
################################################################################
# SCRIPT FUNCTIONS
################################################################################
function _printUsage()
{
echo -n "$(basename "$0") [OPTION]...
Backup Magento2 Codebase + Database.
Version $VERSION
Options:
-sd, --src-dir Source directory (from where backup file will be created)
-dd, --dest-dir Destination directory (to where the backup file will be moved)
-bt, --type Backup Type. Default: all
Options:
1. db (for database only)
2. code (for codebase only)
3. all (for database + codebase)
-sm, --skip-media Skip media folder from code backup.
Default: 1
-h, --help Display this help and exit
-v, --version Output version information and exit
Examples:
$(basename "$0") --type=all --skip-media=1 --src-dir=... --dest-dir=...
"
_printPoweredBy
exit 1
}
function processArgs()
{
# Parse Arguments
for arg in "$@"
do
case $arg in
-bt=*|--type=*)
M2_BACKUP_TYPE="${arg#*=}"
;;
-sd=*|--src-dir=*)
M2_SRC_DIR="${arg#*=}"
;;
-dd=*|--dest-dir=*)
M2_DEST_DIR="${arg#*=}"
;;
-sm=*|--skip-media=*)
M2_SKIP_MEDIA="${arg#*=}"
;;
-bn=*|--backup-name=*)
M2_BACKUP_NAME="${arg#*=}"
;;
--debug)
DEBUG=1
;;
-h|--help)
_printUsage
;;
*)
_printUsage
;;
esac
done
validateArgs
sanitizeArgs
}
function validateArgs()
{
ERROR_COUNT=0
if [[ -z "$M2_BACKUP_TYPE" ]]; then
_error "Backup type parameter missing."
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
if [[ ! -z "$M2_BACKUP_TYPE" && "$M2_BACKUP_TYPE" != @(db|code|all) ]]; then
_error "Backup type must be one of db|code|all."
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
if [[ -z "$M2_SRC_DIR" ]]; then
_error "Source directory parameter missing."
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
if [[ ! -z "$M2_SRC_DIR" && ! -f "$M2_SRC_DIR/app/etc/env.php" ]]; then
_error "Source directory must be Magento 2 root folder."
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
if [[ -z "$M2_DEST_DIR" ]]; then
_error "Destination directory parameter missing."
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
if [[ ! -z "$M2_DEST_DIR" ]] && ! mkdir -p "$M2_DEST_DIR"; then
_error "Unable to create destination directory."
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
echo "$ERROR_COUNT"
[[ "$ERROR_COUNT" -gt 0 ]] && exit 1
}
function sanitizeArgs()
{
# remove trailing /
if [[ ! -z "$M2_SRC_DIR" ]]; then
M2_SRC_DIR="${M2_SRC_DIR%/}"
fi
if [[ ! -z "$M2_DEST_DIR" ]]; then
M2_DEST_DIR="${M2_DEST_DIR%/}"
fi
}
function prepareBackupName()
{
if [[ -z "$M2_BACKUP_NAME" ]]; then
#MD5=`echo \`date\` $RANDOM | md5sum | cut -d ' ' -f 1`
DATETIME=$(date +"%Y-%m-%d-%H-%M-%S")
M2_BACKUP_NAME="mage2-backup.$DATETIME"
fi
}
function prepareCodebaseFilename()
{
M2_CODE_BACKUP_FILE="${M2_DEST_DIR}/${M2_BACKUP_NAME}.tar.gz"
}
function prepareDatabaseFilename()
{
M2_DB_BACKUP_FILE="${M2_DEST_DIR}/${M2_BACKUP_NAME}.sql.gz"
}
function createDbBackup()
{
_success "Dumping MySQL..."
local host username password dbName
host=$(grep host "${M2_SRC_DIR}/app/etc/env.php" | cut -d "'" -f 4)
username=$(grep username "${M2_SRC_DIR}/app/etc/env.php" | cut -d "'" -f 4)
password=$(grep password "${M2_SRC_DIR}/app/etc/env.php" | cut -d "'" -f 4)
dbName=$(grep dbname "${M2_SRC_DIR}/app/etc/env.php" |cut -d "'" -f 4)
# @todo option to skip log tables
mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > "$M2_DB_BACKUP_FILE"
_success "Done!"
}
function createCodeBackup()
{
_success "Archiving Codebase..."
if [[ "$M2_SKIP_MEDIA" == 1 ]]; then
tar -zcf "$M2_CODE_BACKUP_FILE" --exclude="./var" --exclude="./pub/media" --exclude="./pub/static" -C "${M2_SRC_DIR}" .
else
tar -zcf "$M2_CODE_BACKUP_FILE" --exclude="./var" --exclude="./pub/static" -C "${M2_SRC_DIR}" .
fi
_success "Done!"
}
function printSuccessMessage()
{
_success "Magento2 Backup Completed!"
echo "################################################################"
echo ""
echo " >> Backup Type : ${M2_BACKUP_TYPE}"
echo " >> Backup Source : ${M2_SRC_DIR}"
if [[ $M2_BACKUP_TYPE = @(db|database|all) ]]; then
echo " >> Database Dump File : ${M2_DB_BACKUP_FILE}"
fi
if [[ $M2_BACKUP_TYPE = @(codebase|code|all) ]]; then
echo " >> Codebase Archive File : ${M2_CODE_BACKUP_FILE}"
fi
echo ""
echo "################################################################"
_printPoweredBy
}
################################################################################
# Main
################################################################################
export LC_CTYPE=C
export LANG=C
DEBUG=0
_debug set -x
VERSION="0.1.0"
M2_SRC_DIR=
M2_DEST_DIR=
M2_BACKUP_TYPE=all
M2_SKIP_MEDIA=1
M2_BACKUP_NAME=
M2_DB_BACKUP_FILE=
M2_CODE_BACKUP_FILE=
function main()
{
[[ $# -lt 1 ]] && _printUsage
processArgs "$@"
prepareBackupName
prepareCodebaseFilename
prepareDatabaseFilename
if [[ "$M2_BACKUP_TYPE" = @(database|db|all) ]]; then
createDbBackup
fi
if [[ "$M2_BACKUP_TYPE" = @(codebase|code|all) ]]; then
createCodeBackup
fi
printSuccessMessage
exit 0
}
main "$@"
_debug set +x