Re: Script to build system information. Guru needed :)
On Tue, 2004-12-21 at 18:30, Bob Alexander wrote:
> Slowly building my "disaster assurance" strategy on top of the file backups.
>
> Amongst other things I want to periodically run a shell script that
> would build a file with useful information. Here it is:
>
> lsmod
> lspci
> dpkg --get-selections
> sudo fdisk -l /dev/hda
> df
> uname -a
>
> Now the questions:
>
> 1) Other useful commands I am not thinking about ?
> 2) I would like to redirect the output to a file called with the
> catenation of 'uname -n'.'uname -r'.config.YYYYMMDDHHMM.txt where
> YYYYMMDDHHMM is a timestamp of the command execution time. How do I
> obtain such value ? How do I create the required backticks on my laptop
> keyboard (no separate numpad)
> 3) If I use cron to run this everyday, being this a laptop client, will
> the due command be executed if it's natural time has expired ?
> 4) What is the easiest way to keep the last N versions of such files ?
>
Here is an excerpt of a backup script I use that shows one way of
keeping the last N versions of some files, probably you can adapt it to
your needs:
hth
peter
#!/bin/sh
(.....)
# Each of these directories corresponds to the archive name in the
backup.conf
# file. If necessary, the script creates the directory. Within the
directory
# are n archive files (configurable by REATAIN=n) with a naming
convention
# <archive>.<YYYYMMDD>.tar.gz
RETAIN=25 #number of copies of the archive to keep
(...)
CUT='/usr/bin/cut'
GZIP='/bin/gzip'
RM='/bin/rm'
WC='/usr/bin/wc'
MKDIR='/bin/mkdir'
DATE='/bin/date'
TAR='/bin/tar'
HEAD='/usr/bin/head'
e
echo "Will backup with stamp $STAMP and DBSTAMP $DBSTAMP"
## Note. $$ evaluates to pid of current proc.
## The following ls command will return an error
## if there do not yet exist db backup files.
${LS} ${WDIR}/backup/all_databases-* > $$.TMP
if [ ! -f $$.TMP ] ; then
echo "Need to create $$.TMP"
touch $$.TMP
fi
NFLDUMP=`${WC} -l < $$.TMP` ## if $$.TMP is empty, we will get ZERO as
needed
echo "NFLDUMP $NFLDUMP"
NUMBER_OF_DUMPS_TO_DEL=`/usr/bin/expr ${NFLDUMP} \- ${RETAIN}`
if [ ${NUMBER_OF_DUMPS_TO_DEL} -gt "0" ]
then
${RM} `${HEAD} -${NUMBER_OF_FILES_TO_DEL} $$.TMP`
fi
echo "NUMBER DUMSP TO DEL $NUMBER_OF_DUMPS_TO_DEL"
${RM} -rf $$.TMP
while read i
do
echo '**** Backing up ' $i
STAMP=`${DATE} "+%Y%m%d"`
TIME=`${DATE} "+%H:%M:%S"` # date and time stamp
echo "**** Time stamp: " ${STAMP} - ${TIME}
TARGET=`echo $i | ${CUT} -f1 -d:`
PATH=`echo $i | ${CUT} -f2 -d:`
LOCATION=${WDIR}
echo Archive: ${TARGET}
echo Path: ${PATH}
echo Stored in: ${LOCATION}/backup
if [ ! -e ${PATH} ]
then
echo Configuration error:
echo Target: ${TARGET}
echo Path: ${PATH} does not exist
echo "CHECK CONFIG FILE !!!!!!!!!!!!!!!!!!!!"
exit 1
fi
if [ ! -d ${LOCATION}/backup/${TARGET} ]
then
echo Creating target directory \`${TARGET}\`
${MKDIR} ${LOCATION}/backup/${TARGET}
fi
cd ${LOCATION}/backup/${TARGET}
${LS} ${LOCATION}/backup/${TARGET}.*.tar.gz > $$.TMP
NFL=`${WC} -l < $$.TMP`
NUMBER_OF_FILES_TO_DEL=`/usr/bin/expr ${NFL} \- ${RETAIN}`
if [ ${NUMBER_OF_FILES_TO_DEL} -gt "0" ]
then
${RM} `${HEAD} -${NUMBER_OF_FILES_TO_DEL} $$.TMP`
fi
${RM} -rf $$.TMP
echo **** Creating ${LOCATION}/backup/${TARGET}.${STAMP}.tar.gz
echo "*** full backup of ${PATH} on ${STAMP} at ${TIME} ****"
echo "*** tarrifying: ${LOCATION}/backup/${TARGET}.${STAMP}.tar.gz
***"
${TAR} cvf - ${PATH} | ${GZIP} -9 >
${LOCATION}/backup/${TARGET}/${TARGET}.${STAMP}.tar.gz
if [ $? != $SUCCESS ] ; then
echo "Could not tar $TARGET"
exit 1
fi
echo 'DONE'
done < $CONFIG_FILE
# Explanations
# 1) Read one line at a time from $CONFIG_FILE, store in i
# while read i
# do
# done < $CONFIG_FILE
#
# 2) cut
# cut -f1 first field; -f2 second field -d delimiter
# cut -f2 -d: means take the second field according to delimiter ':'
# in this case we have myPerl:/home/peter/myPerl being processed
# to /home/peter/myPerl
#
--
Peter N. Robinson
peter.robinson@t-online.de
peter.robinson@charite.de
http://www.charite.de/ch/medgen/robinson/
Reply to: