设为首页 加入收藏

TOP

基于Python和Xtrbackup的自动化备份与还原实现(五)
2019-08-13 05:35:48 】 浏览:161
Tags:基于 Python Xtrbackup 自动化 备份 还原 实现
bsp;           value =line.split("=")[1].strip()
    return value



def stop_mysql_service():
    print("################stop mysql service###################")
    print("systemctl stop {}".format(instance_name))


def start_mysql_service():
    print("################stop mysql service###################")
    print("systemctl start {0}".format(instance_name))



#返回备份日志中的最新的一个早于stop_at时间的完整备份,以及其后面的增量备份
def get_restorefile_list():
    list_backup = []
    list_restore_file = []
    if os.path.exists(backupfilelist):
        with open(backupfilelist, 'r') as f:
            lines = f.readlines()
            for line in lines:
                list_backup.append(line.replace("\n",""))
    if (list_backup):
        for i in range(len(list_backup) - 1, -1, -1):
            list_restore_file.append(list_backup[i])
            backup_name = list_backup[i].split("|")[2]
            if "full" in backup_name:
                full_backup_time = list_backup[i].split("|")[1]
                if(stop_at<full_backup_time):
                    break
                else:
                    list_restore_file = None
    #restore file in the list_restore_log
    list_restore_file.reverse()
    return list_restore_file


 


#解压缩需要还原的备份文件,包括一个完整备份以及N个增量备份(N>=0)
def uncompress_backup_file():
    print("################uncompress backup file###################")
    list_restore_backup = get_restorefile_list()



    #如果没有生成时间早于stop_at的完整备份,无法恢复,退出
    if not list_restore_backup:
        raise("There is no backup that can be restored")
        exit(1)



    for restore_log in list_restore_backup:
        #解压备份文件
        backup_name = restore_log.split("|")[2]
        backup_path = restore_log.split("|")[2]
        backup_full_name = os.path.join(backup_dir,backup_path,backup_name)
        backup_path = os.path.join(backup_dir,restore_log.split("|")[-1])
        #print('''[ ! -d {0} ] && mkdir -p {0}'''.format(os.path.join(dest_dir,backup_name)))
        os.system('''[ ! -d {0} ] && mkdir -p {0}'''.format(os.path.join(dest_dir,backup_name)))
        #print("xbstream -x < {0}.xbstream -C {1}".format(backup_full_name,os.path.join(dest_dir,backup_name)))
        os.system("xbstream -x < {0}.xbstream -C {1}".format(backup_full_name,os.path.join(dest_dir,b

首页 上一页 2 3 4 5 6 7 8 下一页 尾页 5/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇数据库包括哪些技术? 下一篇Redis分布式锁实现理解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目