def create_xtrabackup_command(instance, timestamp, tmp_log): """ Create a xtrabackup command Args: instance - A hostAddr object timestamp - A timestamp tmp_log - A path to where xtrabackup should log Returns: a list that can be easily ingested by subprocess """ if host_utils.get_hiera_role() in host_utils.MASTERFUL_PUPPET_ROLES: cnf = host_utils.OLD_CONF_ROOT.format(port=instance.port) cnf_group = 'mysqld' else: cnf = host_utils.MYSQL_CNF_FILE cnf_group = 'mysqld{port}'.format(port=instance.port) datadir = host_utils.get_cnf_setting('datadir', instance.port) (xtra_user, xtra_pass) = mysql_lib.get_mysql_user_for_role(USER_ROLE_XTRABACKUP) return XTRABACKUP_CMD.format(datadir=datadir, xtra_user=xtra_user, xtra_pass=xtra_pass, cnf=cnf, cnf_group=cnf_group, port=instance.port, tmp_log=tmp_log).split()
def create_xtrabackup_command(instance, timestamp, tmp_log): """ Create a xtrabackup command Args: instance - A hostAddr object timestamp - A timestamp tmp_log - A path to where xtrabackup should log Returns: a list that can be easily ingested by subprocess """ if host_utils.get_hiera_role() in host_utils.MASTERFUL_PUPPET_ROLES: cnf = host_utils.OLD_CONF_ROOT.format(port=instance.port) cnf_group = 'mysqld' else: cnf = host_utils.MYSQL_CNF_FILE cnf_group = 'mysqld{port}'.format(port=instance.port) datadir = host_utils.get_cnf_setting('datadir', instance.port) xtra_user, xtra_pass = mysql_lib.get_mysql_user_for_role( USER_ROLE_XTRABACKUP) return XTRABACKUP_CMD.format(datadir=datadir, xtra_user=xtra_user, xtra_pass=xtra_pass, cnf=cnf, cnf_group=cnf_group, port=instance.port, tmp_log=tmp_log).split()
def xtrabackup_instance(instance): """ Take a compressed mysql backup Args: instance - A hostaddr instance Returns: A string of the path to the finished backup """ # Prevent issues with too many open files resource.setrlimit(resource.RLIMIT_NOFILE, (131072, 131072)) (temp_path, target_path) = get_paths(port=str(instance.port)) backup_file = ("mysql-{host}-{port}-{timestamp}.xbstream").format( host=instance.hostname, port=str(instance.port), timestamp=time.strftime('%Y-%m-%d-%H:%M:%S')) tmp_xtra_path = os.path.join(temp_path, backup_file) target_xtra_path = os.path.join(target_path, backup_file) tmp_log = ''.join((tmp_xtra_path, '.log')) target_log = ''.join((tmp_xtra_path, '.log')) if host_utils.get_hiera_role() in host_utils.MASTERFUL_PUPPET_ROLES: cnf = host_utils.OLD_CONF_ROOT.format(port=instance.port) cnf_group = 'mysqld' else: cnf = host_utils.MYSQL_CNF_FILE cnf_group = 'mysqld{port}'.format(port=instance.port) datadir = host_utils.get_cnf_setting('datadir', instance.port) xtra_user, xtra_pass = mysql_lib.get_mysql_user_for_role('xtrabackup') cmd = ('/bin/bash -c "/usr/bin/innobackupex {datadir} {XTRA_DEFAULTS} ' '--user={xtra_user} --password={xtra_pass} ' '--defaults-file={cnf} --defaults-group={cnf_group} ' '--port={port} 2>{tmp_log} ' '>{dest}"').format(datadir=datadir, XTRA_DEFAULTS=XTRA_DEFAULTS, xtra_user=xtra_user, xtra_pass=xtra_pass, cnf=cnf, cnf_group=cnf_group, port=instance.port, tmp_log=tmp_log, dest=tmp_xtra_path) log.info(cmd) xtra = subprocess.Popen(cmd, shell=True) xtra.wait() with open(tmp_log, 'r') as log_file: xtra_log = log_file.readlines() if 'innobackupex: completed OK!' not in xtra_log[-1]: raise Exception('innobackupex failed. ' 'log_file: {tmp_log}'.format(tmp_log=tmp_log)) log.info('Moving backup and log to {target}'.format(target=target_path)) os.rename(tmp_xtra_path, target_xtra_path) os.rename(tmp_log, target_log) log.info('Xtrabackup was successful') return target_xtra_path
def xtrabackup_instance(instance, timestamp): """ Take a compressed mysql backup Args: instance - A hostaddr instance timestamp - A timestamp which will be used to create the backup filename Returns: A string of the path to the finished backup """ # Prevent issues with too many open files resource.setrlimit(resource.RLIMIT_NOFILE, (131072, 131072)) (temp_path, target_path) = get_paths(port=str(instance.port)) backup_file = BACKUP_FILE.format(hostname=instance.hostname, port=instance.port, timestamp=time.strftime( '%Y-%m-%d-%H:%M:%S', timestamp), backup_type=BACKUP_TYPE_XBSTREAM) tmp_xtra_path = os.path.join(temp_path, backup_file) target_xtra_path = os.path.join(target_path, backup_file) tmp_log = ''.join((tmp_xtra_path, '.log')) target_log = ''.join((tmp_xtra_path, '.log')) if host_utils.get_hiera_role() in host_utils.MASTERFUL_PUPPET_ROLES: cnf = host_utils.OLD_CONF_ROOT.format(port=instance.port) cnf_group = 'mysqld' else: cnf = host_utils.MYSQL_CNF_FILE cnf_group = 'mysqld{port}'.format(port=instance.port) datadir = host_utils.get_cnf_setting('datadir', instance.port) xtra_user, xtra_pass = mysql_lib.get_mysql_user_for_role( USER_ROLE_XTRABACKUP) cmd = XTRABACKUP_CMD.format(datadir=datadir, xtra_user=xtra_user, xtra_pass=xtra_pass, cnf=cnf, cnf_group=cnf_group, port=instance.port, tmp_log=tmp_log, tmp_xtra_path=tmp_xtra_path) log.info(cmd) xtra = subprocess.Popen(cmd, shell=True) xtra.wait() with open(tmp_log, 'r') as log_file: xtra_log = log_file.readlines() if INNOBACKUP_OK not in xtra_log[-1]: raise Exception('innobackupex failed. ' 'log_file: {tmp_log}'.format(tmp_log=tmp_log)) log.info('Moving backup and log to {target}'.format(target=target_path)) os.rename(tmp_xtra_path, target_xtra_path) os.rename(tmp_log, target_log) log.info('Xtrabackup was successful') return target_xtra_path
def xtrabackup_instance(instance, timestamp): """ Take a compressed mysql backup Args: instance - A hostaddr instance timestamp - A timestamp which will be used to create the backup filename Returns: A string of the path to the finished backup """ # Prevent issues with too many open files resource.setrlimit(resource.RLIMIT_NOFILE, (131072, 131072)) (temp_path, target_path) = get_paths(port=str(instance.port)) backup_file = BACKUP_FILE.format(hostname=instance.hostname, port=instance.port, timestamp=time.strftime('%Y-%m-%d-%H:%M:%S', timestamp), backup_type=BACKUP_TYPE_XBSTREAM) tmp_xtra_path = os.path.join(temp_path, backup_file) target_xtra_path = os.path.join(target_path, backup_file) tmp_log = ''.join((tmp_xtra_path, '.log')) target_log = ''.join((tmp_xtra_path, '.log')) if host_utils.get_hiera_role() in host_utils.MASTERFUL_PUPPET_ROLES: cnf = host_utils.OLD_CONF_ROOT.format(port=instance.port) cnf_group = 'mysqld' else: cnf = host_utils.MYSQL_CNF_FILE cnf_group = 'mysqld{port}'.format(port=instance.port) datadir = host_utils.get_cnf_setting('datadir', instance.port) xtra_user, xtra_pass = mysql_lib.get_mysql_user_for_role(USER_ROLE_XTRABACKUP) cmd = XTRABACKUP_CMD.format(datadir=datadir, xtra_user=xtra_user, xtra_pass=xtra_pass, cnf=cnf, cnf_group=cnf_group, port=instance.port, tmp_log=tmp_log, tmp_xtra_path=tmp_xtra_path) log.info(cmd) xtra = subprocess.Popen(cmd, shell=True) xtra.wait() with open(tmp_log, 'r') as log_file: xtra_log = log_file.readlines() if INNOBACKUP_OK not in xtra_log[-1]: raise Exception('innobackupex failed. ' 'log_file: {tmp_log}'.format(tmp_log=tmp_log)) log.info('Moving backup and log to {target}'.format(target=target_path)) os.rename(tmp_xtra_path, target_xtra_path) os.rename(tmp_log, target_log) log.info('Xtrabackup was successful') return target_xtra_path