-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackupbot
700 lines (638 loc) · 32.6 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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#!/bin/sh
################################################################################
# Version 1.8.1-RELEASE (06-12-2024)
################################################################################
#############################################################################
# SPDX-License-Identifier: Apache-2.0
#
# Copyright 2019 Sebas Veeke. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#############################################################################
#############################################################################
# Contact:
# > E-mail [email protected]
# > Website https://nozel.org
# > Codeberg https://codeberg.org/nozel/backupbot
#############################################################################
################################################################################
# PROGRAM VARIABLES
################################################################################
# backupbot version
BACKUPBOT_VERSION='1.8.1'
BACKUPBOT_VERSION_DATE='06-12-2024'
# file locations
BACKUPBOT_CONFIGURATION='/usr/local/etc/backupbot.conf'
BACKUPBOT_CRONFILE='/etc/cron.d/backupbot'
#############################################################################
# CONFIGURATION FILE HANDLING
#############################################################################
# check whether backupbot.conf is available and source it
if [ -f "${BACKUPBOT_CONFIGURATION}" ]; then
# populate all usable variables to have defaults and check validity later on
BACKUPBOT_LOG='0'
AUTOMATIC_BACKUP_ENABLE='0'
AUTOMATIC_BACKUP_DAILY='0'
AUTOMATIC_BACKUP_WEEKLY='0'
RANDOM_DELAY='0'
BACKUP_FILES_ENABLE='0'
BACKUP_FILES_DESTINATION='0'
BACKUP_FILES_RETENTION_DAILY='0'
BACKUP_FILES_RETENTION_WEEKLY='0'
BACKUP_FILES_COMPRESSION='0'
BACKUP_FILES_ENCRYPTION='0'
BACKUP_FILES_SPLIT='0'
BACKUP_FILES_OWNER='0'
BACKUP_FILES_GROUP='0'
BACKUP_FILES_PERMISSIONS='0'
BACKUP_FILES='0'
BACKUP_FILES_PREFIX='0'
BACKUP_MYSQL_ENABLE='0'
BACKUP_MYSQL_DESTINATION='0'
BACKUP_MYSQL_RETENTION_DAILY='0'
BACKUP_MYSQL_RETENTION_WEEKLY='0'
BACKUP_MYSQL_COMPRESSION='0'
BACKUP_MYSQL_ENCRYPTION='0'
BACKUP_MYSQL_OWNER='0'
BACKUP_MYSQL_GROUP='0'
BACKUP_MYSQL_PERMISSIONS='0'
BACKUP_MYSQL_PREFIX='0'
# and source backupbot.conf
. "${BACKUPBOT_CONFIGURATION}"
# check whether backupbot.conf configuration parameters are used
# general settings
if [ "${BACKUPBOT_LOG}" = '0' ]; then
BACKUPBOT_LOG='/var/log/backupbot.log'
fi
# automatic backup settings
if [ "${AUTOMATIC_BACKUP_ENABLE}" = '0' ]; then
AUTOMATIC_BACKUP_ENABLE='NO'
fi
if [ "${RANDOM_DELAY}" = '0' ]; then
RANDOM_DELAY='NO'
fi
# feature backup settings
if [ "${BACKUP_FILES_ENABLE}" = '0' ]; then
BACKUP_FILES_ENABLE='NO'
fi
if [ "${BACKUP_FILES_ENCRYPTION}" = '0' ]; then
BACKUP_FILES_ENCRYPTION='NO'
fi
if [ "${BACKUP_FILES_SPLIT}" = '0' ]; then
BACKUP_FILES_SPLIT='NO'
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_FILES}" = '0' ]; then
BACKUP_FILES='0'
fi
if [ "${BACKUP_FILES_PREFIX}" = '0' ]; then
BACKUP_FILES_PREFIX="$(date +%y%m%dT%H%M%S)"
fi
# feature mysql settings
if [ "${BACKUP_MYSQL_ENABLE}" = '0' ]; then
BACKUP_MYSQL_ENABLE='NO'
fi
if [ "${BACKUP_MYSQL_ENCRYPTION}" = '0' ]; then
BACKUP_MYSQL_ENCRYPTION='NO'
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
if [ "${BACKUP_MYSQL_PREFIX}" = '0' ]; then
BACKUP_MYSQL_PREFIX="$(date +%y%m%dT%H%M%S)"
fi
else
# if backupbot.conf does not exist, return this error
printf "%s is required but cannot be found" "${BACKUPBOT_CONFIGURATION}"
fi
####################################################################################################
# ARGUMENT HANDLING
####################################################################################################
# populate validation variables with zeros for validation
ARGUMENT_VERSION='0'
ARGUMENT_HELP='0'
ARGUMENT_CRON='0'
ARGUMENT_BACKUP='0'
ARGUMENT_FILES='0'
ARGUMENT_MYSQL='0'
ARGUMENT_ZFS='0'
ARGUMENT_HOURLY='0'
ARGUMENT_DAILY='0'
ARGUMENT_WEEKLY='0'
ARGUMENT_NONE='0'
# read arguments
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
;;
--zfs|zfs|-z)
ARGUMENT_ZFS='1'
shift
;;
# other
--hourly)
ARGUMENT_HOURLY='1'
shift
;;
--daily)
ARGUMENT_DAILY='1'
shift
;;
--weekly)
ARGUMENT_WEEKLY='1'
shift
;;
*)
ARGUMENT_NONE='1'
shift
;;
esac
done
####################################################################################################
# ERROR AND LOGGING FUNCTIONS
####################################################################################################
program_error() {
printf "backupbot: error: %s %s %s\n" "${1}" "${2}" "${3}"
exit 1
}
send_notice_to_customlog() {
DATE="$(date +%Y-%m-%d)"
TIME="$(date +%H:%M:%S)"
printf "[%s][%s][NOTICE]%s %s\n" "${DATE}" "${TIME}" "${1}" "${2}" >> "${BACKUPBOT_LOG}"
}
send_notice_to_logger() {
logger -t backupbot "${1}" "${2}"
}
################################################################################
# REQUIREMENT AND VALIDATION FUNCTIONS
################################################################################
requirement_root() {
# show error when backupbot isn't run with root privileges
if [ "$(id -u)" -ne '0' ]; then
program_error 'this feature must be run with root privileges'
fi
}
requirement_os() {
# show error when freebsd-version cannot be found
if [ ! "$(command -v freebsd-version)" ]; then
program_error 'operating system is not supported'
fi
}
requirement_gpg() {
# show error when gpg cannot be found
if [ ! "$(command -v gpg)" ]; then
program_error 'gpg is required but not installed, install gpg with "pkg install gnupg"'
fi
}
requirement_argument_validity() {
# enabled backup features must have file destinations
if [ "${BACKUP_FILES_ENABLE}" = 'YES' ] && [ "${BACKUP_FILES_DESTINATION}" = '0' ]; then
program_error "BACKUP_*_DESTINATION is not configured properly in ${BACKUPBOT_CONFIGURATION}"
fi
if [ "${BACKUP_MYSQL_ENABLE}" = 'YES' ] && [ "${BACKUP_MYSQL_DESTINATION}" = '0' ]; then
program_error "BACKUP_*_DESTINATION is not configured properly in ${BACKUPBOT_CONFIGURATION}"
fi
# enabled backup features must have configured file sources
if [ "${BACKUP_FILES_ENABLE}" = 'YES' ] && [ "${BACKUP_FILES}" = '0' ]; then
program_error "BACKUP_FILES is not configured properly in ${BACKUPBOT_CONFIGURATION}"
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
program_error "no backup feature is enabled in ${BACKUPBOT_CONFIGURATION}"
fi
fi
# backup feature retention must be a integer
if ! [ "${BACKUP_FILES_RETENTION_DAILY}" -eq "${BACKUP_FILES_RETENTION_DAILY}" ] 2> /dev/null; then
program_error "BACKUP_FILES_RETENTION_* should be a positive number in ${BACKUPBOT_CONFIGURATION}"
fi
if ! [ "${BACKUP_FILES_RETENTION_WEEKLY}" -eq "${BACKUP_FILES_RETENTION_WEEKLY}" ] 2> /dev/null; then
program_error "BACKUP_FILES_RETENTION_* should be a positive number in ${BACKUPBOT_CONFIGURATION}"
fi
if ! [ "${BACKUP_MYSQL_RETENTION_DAILY}" -eq "${BACKUP_MYSQL_RETENTION_DAILY}" ] 2> /dev/null; then
program_error "BACKUP_FILES_RETENTION_* should be a positive number in ${BACKUPBOT_CONFIGURATION}"
fi
if ! [ "${BACKUP_MYSQL_RETENTION_WEEKLY}" -eq "${BACKUP_MYSQL_RETENTION_WEEKLY}" ] 2> /dev/null; then
program_error "BACKUP_FILES_RETENTION_* should be a positive number in ${BACKUPBOT_CONFIGURATION}"
fi
# backup permissions must be a integer
if ! [ "${BACKUP_FILES_PERMISSIONS}" -eq "${BACKUP_FILES_PERMISSIONS}" ] 2> /dev/null; then
program_error "BACKUP_FILES_PERMISSIONS should be a valid unix file system permission (i.e. 640) in ${BACKUPBOT_CONFIGURATION}"
fi
if ! [ "${BACKUP_MYSQL_PERMISSIONS}" -eq "${BACKUP_MYSQL_PERMISSIONS}" ] 2> /dev/null; then
program_error "BACKUP_FILES_PERMISSIONS should be a valid unix file system permission (i.e. 640) in ${BACKUPBOT_CONFIGURATION}"
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' ] && \
[ "${BACKUP_FILES_COMPRESSION}" != '4' ] && [ "${BACKUP_FILES_COMPRESSION}" != '5' ] && \
[ "${BACKUP_FILES_COMPRESSION}" != '6' ] && [ "${BACKUP_FILES_COMPRESSION}" != '7' ] && \
[ "${BACKUP_FILES_COMPRESSION}" != '8' ] && [ "${BACKUP_FILES_COMPRESSION}" != '101' ] && \
[ "${BACKUP_FILES_COMPRESSION}" != '102' ] && [ "${BACKUP_FILES_COMPRESSION}" != '107' ]; then
program_error "BACKUP_FILES_COMPRESSION is not configured properly in ${BACKUPBOT_CONFIGURATION}"
fi
if [ "${BACKUP_MYSQL_COMPRESSION}" != '0' ] && [ "${BACKUP_MYSQL_COMPRESSION}" != '1' ] && \
[ "${BACKUP_MYSQL_COMPRESSION}" != '2' ] && [ "${BACKUP_MYSQL_COMPRESSION}" != '3' ] && \
[ "${BACKUP_MYSQL_COMPRESSION}" != '4' ] && [ "${BACKUP_MYSQL_COMPRESSION}" != '5' ] && \
[ "${BACKUP_MYSQL_COMPRESSION}" != '6' ] && [ "${BACKUP_MYSQL_COMPRESSION}" != '7' ] && \
[ "${BACKUP_MYSQL_COMPRESSION}" != '8' ] && [ "${BACKUP_MYSQL_COMPRESSION}" != '101' ] && \
[ "${BACKUP_MYSQL_COMPRESSION}" != '102' ] && [ "${BACKUP_MYSQL_COMPRESSION}" != '107' ]; then
program_error "BACKUP_MYSQL_COMPRESSION is not configured properly in ${BACKUPBOT_CONFIGURATION}"
fi
# weekly and daily backup arguments can't be used at the same time
if [ "${ARGUMENT_DAILY}" = '1' ] && [ "${ARGUMENT_WEEKLY}" = '1' ]; then
program_error 'invalid argument used'
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 %s (%s)\n" "${BACKUPBOT_VERSION}" "${BACKUPBOT_VERSION_DATE}"
printf "Copyright (C) 2019 Sebas Veeke. All rights reserved.\n"
printf "SPDX-License-Identifier: Apache-2.0.\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 backupbot 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 "${BACKUPBOT_CRONFILE}"
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 %s * * * root /usr/local/bin/backupbot --backup --daily\n\n" "${AUTOMATIC_BACKUP_DAILY}" > "${BACKUPBOT_CRONFILE}"
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 %s * * %s root /usr/local/bin/backupbot --backup --weekly\n" "${AUTOMATIC_BACKUP_DAILY}" "${AUTOMATIC_BACKUP_WEEKLY}" >> "${BACKUPBOT_CRONFILE}"
fi
printf "\nAll done! Your new schedule can be found in %s\n" "${BACKUPBOT_CRONFILE}"
send_notice_to_logger "[CRON]" "Effectuated cron configuration from ${BACKUPBOT_CONFIGURATION} to ${BACKUPBOT_CRONFILE}"
else
printf 'Automatic backup is disabled, skipping creation of cronjob\n'
fi
}
send_notice_to_customlog() {
DATE="$(date +%Y-%m-%d)"
TIME="$(date +%H:%M:%S)"
printf "[%s][%s][NOTICE]%s %s\n" "${DATE}" "${TIME}" "${1}" "${2}" >> "${BACKUPBOT_LOG}"
}
send_notice_to_logger() {
logger -t backupbot "${1}" "${2}"
}
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_logger "[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
# --use-compress-program=gzip (--gzip) (-z) compress the resulting archive with gzip 1
# --use-compress-program=bzip2 (--bzip2) (-j) compress the resulting archive with bzip2 2
# --use-compress-program=xz (--xz) (-J) compress the resulting archive with xz 3
# --use-compress-program=lz4 (--lz4) compress the resulting archive with lz4 4
# --use-compress-program=zstd (--zstd) compress the resulting archice with zstd 5
# --use-compress-program=lrzip (--lrzip) compress the resulting archice with lrzip (needs package installed) 6
# --use-compress-program=lzip compress the resulting archive with lzip (needs package installed) 7
# --use-compress-program=lzop compress the resulting archive with lzop (needs package installed) 8
# --use-compress-program=pigz compress the resulting archive with pigz (needs package installed) 101
# --use-compress-program=pbzip2 compress the resulting archive with pbzip2 (needs package installed) 102
# --use-compress-program=plzip compress the resulting archive with plzip (needs package installed) 107
# --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='--use-compress-program=gzip'
BACKUP_FILES_EXTENSION='tar.gz'
elif [ "${BACKUP_FILES_COMPRESSION}" = '2' ]; then
TAR_COMPRESSION_ARGUMENT='--use-compress-program=bzip2'
BACKUP_FILES_EXTENSION='tar.bz2'
elif [ "${BACKUP_FILES_COMPRESSION}" = '3' ]; then
TAR_COMPRESSION_ARGUMENT='--use-compress-program=xz'
BACKUP_FILES_EXTENSION='tar.xz'
elif [ "${BACKUP_FILES_COMPRESSION}" = '4' ]; then
TAR_COMPRESSION_ARGUMENT='--use-compress-program=lz4'
BACKUP_FILES_EXTENSION='tar.lz4'
elif [ "${BACKUP_FILES_COMPRESSION}" = '5' ]; then
TAR_COMPRESSION_ARGUMENT='--use-compress-program=zstd'
BACKUP_FILES_EXTENSION='tar.zst'
elif [ "${BACKUP_FILES_COMPRESSION}" = '6' ]; then
TAR_COMPRESSION_ARGUMENT='--use-compress-program=lrzip'
BACKUP_FILES_EXTENSION='tar.lrz'
elif [ "${BACKUP_FILES_COMPRESSION}" = '7' ]; then
TAR_COMPRESSION_ARGUMENT='--use-compress-program=lzip'
BACKUP_FILES_EXTENSION='tar.lz'
elif [ "${BACKUP_FILES_COMPRESSION}" = '8' ]; then
TAR_COMPRESSION_ARGUMENT='--use-compress-program=lzop'
BACKUP_FILES_EXTENSION='tar.lzo'
elif [ "${BACKUP_FILES_COMPRESSION}" = '101' ]; then
TAR_COMPRESSION_ARGUMENT='--use-compress-program=pigz'
BACKUP_FILES_EXTENSION='tar.gz'
elif [ "${BACKUP_FILES_COMPRESSION}" = '102' ]; then
TAR_COMPRESSION_ARGUMENT='--use-compress-program=pbzip2'
BACKUP_FILES_EXTENSION='tar.bz2'
elif [ "${BACKUP_FILES_COMPRESSION}" = '107' ]; then
TAR_COMPRESSION_ARGUMENT='--use-compress-program=plzip'
BACKUP_FILES_EXTENSION='tar.lz'
fi
# check configured backup splitting and create variables for arguments
if [ "${BACKUP_FILES_SPLIT}" != 'NO' ]; then
if [ "${BACKUP_FILES_SPLIT}" -ge '1' ] && [ "${BACKUP_FILES_SPLIT}" -le '10000000' ]; then
SPLIT_ARGUMENT="-d -b${BACKUP_FILES_SPLIT}G"
else
SPLIT_ARGUMENT=''
BACKUP_FILES_SPLIT='NO'
send_notice_to_logger "[FILES]" "[Unsupported value used for BACKUP_FILES_SPLIT, splitting will be disabled]"
program_error 'invalid value used for BACKUP_FILES_SPLIT'
fi
fi
# encryption + splitting
if [ "${BACKUP_FILES_ENCRYPTION}" != 'NO' ] && [ "${BACKUP_FILES_SPLIT}" != 'NO' ]; then
requirement_gpg
send_notice_to_logger "[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_logger "[FILES]" "Finished encrypted file backup to ${BACKUP_FILES_DESTINATION}/${BACKUP_FILES_PREFIX}-${BACKUP_FILES_SUFFIX}.${BACKUP_FILES_EXTENSION}.gpg"
# encryption only
elif [ "${BACKUP_FILES_ENCRYPTION}" != 'NO' ] && [ "${BACKUP_FILES_SPLIT}" = 'NO' ]; then
requirement_gpg
send_notice_to_logger "[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_logger "[FILES]" "Finished encrypted file backup to ${BACKUP_FILES_DESTINATION}/${BACKUP_FILES_PREFIX}-${BACKUP_FILES_SUFFIX}.${BACKUP_FILES_EXTENSION}.gpg"
# splitting only
elif [ "${BACKUP_FILES_ENCRYPTION}" = 'NO' ] && [ "${BACKUP_FILES_SPLIT}" != 'NO' ]; then
send_notice_to_logger "[FILES]" "Starting file backup for ${BACKUP_FILES}"
tar --create --preserve-permissions ${TAR_COMPRESSION_ARGUMENT} --file - ${BACKUP_FILES} | split ${SPLIT_ARGUMENT} - "${BACKUP_FILES_DESTINATION}/${BACKUP_FILES_PREFIX}-${BACKUP_FILES_SUFFIX}.${BACKUP_FILES_EXTENSION}.part"
send_notice_to_logger "[FILES]" "Finished file backup to ${BACKUP_FILES_DESTINATION}/${BACKUP_FILES_PREFIX}-${BACKUP_FILES_SUFFIX}.${BACKUP_FILES_EXTENSION}"
# no encryption and splitting
else
send_notice_to_logger "[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_logger "[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_logger "[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_logger "[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_logger "[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_logger "[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'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '4' ]; then
MYSQL_COMPRESSION_ARGUMENT='lz4'
BACKUP_MYSQL_EXTENSION='sql.lz4'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '5' ]; then
MYSQL_COMPRESSION_ARGUMENT='zstd'
BACKUP_MYSQL_EXTENSION='sql.zst'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '6' ]; then
MYSQL_COMPRESSION_ARGUMENT='lrzip'
BACKUP_MYSQL_EXTENSION='sql.lrz'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '7' ]; then
MYSQL_COMPRESSION_ARGUMENT='lzip'
BACKUP_MYSQL_EXTENSION='sql.lz'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '8' ]; then
MYSQL_COMPRESSION_ARGUMENT='lzop'
BACKUP_MYSQL_EXTENSION='sql.lzo'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '101' ]; then
MYSQL_COMPRESSION_ARGUMENT='pigz'
BACKUP_MYSQL_EXTENSION='sql.gz'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '102' ]; then
MYSQL_COMPRESSION_ARGUMENT='pbzip2'
BACKUP_MYSQL_EXTENSION='sql.bz2'
elif [ "${BACKUP_MYSQL_COMPRESSION}" = '107' ]; then
MYSQL_COMPRESSION_ARGUMENT='plzip'
BACKUP_MYSQL_EXTENSION='sql.lz'
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_logger "[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_logger "[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_logger "[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_logger "[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_logger "[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_logger "[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_logger "[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_logger "[MYSQL]" "Removed weekly database backups older than ${BACKUP_MYSQL_RETENTION_WEEKLY} days from ${BACKUP_MYSQL_DESTINATION}"
fi
fi
}
feature_zfs() {
if [ "${BACKUP_ZFS_ENABLE}" = 'YES' ]; then
if [ "${ARGUMENT_HOURLY}" = '0' ] && [ "${ARGUMENT_WEEKLY}" = '0' ]; then
echo 'wip'
# change file name depending on whether its a manual, daily or weekly backup
fi
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
fi
}
####################################################################################################
# MAIN FUNCTIONS
####################################################################################################
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
elif [ "${ARGUMENT_ZFS}" = '1' ]; then
requirement_root
requirement_os
feature_zfs
exit 0
# return error on invalid argument
elif [ "${ARGUMENT_NONE}" = '1' ]; then
program_error 'used argument is invalid, use "backuptbot --help" for proper usage'
fi
}
####################################################################################################
# CALL MAIN FUNCTIONS
####################################################################################################
backupbot_main