-
Notifications
You must be signed in to change notification settings - Fork 4
/
nBackup.sh
421 lines (350 loc) · 19.5 KB
/
nBackup.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
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
#!/bin/bash
# nBackup
# ----------------------------------------------------------------------------
# nBackup is a simple bash script for making versioned, dependency-less
# backups of your data where versions are browsable as current backups,
# snapshot-in-time folders, and running file versions.
#
# url : https://github.com/imthenachoman/nBackup
# author : Anchal Nigam
# e-mail : imthenachoman@gmail.com
#
# Copyright (c) 2019 Anchal Nigam (imthenachoman@gmail.com)
# Licensed under the MIT license: http://opensource.org/licenses/MIT
# ----------------------------------------------------------------------------
###############################################################################
# FUNCTIONS
###############################################################################
function readConfigFile
{
# first check if the config file is there
if [[ ! -e ~/.nBackup.conf ]] ; then
echo "ERROR: \"~/.nBackup.conf\" does not exist!" 1>&2
echo " Check the app installation directory for an example nBackup.conf file." 1>&2
echo " For example: cp \"$( cd "$(dirname "$0")" ; pwd -P )/nBackup.conf\" ~/.nBackup.conf" 1>&2
exit 1
fi
# check if we can read the file
if [[ ! -r ~/.nBackup.conf ]] ; then
echo "ERROR: Unable to read \"~/.nBackup.conf\"!" 1>&2
exit 1
fi
# make sure it has the right permissions
if stat -c "%a" ~/.nBackup.conf | grep --quiet -v 600 ;
then
echo "ERROR: \"~/.nBackup.conf\" has invalid file permissions!" 1>&2
echo " File permissions for \"~/.nBackup.conf\" should be 600." 1>&2
echo " For example: chmod 600 ~/.nBackup.conf" 1>&2
exit 1
fi
# make sure the file is owened by the current user
if [[ "$(stat -c "%U:%G" ~/.nBackup.conf)" != "$(whoami):$(whoami)" ]] ; then
echo "ERROR: \"~/.nBackup.conf\" does not have the correct file ownership!" 1>&2
echo " \"~/.nBackup.conf\" should be \"$(whoami):$(whoami)\"." 1>&2
exit 1
fi
# source the config file
# ideally sourcing files is not very secure because someone could have
# entered dangerous code like rm -rf and sourcing the file would execute it
# but since we've already confirmed that only the current user can modify the file
# we'll assume everything is okay
source ~/.nBackup.conf
# make relevant folders if they don't exist
# and make sure they are writable
for folder in BACKUP_DESTINATION_CURRENT BACKUP_DESTINATION_COMBINED BACKUP_DESTINATION_FOLDERS LOG_DESTINATION; do
if ! mkdir -p "${!folder}" &>/dev/null ; then
echo "ERROR: Unable to make $folder \"${!folder}\"!" 1>&2
exit 1
fi
if [[ ! -w "${!folder}" ]] ; then
echo "ERROR: Unable to write to $folder \"${!folder}\"!" 1>&2
exit 1
fi
done
# make sure the include and exclude files are readable
for file in BACKUP_INCLUDES_FILE BACKUP_EXCLUDES_FILE; do
if [[ ! -r "${!file}" ]] ; then
echo "ERROR: Unable to read $file \"${!file}\"!" 1>&2
echo " Check the app installation directory for an example file." 1>&2
exit 1
fi
done
# make sure binary options have valid values
for option in DELETE_OLD_FOLDER_BACKUPS DELETE_OLD_COMBINED_BACKUPS; do
if [[ "${!option}" != "1" && "${!option}" != "0" ]] ; then
echo "ERROR: Invalid option \"${!option}\" for ${option}!" 1>&2
echo " Valid options are: \"1\" or \"0\"." 1>&2
exit 1
fi
done
# make sure count options are valid integers
for option in KEEP_OLD_FOLDER_BACKUPS_COUNT KEEP_OLD_COMBINED_BACKUPS_COUNT; do
if ! [[ "${!option}" =~ ^[0-9]+$ ]] ; then
echo "ERROR: Invalid integer \"${!option}\" for ${option}!" 1>&2
echo " Please enter a valid integer." 1>&2
exit 1
fi
done
# make sure age options are valid
for option in KEEP_OLD_FOLDER_BACKUPS_AGE KEEP_OLD_COMBINED_BACKUPS_AGE; do
if ! [[ "${!option}" =~ ^[0-9]+\ (second|minute|hour|day|week|month|year)s? ]] ; then
echo "ERROR: Invalid age \"${!option}\" for ${option}!" 1>&2
echo " Valid ages are: # second/minute/hour/day/week/month/year(s)." 1>&2
exit 1
fi
# to do
# check if age actually works as a valid option for date -d ...
done
# make sure criteria options are valid
for option in KEEP_OLD_FOLDER_BACKUPS_CRITERIA KEEP_OLD_COMBINED_BACKUPS_CRITERIA; do
if [[ "${!option}" != "and" && "${!option}" != "or" ]] ; then
echo "ERROR: Invalid criteria \"${!option}\" for ${option}!" 1>&2
echo " Valid options are: \"and\" or \"or\"." 1>&2
exit 1
fi
done
}
# pretty date/time difference of two dates/times represented as seconds
# dateDiff startDateTimeInSeconds startDateTimeNanoSconds endDateTimeInSeconds
# $1 = start date/time seconds since epoch
# $2 = start date/time nanoseconds
# $3 = end date/time seconds since epoch
# $4 = end date/time nanoseconds
function dateDiff
{
local differenceInSeconds=$(expr $3 - $1)
local days=$((differenceInSeconds/60/60/24))
local hours=$((differenceInSeconds/60/60%24))
local minutes=$((differenceInSeconds/60%60))
local seconds=$((differenceInSeconds%60))
local nanoseconds=$(echo $(expr $4 - $2) | tr -d -)
printf "%2d days, %2d hours, %2d minutes, %2d seconds, %d nanoseconds" $days $hours $minutes $seconds $nanoseconds
}
# filter a list of files/folders that should be deleted; read from stdin
# $1 = criteria: and/or
# $2 = keepcount
# $3 = minage
function filterFilesToDelete
{
# counter to keep track of the # of rows read
count=1
# reverse sort the data
# exclude the top one since its the latest backup
# then process each line
sort -r - | tail -n +2 | while read line ; do
# find the archive date
archive_date=${line##*[./]}
# for 'and' we want to:
# check if the row is greater than the keep count
# and
# check if the archive date is less than the min age
if [[ "$1" = "and" && "$count" -gt "$2" && "$archive_date" < "$3" ]] ; then
echo "$line"
# for 'or' we want to:
# check if the row is greater than the keep count
# or
# check if the archive date is less than the min age
elif [[ $1 = "or" && ( "$count" -gt "$2" || "$archive_date" < "$3" ) ]] ; then
echo "$line"
fi
count=$[count + 1]
done
}
function doCurrentBackup
{
local sectionName="1. backup current"
local sectionLog="1_backup_current"
local start_time=$(date +"%s %N")
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "starting" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "info" "destination => $BACKUP_DESTINATION_CURRENT"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "info" "this could take a while depending on the number of files"
# rsync the files
rsync --archive --delete --recursive -xx --relative --stats --out-format="%t | %5o | %15l | %f" --files-from="$BACKUP_INCLUDES_FILE" --exclude-from="$BACKUP_EXCLUDES_FILE" / "$BACKUP_DESTINATION_CURRENT" 1>>"$LOG_DESTINATION_NOW/${sectionLog}.out.log" 2>>"$LOG_DESTINATION_NOW/${sectionLog}.err.log"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "finished" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "duration" "$(dateDiff $start_time $(date +"%s %N"))"
# get rsync stats
rsync_stats=$(tail -50 "$LOG_DESTINATION_NOW/${sectionLog}.out.log" | egrep -A 50 "^Number of files: ")
# get the number of files that were created or deleted
num_files_created=$([[ $rsync_stats =~ $(echo "Number of created files: ([0-9,]+)") ]] && echo "${BASH_REMATCH[1]}" | sed 's/,//')
num_files_deleted=$([[ $rsync_stats =~ $(echo "Number of deleted files: ([0-9,]+)") ]] && echo "${BASH_REMATCH[1]}" | sed 's/,//')
num_files_transferred=$([[ $rsync_stats =~ $(echo "Number of regular files transferred: ([0-9,]+)") ]] && echo "${BASH_REMATCH[1]}" | sed 's/,//')
# was anything actually synced
if [[ "$num_files_created" -ne 0 || "$num_files_deleted" -ne 0 || "$num_files_transferred" -ne 0 ]] ; then
# print stats
echo "$rsync_stats" | while read line ; do
if [[ "$line" != "" ]] ; then
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "1. backup current" "stats" "$line"
fi
done
return 0
else
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "stats" "no changes; nothing backed up"
return 1
fi
}
function doFolderBackup
{
local sectionName="2. backup folder"
local sectionLog="2_backup_folder"
local start_time=$(date +"%s %N")
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "starting" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "info" "destination => $BACKUP_DESTINATION_FOLDERS/$ARCHIVE_DATE"
cp -alv "${BACKUP_DESTINATION_CURRENT}" "$BACKUP_DESTINATION_FOLDERS/$ARCHIVE_DATE" 1>>"$LOG_DESTINATION_NOW/${sectionLog}.out.log" 2>>"$LOG_DESTINATION_NOW/${sectionLog}.err.log"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "finished" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "duration" "$(dateDiff $start_time $(date +"%s %N"))"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "stats" "linked $(find "$BACKUP_DESTINATION_FOLDERS/$ARCHIVE_DATE" -type f | wc -l) files"
}
function doCombinedBackup
{
local sectionName="3. backup combined"
local sectionLog="3_backup_combined"
local start_time=$(date +"%s %N")
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "starting" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "info" "destination => $BACKUP_DESTINATION_COMBINED"
# make a combined viewd
# - find all files with 2 links
# - one link is to the file in the $BACKUP_DESTINATION/current
# - the other link is to the file in $BACKUP_DESTINATION/$ARCHIVE_DATE
# - there should never be any files with only 1 hard link since the previous command
# is sure to have created a second link
# - any files with more than 2 links were, hopefully, already covered during a previous iteration
cd "${BACKUP_DESTINATION_CURRENT}" && find * -type f -links 2 -print0 | while IFS= read -r -d $'\0' filePath ; do
# get the name of the file
fileName="$(basename "$filePath")"
# get the folder it is in
fileFolder="$(dirname "$filePath")"
# where the file will live in the combined folder
# need to mirror the folder structure
destinationFolder="$BACKUP_DESTINATION_COMBINED/$fileFolder"
if [[ ! -d "$destinationFolder" ]] ; then
mkdir -pv "$destinationFolder" 1>>"$LOG_DESTINATION_NOW/${sectionLog}.out.log" 2>>"$LOG_DESTINATION_NOW/${sectionLog}.err.log"
fi
# make a hard link to it
cp -alv "${BACKUP_DESTINATION_CURRENT}/$filePath" "$destinationFolder/$fileName.$ARCHIVE_DATE" 1>>"$LOG_DESTINATION_NOW/${sectionLog}.out.log" 2>>"$LOG_DESTINATION_NOW/${sectionLog}.err.log"
done
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "finished" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "duration" "$(dateDiff $start_time $(date +"%s %N"))"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "stats" "linked $(find "$BACKUP_DESTINATION_COMBINED" -type f -name "*.$ARCHIVE_DATE" | wc -l) files"
}
function deleteOldFolderBackups
{
local sectionName="4. delete folders"
local sectionLog="4_delete_folders"
local start_time=$(date +"%s %N")
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "starting" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "info" "keep count => $KEEP_OLD_FOLDER_BACKUPS_COUNT"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "info" "minimum age => $KEEP_OLD_FOLDER_BACKUPS_AGE => $(date -d "-$KEEP_OLD_FOLDER_BACKUPS_AGE" +"$ARCHIVE_DATE_FORMAT")"
# find all direct sub-folders in BACKUP_DESTINATION_FOLDERS
# exclude BACKUP_DESTINATION_CURRENT, BACKUP_DESTINATION_COMBINED, and LOG_DESTINATION
# filter based on config criteria
find "$BACKUP_DESTINATION_FOLDERS" -mindepth 1 -maxdepth 1 \( -path "$BACKUP_DESTINATION_CURRENT" -o -path "$BACKUP_DESTINATION_COMBINED" -o -path "$LOG_DESTINATION" \) -prune -o -type d | filterFilesToDelete "$KEEP_OLD_FOLDER_BACKUPS_CRITERIA" "$KEEP_OLD_FOLDER_BACKUPS_COUNT" "$(date -d "-$KEEP_OLD_FOLDER_BACKUPS_AGE" +"$ARCHIVE_DATE_FORMAT")" | while read folder ; do
# delete the folder
rm -rf "$folder" 1>>/dev/null 2>>"$LOG_DESTINATION_NOW/${sectionLog}.err.log"
# log output if it was deleted
if [[ ! -d "$folder" ]] ; then
echo "removed: $folder" 1>>"$LOG_DESTINATION_NOW/${sectionLog}.out.log"
fi
done
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "finished" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "duration" "$(dateDiff $start_time $(date +"%s %N"))"
if [[ -e "$LOG_DESTINATION_NOW/${sectionLog}.out.log" ]] ; then
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "stats" "deleted $(cat "$LOG_DESTINATION_NOW/${sectionLog}.out.log" | wc -l) old backup folders"
else
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "stats" "deleted 0 old backup folders"
fi
}
function deleteOldCombinedBackups
{
local sectionName="5. delete combined"
local sectionLog="5_delete_combined"
local start_time=$(date +"%s %N")
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "starting" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "info" "keep count => $KEEP_OLD_COMBINED_BACKUPS_COUNT"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "info" "minimum age => $KEEP_OLD_COMBINED_BACKUPS_AGE => $(date -d "-$KEEP_OLD_COMBINED_BACKUPS_AGE" +"$ARCHIVE_DATE_FORMAT")"
# find all folders in BACKUP_DESTINATION_COMBINED
find "$BACKUP_DESTINATION_COMBINED" -type d | while read folder ; do
# for each folder
# find direct files
# remove the file's extension (archive date/time stamp)
# sort and unique the list
# and go through each unique base file
cd "$folder" && find . -maxdepth 1 -type f -exec bash -c 'printf "%s\n" "${@%.*}"' _ {} + | sort -u | while read file ; do
# find all the backups for the base file
# filter based on config criteria
ls "$file".* | filterFilesToDelete "$KEEP_OLD_COMBINED_BACKUPS_CRITERIA" "$KEEP_OLD_COMBINED_BACKUPS_COUNT" "$(date -d "-$KEEP_OLD_COMBINED_BACKUPS_AGE" +"$ARCHIVE_DATE_FORMAT")" | while read line ; do
# delete the file
rm -rfv "$line" 1>>"$LOG_DESTINATION_NOW/${sectionLog}.out.log" 2>>"$LOG_DESTINATION_NOW/${sectionLog}.err.log"
done
done
done
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "finished" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "duration" "$(dateDiff $start_time $(date +"%s %N"))"
if [[ -e "$LOG_DESTINATION_NOW/${sectionLog}.out.log" ]] ; then
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "stats" "deleted $(cat "$LOG_DESTINATION_NOW/${sectionLog}.out.log" | wc -l) old backup files"
else
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "stats" "deleted 0 old backup files"
fi
}
function doIt
{
local sectionName="0. nBackup"
local start_time=$(date +"%s %N")
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "starting" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "log path" "$LOG_DESTINATION_NOW"
cat ~/.nBackup.conf | egrep -v '^#|^$' | while read line ; do
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "configuration" "$line"
done
# do the backup
if doCurrentBackup ; then
# and if something was backed up then make folder and combined views
doFolderBackup
doCombinedBackup
fi
# delete old backups?
[[ "$DELETE_OLD_FOLDER_BACKUPS" = "1" ]] && deleteOldFolderBackups
[[ "$DELETE_OLD_COMBINED_BACKUPS" = "1" ]] && deleteOldCombinedBackups
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "finished" "$(date)"
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "duration" "$(dateDiff $start_time $(date +"%s %N"))"
# print all the log files
ls "$LOG_DESTINATION_NOW/"*.log | while read line ; do
# if the file file is not empty then print it
if [[ -s "$line" ]] ; then
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "log file" "$line => $(cat "$line" | wc -l) lines"
# else delete it
else
rm -rf "$line"
fi
done
# alert if there were there any error files
if ls "$LOG_DESTINATION_NOW"/*.err.log 1>/dev/null 2>&1 ; then
printf "$LOG_OUTPUT_FORMAT" "$(date +"$LOG_TIME_FORMAT")" "$sectionName" "!!! ERROR !!!" "there were errors. check the log files for details"
fi
}
function sendEmail
{
# get rsync stats
rsync_stats=$(tail -50 "$LOG_DESTINATION_NOW/1_backup_current.out.log" | egrep -A 50 "^Number of files: ")
# get the number of files that were created or deleted
num_files_created=$([[ $rsync_stats =~ $(echo "Number of created files: ([0-9,]+)") ]] && echo "${BASH_REMATCH[1]}" | sed 's/,//')
num_files_deleted=$([[ $rsync_stats =~ $(echo "Number of deleted files: ([0-9,]+)") ]] && echo "${BASH_REMATCH[1]}" | sed 's/,//')
num_files_transferred=$([[ $rsync_stats =~ $(echo "Number of regular files transferred: ([0-9,]+)") ]] && echo "${BASH_REMATCH[1]}" | sed 's/,//')
errors=$(ls "$LOG_DESTINATION_NOW"/*.err.log 1>/dev/null 2>&1 && echo "ERRORS - " || echo "")
cat "$LOG_DESTINATION_NOW/0_nBackup.log" | mail -s "${errors}$(hostname) backup report - $ARCHIVE_DATE (created: $num_files_created / deleted: $num_files_deleted / files transferred: $num_files_transferred)" "$EMAIL_ADDRESS"
}
###############################################################################
# MAIN
###############################################################################
readConfigFile
NOW=$(date +"$ARCHIVE_DATE_FORMAT")
ARCHIVE_DATE=$(date +"$ARCHIVE_DATE_FORMAT")
LOG_OUTPUT_FORMAT="%s | %-20s | %-15s | %s\n"
LOG_DESTINATION_NOW="$LOG_DESTINATION/$NOW"
if [[ -d "$BACKUP_DESTINATION_FOLDERS/$ARCHIVE_DATE" || -d "$LOG_DESTINATION_NOW" || -e "$LOG_DESTINATION_NOW/0_nBackup.log" ]] ; then
echo "ERROR: A backup for ARCHIVE_DATE=$ARCHIVE_DATE already exists!" 1>&2
exit 1
fi
# make date folder for logs
mkdir -p "$LOG_DESTINATION_NOW"
# start it and log everything
doIt | tee "$LOG_DESTINATION_NOW/0_nBackup.log"
sendEmail