Exemple #1
0
def start(mysqldump,
          schema=None,
          lock_method='auto-detect',
          file_per_database=True,
          open_stream=open,
          compression_ext=''):
    """Run a mysqldump backup"""

    if not schema and file_per_database:
        raise BackupError("file_per_database specified without a valid schema")

    if not schema:
        target_databases = ALL_DATABASES
    else:

        if len(schema.databases) == 0:
            raise BackupError("No databases found to backup")

        if not file_per_database and not [
                x for x in schema.excluded_databases
        ]:
            target_databases = ALL_DATABASES
        else:
            target_databases = [
                db for db in schema.databases if not db.excluded
            ]
            write_manifest(schema, open_stream, compression_ext)

    if file_per_database:
        flush_logs = '--flush-logs' in mysqldump.options
        if flush_logs:
            mysqldump.options.remove('--flush-logs')
        last = len(target_databases)
        for count, db in enumerate(target_databases):
            more_options = [mysqldump_lock_option(lock_method, [db])]
            # add --flush-logs only to the last mysqldump run
            if flush_logs and count == last:
                more_options.append('--flush-logs')
            db_name = encode(db.name)[0]
            if db_name != db.name:
                LOG.warning("Encoding file-name for database %s to %s",
                            db.name, db_name)
            try:
                stream = open_stream('%s.sql' % db_name, 'w')
            except (IOError, OSError), exc:
                raise BackupError("Failed to open output stream %s: %s" %
                                  ('%s.sql' + compression_ext, str(exc)))
            try:
                mysqldump.run([db.name], stream, more_options)
            finally:
                try:
                    stream.close()
                except (IOError, OSError), exc:
                    if exc.errno != errno.EPIPE:
                        LOG.error("%s", str(exc))
                        raise BackupError(str(exc))
Exemple #2
0
def start(mysqldump,
          schema=None,
          lock_method='auto-detect',
          file_per_database=True,
          open_stream=open,
          compression_ext=''):
    """Run a mysqldump backup"""

    if not schema and file_per_database:
        raise BackupError("file_per_database specified without a valid schema")

    if not schema:
        target_databases = ALL_DATABASES
    else:

        if len(schema.databases) == 0:
            raise BackupError("No databases found to backup")

        if not file_per_database and not [x for x in schema.excluded_databases]:
            target_databases = ALL_DATABASES
        else:
            target_databases = [db for db in schema.databases
                                    if not db.excluded]
            write_manifest(schema, open_stream, compression_ext)

    if file_per_database:
        flush_logs = '--flush-logs' in mysqldump.options
        if flush_logs:
            mysqldump.options.remove('--flush-logs')
        last = len(target_databases)
        for count, db in enumerate(target_databases):
            more_options = [mysqldump_lock_option(lock_method, [db])]
            # add --flush-logs only to the last mysqldump run
            if flush_logs and count == last:
                more_options.append('--flush-logs')
            db_name = encode(db.name)[0]
            if db_name != db.name:
                LOG.warning("Encoding file-name for database %s to %s", db.name, db_name)
            try:
                stream = open_stream('%s.sql' % db_name, 'w')
            except (IOError, OSError), exc:
                raise BackupError("Failed to open output stream %s: %s" %
                                  ('%s.sql' + compression_ext, str(exc)))
            try:
                mysqldump.run([db.name], stream, more_options)
            finally:
                try:
                    stream.close()
                except (IOError, OSError), exc:
                    if exc.errno != errno.EPIPE:
                        LOG.error("%s", str(exc))
                        raise BackupError(str(exc))
Exemple #3
0
def write_manifest(schema, open_stream, ext):
    """Write real database names => encoded names to MANIFEST.txt"""
    manifest_fileobj = open_stream("MANIFEST.txt", "w", method="none")

    try:
        for database in schema.databases:
            if database.excluded:
                continue
            name = database.name
            encoded_name = encode(name)
            line = u"%s %s\n" % (name, encoded_name + ".sql" + ext)
            manifest_fileobj.write(line)
    finally:
        manifest_fileobj.close()
        LOG.info("Wrote backup manifest %s", manifest_fileobj.name)
Exemple #4
0
def write_manifest(schema, open_stream, ext):
    """Write real database names => encoded names to MANIFEST.txt"""
    manifest_fileobj = open_stream('MANIFEST.txt', 'w', method='none')
    manifest = csv.writer(manifest_fileobj,
                          dialect=csv.excel_tab,
                          lineterminator="\n",
                          quoting=csv.QUOTE_MINIMAL)
    for database in schema.databases:
        if database.excluded:
            continue
        name = database.name
        encoded_name = encode(name)[0]
        manifest.writerow([name.encode('utf-8'), encoded_name + '.sql' + ext])
    manifest_fileobj.close()
    LOG.info("Wrote backup manifest %s", manifest_fileobj.name)
Exemple #5
0
def write_manifest(schema, open_stream, ext):
    """Write real database names => encoded names to MANIFEST.txt"""
    manifest_fileobj = open_stream('MANIFEST.txt', 'w', method='none')
    manifest = csv.writer(manifest_fileobj,
                          dialect=csv.excel_tab,
                          lineterminator="\n",
                          quoting=csv.QUOTE_MINIMAL)
    for database in schema.databases:
        if database.excluded:
            continue
        name = database.name
        encoded_name = encode(name)[0]
        manifest.writerow([name.encode('utf-8'), encoded_name + '.sql' + ext])
    manifest_fileobj.close()
    LOG.info("Wrote backup manifest %s", manifest_fileobj.name)
Exemple #6
0
def write_manifest(schema, open_stream, ext):
    """Write real database names => encoded names to MANIFEST.txt"""
    manifest_fileobj = open_stream("MANIFEST.txt", "w", method="none")

    try:
        for database in schema.databases:
            if database.excluded:
                continue
            name = database.name
            encoded_name = encode(name)
            line = "%s %s\n" % (name, encoded_name + ".sql" + ext)
            manifest_fileobj.write(line)
    finally:
        manifest_fileobj.close()
        LOG.info("Wrote backup manifest %s", manifest_fileobj.name)
Exemple #7
0
def write_manifest(schema, open_stream, ext):
    """Write real database names => encoded names to MANIFEST.txt"""
    if sys.version_info > (3, 0):
        manifest_fileobj = open_stream("MANIFEST.txt", "w", method="none")
    else:
        manifest_fileobj = open_stream("MANIFEST.txt", "wb", method="none")

    try:
        manifest = csv.writer(
            manifest_fileobj, dialect=csv.excel_tab, lineterminator="\n", quoting=csv.QUOTE_MINIMAL
        )
        for database in schema.databases:
            if database.excluded:
                continue
            name = database.name
            encoded_name = encode(name)
            manifest.writerow([name, encoded_name + ".sql" + ext])
    finally:
        manifest_fileobj.close()
        LOG.info("Wrote backup manifest %s", manifest_fileobj.name)
Exemple #8
0
def start(
    mysqldump,
    schema=None,
    lock_method="auto-detect",
    file_per_database=True,
    open_stream=open,
    compression_ext="",
    arg_per_database=None,
):
    """Run a mysqldump backup"""
    if not schema and file_per_database:
        raise BackupError("file_per_database specified without a valid schema")

    if not schema:
        target_databases = ALL_DATABASES
    else:

        if not schema.databases:
            raise BackupError("No databases found to backup")

        if not file_per_database and not list(schema.excluded_databases):
            target_databases = ALL_DATABASES
        else:
            target_databases = [
                db for db in schema.databases if not db.excluded
            ]
            write_manifest(schema, open_stream, compression_ext)

    if file_per_database:
        if arg_per_database:
            arg_per_database = json.loads(arg_per_database)
        flush_logs = "--flush-logs" in mysqldump.options
        if flush_logs:
            mysqldump.options.remove("--flush-logs")
        last = len(target_databases)
        for count, target_db in enumerate(target_databases):
            more_options = [mysqldump_lock_option(lock_method, [target_db])]
            # add --flush-logs only to the last mysqldump run
            if flush_logs and count == last:
                more_options.append("--flush-logs")
            db_name = encode(target_db.name)
            if db_name != target_db.name:
                LOG.warning("Encoding file-name for database %s to %s",
                            target_db.name, db_name)
            try:
                stream = open_stream("%s.sql" % db_name, "w")
            except (IOError, OSError) as exc:
                raise BackupError(
                    "Failed to open output stream %s: %s" %
                    (db_name + ".sql" + compression_ext, str(exc)))
            try:
                if db_name in arg_per_database:
                    more_options.append(arg_per_database[db_name])
                mysqldump.run([target_db.name], stream, more_options)
            finally:
                try:
                    stream.close()
                except (IOError, OSError) as exc:
                    if exc.errno != errno.EPIPE:
                        LOG.error("%s", str(exc))
                        raise BackupError(str(exc))
    else:
        more_options = [mysqldump_lock_option(lock_method, target_databases)]
        try:
            stream = open_stream("all_databases.sql", "w")
        except (IOError, OSError) as exc:
            raise BackupError("Failed to open output stream %s: %s" %
                              ("all_databases.sql" + compression_ext, exc))
        try:
            if target_databases is not ALL_DATABASES:
                target_databases = [db.name for db in target_databases]
            mysqldump.run(target_databases, stream, more_options)
        finally:
            try:
                stream.close()
            except (IOError, OSError) as exc:
                if exc.errno != errno.EPIPE:
                    LOG.error("%s", str(exc))
                    raise BackupError(str(exc))
Exemple #9
0
def start(
    mysqldump,
    schema=None,
    lock_method="auto-detect",
    file_per_database=True,
    open_stream=open,
    compression_ext="",
    arg_per_database=None,
):
    """Run a mysqldump backup"""
    if not schema and file_per_database:
        raise BackupError("file_per_database specified without a valid schema")

    if not schema:
        target_databases = ALL_DATABASES
    else:

        if not schema.databases:
            raise BackupError("No databases found to backup")

        if not file_per_database and not [x for x in schema.excluded_databases]:
            target_databases = ALL_DATABASES
        else:
            target_databases = [db for db in schema.databases if not db.excluded]
            write_manifest(schema, open_stream, compression_ext)

    if file_per_database:
        if arg_per_database:
            arg_per_database = json.loads(arg_per_database)
        flush_logs = "--flush-logs" in mysqldump.options
        if flush_logs:
            mysqldump.options.remove("--flush-logs")
        last = len(target_databases)
        for count, target_db in enumerate(target_databases):
            more_options = [mysqldump_lock_option(lock_method, [target_db])]
            # add --flush-logs only to the last mysqldump run
            if flush_logs and count == last:
                more_options.append("--flush-logs")
            db_name = encode(target_db.name)
            if db_name != target_db.name:
                LOG.warning("Encoding file-name for database %s to %s", target_db.name, db_name)
            try:
                stream = open_stream("%s.sql" % db_name, "w")
            except (IOError, OSError) as exc:
                raise BackupError(
                    "Failed to open output stream %s: %s"
                    % (db_name + ".sql" + compression_ext, str(exc))
                )
            try:
                if db_name in arg_per_database:
                    more_options.append(arg_per_database[db_name])
                mysqldump.run([target_db.name], stream, more_options)
            finally:
                try:
                    stream.close()
                except (IOError, OSError) as exc:
                    if exc.errno != errno.EPIPE:
                        LOG.error("%s", str(exc))
                        raise BackupError(str(exc))
    else:
        more_options = [mysqldump_lock_option(lock_method, target_databases)]
        try:
            stream = open_stream("all_databases.sql", "w")
        except (IOError, OSError) as exc:
            raise BackupError(
                "Failed to open output stream %s: %s" % ("all_databases.sql" + compression_ext, exc)
            )
        try:
            if target_databases is not ALL_DATABASES:
                target_databases = [db.name for db in target_databases]
            mysqldump.run(target_databases, stream, more_options)
        finally:
            try:
                stream.close()
            except (IOError, OSError) as exc:
                if exc.errno != errno.EPIPE:
                    LOG.error("%s", str(exc))
                    raise BackupError(str(exc))