def checksum_remote(self, host = '127.0.0.1', username = '', password = '', file_name = None): """对远程文件进行checksum 对远程文件进行md5sum并放回md5 值,已经执行远程命令的输出 Args: host: ip username: 远程OS用户名 password: 远程OS密码 file: 需要进行checksum的文件 Return md5(str) 返回一个md5值或者 None Raise: None """ # 构造执行shell命令 if file_name: self.remote_md5sum = Toolkit.md5_for_file_remote( host = host, username = username, password = password, file_name = file_name) elif self.remote_backup_file: self.remote_md5sum = Toolkit.md5_for_file_remote( host = host, username = username, password = password, file_name = self.remote_backup_file) return self.remote_md5sum
def checksum_remote(self, host='127.0.0.1', username='', password='', file_name=None): """对远程文件进行checksum 对远程文件进行md5sum并放回md5 值,已经执行远程命令的输出 Args: host: ip username: 远程OS用户名 password: 远程OS密码 file: 需要进行checksum的文件 Return md5(str) 返回一个md5值或者 None Raise: None """ # 构造执行shell命令 if file_name: self.remote_md5sum = Toolkit.md5_for_file_remote( host=host, username=username, password=password, file_name=file_name) elif self.remote_backup_file: self.remote_md5sum = Toolkit.md5_for_file_remote( host=host, username=username, password=password, file_name=self.remote_backup_file) return self.remote_md5sum
def backup_mycnf(self, backup_mycnf_file=None): """备份MySQL配置文件 通过传入的my.cnf备份位置, 执行操作系统cp命令实现备份. Args: backup_mycnf_file: MySQL配置文件备份路径 Return: True/False 返回是否备份成功 Raise: None """ # 先将备份 my.cnf路径进行赋值给backup_mycnf_file if backup_mycnf_file: self.backup_mycnf_file = backup_mycnf_file else: self.backup_mycnf_file = '{backup_dir}/my.cnf'.format( backup_dir = super(Mysqldump, self).dir) is_ok = False # 构造 cp 备份命令 cmd = 'cp {my_cnf} {backup_mycnf_file}'.format( my_cnf = self.my_cnf, backup_mycnf_file = self.backup_mycnf_file) is_ok = Toolkit.exec_cmd(cmd) return is_ok
def checksum(self, file=None): """给备份文件做checksum 给备份完的文件做一个checksum. 如果备份文件压缩了, 则是checksum压缩文件 Args: None Return: str 返回一个字符串, 该字符串是文件的checksum值 Raise: None """ if file: self.md5sum = Toolkit.md5_for_file(file) elif self.compress_file: self.md5sum = Toolkit.md5_for_file(self.compress_file) elif self.backup_file: self.md5sum = Toolkit.md5_for_file(self.backup_file) return self.md5sum
def backup_binlog(self, from_dir=None, to_dir=None, cmin=86400): """备份 BINLOG 根据时间所给的时间和目录来备份binllog Args: from_dir: binlog生成 to_dir: binlog备份存放 cmin: 备份几分钟前的binlog (默认86400分钟/24小时) return: True/False 备份是否成功 Raise: None """ # 判断复制给实例的binlog原路径和备份路劲 if from_dir: self.binlog_dir = from_dir if to_dir: self.backup_binlog_dir = to_dir # binlog相关路径记录日志 ToolLog.log_info('binlog dir: {dir}'.format(dir=self.binlog_dir)) ToolLog.log_info( 'binlog backup dir: {dir}'.format(dir=self.backup_binlog_dir)) is_ok = False if self.binlog_dir and self.backup_binlog_dir: cmd = ('mkdir -p {to_dir} && ' 'find {from_dir} -maxdepth 1 -cmin -{cmin} -print | ' 'sed "1d" | xargs -i cp -r {brace} {to_dir}'.format( to_dir=self.backup_binlog_dir, from_dir=self.binlog_dir, cmin=cmin, brace='{}')) is_ok = Toolkit.exec_cmd(cmd) return is_ok
def backup_size(self, file=None): """计算备份文件的大小 通过文件路径和名称, 获得该文件的大小. 如果有压缩的备份文件, 则计算压缩的备份文件 Args: None Return: str 返回一个字符串, 表示了文件的大小(单位 byte) Raise: None """ if file: self.backup_file_size = Toolkit.get_file_size(file) elif self.compress_file: self.backup_file_size = Toolkit.get_file_size(self.compress_file) elif self.backup_file: self.backup_file_size = Toolkit.get_file_size(self.backup_file) return self.backup_file_size
def backup_data(self, cmd): """执行备份 将给与的命令在操作系统上运行 Args: cmd: 需要执行的命令 Return: True/False 返回命令执行成功还是失败 """ # 创建备份目录 if self.dir: mkdir_cmd = 'mkdir -p {dir}'.format(dir = self.dir) Toolkit.exec_cmd(mkdir_cmd) is_ok = Toolkit.exec_cmd(cmd) if is_ok: ToolLog.log_info('backup successful!') else: ToolLog.log_error('backup fail !!!') return is_ok
def backup_data(self, cmd): """执行备份 将给与的命令在操作系统上运行 Args: cmd: 需要执行的命令 Return: True/False 返回命令执行成功还是失败 """ # 创建备份目录 if self.dir: mkdir_cmd = 'mkdir -p {dir}'.format(dir=self.dir) Toolkit.exec_cmd(mkdir_cmd) is_ok = Toolkit.exec_cmd(cmd) if is_ok: ToolLog.log_info('backup successful!') else: ToolLog.log_error('backup fail !!!') return is_ok
def main(): """ # 查询 CmdbO 对象 dao_cmdb_os = DaoCmdbO() cols = [CmdbO.os_id, CmdbO.username, CmdbO.password] cmdb_os = dao_cmdb_os.get_obj_by_pri(1, cols) print cmdb_os.os_id """ # 插入一个对象 dao_cmdb_os = DaoCmdbO() cmdb_os = CmdbO(hostname='normal_11', alias='normal_11', ip=Toolkit.ip2num('192.168.137.11'), username='******', password='******', remark='虚拟主机 normal 11') print dao_cmdb_os.insert_obj(cmdb_os)
def main(): """ # 查询 CmdbO 对象 dao_cmdb_os = DaoCmdbO() cols = [CmdbO.os_id, CmdbO.username, CmdbO.password] cmdb_os = dao_cmdb_os.get_obj_by_pri(1, cols) print cmdb_os.os_id """ # 插入一个对象 dao_cmdb_os = DaoCmdbO() cmdb_os = CmdbO(hostname = 'normal_11', alias = 'normal_11', ip = Toolkit.ip2num('192.168.137.11'), username = '******', password = '******', remark = '虚拟主机 normal 11') print dao_cmdb_os.insert_obj(cmdb_os)
def backup_binlog(self, from_dir=None, to_dir=None, cmin=86400): """备份 BINLOG 根据时间所给的时间和目录来备份binllog Args: from_dir: binlog生成 to_dir: binlog备份存放 cmin: 备份几分钟前的binlog (默认86400分钟/24小时) return: True/False 备份是否成功 Raise: None """ # 判断复制给实例的binlog原路径和备份路劲 if from_dir: self.binlog_dir = from_dir if to_dir: self.backup_binlog_dir = to_dir # binlog相关路径记录日志 ToolLog.log_info('binlog dir: {dir}'.format(dir = self.binlog_dir)) ToolLog.log_info('binlog backup dir: {dir}'.format( dir = self.backup_binlog_dir)) is_ok = False if self.binlog_dir and self.backup_binlog_dir: cmd = ( 'mkdir -p {to_dir} && ' 'find {from_dir} -maxdepth 1 -cmin -{cmin} -print | ' 'sed "1d" | xargs -i cp -r {brace} {to_dir}'.format( to_dir = self.backup_binlog_dir, from_dir = self.binlog_dir, cmin = cmin, brace = '{}' ) ) is_ok = Toolkit.exec_cmd(cmd) return is_ok
def backup_mycnf(self, backup_mycnf_file=None): """备份MySQL配置文件 通过传入的my.cnf备份位置, 执行操作系统cp命令实现备份. Args: backup_mycnf_file: MySQL配置文件备份路径 Return: True/False 返回是否备份成功 Raise: None """ # 先将备份 my.cnf路径进行赋值给backup_mycnf_file if backup_mycnf_file: self.backup_mycnf_file = backup_mycnf_file else: self.backup_mycnf_file = '{backup_dir}/my.cnf'.format( backup_dir=super(Xtrabackup, self).backup_file) is_ok = False # 构造 cp 备份命令 cmd = 'cp {my_cnf} {backup_mycnf_file}'.format( my_cnf=self.my_cnf, backup_mycnf_file=self.backup_mycnf_file) is_ok = Toolkit.exec_cmd(cmd) return is_ok