#!/bin/sh # Script to attempt to salvage a useable (and perhaps correct) # Red Hat rpm database. Intended for disaster recovery only. # Copyright (C) 2002 Michael Fratoni # # 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. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. usage () { cat <&2 Usage: `basename $0` -f [file_name] -p [path[s] to rpm files]. Where 'file_name' is a file containing a list of rpm packages that should be included in the rpm database, and -p is the path or paths that contain the rpm packages. EOF exit 0 } # Make sure we don't have less than 4 arguments. # (-f file -p path) if [ $# -lt "4" ]; then usage fi while getopts "f:p:" flags; do case $flags in f ) file_name=$OPTARG # File exists and is a regular file? if [ ! -f "$file_name" ]; then echo "Unable to locate the file "$file_name"." usage fi ;; p ) path=$OPTARG # Given path is a directory? if [ -d "$path" ]; then paths="$path" else echo "The directory $path doesn't exist, skipping." sleep 2 fi # Decrement the argument pointer. shift $(($OPTIND -1)) OPTIND="1" # Allow multiple paths by parsing options until there are # no more options or the -f flag is encountered. Allows # for people who may insist on reversing the options flags. until [ -z "$1" ] || [ "$1" == "-f" ]; do path="$1" # Path exists? if [ -d "$path" ]; then paths="$paths $path" else echo "$path isn't a valid directory, ignoring it..." echo sleep 2 fi shift done if [ -z "$paths" ]; then echo "No valid paths found!" echo "Exiting..." exit 1 fi if [ -z "$file_name" ]; then echo "No valid file list found!" echo "Exiting..." exit 1 fi ;; * ) usage ;; esac done # Only root can open the rpmdb files. if [ "`id -u`" != "0" ]; then cat <&2 You must be root to run this script! Only root has write permission to the rpm db. I don't like it any more than you do... Exiting... EOF exit 1 fi cat <&2 ############################################################" This script will attempt to recreate a useable and accurate rpm database. It is intended for disaster recovery only! Unless you have an empty or useless rpm database, you don't want to run this script. Any packages not found, and therefor not added to the rpm database will be listed in the file recover-rpm-db.missing. These packages should be added to the database manually. If you are able to obtain the files, this script can be run again, specifying recover-rpm-db.missing as the file to use. ############################################################" EOF echo -n "You have been warned! Shall I continue? [ yes / no ]: " read answer case $answer in [yY][eE][sS] | [yY] ) break ;; * ) echo "Exiting at the users request." exit 0 ;; esac # initialize "found" found=0 # Read the file containing the list of rpm packages. for rpm_name in $(cat $file_name); do for path in $paths; do if [ -e ${path}/${rpm_name} ]; then echo "Located ${rpm_name}, adding to database." /bin/rpm -ivh --justdb --force --nodeps ${path}/${rpm_name} found=1 # We have a match, break out of the search. # Saves some CPU cycles, and prevents finding duplicate # packages in another location. break fi done if [ "$found" = 0 ]; then echo "${rpm_name}" >> recover-rpm-db.missing fi # Reset found found=0 done echo exit 0