Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit 8beffb1f21bfa7a58e085cb8bf5b31b950ffb3f5 0 parents
Andreas Olsson authored

Showing 4 changed files with 305 additions and 0 deletions. Show diff stats Hide diff stats

  1. +22 0 COPYING
  2. +22 0 README
  3. +103 0 check_tarsnap
  4. +158 0 tarsnap_completion
22 COPYING
... ... @@ -0,0 +1,22 @@
  1 +Copyright (c) 2009, 2010, 2011 Andreas Olsson
  2 +
  3 +Permission is hereby granted, free of charge, to any person
  4 +obtaining a copy of this software and associated documentation
  5 +files (the "Software"), to deal in the Software without
  6 +restriction, including without limitation the rights to use,
  7 +copy, modify, merge, publish, distribute, sublicense, and/or sell
  8 +copies of the Software, and to permit persons to whom the
  9 +Software is furnished to do so, subject to the following
  10 +conditions:
  11 +
  12 +The above copyright notice and this permission notice shall be
  13 +included in all copies or substantial portions of the Software.
  14 +
  15 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16 +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  17 +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18 +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19 +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20 +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21 +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22 +OTHER DEALINGS IN THE SOFTWARE.
22 README
... ... @@ -0,0 +1,22 @@
  1 +
  2 +My tarsnap helper scripts.
  3 +
  4 +* tarsnap_completion
  5 +A set of bash completions. Either drop it in your equivalent of
  6 +/etc/bash_completion.d/, or source it some other way.
  7 +
  8 +* check_tarsnap
  9 +Nagios plugin which works by reading a text file containing a Unix
  10 +timestamp of the most recent tarsnap backup. One way of creating such
  11 +a status file is by running the following at the end of your tarsnap
  12 +backup script.
  13 +
  14 + export TZ=UTC
  15 + RECENT=$(tarsnap --list-archives -v | grep -v "\.part" \
  16 + | sed -nre "s/^.+\t([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})$/\1/p" \
  17 + | sort | tail -n 1)
  18 + [ -n "$RECENT" ] && date -d "$RECENT" +%s > /var/lib/tarsnap/status/time
  19 +
  20 +Feel free to ping me at andreas@arrakis.se, if you have any comments or questions.
  21 +
  22 +// Andreas
103 check_tarsnap
... ... @@ -0,0 +1,103 @@
  1 +#!/usr/bin/env perl
  2 +
  3 +# export TZ=UTC
  4 +# RECENT=$(tarsnap --list-archives -v | grep -v "\.part" \
  5 +# | sed -nre "s/^.+\t([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})$/\1/p" \
  6 +# | sort | tail -n 1)
  7 +# [ -n "$RECENT" ] && date -d "$RECENT" +%s > /var/lib/tarsnap/status/time
  8 +
  9 +use strict;
  10 +use warnings;
  11 +use Getopt::Long;
  12 +Getopt::Long::Configure(qw(bundling no_getopt_compat));
  13 +
  14 +my ($version, $help, $file, $critical, $warning);
  15 +
  16 +my $result = GetOptions ("critical|c=i" => \$critical,
  17 + "warning|w=i" => \$warning,
  18 + "file|f=s" => \$file,
  19 + "help|h" => \$help);
  20 +
  21 +
  22 +
  23 +
  24 +unless($warning or $critical or $file or $help){
  25 + print << "EOSH";
  26 +usage:
  27 +check_tarsnap -f <file_with_unix_time> -w <hours_ago> -c <hours_ago>
  28 +check_tarsnap --help
  29 +EOSH
  30 + exit 3;
  31 +}
  32 +
  33 +if($help){
  34 + print << "EOH";
  35 +
  36 +usage:
  37 +check_tarsnap -f <file_with_unix_time> -w <hours_ago> -c <hours_ago>
  38 +check_tarsnap --help
  39 +
  40 +options:
  41 + -h, --help
  42 + Print this help screen
  43 + -f, --file=FILENAME
  44 + Time of most recent backup, specified as Unix time, is read from FILENAME
  45 + -w, --warning=INTEGER
  46 + Exit with WARNING status if INTEGER hours have passed since most recent backup
  47 + -c, --critical=INTEGER
  48 + Exit with CRITICAL status if INTEGER hours have passed since most recent backup
  49 +
  50 +EOH
  51 + exit 0;
  52 +}
  53 +
  54 +unless($file){
  55 + print "TARSNAP UNKNOWN - File, containing Unix time, must be specified\n";
  56 + exit 3;
  57 +}
  58 +
  59 +unless($warning or $critical){
  60 + print "TARSNAP UNKNOWN - At least either a Warning or a Critical value is required\n";
  61 + exit 3;
  62 +}
  63 +
  64 +unless(open(STATUS, '<', $file)){
  65 + print "TARSNAP UNKNOWN - Failed to read: $file\n";
  66 + exit 3;
  67 +}
  68 +
  69 +my $recent_backup = <STATUS>;
  70 +chomp($recent_backup);
  71 +close(STATUS);
  72 +
  73 +unless(length($recent_backup)){
  74 + print "TARSNAP CRITICAL - Backup date missing\n";
  75 + exit 2;
  76 +}
  77 +
  78 +unless($recent_backup =~ /^\d{10}$/){
  79 + print "TARSNAP CRITICAL - Invalid date: $recent_backup\n";
  80 + exit 2;
  81 +}
  82 +
  83 +my $now = time;
  84 +
  85 +if ($recent_backup > $now){
  86 + print "TARSNAP UNKNOWN - Backup claims to be in the future\n";
  87 + exit 3;
  88 +}
  89 +
  90 +my $delta = int(($now - $recent_backup) / 3600);
  91 +
  92 +if($critical and $delta >= $critical){
  93 + print "TARSNAP CRITICAL - Most recent backup $delta hours ago\n";
  94 + exit 2;
  95 +}
  96 +
  97 +if($warning and $delta >= $warning){
  98 + print "TARSNAP Warning - Most recent backup $delta hours ago\n";
  99 + exit 1;
  100 +}
  101 +
  102 +print "TARSNAP OK - Most recent backup $delta hours ago\n";
  103 +exit 0;
158 tarsnap_completion
... ... @@ -0,0 +1,158 @@
  1 +# /etc/bash_completion.d/tarsnap - bash-completion for tarsnap
  2 +#
  3 +# Besides supplying options it will also try to determine
  4 +# when it is suitible to complete what.
  5 +#
  6 +# Feel free to send comments or suggestions.
  7 +
  8 +shopt -s extglob
  9 +
  10 +_tarsnap ()
  11 +{
  12 + local cur prev wfilearg wpatharg wotherarg longopts shortopts options
  13 + COMPREPLAY=()
  14 + cur="${COMP_WORDS[COMP_CWORD]}"
  15 + prev="${COMP_WORDS[COMP_CWORD-1]}"
  16 +
  17 + # These options will be completed by the path to a filename.
  18 + wfilearg="--keyfile|--newer-than|--newer-mtime-than|--snaptime|--configfile|-T|-X"
  19 +
  20 + # These options will be completed by the path to a directory.
  21 + wpatharg="--cachedir"
  22 +
  23 + # These options requires a non-completable argument.
  24 + # They won't be completed at all.
  25 + wotherarg="--checkpoint-bytes|--exclude|-f|--include|--maxbw|--maxbw-rate|
  26 + |--maxbw-rate-down|--maxbw-rate-up|--newer|--newer-mtime|-s|
  27 + |--strip-components|--disk-pause"
  28 +
  29 + # Availible long options
  30 + longopts="--list-archives --print-stats --fsck --nuke --aggressive-networking \
  31 + --cachedir --check-links --checkpoint-bytes --chroot --dry-run \
  32 + --exclude --humanize-numbers --include --keep-newer-files --keyfile \
  33 + --lowmem --maxbw --maxbw-rate --maxbw-rate-down --maxbw-rate-up \
  34 + --newer --newer-mtime --newer-than --newer-mtime-than --nodump \
  35 + --noisy-warnings --null --numeric-owner --one-file-system --print-stats \
  36 + --fast-read --strip-components --snaptime --verylowmem --version \
  37 + --disk-pause --fast-read --normalmem --no-aggressive-networking \
  38 + --no-config-exclude --no-config-include --no-disk-pause --no-humanize-numbers \
  39 + --no-maxbw --no-maxbw-rate-down --no-maxbw-rate-up --no-nodump --no-print-stats \
  40 + --no-snaptime --no-store-atime --no-totals --store-atime --totals --recover \
  41 + --insane-filesystems --quiet --configfile --no-default-config --fsck-prune"
  42 +
  43 + # Availible short options
  44 + shortopts="-c -d -t -x -r -C -f -H -h -I -k -L -l -m -n -O -o -P -p -q -S -s -T -U -v -w -X -q"
  45 +
  46 + options=${longopts}" "${shortopts}
  47 +
  48 + case "$prev" in
  49 + @($wfilearg))
  50 + _filedir
  51 + return 0
  52 + ;;
  53 +
  54 + @($wpatharg))
  55 + _filedir -d
  56 + return 0
  57 + ;;
  58 +
  59 + @($wotherarg))
  60 + return 0
  61 + ;;
  62 +
  63 + esac
  64 +
  65 + if [[ ${cur} == -* ]]
  66 + then
  67 + COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
  68 + return 0
  69 + else
  70 + _filedir
  71 + return 0
  72 + fi
  73 +}
  74 +
  75 +_tarsnap_keygen ()
  76 +{
  77 + local cur prev wfilearg wotherarg longopts
  78 + COMPREPLAY=()
  79 + cur="${COMP_WORDS[COMP_CWORD]}"
  80 + prev="${COMP_WORDS[COMP_CWORD-1]}"
  81 +
  82 + # These options will be completed by the path to a filename.
  83 + wfilearg="--keyfile"
  84 +
  85 + # These options requires a non-completable argument.
  86 + # They won't be completed at all.
  87 + wotherarg="--user|--machine|--passphrase-mem"
  88 +
  89 + # Availible long options
  90 + longopts="--user --machine --keyfile --passphrased --passphrase-mem"
  91 +
  92 + case "$prev" in
  93 + @($wfilearg))
  94 + _filedir
  95 + return 0
  96 + ;;
  97 +
  98 + @($wotherarg))
  99 + return 0
  100 + ;;
  101 +
  102 + esac
  103 +
  104 + if [[ ${cur} == -* ]]
  105 + then
  106 + COMPREPLY=( $(compgen -W "${longopts}" -- ${cur}) )
  107 + return 0
  108 + fi
  109 +}
  110 +
  111 +_tarsnap_keymgmt ()
  112 +{
  113 + local cur prev wfilearg wotherarg longopts shortopts options
  114 + COMPREPLAY=()
  115 + cur="${COMP_WORDS[COMP_CWORD]}"
  116 + prev="${COMP_WORDS[COMP_CWORD-1]}"
  117 +
  118 + # These options will be completed by the path to a filename.
  119 + wfilearg="--outkeyfile"
  120 +
  121 + # These options requires a non-completable argument.
  122 + # They won't be completed at all.
  123 + wotherarg="--passphrase-mem"
  124 +
  125 + # Availible long options
  126 + longopts="--outkeyfile --nuke --passphrased --passphrase-mem"
  127 +
  128 + # Availible short options
  129 + shortopts="-r -w -d"
  130 +
  131 + options=${longopts}" "${shortopts}
  132 +
  133 + case "$prev" in
  134 + @($wfilearg))
  135 + _filedir
  136 + return 0
  137 + ;;
  138 +
  139 + @($wotherarg))
  140 + return 0
  141 + ;;
  142 +
  143 + esac
  144 +
  145 + if [[ ${cur} == -* ]]
  146 + then
  147 + COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
  148 + return 0
  149 + else
  150 + _filedir
  151 + return 0
  152 + fi
  153 +}
  154 +
  155 +
  156 +complete -F _tarsnap -o filenames tarsnap
  157 +complete -F _tarsnap_keygen -o filenames tarsnap-keygen
  158 +complete -F _tarsnap_keymgmt -o filenames tarsnap-keymgmt

0 comments on commit 8beffb1

Please sign in to comment.
Something went wrong with that request. Please try again.