Quick and dirty log file cleanup with python
I have a problem, my co-workers like to make tons of different log files for their web services and they don’t like to clean them up! Ok, so maybe I’m to blame too, but I hate that day every few months where my log partition fills up and I have to clean up after us. So I cobbled together a small script to solve this problem once and for all (or at least until we all agree on how to solve this problem).
I’ve wanted to write something in python for a good lone while now. I just was never in a position to give the language a try. So I decided this was a perfect opportunity to give it a try. I used this example and a few other websites to get started. Overall I really enjoy writing python and I hope to find a way to use more of it in my day to day tasks.
I just thought I’d put this here in case anyone else finds this useful. To use it just supply a path to search and any of the options you would like (deleting files, archiving them, what kind of compression, etc). Use the -h option to see all the features.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #!/usr/bin/python # This script cleans up log files # Author: Don Magee # Date: 2012-01-23 # Version: 1.0 # License: MIT http://www.opensource.org/licenses/mit-license.php import os import re import optparse import glob import tarfile import time from datetime import date def main(): usage = "usage: %prog [options]" parser = optparse.OptionParser(usage) parser.add_option("-y", "--year", action="store", type="int", dest="filterYear", default=False, help="Enter the year.") parser.add_option("-m", "--month", action="store", type="int", dest="filterMonth", default=False, help="Enter the Month.") parser.add_option("-d", "--day", action="store", type="int", dest="filterDay", default=False, help="Enter the Day.") parser.add_option("-p", "--path", action="store", type="string", dest="filterPath", default=False, help="Enter the path") parser.add_option("-a", "--archive", action="store", type="string", dest="filterArchive", default=False, help="Enter the path to store the archive file (if using the -t or --tar option)") parser.add_option("-r", "--remove", action="store_true", dest="removeFiles", default=False, help="Delete original files.") parser.add_option("-t", "--tar", action="store_true", dest="tarFiles", default=False, help="Tar all files using bzip2 compression as a default. If --gizp or --no-compression are used then tar will be automatically used.") parser.add_option('-g', "--gzip", action="store_true", dest="gzipFiles", default=False, help="Use GZIP compression instead of bzip2") parser.add_option("-n", "--no-compression", action="store_true", dest="noCompression", default=False, help="Do not use any compression for tar.") parser.add_option("-l", "--list", action="store_true", dest="listFiles", default=True, help="List all files that would be affected. This is the default option.") options, args = parser.parse_args() if not options.filterPath: parser.error('You must enter a path to clean (try --path)') if options.gzipFiles and options.noCompression: parser.error('You can not use both gzip compression and no compression. Please choose one or the other.') if options.listFiles and (options.tarFiles or options.removeFiles): options.listFiles = False # setup compression type tarType = "w:bz2" tarExt = 'tar.bz2' if options.gzipFiles: options.tarFiles = True tarType = "w:gz" tarExt = '.tar.gz' if options.noCompression: options.tarFiles = True tarType = "w" tarExt = '.tar' archiveName = '' #Base name for archive today = date.today() day = today.day month = today.month year = today.year if(options.filterYear): year = options.filterYear if(options.filterMonth): month = options.filterMonth if(options.filterDay): day = options.filterDay filterDate = str(year) + '-' + str(month) + '-' + str(day) #create a tar file and open it with bzip2 compression if options.tarFiles: if options.filterArchive: archiveName = options.filterArchive archiveName += filterDate + '_archive' + tarExt archive = tarfile.open(archiveName, tarType) print '-'*50 fileCount = 0 for folder in glob.glob(options.filterPath): print 'Looking for old log files in: %s' % folder for logFile in glob.glob(folder + '/*.log'): stats = os.stat(logFile) lastmodDate = time.localtime(stats[8]) expDate = time.strptime(filterDate, '%Y-%m-%d') if expDate > lastmodDate: if options.listFiles: print 'The following files will be affected:' print logFile, time.strftime("%m/%d/%y", lastmodDate) if options.tarFiles: try: print 'Compressing', logFile, time.strftime("(older than %m/%d/%y)", expDate) fileCount += 1 archive.add(logFile) except OSError: print 'Could not compress', logfile if options.removeFiles: try: print 'Removing', logFile, time.strftime("(older than %m/%d/%y)", expDate) os.remove(logFile) # commented out for testing except OSError: print 'Could not remove', logFile if options.tarFiles: archive.close() if fileCount == 0: os.remove(archiveName) if __name__ == "__main__": main() |
