Networker

[Networker] SHARED: Client-side report script

2006-10-31 15:53:29
Subject: [Networker] SHARED: Client-side report script
From: Andrew Dietz <andrew.dietz AT OIT.GATECH DOT EDU>
To: NETWORKER AT LISTSERV.TEMPLE DOT EDU
Date: Tue, 31 Oct 2006 15:19:12 -0500
For whatever it's worth, I thought I'd share a script we wrote to allow server administrators to run their own reports from their Linux/Solaris machines to get extended backup status information from the Networker server.

A sample output from a Linux workstation configured to only backup / and /home is as follows:

# gtebsreport
      name                date      level       size     volume
---------- ------------------- ---------- ---------- ----------
  /dev/shm
         / 10/31/2006 01:00:06       incr      84 MB     QA0251
      /sys
  /dev/pts
     /home 10/31/2006 01:00:05       incr    3533 MB     QA0249
     /proc


Here's the script:

<snip>

#!/bin/sh

USAGE="gtebsreport [-r]"

#
# print a report on the backup status for all the partitions on a client
#

PATH=/usr/ucb:/bin:/usr/bin:/usr/bsd:/etc:/usr/sbin:/usr/local/bin; export PATH
TMP="/tmp/bkup$$"
EXCLUDE="/var/gtebs/backup.exclude"
EXCLUDE="/tmp/backup.exclude"
SRVLIST=/nsr/res/servers
BKUPHIST=/tmp/backup.history-$USER
CLIENT=`hostname`

RAWDATE=
while getopts r i; do
    case $i in
        r)      RAWDATE="YES";;
        \?)     echo $USAGE; exit 1;;
    esac
done
shift `expr $OPTIND - 1`

# get paritions on client
if [ -r "/etc/fstab" ]; then
awk '$1 !~/^#/ && $2 ~/^\// && $3 !~/nfs/ && $3 !~/hsfs/ && $4 !~/noauto/ { print $2 }' /etc/fstab | sort >$TMP.client
elif [ -r "/etc/vfstab" ]; then
awk '$1 !~/^#/ && $3 ~/^\// && $4 ~/^ufs/ { print $3 }' /etc/vfstab | sort >$TMP.client
else
    echo "Error: Do not know how to determine partitions"
    exit 1
fi

SERVER=`grep -v "^#" $SRVLIST | head -1`
if [ -f /usr/sbin/nsr/mminfo ]; then
    MMINFO="/usr/sbin/nsr/mminfo"
elif [ -f /usr/sbin/mminfo ]; then
    MMINFO="/usr/sbin/mminfo"
elif [ -f /usr/etc/mminfo ]; then
    MMINFO="/usr/etc/mminfo"
else
    echo "Error: could not find mminfo"
    exit 0
fi
if [ -f /usr/sbin/nsr/nsradmin ]; then
    NSRADMIN="/usr/sbin/nsr/nsradmin"
elif [ -f /usr/sbin/nsradmin ]; then
    NSRADMIN="/usr/sbin/nsradmin"
elif [ -f /usr/etc/nsradmin ]; then
    NSRADMIN="/usr/etc/nsradmin"
else
    echo "Error: could not find nsradmin"
    exit 0
fi

# get partitions listed in backup trail
cat <<EOF >$TMP
show save set
print type: NSR client; name: $CLIENT
EOF
$NSRADMIN -s $SERVER -i $TMP > $TMP.trail

# get recent backups for client
$MMINFO -c $CLIENT -s $SERVER -t '7 days ago' -ont -xc: -r 'name,nsavetime,level,sumsize,volume' >$TMP.bkup

perl -e <$TMP.client '
        use File::stat;

        $raw = $ARGV[4];

        # initialize partitions
        while (<STDIN>) {
            chop;
            $client{$_} = 0;
        }

        # get exclutions
        if (open(EXCLD, $ARGV[2])) {
            while(<EXCLD>) {
                chomp;
                $excld{$_} = 1;
            }
            close(EXCLD);
        }

        # get history
        if (open(HIST, $ARGV[3])) {
            while(<HIST>) {
                chomp;
                $history{$_} = 1;
            }
            close(HIST);
        }

        # get backup report
        if (open(BKUP, $ARGV[1])) {
            while(<BKUP>) {
                chomp;
                next if (/^name:/);    # throw away header line
                @x = split(/:/);
                $report{$x[0]} = join(":", @x[1..$#x]);
            }
            close(BKUP);
        }

        # get partitions in backup trail
        if (open(BKUP, $ARGV[0])) {
            while(<BKUP>) {
                chomp;
                s/^\s+save set: +//;
                s/, +/,/g;
                s/;$//;
                foreach (split(/,/)) {
                    if (m/all/i) {
                        foreach $x (keys %client) {
                            $trail{$x} = "";
                        }
                    } else {
                        $trail{$_} = "";
                    }
                }
            }
            close(BKUP);
        }

        # check against backed up partitions
        $fix = 0;
        foreach (keys %trail) {
            if (defined($client{$_})) {
                $client{$_} |= 1;
            } else {
                $trail{$_} = "na";
            }
        }

        # check against excluded partitions
        foreach (keys %excld) {
            if (defined($client{$_})) {
                $client{$_} |= 2;
            } else {
                $excld{$_} = "na";
            }
        }

        # print out report
printf STDOUT "%10s %19s %10s %10s %10s\n", "name","date","level","size","volume"; print STDOUT "---------- ------------------- ---------- ---------- ----------\n";
        foreach (keys %trail) {
            if ($trail{$_} eq "") {
                printf STDOUT "%10s", $_;
                if ($report{$_} ne "") {
                    @x = split(/:/, $report{$_});
                    $date = $x[0];
                    if ($raw eq "") {
                        @tm = localtime($x[0]);
$date = sprintf("%02d/%02d/%04d %02d:%02d:%02d", $tm[4] + 1, $tm[3], $tm[5] + 1900, @tm[2,1,0]);
                    }
                    printf STDOUT " %19s %10s %10s %10s", $date, @x[1,2,3];
                }
                print STDOUT "\n";
            } else {
                printf STDOUT "%10s <%s>\n", $_, $trail{$_};
            }
        }
        foreach (keys %excld) {
            printf STDOUT "%10s <%s", $_, "excluded";
            if ($excld{$_} eq "") {
                printf STDOUT "-%s", $excld{$_};
            }
            print STDOUT ">\n";
        }
        foreach (keys %client) {
            next if $client{$_};   # report only missing partitions
            printf STDOUT "%10s <missing", $_;
            if (!-d $_) {
                print STDOUT "-na";
            }
            if (! defined $history{$_}) {
                print STDOUT "-no-backup";
            }
            print STDOUT ">\n";
        }

        # update history
        if (open(HIST, ">$ARGV[3].N")) {
            foreach (keys %report) {
                print HIST "$_\n";
            }
            close(HIST);
        }

        exit 0;' $TMP.trail $TMP.bkup $EXCLUDE $BKUPHIST $RAWDATE

rm -f $BKUPHIST.old
if [ -f $BKUPHIST ]; then
    ln $BKUPHIST $BKUPHIST.old
fi
mv $BKUPHIST.N $BKUPHIST

rm -rf $TMP*

exit 0
</snip>

Feel free to modify it to your needs.

Cheers,
Andrew

To sign off this list, send email to listserv AT listserv.temple DOT edu and type 
"signoff networker" in the
body of the email. Please write to networker-request AT listserv.temple DOT edu 
if you have any problems
wit this list. You can access the archives at 
http://listserv.temple.edu/archives/networker.html or
via RSS at http://listserv.temple.edu/cgi-bin/wa?RSS&L=NETWORKER

<Prev in Thread] Current Thread [Next in Thread>
  • [Networker] SHARED: Client-side report script, Andrew Dietz <=