Beispiel #1
0
def backup_mode_sql_server(backup_opt_dict, storage):
    """
    Execute a SQL Server DB backup. Currently only backups with shadow
    copy are supported. This mean, as soon as the shadow copy is created
    the db writes will be blocked and a checkpoint will be created, as soon
    as the backup finish the db will be unlocked and the backup will be
    uploaded. A sql_server.conf_file is required for this operation.
    """
    with open(backup_opt_dict.sql_server_conf, 'r') as sql_conf_file_fd:
        for line in sql_conf_file_fd:
            line = line.strip().split('#', 1)[0]
            if not line:
                continue

            key, value = line.split('=')
            # remove white spaces
            key = key.strip()
            value = value.strip()

            if key == 'instance':
                db_instance = utils.dequote(value)
                backup_opt_dict.sql_server_instance = db_instance
                continue
            else:
                raise Exception('Please indicate a valid SQL Server instance')

    try:
        stop_sql_server(backup_opt_dict.sql_server_instance)
        backup(backup_opt_dict, storage, backup_opt_dict.engine)
    finally:
        if not backup_opt_dict.vssadmin:
            # if vssadmin is false, wait until the backup is complete
            # to start sql server again
            start_sql_server(backup_opt_dict.sql_server_instance)
Beispiel #2
0
def backup_mode_mysql(backup_opt_dict, storage):
    """
    Execute a MySQL DB backup. currently only backup with lvm snapshots
    are supported. This mean, just before the lvm snap vol is created,
    the db tables will be flushed and locked for read, then the lvm create
    command will be executed and after that, the table will be unlocked and
    the backup will be executed. It is important to have the available in
    backup_args.mysql_conf the file where the database host, name, user,
    password and port are set.
    """

    try:
        import pymysql as MySQLdb
    except ImportError:
        raise ImportError('Please install PyMySQL module')

    if not backup_opt_dict.mysql_conf:
        raise ValueError('MySQL: please provide a valid config file')
    # Open the file provided in backup_args.mysql_conf and extract the
    # db host, name, user, password and port.
    db_user = db_host = db_pass = False
    # Use the default mysql port if not provided
    db_port = 3306
    with open(backup_opt_dict.mysql_conf, 'r') as mysql_file_fd:
        for line in mysql_file_fd:
            line = line.strip().split('#', 1)[0]
            if not line:
                continue

            key, value = line.split('=')
            # remove white spaces
            key = key.strip()
            value = value.strip()

            if key == 'host':
                db_host = utils.dequote(value)
                continue

            if key == 'user':
                db_user = utils.dequote(value)
                continue

            if key == 'password':
                db_pass = utils.dequote(value)
                continue

            if key == 'port':
                db_port = utils.dequote(value)
                try:
                    db_port = int(db_port)
                except ValueError:
                    raise ValueError('[*] MySQL port should be integer')
                continue

    # Initialize the DB object and connect to the db according to
    # the db mysql backup file config
    try:
        backup_opt_dict.mysql_db_inst = MySQLdb.connect(
            host=db_host, port=db_port, user=db_user, passwd=db_pass)
    except Exception as error:
        raise Exception('[*] MySQL: {0}'.format(error))

    # Execute backup
    backup(backup_opt_dict, storage, backup_opt_dict.engine)
Beispiel #3
0
def find_all(regex, lines):
    return dict([(k.strip(), utils.dequote(v.strip())) for k, v in
                 regex.findall(lines)])