-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSanityCheckRotatingBackup.sh
executable file
·95 lines (60 loc) · 2.09 KB
/
SanityCheckRotatingBackup.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
#!/bin/bash
# Example script to sanity-check rotating backups.
#
# Copyright (c) 2018-2021 R. Diez - Licensed under the GNU AGPLv3
set -o errexit
set -o nounset
set -o pipefail
declare -r CHECK_SCRIPT="./CheckIfAnyFilesModifiedRecently.sh"
declare -r SOME_BASE_DIR="$HOME/Some/Dir"
declare -r -i ONE_WEEK_IN_MINUTES="$(( 7 * 24 * 60 ))"
abort ()
{
echo >&2 && echo "Error in script \"$0\": $*" >&2
exit 1
}
check_if_any_files_modified_recently ()
{
local -r DIR="$1"
local -r -i MINUTES="$2"
printf "Checking if any files modified recently in \"%s\" ...\\n" "$DIR"
"$CHECK_SCRIPT" --since-minutes="$MINUTES" -- "$DIR"
}
get_dir_file_count ()
{
local -r DIR="$1"
shopt -s nullglob # In case the directory is empty.
# We are not including hidden files.
# shopt -s dotglob
local -a ALL_FILENAMES
pushd "$DIR" >/dev/null
ALL_FILENAMES=( * )
popd >/dev/null
if false; then
echo "Files found: ${ALL_FILENAMES[*]}"
fi
DIR_FILE_COUNT="${#ALL_FILENAMES[@]}"
}
# Checks whether any files have been modified recently,
# and also if the number of files lies within the limits.
check_rotating_backup ()
{
local -r DIR="$1"
local -r -i MINUTES="$2"
local -r -i MIN_FILE_COUNT="$3"
local -r -i MAX_FILE_COUNT="$4"
printf "Checking file count in \"%s\" ...\\n" "$DIR"
get_dir_file_count "$DIR"
if (( DIR_FILE_COUNT < MIN_FILE_COUNT )); then
abort "Error in directory \"$DIR\": the file and directory count is $DIR_FILE_COUNT, but the minimum is $MIN_FILE_COUNT."
fi
if (( DIR_FILE_COUNT > MAX_FILE_COUNT )); then
abort "Error in directory \"$DIR\": the file and directory count is $DIR_FILE_COUNT, but the maximum is $MAX_FILE_COUNT."
fi
check_if_any_files_modified_recently "$DIR" "$MINUTES"
}
check_if_any_files_modified_recently "$SOME_BASE_DIR/Dir1" "$ONE_WEEK_IN_MINUTES"
check_if_any_files_modified_recently "$SOME_BASE_DIR/Dir2" "$ONE_WEEK_IN_MINUTES"
check_rotating_backup "$SOME_BASE_DIR/Dir3" "$ONE_WEEK_IN_MINUTES" 10 99
check_rotating_backup "$SOME_BASE_DIR/Dir4" "$ONE_WEEK_IN_MINUTES" 100 999
echo "All checks finished."