Amanda-Users

Re: Search file in amanda database

2005-06-09 16:53:32
Subject: Re: Search file in amanda database
From: Anthony Valentine <avalentine AT sbsalaska DOT com>
To: "Montagni, Giovanni" <gmontagni AT manord DOT com>
Date: Thu, 09 Jun 2005 12:30:04 -0800
Quite some time ago, I wrote a script to do this.  It still works on my
system, but I am still running 2.4.2p2 on AIX.  I can't promise that it
will work for you, but here it is if you want to try.

Anthony



Montagni, Giovanni wrote:
> Hi to all! 
> I've read the docs, searched through the web, but i cannot find the answer to 
> this question:
> 
> is possible to search a file in amanda database? for example i have a file 
> called pippo.sh backed up somewhere in a tape i don't rebember. i only know 
> filename.
> Can i do a search  to find the tape and the path of pippo.sh?
> 
> thanks for your help,
> 
> Montagni Giovanni
> 
> 

#!/bin/bash

### Note above that this script uses Bash.  It should run fine under a 
different shell,
### but I haven't tested it.

### Start of Script
###########################################################################
#    amindexsearch  -  Searches through and Amanda index directory searching
#                      for specified patterns and (optionally) a date
#                             -------------------
#    begin                : Thu Jul 25 09:02:56 AKDT 2002
#    version              : 0.2
#    copyright            : (C) 2002 by Anthony Valentine
#    email                : amv AT akvalentine DOT com
###########################################################################

###########################################################################
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 2 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
###########################################################################


### The following line sets up our local environment.  Everybody else should
### leave it commented out.
#. /etc/profile.gemini


## Turn on debugging
DEBUG=false
if [ "${DEBUG}" = "true" ]; then
     set -vx
fi


if [ $# -lt 2 ] || [ "${1}" = "--help" ] || [ "${1}" = "-h" ]; then
     echo " "
     echo "Usage: $(basename $0) <configname> [-d datestring] <pattern1> 
[pattern2] ... [paternN]"
     echo " "
     echo "Where:  configname is an Amanda configuration name."
     echo "        datestring is a string in YYYYMMDD format (ex. 20020725)."
     echo "        patternX are the grep regexp patterns to search for."
     echo " "
     echo "Note: The Config name is case sensitive, but the search patterns are 
not."
     echo "Note: You must include a search pattern even if supply a datestring. 
 If"
     echo "      you want to search for everything for a particular date, use a 
'.' for"
     echo "      the search pattern"
     echo " "
     exit 1
fi


### Set TEMPFILE variables; remove the files before we start
TEMPFILE1=/tmp/amindextmp1.$$.${RANDOM}
rm -f ${TEMPFILE1}

### Set program names and locations
ZCAT=/usr/linux/bin/zcat
GREP=/usr/linux/bin/grep
SUDO=/bin/sudo
#PAGER=/bin/less

### Set amanda user and group
AMANDAUSER=amanda
AMANDAGROUP=backup


### Find the amanda home directory in the password file
AMANDAHOME=$(cat /etc/passwd |${GREP} "^${AMANDAUSER}:" | cut -d: -f 6)

### If your Amanda config directory isn't in the standard location,
### uncomment the following line and set it to it's location.
#AMCONFIG=""/usr/local/etc/amanda/${1}"


### If ${AMCONFIG} variable has no value, check standard locations for a valid 
config directory
if [ -z "${AMCONFIG}" ]; then
     if [ -d ${AMANDAHOME}/${1} ]; then   ### Check first in Amanda's home dir 
(that's where I keep mine)
          AMCONFIG=${AMANDAHOME}/${1}
     elif [ -d "/usr/local/etc/amanda/${1}" ]; then  ### Then check default 
location
          AMCONFIG="/usr/local/etc/amanda/${1}"
     else
          echo " "
          echo "Unknown Config Name"  ### If not found, exit with an error
          echo " "
          exit 2
     fi
fi


### Check for datestring; note that DATESTRING is NOT currently validated as an 
actual date
if [ "${2}" = "-d" ]; then
     DATESTRING=${3}
     shift 2
     ### Check for the existance of a search pattern; exit if none
     if [ -z "${2}" ]; then
          echo "No search pattern found.  Aborting. . ."
          exit 3
     fi
else
     DATESTRING="."
fi

### Get the index directory from the amanda.conf file
INDEXDIR=$(cat ${AMCONFIG}/amanda.conf | ${GREP} "^indexdir" | awk '{print $2}' 
| sed 's/"//g')


### Create a file of search patterns entered on the command line
shift 1
while [ $# -gt 0 ]
do
     echo ${1} >> ${TEMPFILE1}
     shift 1
done


### Print a header
echo "LVL HOST      DATE     DISK                     FILE"
echo "--- --------- -------- ------------------------ 
---------------------------------"


### Search through index dir for matches.
cd ${INDEXDIR}
for INDEX in $(find . -print | ${GREP} ${DATESTRING})
do
     ### Search inside each compressed index file; 
     for FILE in $(${SUDO} -u ${AMANDAUSER} ${ZCAT} ${INDEX} 2>/dev/null | 
${GREP} -if ${TEMPFILE1} 2>/dev/null)
     do
          ### get the HOST and pad it out to 10 chars long (for neater printing)
          SETHOSTTMP=$(echo ${INDEX} | awk -F/ '{print $2}')
          SETHOST=$(echo "${SETHOSTTMP}"'""""""""""' | sed 
's/^\(..........\).*$/\1/')

          ### get the DISKLIST and pad it out to 25 chars (for neater printing)
          SETDISKTMP=$(echo ${INDEX} | awk -F/ '{print $3}' | sed 's/_/\//g')
          SETDISK=$(echo "${SETDISKTMP}"'"""""""""""""""""""""""""' | sed 
's/^\(.........................\).*$/\1/')

          ### get the DATE and BACKUP LEVEL
          DATELEVEL=$(echo ${INDEX} | awk -F/ '{print $4}')
          SETDATE=$(echo ${DATELEVEL} | awk -F_ '{print $1}')
          LEVEL=$(echo ${DATELEVEL} | awk -F_ '{print $2}' | awk -F. '{print 
$1}')

          ### Print all that, the name of the file and replace the pad char " 
with spaces
          echo "${LEVEL}   ${SETHOST}${SETDATE} ${SETDISK}${FILE}"| sed 's/\"/ 
/g'
     done
done


### Clean up
rm -f ${TEMPFILE1}


### End of Script
exit 0
<Prev in Thread] Current Thread [Next in Thread>