Changeset 51979

Show
Ignore:
Timestamp:
09/17/07 16:22:25 (2 years ago)
Author:
madarche
Message:

- Added GZIP compression for ZODB backups in cpshousekeeping.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • CPS3/products/CPSUtil/trunk/CHANGES

    r51804 r51979  
    44New features 
    55~~~~~~~~~~~~ 
    6 - 
     6- Added GZIP compression for ZODB backups in cpshousekeeping. 
    77Bug fixes 
    88~~~~~~~~~ 
  • CPS3/products/CPSUtil/trunk/bin/cpshousekeeping

    r51628 r51979  
    3939import sys 
    4040import os 
     41from gzip import GzipFile 
    4142import base64 
    4243import urllib2 
     
    6263CPS_LOGIN_URL_PATTERN = "http://%s:%s/%s/logged_in" 
    6364TIME_FORMAT = '%Y-%m-%d_%H:%M' 
     65# This regexp matches file names of the form 
     66# YYYY-MM-dd_hh:mm-xxxxxxxxxx 
     67# Examples: 
     68# 2007-09-17_11:24-Data.fs 
     69# 2007-09-17_11:24-Data.fs.gz 
    6470ZODB_BACKUP_FILENAME_REGEXP = re.compile(r'\d+-\d{2}-\d{2}_\d{2}:\d{2}-.+') 
    6571SEND_NOTIFICATIONS_PATTERN = "http://%s:%s/%s/cps_subscriptions_schedule_notifications?subscription_mode=%s" 
     
    6975    """Analyze command line arguments. 
    7076    """ 
    71     usage = "usage: %prog [options]" 
     77    usage = """Usage: %prog [options] 
     78 
     79Example: 
     80%prog -vrlPb -u admin -w 'xxx' --host localhost -p 8080 -i cps -z /usr/local/zope/instance/cps/var/Data.fs -k /var/backups/zodb/www.mysite.net 
     81    """ 
    7282    parser = OptionParser(usage=usage) 
    7383 
     
    182192                      "ZODB backups. " 
    183193                      "The default is %s." % DEFAULT_ZODB_BACKUP_DIR_PATH) 
     194 
     195    parser.add_option('--nocompress', 
     196                      action='store_true', 
     197                      dest='nocompress', 
     198                      default=False, 
     199                      help="Don't compress the ZODB backups.") 
    184200 
    185201    parser.add_option('-B', '--keep-backups-count', 
     
    250266    if options.backup: 
    251267        backupZodb(options.zodbfile_path, options.backupdir_path, 
    252                    options.backups_keep_count) 
     268                   not options.nocompress, options.backups_keep_count) 
    253269 
    254270 
     
    271287    log("Successfully packed ZODB of host %s" % host_name) 
    272288 
    273 def backupZodb(zodb_path, backupdir_path, backups_keep_count=0): 
     289def backupZodb(zodb_path, backupdir_path, compress=True, backups_keep_count=0): 
    274290    """TODO : backupZodb should WARN and not perform this action if there isn't 
    275     enough space left on target device. 
     291    enough space left on target device. But unfortunately this requires parsing 
     292    of platform specific information and there isn't any native python-way to 
     293    do this yet. 
    276294    """ 
    277295    log("Checking for backups ...") 
     
    282300    log("command = %s" % command, increment=1) 
    283301    os.system(command) 
     302    if compress: 
     303        compressFile(zodb_backup_path) 
     304 
    284305    if backups_keep_count > 0: 
    285         log("There are some backups to remove ...", increment=1) 
     306        log("Checking for potential backups to remove, " 
     307            "keeping only the %s ones ..." % backups_keep_count, increment=1) 
    286308        file_names = os.listdir(backupdir_path) 
    287309        file_names = [x for x in file_names 
     
    296318 
    297319 
     320def compressFile(file_path): 
     321    """Compress a file using GZIP. 
     322    """ 
     323    log("Compressing %s ..." % file_path) 
     324    f = file(file_path, 'rb') 
     325    content = f.read() 
     326    f.close() 
     327    gzip_file_path = file_path + '.gz' 
     328    f = GzipFile(gzip_file_path, 'wb') 
     329    f.write(content) 
     330    f.close() 
     331    os.remove(file_path) 
     332    log("Compressing done : %s" % gzip_file_path) 
     333 
     334 
    298335def callUrl(url, username, password): 
    299336    """Call urllib2.urlopen with forced HTTP Basic Auth header. 
     
    301338    request = urllib2.Request(url) 
    302339    base64string = base64.encodestring('%s:%s' % (username, password))[:-1] 
    303     request.add_header("Authorization", "Basic %s" % base64string) 
     340    request.add_header('Authorization', 'Basic %s' % base64string) 
     341    # Adding this to make the request be a POST request, 
     342    # because those methods need to be POST because they modify 
     343    # the state of the application. 
     344    request.add_data("This is a POST method") 
    304345    try: 
    305346        urllib2.urlopen(request)