summaryrefslogtreecommitdiff
path: root/last-backups.in
blob: 84e8f642dcdfb647f22f3bff2689fa51055da3a8 (plain)
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
#!/bin/bash

# last-backups version #VERSION#

[ -r "#ETCDIR#/backup.conf" ] && \
  . "#ETCDIR#/backup.conf"

usage () {
  >&2 echo \
'Usage: last-backups
Show information about date of last backups and warn about outdated ones.

Options:
#HELPTEXT#  #'
  [ -n "$1" ] && exit $1
  exit 1
}

if [ $# -eq 1 ]
then
  if [ "$1" == "--help" ]
  then
    usage 0
  elif [ "$1" == "--version" ]
  then
    >&2 echo '#VERSION#'
    exit 0
  fi
  usage
elif [ $# -gt 1 ]
then
  usage
fi

unset namen
unset veraltet
unset daten

for backupID in ${!backups[@]}
do
  dest="${backups["${backupID}"]%% *}"
  dest="${dest%/}"
  while [ ! -d "${dest}" ] && [ ${maxWait} -gt 0 ]
  do
    sleep 1
    maxWait=$[${maxWait}-1]
  done

  lbu=$(
    ls -1 ${dest} | \
      grep -v "^aktuell$" | \
      sed "s/^duplicity-\(inc\|full\)\.\(.*\.\)\?\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)T[0-9]\{6\}Z\..*$/\3_\4_\5/" | \
      sort -n | \
      uniq | \
      grep "^[0-9]\{4\}_[0-9]\{2\}_[0-9]\{2\}$" | \
      tail -n1
    )
  [ "${lbu}" == "" ] && lbu="1970_01_01"
  delta=$[$(date +%s) - $(date -d $(echo ${lbu} | sed "s/_//g") +%s)]
  namen[${#namen[@]}]=$(
    basename $(
      printf '%s\n' "${dest}" \
      | sed '
        s#/rsync$##
        s#/duplicity$##
      ' \
      | sed '
        :a
        s#/\('"${recognSubdirRegex}"'\)$#_\1#
        ta
      '
    )
  )
  if [ -f "/tmp/${backupID}.pid" ] && [ -d "/proc/$(cat "/tmp/${backupID}.pid")" ]; then
    laeuft='_laeuft'
  else
    laeuft=''
  fi
  if [ ! -d "${dest}/${lbu}" ]
  then
    veraltet[${#veraltet[@]}]="LEER!${laeuft}"
  elif [ ${delta} -gt ${outdatedLimit} ] && \
    ! printf '%s\n' "${seldomBackups[@]}" | \
      grep -qxF "${backupID}"
  then
    veraltet[${#veraltet[@]}]="VERALTET!${laeuft}"
  elif [ ${delta} -gt ${outdatedSeldomLimit} ]
  then
    veraltet[${#veraltet[@]}]="VERALTET!${laeuft}"
  else
    veraltet[${#veraltet[@]}]="${laeuft}"
  fi
  daten[${#daten[@]}]="${lbu}"
done

nl=0
vl=0
dl=0

for ((i=0; i<${#namen[@]}; i++))
{
  [ ${#namen[${i}]} -gt ${nl} ] && nl=${#namen[${i}]}
  [ ${#veraltet[${i}]} -gt ${vl} ] && vl=${#veraltet[${i}]}
  [ ${#daten[${i}]} -gt ${dl} ] && dl=${#daten[${i}]}
}

for ((i=0; i<${#namen[@]}; i++))
{
  while [ ${#namen[${i}]} -lt ${nl} ]
  do
    namen[${i}]="${namen[${i}]} "
  done
  while [ ${#veraltet[${i}]} -lt ${vl} ]
  do
    veraltet[${i}]="${veraltet[${i}]} "
  done
  while [ ${#daten[${i}]} -lt ${dl} ]
  do
    daten[${i}]="${daten[${i}]} "
  done
}

for ((i=0; i<${#namen[@]}; i++))
{
  echo "${daten[${i}]} ${veraltet[${i}]} ${namen[${i}]} ${daten[${i}]}"
} | \
  sort | \
  sed "s/^\S\+\s//"