(archive_type, dump_path)
Create compressed archive of dump_path.

    Accepts archive_type of zip or tar and requires dump_path, directory added to archive
Create compressed archive of dump_path.
Create compressed archive of dump_path .
def do_archive(archive_type, dump_path):
    """
    Create compressed archive of dump_path.

    Accepts archive_type of zip or tar and requires dump_path, directory added to archive
    """

    archive_base = dump_path

    if archive_type.lower() == "tar":
        archive = archive_base + ".tar.bz2"
        try:
            logging.info("Creating tar file " + archive + "...")
            with tarfile.open(name=archive, mode="w:bz2") as a:
                for root, dirs, files in os.walk(archive_base):
                    for file in files:
                        a.add(os.path.join(root, file))
                return True, archive
        except tarfile.CompressionError as e:
            logging.exception(
                "compression method is not supported or the data cannot be"
                " decoded properly.\n\n" + str(e)
            )
            sys.exit(1)
        except tarfile.TarError as e:
            logging.exception("Error creating tarfile archive.\n\n" + str(e))
            sys.exit(1)

    elif archive_type.lower() == "zip":
        try:
            logging.info("Creating zip file...")
            archive = archive_base + ".zip"
            with zipfile.ZipFile(archive, "w") as z:
                for root, dirs, files in os.walk(archive_base):
                    for file in files:
                        z.write(os.path.join(root, file))
                return True, archive
        except zipfile.BadZipFile as e:
            logging.exception("Problem creating zip file\n\n" + str(e))
            sys.exit(1)
        except zipfile.LargeZipFile:
            logging.exception(
                "Zip file would be too large.  Update code to use Zip64 to continue."
            )
            sys.exit(1)

    else:
        logging.error(
            "Unsupported archive format received.  Probably shouldn't have "
            "made it to this code path.  Skipping attempt at creating archive file"
        )
        return False, None
def do_archive ( archive_type , dump_path ) : archive_base = dump_path if archive_type . lower ( ) == "tar" : archive = archive_base + ".tar.bz2" try : logging . info ( "Creating tar file " + archive + "..." ) with tarfile . open ( name = archive , mode = "w:bz2" ) as a : for root , dirs , files in os . walk ( archive_base ) : for file in files : a . add ( os . path . join ( root , file ) ) return True , archive except tarfile . CompressionError as e : logging . exception ( "compression method is not supported or the data cannot be" " decoded properly.\n\n" + str ( e ) ) sys . exit ( 1 ) except tarfile . TarError as e : logging . exception ( "Error creating tarfile archive.\n\n" + str ( e ) ) sys . exit ( 1 ) elif archive_type . lower ( ) == "zip" : try : logging . info ( "Creating zip file..." ) archive = archive_base + ".zip" with zipfile . ZipFile ( archive , "w" ) as z : for root , dirs , files in os . walk ( archive_base ) : for file in files : z . write ( os . path . join ( root , file ) ) return True , archive except zipfile . BadZipFile as e : logging . exception ( "Problem creating zip file\n\n" + str ( e ) ) sys . exit ( 1 ) except zipfile . LargeZipFile : logging . exception ( "Zip file would be too large.  Update code to use Zip64 to continue." ) sys . exit ( 1 ) else : logging . error ( "Unsupported archive format received.  Probably shouldn't have " "made it to this code path.  Skipping attempt at creating archive file" ) return False , None