#! /bin/bash # # 'saveconfig' # # - writes important configuration data to USB stick # - archives important configuration files to USB stick # # Note: - a few files/commands are only accessible as root. # - most file locations here "should be" LSB compliant # # ------------------------------------------------------------------- # Creation: 2000-04-20, JHa. # Revision: 2001-01-07, JHa (sorted & multicolumn rpm output) # 2001-11-04, JHa (adjusted for new directory structure) # 2002-09-29, JHa (some changed file locations) # 2004-12-29, JHa (some changed file locations) # 2005-05-21, JHa (now /media/floppy) # 2006-12-26, JHa (updated file list; now allows other media) # 2007-12-21, JHa (misc updates; saves to usb) # 2011-01-01, JHa (complete rewrite in bash) # ------------------------------------------------------------------- # Copyright (c) 2000-2011 Joerg Hau . # # This program is free software; you can redistribute it and/or # modify it under the terms of version 2 of the GNU General Public # License as published by the Free Software Foundation, provided # that the copyright notice remains intact even in future versions. # # 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. # ------------------------------------------------------------------- # ------------------------------------------------------------ # subroutine to check error status of last command # argument: error message # will exit with rc=1 if errorlevel was not 0 # ------------------------------------------------------------ function errcheck() { if [ $? != 0 ]; then echo "Abort: $1, status: $?" exit 1 fi } # ------------------------------------------------------------ # subroutine to print usage mode # ------------------------------------------------------------ function usage() { cat << eof ${0##*/} - Extract and save system configuration data. Copyright (c) 2000...2011 Joerg Hau . This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. This program is distribute[ -z $DEST ] || errcheck "You must specify the destination directory."d 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. Usage: ${0##*/} /path/to/save-directory eof } # # === main script starts here === # # see if you have the right to access all this stuff # if [ `whoami` != 'root' ]; then echo "You must be root to run this script." exit 2 fi # if script is called w/o arguments, give usage instructions # if (( $# < 1 )) ; then usage exit 1 fi # DEST was passed as an argument on the command line # DEST="$1" [ -d $DEST ] || errcheck "Directory '$DEST' does not exist" [ -r $DEST ] || errcheck "Directory '$DEST' exists but is not writeable" # Get the system name (without domain suffix) # HOST=${HOSTNAME%%.*} # derive archive basename from system name and actual date (not time) # ARCH=$HOST.`date +%Y%m%d` LOGFILE=$DEST/$ARCH.txt # Prepare a file containig the names of files and directories to be archived # LISTFILE="/tmp/temp.$$" echo "--- Going to save configuration data and files to $DEST ---" ######################################################################## # Part I : extract configuration data and write them into a text file ######################################################################## # list of files to print # filelist="/etc/fstab \ /etc/init.d/boot.local \ /boot/grub/menu.lst \ /etc/crontab \ /etc/dhcpd.conf \ /etc/ntp.conf \ /etc/hosts /etc/hosts.equiv \ /etc/hosts.allow /etc/hosts.deny \ /etc/hosts.equiv /etc/hosts.lpd \ /etc/exports /etc/resolv.conf \ /etc/printcap /etc/ppp/options" # Here we go ... # echo "==================================================================" > $LOGFILE echo "Configuration of $HOST, as of "`date`"." >> $LOGFILE echo "==================================================================" >> $LOGFILE echo "" >> $LOGFILE echo "----- Output of 'uname -a' -----" >> $LOGFILE /bin/uname -a >> $LOGFILE echo "" >> $LOGFILE echo " " >> $LOGFILE echo "----- Output of 'fdisk -l' -----" >> $LOGFILE /sbin/fdisk -l >> $LOGFILE echo " " >> $LOGFILE echo " " >> $LOGFILE echo "----- Disk usage -----" >> $LOGFILE /bin/df -h >> $LOGFILE echo " " >> $LOGFILE echo " " >> $LOGFILE echo "----- Networking -----" >> $LOGFILE /sbin/ifconfig >> $LOGFILE echo " " >> $LOGFILE echo " " >> $LOGFILE echo "----- Running Services -----" >> $LOGFILE /sbin/chkconfig --list|grep ":on" >> $LOGFILE echo " " >> $LOGFILE for i in $filelist; do echo " " >> $LOGFILE echo "----- "$i " -----" >> $LOGFILE # 1st grep removes comment lines (starting with #) # 2nd removes empty lines grep -v "^#" $i | grep -v ^$ >> $LOGFILE echo " " >> $LOGFILE done echo " " >> $LOGFILE echo "----- iptables-save -----" >> $LOGFILE iptables-save >> $LOGFILE echo " " >> $LOGFILE echo " " >> $LOGFILE echo "----- ip6tables-save -----" >> $LOGFILE ip6tables-save >> $LOGFILE echo " " >> $LOGFILE echo " " >> $LOGFILE echo "----- /etc/modprobe.d/ -----" >> $LOGFILE ls /etc/modprobe.d/ >> $LOGFILE echo " " >> $LOGFILE echo " " >> $LOGFILE echo "----- rpm packages installed -----" >> $LOGFILE /bin/rpm -qa | sort | pr -3 -l 1 >> $LOGFILE echo " " >> $LOGFILE echo " " >> $LOGFILE echo "----- Listing of '/usr/local/bin' -----" >> $LOGFILE ls /usr/local/bin | pr -3 -l 1 >> $LOGFILE echo " " >> $LOGFILE echo " " >> $LOGFILE echo "----- Listing of '/usr/local/src' -----" >> $LOGFILE ls /usr/local/src | pr -3 -l 1 >> $LOGFILE echo " " >> $LOGFILE echo " " >> $LOGFILE echo "----- Listing of '/opt' -----" >> $LOGFILE ls /opt | pr -3 -l 1 >> $LOGFILE echo " " >> $LOGFILE echo "System configuration log written to $LOGFILE." ######################################################################## # Part II: tar the configuration files to $DEST ######################################################################## # create $LISTFILE ... # echo "/etc/fstab /etc/passwd /etc/group /etc/sysconfig/ /etc/init.d/ /etc/profile.local /etc/X11/ /etc/crontab /etc/xinetd.conf /etc/xinetd.d /etc/services /etc/dhcp.conf /etc/ntp.conf /etc/resolv.conf /etc/nsswitch.conf /etc/samba/ /etc/httpd/ /boot/grub/menu.lst /etc/modprobe.d/ /etc/hosts /etc/hosts.equiv /etc/hosts.allow /etc/hosts.deny /etc/hosts.equiv /etc/hosts.lpd /etc/exports /etc/networks /etc/printcap /etc/ppp/" > $LISTFILE echo "Archiving configuration data of $HOST to $DEST/$ARCH.tgz ..." # We do NOT use absolute pathnmames. This is safer on extraction, # as you will have to be in '/' to accidently overwrite files ;-) # tar -czf $DEST/$ARCH.tgz -T $LISTFILE # no error check here; we do not want to interrupt the script # if some file is not present on the system. # Note: With these files, you may want to use interactive mode # while extracting, this is 'tar xwzf /floppy/archivename'. # (Extraction is relative to your actual directory). echo "Configuration files copied." rm $LISTFILE exit 0