Advertisement
petschko

logClear.sh

Feb 12th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.30 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. # Clears Logfiles (example HTTPD-Logs) and stores them for a certain amount in archive
  4.  
  5. # How to call this script?
  6. #
  7. # You can call this script like this:
  8. # ./logClear.sh /pathtodir/   - Will cleanup the files and store them by default expire time
  9. # ./logClear.sh /pathtodir/ 2   - Will cleanup the files and store them 2 Days
  10. # ./logClear.sh /pathtodir/ 0   - Will cleanup the files and DON'T store them
  11. # ./logClear.sh /pathtodir/ -1   - Will cleanup the files and store them FOREVER
  12. #
  13. # Syntax: ./logClear.sh LOGDIR [Optional STORE_TIME_DAYS]
  14.  
  15. # Settings
  16. default_expire_time=6        # Days - 0=turns archiving off - -1=Files will saved forever
  17. subdir=""                    # If you have in every directory a same named log folder, you can set it up here
  18. archive_dir="archive/"       # Archiv dir relative to logdir
  19. logext="log"                 # File-Extension of the logfile (mostly .txt > "txt" or .log > "log")
  20.  
  21. echo "Starting logClear v0.0.1 by Petschko [peter-91@hotmail.de]..."
  22.  
  23. # Check if there is a Parameter
  24. if [ $# -gt 0 ]
  25. then
  26.     echo "[Info]: Start Script..."
  27.    
  28.     # Setup vars   
  29.     logdir="${1}${subdir}"
  30.     archivedir="${logdir}${archive_dir}"
  31.     delete_after=$default_expire_time
  32.     date=$(date +%Y-%m-%d_%H-%M-%S)
  33.    
  34.     # Check expire time and if archiving is on
  35.     archiv_on="yes"
  36.     if [ $default_expire_time -eq 0 ]
  37.     then
  38.         archiv_on="no"
  39.     fi
  40.    
  41.     # Check if there is an other parameter
  42.     if [ $# -gt 1 ]
  43.     then
  44.         if [ $2 -eq 0 ]
  45.         then
  46.             archiv_on="no"
  47.         else
  48.             archiv_on="yes"
  49.         fi
  50.         # Overwrite delete time
  51.         delete_after=$2
  52.     fi
  53.    
  54.     # Exists File
  55.     if [ -d $logdir ]
  56.     then
  57.         echo "[Info]: Directory logdir found."
  58.        
  59.         # Check if this script can read
  60.         if [ ! -r $logdir ]
  61.         then
  62.             echo "[Warning]: Directory $logdir is not readable!"
  63.         fi
  64.         # Check if this script can write
  65.         if [ ! -w $logdir ]
  66.         then
  67.             echo "[Error]: Directory $logdir is not writeable!"
  68.             echo "[Info]: Exiting..."
  69.             exit
  70.         fi
  71.        
  72.         if [ "$archiv_on" = "yes" ];
  73.         then
  74.             # Check if Archive exists if not create it
  75.             echo "[Info]: Checking Archive Directory... (${archivedir})"
  76.             if [ ! -d $archivedir ]
  77.             then
  78.                 echo "[Warning]: Directory $archivedir not found! Try to create it..."
  79.                 mkdir $archivedir
  80.                 # Check if it was created
  81.                 if [ ! -d $archivedir ]
  82.                 then
  83.                     echo "[Error]: Can't create Directory ${archivedir}... (Missing Permissions maybe?)"
  84.                     echo "[Info]: Exiting..."
  85.                     exit
  86.                 fi
  87.                 echo "[Info]: Directory successfully created!"
  88.             else
  89.                 echo "[Info]: Archive found!"
  90.             fi
  91.        
  92.             # Check Archive Directory for read
  93.             if [ ! -r $archivedir ]
  94.             then
  95.                 echo "[Warning]: Directory $archivedir is not readable!"
  96.             fi
  97.             # Check if this script can write
  98.             if [ ! -w $archivedir ]
  99.             then
  100.                 echo "[Error]: Directory $archivedir is not writeable!"
  101.                 echo "[Info]: Exiting..."
  102.                 exit
  103.             fi
  104.         else
  105.             echo "[Info]: Skipping Archive-Checks - It's turned off"
  106.         fi
  107.        
  108.         # All prechecks done
  109.        
  110.         # Switch WorkDir
  111.         echo "[Info]: Change Workdir to ${logdir}"
  112.         cd $logdir
  113.        
  114.         # Reading files
  115.         echo "[Info]: Reading Files..."
  116.         for logfile in ${logdir}*.${logext}
  117.         do
  118.             clearfile="yes"
  119.             fname=$(basename "$logfile")
  120.             echo "[Info]: Processing file ${fname}";
  121.            
  122.             if [ "$archiv_on" = "yes" ]
  123.             then
  124.                 backupfilename="${archivedir}${date}_${fname}.tar.gz"
  125.                
  126.                 # Compress file and save it to Archive
  127.                 if [ -r $logfile ]
  128.                 then
  129.                     # Don't save empty files
  130.                     if [ $(stat -c %s $logfile) -gt 0 ]
  131.                     then
  132.                         echo "[Info]: Backing up file ${logfile} as TAR.GZ..."
  133.                         tar -czf $backupfilename $fname
  134.                    
  135.                         # Check if new file exist
  136.                         if [ ! -f $backupfilename ]
  137.                         then
  138.                             clearfile="no"
  139.                             echo "[Warning]: Can't create backup file! Will keep logfile."
  140.                         fi
  141.                     else
  142.                         echo "[Notice]: File ${logfile} was not saved as a Backup. It's empty!"
  143.                     fi
  144.                 else
  145.                     echo "[Warning]: File ${logfile} is not readable!"
  146.                 fi
  147.             fi
  148.            
  149.             # Clearfile if all is done
  150.             if [ $clearfile = "yes" ]
  151.             then
  152.                 if [ -w $logfile ]
  153.                 then
  154.                     > $logfile
  155.                 else
  156.                     echo "[Warning]: File ${logfile} is not writeable! (Can't clear it)"
  157.                 fi
  158.             fi
  159.         done
  160.        
  161.         echo "[Info]: Step 1/2 done."
  162.         # Done clearing and backup files
  163.         # -------------------------------
  164.         # Start delete old files
  165.         if [ $archiv_on = "yes" ]
  166.         then
  167.             # Change workdir to archive
  168.             echo "[Info]: Change Workdir to ${archivedir}"
  169.             cd $archivedir
  170.             echo "[Info]: Reading Archiv files..."
  171.            
  172.             if [ ! $delete_after -lt 0 ]
  173.             then
  174.                 # Delete old files
  175.                 for archivfile in ${archivedir}*.tar.gz
  176.                 do
  177.                     fname=$(basename "$archivfile")
  178.                     echo "[Info]: Checking file ${fname}..."
  179.                    
  180.                     # Get file date
  181.                     file_year=${fname:0:4}
  182.                     file_mon=${fname:5:2}
  183.                     file_day=${fname:8:2}
  184.                     file_timestamp=$(date +%s -d "${file_year}-${file_mon}-${file_day}")
  185.                
  186.                     if [ $(( $file_timestamp + $delete_after * 86400 )) -lt $(date +%s) ]
  187.                     then
  188.                         # Delete file if it reach a specific age
  189.                         echo "[Notice]: Delete File ${archivfile} (Date expired)..."
  190.                    
  191.                         if [ -w $archivfile ]
  192.                         then
  193.                             rm $archivfile
  194.                             # Check if it is really deleted
  195.                             if [ -f $archivfile ]
  196.                             then
  197.                                 echo "[Warning]: Can't delete ${archivfile}... Skip it"
  198.                             fi
  199.                         else
  200.                             echo "[Warning]: File ${archivfile} is not writeable! (Can't delete it)"
  201.                         fi
  202.                     fi
  203.                 done
  204.             else
  205.                 echo "[Notice]: Delete-Archiv-Service is disabled. Skipping..."
  206.             fi
  207.         else
  208.             echo "[Notice]: Archiv-Service is turned off. Skip this step"
  209.         fi
  210.        
  211.         # Done Message
  212.         echo "[Info]: Step 2/2 done."
  213.         echo "[Info]: All done! :)"
  214.        
  215.     else
  216.         echo "[Error]: Directory $logdir doesn't exists!"
  217.     fi
  218. else
  219.     echo "[Error]: Please specify a Directory!"
  220.     echo "----------------------------------"
  221.     echo "Usage: logClear.sh PATH [Optional DELETE_AFTER_X_DAYS]"
  222.     echo ""
  223.     echo "Directory-PATH has to end with an /!"
  224.     echo ""
  225.     echo "Delete after x Days means, that you can specify a delete date after x days."
  226.     echo "0 turns archiving off (Direct delete on call)"
  227.     echo "-1 (Will not delete Archived Files)"
  228.     echo "If you ignore this parameter it will use the default delete time"
  229.     echo "---------------------------------"
  230. fi
  231.  
  232. echo "[Info]: Exiting..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement