Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rb:compresslogfiles [15/10/2012 11:25]
andrew
rb:compresslogfiles [10/10/2013 15:21] (current)
Line 12: Line 12:
  
  
-  [user@log01 scripts]$ ​cat compresslogfiles.sh +  [works@gy-asp-log01 scripts]$ ​more compresslogfiles.sh 
-  #​!/​bin/​bash +  #​!/​bin/​bash ​                  ​ 
-   +                                                                                                                                                                 ​ 
-  #Written Andrew Stringer 28 September 2012 to compress log files on gy-asp-log01 +  #Written Andrew Stringer 28 September 2012 to compress log files on srv-log01 ​                    ​ 
-  +                                                                                                                                                                 ​
   #turn on debugging, -x starts, +x stops   #turn on debugging, -x starts, +x stops
   set +x   set +x
   ​   ​
-  DIRTOCOMPRESS=/​home/user/​scripts/​dir-to-compress.txt+  DIRTOCOMPRESS=/​home/​scripts/​dir-to-compress.txt
   ​   ​
-  #set to 3 for testing, this forces sending mail, normally should be 0+  #set to 3 for testing, this forces sending mail, 1 & 2 are used for errors, normally should be 0
   SENDEMAIL=3   SENDEMAIL=3
 +  #​EMAIL=ww@zzz.com
   EMAIL=andrew.stringer@zzz.com   EMAIL=andrew.stringer@zzz.com
   ​   ​
Line 34: Line 35:
   DAY=`date +%a`   DAY=`date +%a`
   ​   ​
-  #Check user is user +  #Check user is works 
-  if [ "​${USER}"​ = "user" ]; then +  if [ "​${USER}"​ = "works" ]; then 
-     echo "​Running as user user" ​ >> ${TMPLOG}+     echo "​Running as user works" ​ >> ${TMPLOG}
      echo " " ​ >> ${TMPLOG}      echo " " ​ >> ${TMPLOG}
      else      else
-     echo "​Sorry,​ you need to be the user user to run this script."​+     echo "​Sorry,​ you need to be the works user to run this script."​
      exit 1      exit 1
   fi   fi
Line 53: Line 54:
   ​   ​
   #Write out header to email tmp file.   #Write out header to email tmp file.
-  echo "Log file compressor on log01" > ${TMPLOG} +  echo "Log file compressor on srv-log01" > ${TMPLOG} 
-  echo "​============================"​ >> ${TMPLOG}+  echo "================================"​ >> ${TMPLOG}
   echo ""​ >> ${TMPLOG}   echo ""​ >> ${TMPLOG}
-  echo "From /home/user/​scripts/​compresslogfiles.sh on log01" >> ${TMPLOG}+  echo "From /home/works/​scripts/​compresslogfiles.sh on srv-log01" >> ${TMPLOG}
   echo ""​ >> ${TMPLOG};   echo ""​ >> ${TMPLOG};
   echo ""​ >> ${TMPLOG};   echo ""​ >> ${TMPLOG};
Line 65: Line 66:
   ​   ​
   #from df   #from df
-  SPACEATSTART=`df -h | grep /var/log/ | cut -d' ' -f28`+  SPACEATSTART=`df -h | grep /var/log/GRY | cut -d' ' -f28`
   ​   ​
   ​   ​
 +  #find /​var/​log/​GRY/​app/​srv-app01/​ -maxdepth 1 -type f -exec bzip2 {} \;
   ​   ​
   #set up some counters   #set up some counters
Line 91: Line 93:
   ​   ​
           #test if file is greater than zero bytes long, else skip it.           #test if file is greater than zero bytes long, else skip it.
-          if [ -s "​${FILE}"​ ] +          if [ -s "​${FILE}"​ ] then
-          ​then+
   ​   ​
           if [[ ${FILE} =~ \.bz2$ ]] ; then           if [[ ${FILE} =~ \.bz2$ ]] ; then
                   #do nothing except move file. Don't add bzip2 to mv as it already ends bzip2                   #do nothing except move file. Don't add bzip2 to mv as it already ends bzip2
                   echo "$FILE is already bzip2'​ed" ​ >> ${DEBUGLOG}                   echo "$FILE is already bzip2'​ed" ​ >> ${DEBUGLOG}
-                  echo "---> mv ${FILE} to  ${DIR}/​archive." ​ >> ${DEBUGLOG}+                  echo "-> mv ${FILE} to  ${DIR}/​archive." ​ >> ${DEBUGLOG}
                   echo " " ​ >> ${DEBUGLOG}                   echo " " ​ >> ${DEBUGLOG}
                   mv ${FILE} ${DIR}/​archive                   mv ${FILE} ${DIR}/​archive
Line 105: Line 106:
                   #do nothing except move, zip file may be a compressed directory structure which bzip won't handle ok                   #do nothing except move, zip file may be a compressed directory structure which bzip won't handle ok
                   echo "​${FILE} is zip archive" ​ >> ${DEBUGLOG}                   echo "​${FILE} is zip archive" ​ >> ${DEBUGLOG}
-                  echo "-----> mv ${FILE} to  ${DIR}/​archive." ​ >> ${DEBUGLOG}+                  echo "-> mv ${FILE} to  ${DIR}/​archive." ​ >> ${DEBUGLOG}
                   echo " " ​ >> ${DEBUGLOG}                   echo " " ​ >> ${DEBUGLOG}
                   mv ${FILE} ${DIR}/​archive                   mv ${FILE} ${DIR}/​archive
Line 111: Line 112:
   ​   ​
           elif [[ ${FILE} =~ \.gz$ ]] ; then           elif [[ ${FILE} =~ \.gz$ ]] ; then
-                  echo "--> ${FILE} is gzip'​ed,​ so decompress it" ​ >> ${DEBUGLOG}+                  echo "-> ${FILE} is gzip'​ed,​ so decompress it" ​ >> ${DEBUGLOG}
                   #use -f to force overwriting to prevent script stalling waiting for user input "​y"​                   #use -f to force overwriting to prevent script stalling waiting for user input "​y"​
                   gunzip -f -v ${FILE}                   gunzip -f -v ${FILE}
Line 118: Line 119:
                   FILE=${FILE%.gz}                   FILE=${FILE%.gz}
   ​   ​
-                  #bzip2 -v application.log.2012-07-16.16Jul 2>&1 | cut -d','​ -f3 | cut -d'​%'​ -f1 - #​getcompress stats+                  #bzip2 -v form.log.2012-07-16.16Jul 2>&1 | cut -d','​ -f3 | cut -d'​%'​ -f1 - #​getcompress stats
                   echo -n "​-->​ bzipping ${FILE}, " ​ >> ${DEBUGLOG}                   echo -n "​-->​ bzipping ${FILE}, " ​ >> ${DEBUGLOG}
                   RATIO=`bzip2 -v ${FILE} 2>&1 | cut -d','​ -f3 | cut -d'​%'​ -f1`                   RATIO=`bzip2 -v ${FILE} 2>&1 | cut -d','​ -f3 | cut -d'​%'​ -f1`
Line 125: Line 126:
                   COMPRESSTOTAL=`echo "​${COMPRESSTOTAL}+${RATIO}"​ | bc -l`                   COMPRESSTOTAL=`echo "​${COMPRESSTOTAL}+${RATIO}"​ | bc -l`
   ​   ​
-                  echo "-------> mv ${FILE}.bz2 to ${DIR}/​archive." ​ >> ${DEBUGLOG}+                  echo "​--->​ mv ${FILE}.bz2 to ${DIR}/​archive." ​ >> ${DEBUGLOG}
                   echo " " ​ >> ${DEBUGLOG}                   echo " " ​ >> ${DEBUGLOG}
                   mv ${FILE}.bz2 ${DIR}/​archive                   mv ${FILE}.bz2 ${DIR}/​archive
Line 132: Line 133:
   ​   ​
   ​   ​
-          else+          else 
                   #assume file is not compressed                   #assume file is not compressed
-                  echo -n "--> ${FILE} is not gz or bzip2, so compress it, " ​ >> ${DEBUGLOG}+                  echo -n "-> ${FILE} is not gz or bzip2, so compress it, " ​ >> ${DEBUGLOG}
                   RATIO=`bzip2 -v ${FILE} 2>&1 | cut -d','​ -f3 | cut -d'​%'​ -f1`                   RATIO=`bzip2 -v ${FILE} 2>&1 | cut -d','​ -f3 | cut -d'​%'​ -f1`
                   echo "ratio is >​$RATIO<" ​ >> ${DEBUGLOG}                   echo "ratio is >​$RATIO<" ​ >> ${DEBUGLOG}
Line 141: Line 142:
   ​   ​
                   #bzip2 adds extension, so we add it to the mv below                   #bzip2 adds extension, so we add it to the mv below
-                  echo "---------> mv ${FILE}.bz2 to ${DIR}/​archive." ​ >> ${DEBUGLOG}+                  echo "​-->​ mv ${FILE}.bz2 to ${DIR}/​archive." ​ >> ${DEBUGLOG}
                   echo " " ​ >> ${DEBUGLOG}                   echo " " ​ >> ${DEBUGLOG}
                   mv ${FILE}.bz2 ${DIR}/​archive                   mv ${FILE}.bz2 ${DIR}/​archive
Line 153: Line 154:
           fi           fi
   ​   ​
 +          #End of file test "​for"​
           done           done
   ​   ​
 +  #end of dirs to test "​for"​
   done   done
   ​   ​
Line 167: Line 170:
   fi   fi
   ​   ​
-  echo "​Average (Mean) compression was ${MEANCOMPRESS}%." ​ >> ${TMPLOG}+  echo "​Average (Mean) compression was ${MEANCOMPRESS} %." ​ >> ${TMPLOG}
   ​   ​
   ​   ​
   #from df   #from df
-  SPACEATEND=`df -h | grep /var/log/logspartition ​| cut -d' ' -f28`+  SPACEATEND=`df -h | grep /var/log/GRS | cut -d' ' -f28`
   ​   ​
   echo "​Returncode is >​${RESULT}<"​ >> ${TMPLOG}   echo "​Returncode is >​${RESULT}<"​ >> ${TMPLOG}
Line 194: Line 197:
   if [ ${RESULT} != 0 ]   if [ ${RESULT} != 0 ]
           then           then
-          echo "Gzip of files on log01 may have errors, return code was not 0, ( >​${RESULT}<​ ). " >> ${TMPLOG}+          echo "Gzip of files on srv-log01 may have errors, return code was not 0, ( >​${RESULT}<​ ). " >> ${TMPLOG}
           echo "​Command returned \"​${OUTPUT}\"​ " >> ${TMPLOG}           echo "​Command returned \"​${OUTPUT}\"​ " >> ${TMPLOG}
           SENDEMAIL=1           SENDEMAIL=1
Line 201: Line 204:
   fi   fi
   ​   ​
-  ​#cat  ${TMPLOG}+  ​
   ​   ​
   echo ""​   echo ""​
Line 223: Line 226:
   ​   ​
   rm ${TMPLOG}   rm ${TMPLOG}
 +  ​
   exit 0   exit 0
   ​   ​
-  [user@log01 scripts]$ 
- 
- 
  

rb/compresslogfiles.txt · Last modified: 10/10/2013 15:21 (external edit)