I have prepared a script to search for backup file information on the Linux server. Script works fine for the most part except the echo statement inside an IF conditional block displays the message ''snapshot directory not found on xxxxx" even though the .snapshot directory is found a specified path. Here is the code.
#!/bin/bash
fdate=`date "+%Y-%m-%d"`
#set -x
df -h | grep sbl| awk '{print $5}' > /home/greg/fs.out
if [ ! -s /home/greg/fs.out ]
then
echo "No netapp filesystems found"
exit 1
else
echo "netapp filesystems found. continuing to the next step"
while read line
do
echo $line
if [ ! -d "${line}"/.snapshot ]
then
echo "snapshot directory not found on "$line""
else
cd ${line}/.snapshot
latest_bkp=`ls -l |grep -v snapmirror | tail -1|awk '{print $9}'`
bkp_fldr_timestamp=`echo "$latest_bkp"|cut -f3 -d .|cut -f1 -d _`
if [ ${fdate} != ${bkp_fldr_timestamp} ]
then
echo "Today's backup is missing for $line on `hostname`"
else
echo "File system backup has been taken for $line on `hostname`"
fi
fi
done < /home/greg/fs.out
fi
rm /home/greg/fs.out
exit 0
Here's the output when I execute the script.
[greg@sbldev01 ~]$ ./fs_bkpchk.sh
netapp filesystems found. continuing to the next step
snapshot directory not found on
/sblmnt/sb_devm2_sbl_01
File system backup has been taken for sblmnt/sb_devm2_sbl_01 on sbldev01
snapshot directory not found on
/sblmnt/sb_devm2_sbl_02
File system backup has been taken for /sblmnt/sb_devm2_sbl_02 on sbldev01
snapshot directory not found on
/sblmnt/sb_devm2_sbl_03
File system backup has been taken for /sblmnt/sb_devm2_sbl_03 on sbldev01
So, one could remove awk alltogether, but then read the file differently:
while read _ _ _ _ _ line
Specialy since the 5th variable is only the percentage used, we only name/use the 6th variable, which will include any later variable (path with spaces) as well.
Did some other small changes.
Hope this helps
#!/bin/bash
#set -x
#
# Variables
#
TMP=$HOME/tmp/${0##*/}-fs.out
HOSTNAME="${HOSTNAME:-$(hostname)}"
# This will print nothing on my system
SEARCH="sdb" ## sbl sdb
#
# Function
#
check4missing() { # VALUE
# Compares the current date versus the latest found backup dir
# Returns false if today's date is not found
fdate=$(date "+%Y-%m-%d")
latest_bkp=$(ls -l |grep -v snapmirror | tail -1|awk '{print $9}')
bkp_fldr_timestamp=$(echo "$latest_bkp"|cut -f3 -d .|cut -f1 -d _)
if [ "${fdate}" != "${bkp_fldr_timestamp}" ]
then return 1
else return 0
fi
}
#
# Action
#
df -h | grep $SEARCH > "$TMP"
#
# Display
#
if [ -s "$TMP" ]
then # Tempfile has content
while read _ _ _ _ line
do
this_snapshot="${line}/.snapshot"
if [ -d "$this_snapshot" ]
then
cd "$this_snapshot"
if check4missing
then
echo "Today's backup is missing for $this_snapshot on $HOSTNAME"
else
echo "File system backup has been taken for $this_snapshot on $HOSTNAME"
fi
else
echo "snapshot directory not found on \"$this_snapshot\""
fi
done < "$TMP"
else # Tempfile is empty
echo "No netapp filesystems found"
exit 1
fi
rm "$TMP"
exit 0
Thanks sea for rewriting the code. However when I execute your code on the server, I get the following output.
snapshot directory not found on ""
snapshot directory not found on ""
snapshot directory not found on ""
snapshot directory not found on ""
snapshot directory not found on ""
snapshot directory not found on ""
I verified that the temp file has some file system entries.
snapshot directory not found on ""
Today's backup is missing for /sblmnt/sb_devm2_sbl_01/.snapshot on sbldev01
snapshot directory not found on ""
Today's backup is missing for /sblmnt/sb_devm2_sbl_02/.snapshot on sbldev01
snapshot directory not found on ""
Today's backup is missing for /sblmnt/sb_devm2_sbl_03/.snapshot on sbldev01
i verified all 3 file systems listed above have .snapshot directories in them and they have today's backup files.
df has - per the POSIX-definition - the option "-P" for exactly this: formatting the output in a consistent way across all standard-adhering platforms.
I suggest you analyze the output of df -P and use this in scripts supposed to run platform-independently.
The problem here is that GNU df can format an entry on one line or on two lines.
It does that even if the output is not a terminal (I consider it a bug - a Unix df only does that if it sees a terminal).
So the better fix is df -P , that forces one line!
And | awk '{print $6}' or while read _ _ _ _ _ line . And no [ -z "$line" ] && continue is needed.