def get_necessary_info(self): log.info('begin get_necessary_info!!!') split_result = '|awk -F \'[=]\' \'{print $2}\'' get_instance = '|grep TINSTANCE{}'.format(split_result) dir_instance = '|grep DIR_INSTANCE{}'.format(split_result) get_instance_cmd = "su - {} -c 'env'{}".format(self.hana_adm, get_instance) get_dir_instance_cmd = "su - {} -c 'env'{}".format( self.hana_adm, dir_instance) try: log.info('get_instance_cmd:{}'.format(get_instance_cmd)) log.info('get_dir_instance_cmd:{}'.format(get_dir_instance_cmd)) instance_result = exec_cmd2(get_instance_cmd) if instance_result['ret'] != 0: raise DbfenError( 20120073, instance_result['msg'] + instance_result['errmsg']) dir_instance_result = exec_cmd2(get_dir_instance_cmd) if dir_instance_result['ret'] != 0: raise DbfenError( 20120074, dir_instance_result['msg'] + dir_instance_result['errmsg']) except Exception as ex: log.exception(ex) raise ex return { 'instance': instance_result['msg'], 'dir_instance': dir_instance_result['msg'] }
def create_tenant_db(self): sql_cmd = r"CREATE DATABASE {} SYSTEM USER PASSWORD Suj000123".format( self.to_db) exec_command = self.system_db_exec_command_str(sql_cmd) result = exec_cmd2(exec_command) if result['ret'] != 0: raise DbfenError(20120080, result['msg'] + result['errmsg'])
def show_hana_databases(self, is_show=True): log.info('begin show_hana_databases!!!') sql_cmd = r"select DATABASE_NAME from SYS.M_DATABASES where ACTIVE_STATUS=\'YES\'" exec_command = self.system_db_exec_command_str(sql_cmd) exec_command_log = exec_command.replace( r'-p \"{}\"'.format(self.system_db_pwd), '-p ******') log.info('check_system_db_cmd:{}'.format(exec_command_log)) try: result = exec_cmd2(exec_command) log.info('result is:{}'.format(result)) status = result['ret'] output = result['msg'].strip() if status != 0 and self.tenant_user == '': log.error( 'system db abnormal,please check system db!!!,maybe is password not correct or others!!!' ) raise DbfenError(20120082, result['msg'] + result['errmsg']) databases = self.split_result_database_str(output) if len(databases) == 1: log.error( 'system db abnormal,please check system db!!!,maybe is password not correct or others!!!' ) raise DbfenError(20120082, result['msg'] + result['errmsg']) if is_show: return databases exec_command = self.system_db_exec_command_str(sql_cmd, False) exec_command_log = exec_command.replace( r'-p \"{}\"'.format(self.tenant_passwd), '-p ******') log.info('check target db {} is whether normal!!!'.format( exec_command_log)) result = exec_cmd2(exec_command) status = result['ret'] output = result['msg'].strip() tenant = self.split_result_database_str(output) if len(tenant) == 0: log.error( 'tenant db not active!!!please check tenant {} status!!!'. format(self.target_db)) raise DbfenError(20120083, result['msg'] + result['errmsg']) if status != 0: log.error('maybe is db: {} password incorrect!!!'.format( self.target_db)) raise DbfenError(20120076, result['msg'] + result['errmsg']) return tenant except Exception as ex: log.exception(ex) raise ex
def reset_tenant_database_password(self): # 暂时不用 default_new_pwd = 'Suj000123' sql_cmd = r"ALTER DATABASE {} SYSTEM USER PASSWORD {}".format( self.target_db, default_new_pwd) self.stop_tenant_database(self.target_db) exec_command = self.system_db_exec_command_str(sql_cmd) result = exec_cmd2(exec_command) if result['ret'] != 0: raise DbfenError(20120079, result['msg'] + result['errmsg'])
def obtain_full_backup_id_from_full_backup_file(self): full_backup_file = os.path.join(self.recv_file_dir, 'full_databackup_0_1') exec_command = "su - {} -c \"hdbbackupcheck -v {} | grep backupId\"|awk -F : \'{{print $2}}\'".format( self.hana_adm, full_backup_file) log.info('obtain_full_backup_id_from_full_backup_file exec_command:{}'. format(exec_command)) result = exec_cmd2(exec_command) if result['ret'] != 0: raise DbfenError(20120081, result['msg'] + result['errmsg']) return result['msg'].replace(' ', '')
def hana_check_version(self): # check hana database version hdb = '{}/HDB'.format(self.dir_instance) find_version_cmd = "su - {} -c '{} version'".format(self.hana_adm, hdb) find_version_result = exec_cmd2(find_version_cmd) if find_version_result['ret'] != 0: raise DbfenError( 20120075, find_version_result['msg'] + find_version_result['errmsg']) version = '' for i in find_version_result['msg'].split(os.linesep): if 'version' in i and 'HDB' not in i: version = i.split(':')[1].replace(' ', '') break return version
def get_hana_last_fullback_backup_id(self): sql_cmd = r'select BACKUP_ID from sys.M_BACKUP_CATALOG where ENTRY_TYPE_NAME=\'complete data backup\' and ' \ r'STATE_NAME = \'successful\' order by SYS_START_TIME desc limit 1' user, passpwd = (self.tenant_user, self.tenant_passwd) if self.target_db.upper() != 'SYSTEMDB' else \ (self.system_db_user, self.system_db_pwd) exec_command = r'su - {} -c "{} -n localhost -i {} -d {} -u {} -p \"{}\" -x {}"'.format( self.hana_adm, self.hdb_sql, self.instance, self.target_db, user, passpwd, sql_cmd) result = exec_cmd2(exec_command) exec_command_log = exec_command.replace( r'-p \"{}\"'.format(self.system_db_pwd), '-p ******') log.info('exec_command:{},{}'.format(result, exec_command_log)) if result['ret'] != 0: raise DbfenError(20120077, result['msg'] + result['errmsg']) else: msg_info = result['msg'].split(os.linesep) # 0 Indicates that a full backup of the database has not been done before last_fullback_backup_id = msg_info[1] if len(msg_info) == 2 else 0 return last_fullback_backup_id
def stop_tenant_database(self, db_name): sql_cmd = r"ALTER SYSTEM STOP DATABASE {}".format(db_name) exec_command = self.system_db_exec_command_str(sql_cmd) result = exec_cmd2(exec_command) if result['ret'] != 0: raise DbfenError(20120078, result['msg'] + result['errmsg'])