summaryrefslogtreecommitdiff
path: root/puppet/modules/backupninja/files/nagios_plugins/duplicity/backupninja_duplicity_freshness.sh
blob: 7af2bf7f9c6b755d0de73556c62d4b9e626dc885 (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
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
#!/bin/bash
# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
# vim: set filetype=sh sw=3 sts=3 expandtab autoindent:

# Load backupninja library/helpers, because why reinventing the wheel? [Because my wheels weren't round]
# some duplication is to be expected
# this is only supposed to work with duplicity

## Functions
# simple lowercase function
function tolower() {
   echo "$1" | tr '[:upper:]' '[:lower:]'
}

# we grab the current time once, since processing
# all the configs might take more than an hour.
nowtime=`LC_ALL=C date +%H`
nowday=`LC_ALL=C date +%d`
nowdayofweek=`LC_ALL=C date +%A`
nowdayofweek=`tolower "$nowdayofweek"`

conffile="/etc/backupninja.conf"

# find $libdirectory
libdirectory=`grep '^libdirectory' $conffile | /usr/bin/awk '{print $3}'`
if [ -z "$libdirectory" ]; then
   if [ -d "/usr/lib/backupninja" ]; then
      libdirectory="/usr/lib/backupninja"
   else
      echo "Could not find entry 'libdirectory' in $conffile."
      fatal "Could not find entry 'libdirectory' in $conffile."
   fi
else
   if [ ! -d "$libdirectory" ]; then
      echo "Lib directory $libdirectory not found."
      fatal "Lib directory $libdirectory not found."
   fi
fi

. $libdirectory/tools

setfile $conffile

# get global config options (second param is the default)
getconf configdirectory /etc/backup.d
getconf scriptdirectory /usr/share/backupninja
getconf reportdirectory
getconf reportemail
getconf reporthost
getconf reportspace
getconf reportsuccess yes
getconf reportinfo no
getconf reportuser
getconf reportwarning yes
getconf loglevel 3
getconf when "Everyday at 01:00"
defaultwhen=$when
getconf logfile /var/log/backupninja.log
getconf usecolors "yes"
getconf SLAPCAT /usr/sbin/slapcat
getconf LDAPSEARCH /usr/bin/ldapsearch
getconf RDIFFBACKUP /usr/bin/rdiff-backup
getconf CSTREAM /usr/bin/cstream
getconf MYSQLADMIN /usr/bin/mysqladmin
getconf MYSQL /usr/bin/mysql
getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
getconf MYSQLDUMP /usr/bin/mysqldump
getconf PGSQLDUMP /usr/bin/pg_dump
getconf PGSQLDUMPALL /usr/bin/pg_dumpall
getconf PGSQLUSER postgres
getconf GZIP /bin/gzip
getconf GZIP_OPTS --rsyncable
getconf RSYNC /usr/bin/rsync
getconf admingroup root

if [ ! -d "$configdirectory" ]; then
   echo "Configuration directory '$configdirectory' not found."
   fatal "Configuration directory '$configdirectory' not found."
fi

# get the duplicity configuration
function get_dupconf(){
   setfile $1
   getconf options
   getconf testconnect yes
   getconf nicelevel 0
   getconf tmpdir
   
   setsection gpg
   getconf password
   getconf sign no
   getconf encryptkey
   getconf signkey
   
   setsection source
   getconf include
   getconf vsnames all
   getconf vsinclude
   getconf exclude
   
   setsection dest
   getconf incremental yes
   getconf increments 30
   getconf keep 60
   getconf keepincroffulls all
   getconf desturl
   getconf awsaccesskeyid
   getconf awssecretaccesskey
   getconf cfusername
   getconf cfapikey
   getconf cfauthurl
   getconf ftp_password
   getconf sshoptions
   getconf bandwidthlimit 0
   getconf desthost
   getconf destdir
   getconf destuser
   destdir=${destdir%/}
}

### some voodoo to mangle the correct commands

function mangle_cli(){

   execstr_options="$options "
   execstr_source=
   if [ -n "$desturl" ]; then
      [ -z "$destuser" ] || warning 'the configured destuser is ignored since desturl is set'
      [ -z "$desthost" ] || warning 'the configured desthost is ignored since desturl is set'
      [ -z "$destdir" ] || warning 'the configured destdir is ignored since desturl is set'
      execstr_serverpart="$desturl"
   else
      execstr_serverpart="scp://$destuser@$desthost/$destdir"
   fi
   
   
   ### Symmetric or asymmetric (public/private key pair) encryption
   if [ -n "$encryptkey" ]; then
      execstr_options="${execstr_options} --encrypt-key $encryptkey"
   fi
   
   ### Data signing (or not)
   if [ "$sign" == yes ]; then
      # duplicity is not able to sign data when using symmetric encryption
      [ -n "$encryptkey" ] || fatal "The encryptkey option must be set when signing."
      # if needed, initialize signkey to a value that is not empty (checked above)
      [ -n "$signkey" ] || signkey="$encryptkey"
      execstr_options="${execstr_options} --sign-key $signkey"
   fi
   
   ### Temporary directory
   precmd=
   if [ -n "$tmpdir" ]; then
      if [ ! -d "$tmpdir" ]; then
         #info "Temporary directory ($tmpdir) does not exist, creating it."
         mkdir -p "$tmpdir"
         [ $? -eq 0 ] || fatal "Could not create temporary directory ($tmpdir)."
         chmod 0700 "$tmpdir"
      fi
      #info "Using $tmpdir as TMPDIR"
      precmd="${precmd}TMPDIR=$tmpdir "
   fi
   
   ### Source
   
   set -o noglob
   
   # excludes
   SAVEIFS=$IFS
   IFS=$(echo -en "\n\b")
   for i in $exclude; do
      str="${i//__star__/*}"
      execstr_source="${execstr_source} --exclude '$str'"
   done
   IFS=$SAVEIFS
   
   # includes
   SAVEIFS=$IFS
   IFS=$(echo -en "\n\b")
   for i in $include; do
      [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
      str="${i//__star__/*}"
      execstr_source="${execstr_source} --include '$str'"
   done
   IFS=$SAVEIFS
   
   set +o noglob
   
   execstr_options="${execstr_options} --ssh-options '$sshoptions'"
   if [ "$bandwidthlimit" != 0 ]; then
      [ -z "$desturl" ] || warning 'The bandwidthlimit option is not used when desturl is set.'
      execstr_precmd="trickle -s -d $bandwidthlimit -u $bandwidthlimit"
   fi
}

#function findlastdates(){
#   outputfile=$1
#   lastfull=0
#   lastinc=0
#   backuptime=0
#   
#   while read line; do
#      atime=0
#      arr=()
#      sort=''
#      test=$(echo $line|awk '{if (NF == 7); if ($1 == "Full" || $1 == "Incremental") {print $4, $3, $6, $5}}'  )
#   
#      if [ -n "$test"  ]; then
#         backuptime=$(date -u -d "$test" +%s)
#   
#         arr=($(echo $line|awk '{print $1, $2, $3, $4, $5, $6}'))
#         if [ ${arr[0]} == "Incremental" ] && [ "$lastinc" -lt "$backuptime" ] ; then
#            lastinc=$backuptime
#         elif [ ${arr[0]} == "Full" ] && [ "$lastfull" -lt "$backuptime" ] ; then
#            lastfull=$backuptime
#         fi
#   
#      fi
#   
#   done < $outputfile
#      # a full backup can be seen as incremental too
#      lastinc=$(echo $lastinc | awk 'max=="" || $1 > max {max=$1} END{ print max}')
#}

function check_status() {
   grep -q 'No orphaned or incomplete backup sets found.' $1
   if [ $? -ne 0 ] ; then
     exit 2
   fi
}

##
## this function handles the freshness check of a backup action
##

function process_action() {
   local file="$1"
   local suffix="$2"
   setfile $file
   get_dupconf $1
   mangle_cli
   
   outputfile=`maketemp backupout`
   export PASSPHRASE=$password
   export FTP_PASSWORD=$ftp_password
   output=` su -c \
            "$execstr_precmd duplicity $execstr_options collection-status $execstr_serverpart >$outputfile 2>&1"`
   exit_code=$?
   echo -n $outputfile

   #check_status
   #findlastdates
}

files=`find $configdirectory -follow -mindepth 1 -maxdepth 1 -type f ! -name '.*.swp' | sort -n`

for file in $files; do
   [ -f "$file" ] || continue
   suffix="${file##*.}"
   base=`basename $file`
   if [ "${base:0:1}" == "0" -o "$suffix" == "disabled" ]; then
      continue
   fi
   if [ -e "$scriptdirectory/$suffix" -a "$suffix" == "dup" ]; then
      process_action $file $suffix
   fi
done