-
Notifications
You must be signed in to change notification settings - Fork 4
/
backup-report
executable file
·197 lines (145 loc) · 5.29 KB
/
backup-report
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
#!/bin/bash
# defaults
C_LAST_BACKUPS_CNT_TO_COMPARE_DEFAULT=3
# error codes
C_OK=0
C_WARNING=1
C_ERROR=10
# color palette
C_RED='\033[0;31m'
C_GREEN='\033[0;32m'
C_YELLOW='\033[1;33m'
C_PURPLE='\033[0;35m'
C_NC='\033[0m'
inventory_file="$1"
aws_cmd=
# -------------------------------------------------------------------------
function ident(){
printf '%*s' $(($1*3)) ""
}
# -------------------------------------------------------------------------
function info(){
local identation=$2
[ -z "$identation" ] && identation=0
echo -e "$(ident $identation)$1"
}
# -------------------------------------------------------------------------
function ok(){
local identation=$2
[ -z "$2" ] && identation=0
echo -e "${C_GREEN}$(ident $identation)$1${C_NC}"
}
# -------------------------------------------------------------------------
function debug(){
local identation=$2
[ -z "$2" ] && identation=0
echo -e "${C_PURPLE}$(ident $identation)$1${C_NC}"
}
# -------------------------------------------------------------------------
function warning(){
local identation=$2
[ -z "$2" ] && identation=0
echo -e "${C_YELLOW}$(ident $identation)$1${C_NC}"
}
# -------------------------------------------------------------------------
function error() {
local identation=$2
[ -z "$2" ] && identation=0
echo -e "${C_RED}$(ident $identation)$1${C_NC}"
}
# -------------------------------------------------------------------------
function check_dependencies(){
aws_cmd=$(which aws)
if [ -z "$aws_cmd" ]; then
error "aws cli not found, please install awscli"
return 1
fi
local jq_cmd=$(which jq)
if [ -z "$jq_cmd" ]; then
error "aws cli not found, please install awscli"
return 1
fi
return 0
}
# -------------------------------------------------------------------------
function check_options(){
local rc=0
if [ -z "$inventory_file" ]; then
rc=1
error "Inventory file was not specified. Use \$1 option"
fi
if [ ! -f $inventory_file ]; then
rc=2
error "File '$inventory_file' not found"
fi
if [ ! -z "$inventory_file" ] && [ -f $inventory_file ] && ! $(cat $inventory_file | jq '.' >/dev/null 2>&1); then
rc=3
error "Cannot parse json file '$inventory_file'"
fi
return $rc
}
# -------------------------------------------------------------------------
function report_on_s3_customer(){
local bucket_path=$(echo $1 | jq '.bucket_path' | tr -d '"')
backups="$(aws s3 ls --human-readable $bucket_path)"
if [ $? -ne 0 ]; then
error "Cannot get list of backups, bucket path '$bucket_path'" 3
return 1
fi
local last_backups_cnt_to_compare=$(echo $1 | jq '.last_backups_cnt_to_compare' | tr -d '"')
if [ "$last_backups_cnt_to_compare" == "null" ]; then
last_backups_cnt_to_compare=$C_LAST_BACKUPS_CNT_TO_COMPARE_DEFAULT
warning "Option 'last_backups_cnt_to_compare' was not used. Going to use default value: $C_LAST_BACKUPS_CNT_TO_COMPARE_DEFAULT" 3
fi
earliest_backup=$(echo "$backups" | tail -1 | awk '{print $1}')
earliest_backup_size=$(echo "$backups" | tail -1 | awk '{print $3,$4}')
oldest_backup=$(echo "$backups" | head -1 | awk '{print $1}')
info "backup info: oldest=$oldest_backup, earliest=$earliest_backup, earliest size=$earliest_backup_size" 2
# are the backups size growing? last three backups checks
#
# sort by date and by size should retain the order
#
data="$(aws s3 ls $bucket_path | awk '{print $1,$2,$3}' | sed 's/ /_/' | tail -$last_backups_cnt_to_compare)"
diff <(echo "$data" | sort -k1) <(echo "$data" | sort -k2) >/dev/null
if [ $? -eq 0 ]; then
ok "Backups are fine. Size of last $last_backups_cnt_to_compare backups backup is growing" 2
else
error "Size of last $last_backups_cnt_to_compare backups backup is _NOT_ growing" 2
fi
}
# -------------------------------------------------------------------------
function provide_backup_report(){
local backup_type=$(echo $1 | jq '.type' | tr '-' '_' | tr -d '"')
if [ "$backup_type" == "null" ]; then
warning "backup type was not set. Skipping it" 2
return $C_ERROR
fi
if declare -f report_on_$backup_type > /dev/null; then
report_on_$backup_type "$1"
else
error "Unknown backup type '$backup_type'. Skipping it"
return $C_ERROR 2
fi
}
# -------------------------------MAIN ------------------------------------------
check_dependencies || exit 1
get_options "$@"
check_options || exit 2
project_with_backups="$(cat $inventory_file | jq -c 'map(select( any(.servers[]; .backup )))')"
projects=$(echo "$project_with_backups" | jq -c '.[].project' | tr -d '"')
for p in $projects; do
info "----------------------------------------"
info "checking project $p"
info "----------------------------------------"
servers=$(echo "$project_with_backups" | jq -c ".[] | select(.project == \"$p\") .servers")
amount_of_servers=$(echo $servers | jq -c '. | length')
# iterate thru servers...
for i in $(seq 0 $(($amount_of_servers-1))); do
server=$(echo $servers | jq -c ".[$i]")
[ $i -ne 0 ] && echo
info "server: $(echo $server | jq '.server')($(echo $server | jq '.host'))" 1
backup_options=$(echo $server | jq '.backup')
provide_backup_report "$backup_options"
done
echo
done