-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_bkp
executable file
·137 lines (125 loc) · 4.38 KB
/
check_bkp
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
#!/bin/bash
# process args
while [ $# -gt 0 ]; do
if [[ $1 == *"--"* ]]; then
v="${1/--/}"
if [[ $v == 'help' ]]; then
declare $v=1
shift
continue
fi
declare $v="$2"
fi
shift
done
usage="$(basename "$0") [--dir] [--force_period] - plugin for check backups according to config plan using bkp list.
params:
--dir - configs location ( default: /etc/opt/bkp )
--force_period - d,w,m - daily,weekly or montly
tells that last backup must be not older then period + 1 day
example:
check_bkp --dir /etc/opt/bkp/solusvm --force_period w"
test $help && echo "$usage" && exit
now=`date +%s`
sec_in_day=86400
exit_status=0
dir=${dir:-/etc/opt/bkp}
case $force_period in
d)
period=2
;;
w)
period=8
;;
m)
period=32
;;
*)
period=2
;;
esac
# check if bkp supports multiple profiles in configs
multiprofile=`bkp -? 2>&1 | grep list-profiles`
exclude='/etc/icinga2/plinc/bkp_exclude'
# find all configs in specified dir old than period
for config in `find $dir -maxdepth 1 -type f -mtime +$[period-1] -name "*.yml"`; do
[[ -f $exclude ]] && grep -q $config $exclude && continue
config_basename=`basename $config`
has_daily=`grep -oP "\s+d:" $config`
test $has_daily && limit=$[sec_in_day*period]
if [ $force_period ]; then
limit=$[sec_in_day*period]
fi
# solusvm part; need .my.cnf file with all stuff
if [[ $dir =~ ^/etc/opt/bkp/solusvm/?$ ]]; then
ctid=`grep -oP "\s+(ctid|vm):.*?\K\w+" $config`
if [ -z $ctid ]; then
exit_text+="can't determine ctid from $config\n"
exit_status=1
fi
type=${config_basename:0:2}
if [ "$type" = "CT" ]; then
if [ ! -f "/etc/vz/conf/${ctid}.conf" ]; then
continue
fi
if [ ! -z "$(sudo vzlist -S -H ${ctid})" ]; then
continue
fi
suspended=`grep -soPi "disabled=.*yes" /etc/vz/conf/${ctid}.conf`
test $suspended && continue
last_unsuspend_seconds=`mysql -sN -e "select max(date) from systemmessages where content like '%/vz/private/$ctid%' and subject like '%[Unsuspend]%'"`
if (( $[now - last_unsuspend_seconds] < $limit )); then
continue
fi
elif [ "$type" = "VM" ]; then
if [ ! -f "/etc/libvirt/qemu/${ctid}.xml" ]; then
continue
fi
if ! virsh domstate $ctid &>/dev/null; then
continue
fi
request="select vm,disabled,max(unsusp_admin) as unsusp_admin ,max(unsusp_api) as unsusp_api from fozzy_kvm_info where vm = '$ctid';"
read -r vm suspended unsusp_admin unsusp_api <<<$(mysql -sN -e "$request")
if [ $suspended -eq 1 ]; then
continue
fi
if (( $[now - unsusp_admin] < $limit || $[now - unsusp_api] < $limit )); then
continue
fi
else
exit_text+="can't determine virtualization type for $config\n"
exit_status=1
fi
fi
if [ -n "$multiprofile" ]; then
for profile in `bkp list-profiles $config`; do
last_archive_date=`bkp --profile=$profile list $config | tail -1 | grep -oP "\d{4}-\d{2}-\d{2}"`
if [ ! $last_archive_date ]; then
exit_text+="can't find last backup for $config_basename:$profile\n"
exit_status=1
continue
fi
last_archive_seconds=`date +%s -d $last_archive_date`
if (( $[now - last_archive_seconds] > $limit )); then
exit_text+="last backup is too old for $config_basename:$profile\n"
exit_status=1
fi
done
else
last_archive_date=`bkp list $config | tail -1 | grep -oP "\d{4}-\d{2}-\d{2}"`
if [ ! $last_archive_date ]; then
exit_text+="can't find last backup for $config_basename\n"
exit_status=1
continue
fi
last_archive_seconds=`date +%s -d $last_archive_date`
if (( $[now - last_archive_seconds] > $limit )); then
exit_text+="last backup is too old for $config_basename\n"
exit_status=1
fi
fi
done
dir=${dir:-/etc/opt/bkp}
exit_text=${exit_text:-"bkp OK"}
echo -e $exit_text
exit $exit_status