Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python2.7
- import os
- import sys
- import re
- import time
- import getopt
- import subprocess
- import logging
- import logging.handlers
- directory = '/tmp/binlogs/'
- verbose = False
- compressed = 0
- found = 0
- total_time = 0
- # logging config
- log = logging.getLogger(__name__)
- log.setLevel(logging.DEBUG)
- handler = logging.handlers.SysLogHandler('/dev/log')
- formatter = logging.Formatter('%(module)s: %(message)s')
- handler.setFormatter(formatter)
- log.addHandler(handler)
- # args parsing
- try:
- opts, args = getopt.getopt(sys.argv[1:], 'd:vh', ['directory=', 'verbose', 'help'])
- except getopt.GetoptError:
- print 'Invalid option. Use -h (--help) for help.'
- log.error("==== The script has been called with illegal arguments")
- sys.exit(2)
- for opt, arg in opts:
- if opt in ('-d', '--directory'):
- directory = arg
- elif opt in ('-v', '--verbose'):
- verbose = True
- elif opt in ('-h', '--help'):
- print """
- -v --verbose for verbose output
- -d --directory for specific dir
- """
- sys.exit(0)
- # start
- msg = "==== The script has started. Searching for matches in %s" % directory
- log.info(msg)
- if verbose:
- print msg
- # file search + compress
- for file in os.listdir(directory):
- if re.search('^mysql-bin\.\d{6}$', file):
- found += 1
- start = time.clock()
- gzip_status = subprocess.call(["gzip", os.path.join(directory, file)])
- total_time += time.clock() - start
- if gzip_status == 0:
- log_message = "== [OK] %s compressed" % file
- compressed += 1
- else:
- log_message = "== [--] %s found, but gzip exited with status %d" % (file, gzip_status)
- log.info(log_message)
- if verbose:
- print log_message
- else:
- print "%s found" % file
- if found > 0:
- end_msg = "==== The script has ended without errors. Compressed %d of %d files succesfully in %.2f seconds" % (found, compressed, total_time)
- else:
- end_msg = "==== The script has ended without matching files"
- log.info(end_msg)
- if verbose:
- print end_msg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement