Example #1
0
def create_zip(archive, files):
    '''Creates a zip file containing the files being backed up.'''
    import zipfile
    from utils.misc import add_file_hash

    try:
        # zipfile always follows links
        with zipfile.ZipFile(archive, 'w') as zipf:
            zipf.comment = 'Created by s3-backup'
            for f in files:
                f = f.strip()
                if os.path.exists(f):
                    zipf.write(f)
                    add_file_hash(archive, f)
                    log.debug('Added %s.' % f)
                else:
                    log.error('%s does not exist.' % f)
                
                if zipf.testzip() != None:
                    log.error('An error occured creating the zip archive.')
    except zipfile.BadZipfile:
        # I assume this only happens on reads? Just in case...
        log.critical('The zip file is corrupt.')
    except zipfile.LargeZipFile:
        log.critical('The zip file is greater than 2 GB.'
                ' Enable zip64 functionality.')
Example #2
0
def create_tar(archive, files, mode, follow_links):
    '''Creates a tar archive of the files being backed up.'''
    import tarfile
    from utils.misc import add_file_hash

    try:
        with tarfile.open(archive, mode, dereference=follow_links) as tar:
            for f in files:
                f = f.strip()
                if os.path.exists(f):
                    tar.add(f)
                    add_file_hash(archive, f);
                    log.debug('Added %s.' % f)
                else:
                    log.error('%s does not exist.' % f)
    except tarfile.CompressionError:
        log.critical('There was an error compressing the backup archive. '
                'Please try again.')
        sys.exit(1)
    except tarfile.TarError:
        log.critical('There was an error creating the backup archive. '
                'Please try again.')
        sys.exit(1)