ShowNextTapeNeeded

From wiki.zmanda.com
Jump to navigation Jump to search

I have been having major issues with having to find time to log onto the server and find out what tapes to put in. We disabled the email from amcheck due to so many emails coming from several servers every day.

So, I wrote a quick script that (I'm surprised is still easy to configure) will go through the tapelist for all backup "programs" you have, output the tapes that will be needed on that day, and then (optionally) eject those tapes so you can put in the next tapes, and then (optionally) output the info to the screen, a printer, or an email.

I figured I'd share it. Feel free to use, edit, and do whatever with! hope it helps somebody! (I fire this at noon each day, so that backups that ran the previous night are ejected from our 2 tape drives and I can quickly swap out what tapes it tells me it will need that night.) Oh, and the "program" names are the names of the configurations of our backups. (Most will use Daily and Weekly, i don't like capitals....)

This should be able to be extended for larger tape-changers to get a list of tapes that are in the slots and compare to the tapes that are needed, ejecting only the slots that are not needed.



#!/bin/bash
#
# Generates a printed or emailed output of what tapes are next
# and what's expected tomorrow.
# ALSO will offline the tapes in the drives... if you want it to.
#
# What programs are there (daily, weekly, monthly, etc..... names) (aka... amanda config names)
# space-delimited
PROGRAMS=(daily weekly);

# Output last-run status check (0 for off, 1 for on)
STATUSCHECKS=(1 1)

# How many tapes does each program need (per run)?
TAPESNEEDED=(2 2);

# tapelist filenames
FILE[0]="/etc/amanda/daily/tapelist";
FILE[1]="/etc/amanda/weekly/tapelist";

# days of the week (0=Sunday) they run (array-like space-delimited string)
# I run daily on Mon-Thursday at like 11PM, and weekly on Saturday mornings at 2AM
DAYS[0]="1 2 3 4";
DAYS[1]="6";

#Names of each day of the week
DAYLIST=("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday");

#Number of days in the future to list (Starting on today, going for this many days)
DAYSTOSHOW=3;

# Other Filenames
OUTFILE="/etc/amanda/nexttape.txt";

# Output to Screen (0|1)
OUTPUT=0
# Print Options (0|1)
PRINT=1
# Email Options (0|1)
USEMAIL=0
[email protected]
SUBJECT="Amanda - Expected Tapes"

#Auto-Eject (performs an ammt offline)
AUTOEJECT=1
#Drives that will be ejected (iff AUTOEJECT)
DRIVES[0]="/dev/nst0"
DRIVES[1]="/dev/nst1"

#_____________________________________________________
#
#        DO NOT EDIT BEYOND THIS LINE
#_____________________________________________________

DAYOFWEEK=$(date "+%w");

DAYSOFTHEWEEK[0]=$DAYOFWEEK;
for((i = 1; i < $DAYSTOSHOW; i++ )); do
        let "DAYSOFTHEWEEK[i]=DAYSOFTHEWEEK[i-1] + 1";
        if [ ${DAYSOFTHEWEEK[$i]} -ge 7 ]; then
                DAYSOFTHEWEEK[$i]=0;
        fi
done

printf "\t%s - %s\n\t%s\n" "Amanda Tape Backup" "Next Expected Tape" "$(date)">> $OUTFILE
printf -- "--------------------------------------------------------------------\n" >> $OUTFILE
printf -- "       Program           Day                   Tapes                \n" >> $OUTFILE
printf -- "-------------------- ---------- ------------------------------------\n" >> $OUTFILE
for ((i = 0; i < ${#PROGRAMS[*]}; i++ )); do
        k=0;
        printf "%20s" "${PROGRAMS[$i]}" >> $OUTFILE
        for((s = 0; s < $DAYSTOSHOW; s++ )); do
                printf " %10s" ${DAYLIST[${DAYSOFTHEWEEK[$s]}]} >> $OUTFILE
                for((t = 0; t < ${TAPESNEEDED[i]}; t++ )); do
                        echo ${DAYS[$i]} | grep -E " *${DAYSOFTHEWEEK[$s]} *" > /dev/null 2>&1
                        if [ $? -eq 0 ]; then
                                let "k = k + 1"
                                j=${#TAPES[*]};
                                TAPES[$j]=$(tail -$k ${FILE[$i]} | head -1 | awk '{print $2 }')
                                if [ $s -eq 0 ]; then
                                        NEXTTAPES[${#NEXTTAPES[*]}]=${TAPES[$j]};
                                fi
                                printf " %10s"  ${TAPES[$j]} >> $OUTFILE
                        else
                                printf " %10s"  "----------" >> $OUTFILE
                        fi
                done

                printf "\n%20s" " " >> $OUTFILE
        done
        printf "\n" >> $OUTFILE
done

printf -- "--------------------------------------------------------------------\n" >> $OUTFILE
printf "Tapes Needed For Next Run:\t%s\n\n" "${NEXTTAPES[*]}" >> $OUTFILE
printf "Status Check:\n" >> $OUTFILE
printf -- "--------------------------------------------------------------------\n" >> $OUTFILE
for ((i = 0; i < ${#PROGRAMS[*]}; i++ )); do
        if [ ${STATUSCHECKS[$i]} -eq 1 ]; then
                printf "%20s\n" "${PROGRAMS[$i]}" >> $OUTFILE
                amstatus ${PROGRAMS[$i]} --summary >> $OUTFILE 2> /dev/null
                printf -- "--------------------------------------------------------------------\n" >> $OUTFILE
        fi
done

if [ $AUTOEJECT -eq 1 ]; then
        for ((i = 0; i < ${#DRIVES[*]}; i++ )); do
                ammt -t ${DRIVES[$i]} offline >> $OUTFILE 2>&1
        done
fi

if [ $USEMAIL -eq 1 ]; then
        cat $OUTFILE | Mail -s "Amanda - Expected Tapes" $EMAIL;
fi
if [ $PRINT -eq 1 ]; then
        lp $OUTFILE;
fi
if [ $OUTPUT -eq 1 ]; then
        cat $OUTFILE;
fi
rm -f $OUTFILE;