-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupbot
560 lines (498 loc) · 23.1 KB
/
backupbot
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/bin/sh
################################################################################
# Version 1.7.1-RELEASE (21-05-2023)
################################################################################
################################################################################
# Copyright 2019-2023 Nozel/Sebas Veeke. Licenced under a Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
#
# See https://creativecommons.org/licenses/by-nc-sa/4.0/
#
# Contact:
# > e-mail [email protected]
# > GitHub onnozel
################################################################################
################################################################################
# VARIABLES
################################################################################
# serverbot version
BACKUPBOT_VERSION='1.7.1'
# backupbot parameters can be configured in /usr/local/etc/backupbot.conf
# if the backupbot configuration file exists, it gets sourced by backupbot
if [ -f /usr/local/etc/backupbot.conf ]; then
# populate all usable variables to have defaults and check validity later on
BACKUPBOT_LOG='0'
RANDOM_DELAY='0'
AUTOMATIC_BACKUP_ENABLE='NO'
AUTOMATIC_BACKUP_DAILY='0'
AUTOMATIC_BACKUP_WEEKLY='0'
BACKUP_FILES_ENABLE='NO'
BACKUP_FILES_DESTINATION='0'
BACKUP_FILES_RETENTION_DAILY='0'
BACKUP_FILES_RETENTION_WEEKLY='0'
BACKUP_FILES_COMPRESSION='0'
BACKUP_FILES_ENCRYPTION='NO'
BACKUP_FILES_OWNER='0'
BACKUP_FILES_GROUP='0'
BACKUP_FILES_PERMISSIONS='0'
BACKUP_FILES='0'
BACKUP_FILES_PREFIX="$(date +%y%m%dT%H%M%S)"
BACKUP_MYSQL_ENABLE='NO'
BACKUP_MYSQL_DESTINATION='0'
BACKUP_MYSQL_RETENTION_DAILY='0'
BACKUP_MYSQL_RETENTION_WEEKLY='0'
BACKUP_MYSQL_COMPRESSION='0'
BACKUP_MYSQL_ENCRYPTION='NO'
BACKUP_MYSQL_OWNER='root'
BACKUP_MYSQL_GROUP='wheel'
BACKUP_MYSQL_PERMISSIONS='0'
BACKUP_MYSQL_PREFIX="$(date +%y%m%dT%H%M%S)"
ARGUMENT_VERSION='0'
ARGUMENT_HELP='0'
ARGUMENT_CRON='0'
ARGUMENT_BACKUP='0'
ARGUMENT_FILES='0'
ARGUMENT_MYSQL='0'
ARGUMENT_DAILY='0'
ARGUMENT_WEEKLY='0'
ARGUMENT_NONE='0'
# and source backupbot.conf
. /usr/local/etc/backupbot.conf
# populate configuration options with defaults when not provided by user
if [ "${BACKUPBOT_LOG}" = '0' ]; then
BACKUPBOT_LOG='/var/log/backupbot.log'
fi
if [ "${BACKUP_FILES_OWNER}" = '0' ]; then
BACKUP_FILES_OWNER='root'
fi
if [ "${BACKUP_FILES_GROUP}" = '0' ]; then
BACKUP_FILES_GROUP='wheel'
fi
if [ "${BACKUP_FILES_PERMISSIONS}" = '0' ]; then
BACKUP_FILES_PERMISSIONS='640'
fi
if [ "${BACKUP_MYSQL_OWNER}" = '0' ]; then
BACKUP_MYSQL_OWNER='root'
fi
if [ "${BACKUP_MYSQL_GROUP}" = '0' ]; then
BACKUP_MYSQL_GROUP='wheel'
fi
if [ "${BACKUP_MYSQL_PERMISSIONS}" = '0' ]; then
BACKUP_MYSQL_PERMISSIONS='640'
fi
else
# if backupbot.conf does not exist, return this error
printf 'backupbot: error: /usr/local/etc/backupbot.conf is required but cannot be found\n'
exit 1
fi
################################################################################
# ARGUMENTS
################################################################################
# enable arguments to backupbot
while test -n "$1"; do
case "$1" in
# options
--version)
ARGUMENT_VERSION='1'
shift
;;
--help|-help|help|--h|-h)
ARGUMENT_HELP='1'
shift
;;
--cron)
ARGUMENT_CRON='1'
shift
;;
# features
--backup|backup|-b)
ARGUMENT_BACKUP='1'
shift
;;
--files|files|-f)
ARGUMENT_FILES='1'
shift
;;
--mysql|mysql|-m)
ARGUMENT_MYSQL='1'
shift
;;
# other
--daily)
ARGUMENT_DAILY='1'
shift
;;
--weekly)
ARGUMENT_WEEKLY='1'
shift
;;
*)
ARGUMENT_NONE='1'
shift
;;
esac
done
################################################################################
# ERROR FUNCTIONS
################################################################################
# requirement errors
error_invalid_argument() {
printf 'backupbot: error: used argument is invalid, use "backuptbot --help" for proper usage\n'
exit 1
}
error_no_root_privileges() {
printf 'backupbot: error: used argument must be run with root privileges\n'
exit 1
}
error_os_not_supported() {
printf 'backupbot: operating system is not supported\n'
exit 1
}
# configuration errors
error_destination_not_configured() {
printf 'backupbot: error: BACKUP_*_DESTINATION is not configured properly in /usr/local/etc/backupbot.conf\n'
exit 1
}
error_source_not_configured() {
printf 'backupbot: error: BACKUP_FILES is not configured properly in /usr/local/etc/backupbot.conf\n'
exit 1
}
error_no_feature_configured() {
printf 'backupbot: error: no backup feature is enabled in /usr/local/etc/backupbot.conf\n'
exit 1
}
error_retention_not_integer() {
printf 'backupbot: error: BACKUP_FILES_RETENTION_* should be a positive number in /usr/local/etc/backupbot.conf\n'
exit 1
}
error_permissions_not_integer() {
printf 'backupbot: error: BACKUP_FILES_PERMISSIONS should be a valid unix file system permission (i.e. 640) in /usr/local/etc/backupbot.conf\n'
exit 1
}
error_feature_not_configured() {
printf 'backupbot: error: one of the invoked features is not configured in /usr/local/etc/backupbot.conf\n'
exit 1
}
error_gpg_not_installed() {
printf 'backupbot: error: gpg is required but not installed\nbackupbot: install gpg with "pkg install gnupg"\n'
exit 1
}
error_compression_not_configured() {
printf 'backupbot: error: BACKUP_FILES_COMPRESSION is not configured properly in /usr/local/etc/backupbot.conf\n'
exit 1
}
error_daily_backup_not_configured() {
printf 'backupbot: error: AUTOMATIC_BACKUP_DAILY is not configured properly in /usr/local/etc/backupbot.conf\n'
exit 1
}
################################################################################
# REQUIREMENT FUNCTIONS
################################################################################
requirement_argument_validity() {
# enabled backup features must have file destinations
if [ "${BACKUP_FILES_ENABLE}" = 'YES' ] && [ "${BACKUP_FILES_DESTINATION}" = '0' ]; then
error_destination_not_configured
fi
if [ "${BACKUP_MYSQL_ENABLE}" = 'YES' ] && [ "${BACKUP_MYSQL_DESTINATION}" = '0' ]; then
error_destination_not_configured
fi
# enabled backup features must have configured file sources
if [ "${BACKUP_FILES_ENABLE}" = 'YES' ] && [ "${BACKUP_FILES}" = '0' ]; then
error_source_not_configured
fi
# at least one backup feature must be enabled
if [ "${ARGUMENT_BACKUP}" = '1' ] || [ "${ARGUMENT_BACKUP}" = '1' ] || [ "${ARGUMENT_BACKUP}" = '1' ]; then
if [ "${BACKUP_FILES_ENABLE}" = 'NO' ] && [ "${BACKUP_MYSQL_ENABLE}" = 'NO' ]; then
error_no_feature_configured
fi
fi
# backup feature retention must be a integer
if ! [ "${BACKUP_FILES_RETENTION_DAILY}" -eq "${BACKUP_FILES_RETENTION_DAILY}" ] 2> /dev/null; then
error_retention_not_integer
fi
if ! [ "${BACKUP_FILES_RETENTION_WEEKLY}" -eq "${BACKUP_FILES_RETENTION_WEEKLY}" ] 2> /dev/null; then
error_retention_not_integer
fi
if ! [ "${BACKUP_MYSQL_RETENTION_DAILY}" -eq "${BACKUP_MYSQL_RETENTION_DAILY}" ] 2> /dev/null; then
error_retention_not_integer
fi
if ! [ "${BACKUP_MYSQL_RETENTION_WEEKLY}" -eq "${BACKUP_MYSQL_RETENTION_WEEKLY}" ] 2> /dev/null; then
error_retention_not_integer
fi
# backup permissions must be a integer
if ! [ "${BACKUP_FILES_PERMISSIONS}" -eq "${BACKUP_FILES_PERMISSIONS}" ] 2> /dev/null; then
error_permissions_not_integer
fi
if ! [ "${BACKUP_MYSQL_PERMISSIONS}" -eq "${BACKUP_MYSQL_PERMISSIONS}" ] 2> /dev/null; then
error_permissions_not_integer
fi
# compression number must be 0, 1, 2 or 3
if [ "${BACKUP_FILES_COMPRESSION}" != '0' ] && [ "${BACKUP_FILES_COMPRESSION}" != '1' ] && [ "${BACKUP_FILES_COMPRESSION}" != '2' ] && [ "${BACKUP_FILES_COMPRESSION}" != '3' ]; then
error_compression_not_configured
fi
if [ "${BACKUP_MYSQL_COMPRESSION}" != '0' ] && [ "${BACKUP_MYSQL_COMPRESSION}" != '1' ] && [ "${BACKUP_MYSQL_COMPRESSION}" != '2' ] && [ "${BACKUP_MYSQL_COMPRESSION}" != '3' ]; then
error_compression_not_configured
fi
# weekly and daily backup arguments can't be used at the same time
if [ "${ARGUMENT_DAILY}" = '1' ] && [ "${ARGUMENT_WEEKLY}" = '1' ]; then
error_invalid_argument
fi
}
requirement_root() {
# show error when backupbot isn't run with root privileges
if [ "$(id -u)" -ne '0' ]; then
error_no_root_privileges
fi
}
requirement_os() {
# show error when freebsd-version cannot be found
if [ ! "$(command -v freebsd-version)" ]; then
error_os_not_supported
fi
}
requirement_gpg() {
# show error when gpg cannot be found
if [ ! "$(command -v gpg)" ]; then
error_gpg_not_installed
fi
}
requirement_prevent_backup_overlap() {
# weekly and daily backup schedules will overlap for one day a week, this will prevent that from happening
if [ "${AUTOMATIC_BACKUP_ENABLE}" = 'YES' ]; then
if [ "${AUTOMATIC_BACKUP_DAILY}" -ge 0 ] && [ "${AUTOMATIC_BACKUP_DAILY}" -le 23 ]; then
if [ "${AUTOMATIC_BACKUP_WEEKLY}" -ge 1 ] && [ "${AUTOMATIC_BACKUP_WEEKLY}" -le 7 ]; then
if [ "${AUTOMATIC_BACKUP_WEEKLY}" -eq "$(date '+%u')" ]; then
if [ "${ARGUMENT_DAILY}" = '1' ]; then
exit 0
fi
fi
fi
fi
fi
}
################################################################################
# GENERAL FUNCTIONS
################################################################################
option_version() {
printf "backupbot version %s\n" "${BACKUPBOT_VERSION}"
printf "Copyright (C) 2019-2023 Nozel.\n"
printf "License CC Attribution-NonCommercial-ShareAlike 4.0 Int.\n\n"
printf "Written by Sebas Veeke\n"
}
option_help() {
printf "Usage:\n"
printf " backupbot [feature/option]...\n\n"
printf "Features:\n"
printf " -b, --backup Backup everything configured in configuration file\n"
printf " -f, --files Backup files configured in configuration file\n"
printf " -m, --mysql Backup MySQL databases configured in configuration file\n\n"
printf "Options:\n"
printf " --cron Effectuate cron changes from serverbot config\n"
printf " --help Display this help and exit\n"
printf " --version Display version information and exit\n"
}
option_cron() {
printf '[1] Removing old backupbot cronjob\n'
rm -f /etc/cron.d/backupbot
if [ "${AUTOMATIC_BACKUP_ENABLE}" = 'YES' ]; then
if [ "${AUTOMATIC_BACKUP_DAILY}" -ge 0 ] && [ "${AUTOMATIC_BACKUP_DAILY}" -le 23 ]; then
printf "[2] Adding daily scheduled backup on %s:00\n" "${AUTOMATIC_BACKUP_DAILY}"
printf "# This cronjob activates backupbot daily on the chosen schedule\n0 ${AUTOMATIC_BACKUP_DAILY} * * * root /usr/local/bin/backupbot --backup --daily\n\n" > /etc/cron.d/backupbot
fi
if [ "${AUTOMATIC_BACKUP_WEEKLY}" -ge 1 ] && [ "${AUTOMATIC_BACKUP_WEEKLY}" -le 7 ]; then
printf "[3] Adding weekly scheduled backup on day %s\n" "${AUTOMATIC_BACKUP_WEEKLY}"
printf "# This cronjob activates backupbot weekly on the chosen schedule\n0 ${AUTOMATIC_BACKUP_DAILY} * * ${AUTOMATIC_BACKUP_WEEKLY} root /usr/local/bin/backupbot --backup --weekly\n" >> /etc/cron.d/backupbot
fi
printf '\nAll done! Your new schedule can be found in /etc/cron.d/backupbot\n'
else
printf 'Automatic backup is disabled, skipping creation of cronjob\n'
fi
}
send_notice_to_log() {
DATE="$(date +%Y-%m-%d)"
TIME="$(date +%H:%M:%S)"
printf "[%s][%s][NOTICE]%s %s\n" "${DATE}" "${TIME}" "${1}" "${2}" >> "${BACKUPBOT_LOG}"
}
random_delay() {
# create random delay for automated tasks between 0 and 3600 seconds when configured
if [ "${RANDOM_DELAY}" = 'YES' ]; then
if [ "${ARGUMENT_DAILY}" = '1' ] || [ "${ARGUMENT_WEEKLY}" = '1' ]; then
RANDOM_DELAY="$(jot -r 1 0 3600)"
send_notice_to_log "[DELAY]" "Add random delay of ${RANDOM_DELAY} seconds before backup starts"
sleep "${RANDOM_DELAY}"
fi
fi
}
################################################################################
# FEATURE FUNCTIONS
################################################################################
# the following settings are used for tar:
# --create (-c) create new archive containing the specified items
# --preserve-permissions (-p) preserve file permissions
# --gzip (-z) compress the resulting archive with gunzip
# --bzip2 (-j) compress the resulting archive with bzip2
# --xz (-J) compress the resulting archive with xz
# --file - (-f) pipe the archive to next command
# --verbose (-v) produce verbose output (disabled by default)
# the following settings are used for gpg:
# --symmetric (-c) use symmetric cipher for encryption
# --cipher-algo AES256 use AES256 as cipher algorithm
# --batch non-interactive mode
# --passphrase use password provided in backupbot.conf
# --output (-o) write output to file
# the following settings are used for mysqldump
# --single-transaction dumps all tables in a single transaction
feature_files() {
if [ "${BACKUP_FILES_ENABLE}" = 'YES' ]; then
# change file name depending on whether its a manual, daily or weekly backup
if [ "${ARGUMENT_DAILY}" = '0' ] && [ "${ARGUMENT_WEEKLY}" = '0' ]; then
BACKUP_FILES_SUFFIX='files'
elif [ "${ARGUMENT_DAILY}" = '1' ] && [ "${ARGUMENT_WEEKLY}" = '0' ]; then
BACKUP_FILES_SUFFIX='daily-files'
elif [ "${ARGUMENT_DAILY}" = '0' ] && [ "${ARGUMENT_WEEKLY}" = '1' ]; then
BACKUP_FILES_SUFFIX='weekly-files'
fi
# check configured backup compression and create variables for arguments and file extension
if [ "${BACKUP_FILES_COMPRESSION}" = '0' ]; then
TAR_COMPRESSION_ARGUMENT=''
BACKUP_FILES_EXTENSION='tar'
elif [ "${BACKUP_FILES_COMPRESSION}" = '1' ]; then
TAR_COMPRESSION_ARGUMENT='--gzip'
BACKUP_FILES_EXTENSION='tar.gz'
elif [ "${BACKUP_FILES_COMPRESSION}" = '2' ]; then
TAR_COMPRESSION_ARGUMENT='--bzip2'
BACKUP_FILES_EXTENSION='tar.bz2'
elif [ "${BACKUP_FILES_COMPRESSION}" = '3' ]; then
TAR_COMPRESSION_ARGUMENT='--xz'
BACKUP_FILES_EXTENSION='tar.xz'
fi
# check whether backup encryption is enabled
if [ "${BACKUP_FILES_ENCRYPTION}" != 'NO' ]; then
requirement_gpg
send_notice_to_log "[FILES]" "Starting encrypted file backup for ${BACKUP_FILES}"
tar --create --preserve-permissions "${TAR_COMPRESSION_ARGUMENT}" --file - ${BACKUP_FILES} | gpg --symmetric --cipher-algo AES256 --batch --passphrase "${BACKUP_FILES_ENCRYPTION}" > "${BACKUP_FILES_DESTINATION}/${BACKUP_FILES_PREFIX}-${BACKUP_FILES_SUFFIX}.${BACKUP_FILES_EXTENSION}.gpg"
send_notice_to_log "[FILES]" "Finished encrypted file backup to ${BACKUP_FILES_DESTINATION}/${BACKUP_FILES_PREFIX}-${BACKUP_FILES_SUFFIX}.${BACKUP_FILES_EXTENSION}.gpg"
else
send_notice_to_log "[FILES]" "Starting file backup for ${BACKUP_FILES}"
tar --create --preserve-permissions "${TAR_COMPRESSION_ARGUMENT}" --file "${BACKUP_FILES_DESTINATION}/${BACKUP_FILES_PREFIX}-${BACKUP_FILES_SUFFIX}.${BACKUP_FILES_EXTENSION}" ${BACKUP_FILES}
send_notice_to_log "[FILES]" "Finished file backup to ${BACKUP_FILES_DESTINATION}/${BACKUP_FILES_PREFIX}-${BACKUP_FILES_SUFFIX}.${BACKUP_FILES_EXTENSION}"
fi
# change ownership of file backups
chown -R ${BACKUP_FILES_OWNER}:${BACKUP_FILES_GROUP} ${BACKUP_FILES_DESTINATION}/*-files.tar*
send_notice_to_log "[FILES]" "Updated ownership of file backups in ${BACKUP_FILES_DESTINATION} to ${BACKUP_FILES_OWNER}:${BACKUP_FILES_GROUP}"
# change permissions of file backups
chmod -R ${BACKUP_FILES_PERMISSIONS} ${BACKUP_FILES_DESTINATION}/*-files.tar*
send_notice_to_log "[FILES]" "Updated permissions of file backups in ${BACKUP_FILES_DESTINATION} to ${BACKUP_FILES_PERMISSIONS}"
# delete backups older than file retention, unless it's zero (infinite)
if [ "${BACKUP_FILES_RETENTION_DAILY}" -gt '0' ]; then
find "${BACKUP_FILES_DESTINATION}/" -type f -mtime +"${BACKUP_FILES_RETENTION_DAILY}" -name '*-daily-files*' -delete
send_notice_to_log "[FILES]" "Removed daily file backups older than ${BACKUP_FILES_RETENTION_DAILY} days from ${BACKUP_FILES_DESTINATION}"
fi
if [ "${BACKUP_FILES_RETENTION_WEEKLY}" -gt '0' ]; then
find "${BACKUP_FILES_DESTINATION}/" -type f -mtime +"${BACKUP_FILES_RETENTION_WEEKLY}" -name '*-weekly-files*' -delete
send_notice_to_log "[FILES]" "Removed weekly file backups older than ${BACKUP_FILES_RETENTION_WEEKLY} days from ${BACKUP_FILES_DESTINATION}"
fi
fi
}
feature_mysql() {
if [ "${BACKUP_MYSQL_ENABLE}" = 'YES' ]; then
# change file name depending on whether its a manual, daily or weekly backup
if [ "${ARGUMENT_DAILY}" = '0' ] && [ "${ARGUMENT_WEEKLY}" = '0' ]; then
BACKUP_MYSQL_SUFFIX='mysql'
elif [ "${ARGUMENT_DAILY}" = '1' ] && [ "${ARGUMENT_WEEKLY}" = '0' ]; then
BACKUP_MYSQL_SUFFIX='daily-mysql'
elif [ "${ARGUMENT_DAILY}" = '0' ] && [ "${ARGUMENT_WEEKLY}" = '1' ]; then
BACKUP_MYSQL_SUFFIX='weekly-mysql'
fi
# check configured backup compression and create variables for arguments and file extension
if [ "${BACKUP_MYSQL_COMPRESSION}" = '0' ]; then
MYSQL_COMPRESSION_ARGUMENT='cat'
BACKUP_MYSQL_EXTENSION='sql'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '1' ]; then
MYSQL_COMPRESSION_ARGUMENT='gzip'
BACKUP_MYSQL_EXTENSION='sql.gz'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '2' ]; then
MYSQL_COMPRESSION_ARGUMENT='bzip2'
BACKUP_MYSQL_EXTENSION='sql.bz2'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '3' ]; then
MYSQL_COMPRESSION_ARGUMENT='xz'
BACKUP_MYSQL_EXTENSION='sql.xz'
fi
# check whether backup encryption is enabled
if [ "${BACKUP_MYSQL_ENCRYPTION}" != 'NO' ]; then
requirement_gpg
# create a list of all user created mysql databases and mysqldump them to their own file
for DB in $(mysql -e 'show databases' -s --skip-column-names | sed '/performance_schema/d' | sed '/information_schema/d' | sed '/mysql/d'); do
send_notice_to_log "[MYSQL]" "Starting encrypted backup for database ${DB}"
mysqldump --single-transaction "${DB}" | ${MYSQL_COMPRESSION_ARGUMENT} | gpg --symmetric --cipher-algo AES256 --batch --passphrase "${BACKUP_MYSQL_ENCRYPTION}" > "${BACKUP_MYSQL_DESTINATION}/${BACKUP_MYSQL_PREFIX}-${BACKUP_MYSQL_SUFFIX}-${DB}.${BACKUP_MYSQL_EXTENSION}.gpg"
send_notice_to_log "[MYSQL]" "Finished encrypted backup for database ${DB} to ${BACKUP_MYSQL_DESTINATION}/${BACKUP_MYSQL_PREFIX}-${BACKUP_MYSQL_SUFFIX}-${DB}.${BACKUP_MYSQL_EXTENSION}.gpg"
done
else
# create a list of all user created mysql databases and mysqldump them to their own file
for DB in $(mysql -e 'show databases' -s --skip-column-names | sed '/performance_schema/d' | sed '/information_schema/d' | sed '/mysql/d'); do
send_notice_to_log "[MYSQL]" "Starting backup for database ${DB}"
mysqldump --single-transaction "${DB}" | ${MYSQL_COMPRESSION_ARGUMENT} > "${BACKUP_MYSQL_DESTINATION}/${BACKUP_MYSQL_PREFIX}-${BACKUP_MYSQL_SUFFIX}-${DB}.${BACKUP_MYSQL_EXTENSION}"
send_notice_to_log "[MYSQL]" "Finished backup for database ${DB} to ${BACKUP_MYSQL_DESTINATION}/${BACKUP_MYSQL_PREFIX}-${BACKUP_MYSQL_SUFFIX}-${DB}.${BACKUP_MYSQL_EXTENSION}"
done
fi
# change ownership of mysql backups
chown -R ${BACKUP_MYSQL_OWNER}:${BACKUP_MYSQL_GROUP} ${BACKUP_MYSQL_DESTINATION}/*-mysql-*
send_notice_to_log "[MYSQL]" "Updated ownership of mysql backups in ${BACKUP_MYSQL_DESTINATION} to ${BACKUP_MYSQL_OWNER}:${BACKUP_MYSQL_GROUP}"
# change permissions of mysql backups
chmod -R ${BACKUP_MYSQL_PERMISSIONS} ${BACKUP_MYSQL_DESTINATION}/*-mysql-*
send_notice_to_log "[MYSQL]" "Updated permissions of mysql backups in ${BACKUP_MYSQL_DESTINATION} to ${BACKUP_MYSQL_PERMISSIONS}"
# delete backups older than mysql retention, unless it's zero (infinite)
if [ "${BACKUP_MYSQL_RETENTION_DAILY}" -gt '0' ]; then
find "${BACKUP_MYSQL_DESTINATION}/" -type f -mtime +"${BACKUP_MYSQL_RETENTION_DAILY}" -name '*-daily-mysql*' -delete
send_notice_to_log "[MYSQL]" "Removed daily database backups older than ${BACKUP_MYSQL_RETENTION_DAILY} days from ${BACKUP_MYSQL_DESTINATION}"
fi
if [ "${BACKUP_MYSQL_RETENTION_WEEKLY}" -gt '0' ]; then
find "${BACKUP_MYSQL_DESTINATION}/" -type f -mtime +"${BACKUP_MYSQL_RETENTION_WEEKLY}" -name '*-weekly-mysql*' -delete
send_notice_to_log "[MYSQL]" "Removed weekly database backups older than ${BACKUP_MYSQL_RETENTION_WEEKLY} days from ${BACKUP_MYSQL_DESTINATION}"
fi
fi
}
################################################################################
# MAIN FUNCTION
################################################################################
backupbot_main() {
# check whether requirements are met
requirement_argument_validity
# call option based on arguments
if [ "${ARGUMENT_VERSION}" = '1' ]; then
option_version
exit 0
elif [ "${ARGUMENT_HELP}" = '1' ]; then
option_help
exit 0
elif [ "${ARGUMENT_CRON}" = '1' ]; then
requirement_root
requirement_os
option_cron
exit 0
# call feature based on arguments
elif [ "${ARGUMENT_BACKUP}" = '1' ]; then
requirement_root
requirement_os
requirement_prevent_backup_overlap
random_delay
feature_files
feature_mysql
exit 0
elif [ "${ARGUMENT_FILES}" = '1' ]; then
requirement_root
requirement_os
feature_files
exit 0
elif [ "${ARGUMENT_MYSQL}" = '1' ]; then
requirement_root
requirement_os
feature_mysql
exit 0
# return error on invalid argument
elif [ "${ARGUMENT_NONE}" = '1' ]; then
error_invalid_argument
fi
}
# call main function
backupbot_main