基于Python和Xtrbackup的自动化备份与还原实现(4)


logging.basicConfig(level=logging.INFO
                    #handlers={logging.FileHandler(filename='restore_log_info.log', mode='a', encoding='utf-8')}
                    )

host = "127.0.0.1"
port = "7000"
user = "root"
password = "root"
instance_name = "mysqld_7000"
stop_at = "2019-08-01 18:50:59"
cnf_file = "/usr/local/mysql57_data/mysql7000/etc/my.cnf"
backup_dir = "/usr/local/backupdata/"
dest_dir = "/temp/restoretmp/"
xtrabackuplog_name = "backuplog.log"
backupfilelist = os.path.join(backup_dir,"backupfilelist.log")


#根据key值,获取MySQL配置文件中的value
def get_config_value(key):
    value = None
    if not key:
        return value
    if os.path.exists(cnf_file):
        with open(cnf_file, 'r') as f:
            for line in f:
                if (line.split("=")[0]):
                    if(line[0:1]!="#" and line[0:1]!="["):
                        if (key==line.split("=")[0].strip()):
                            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,backup_name)))
        #print("cd {0}".format(os.path.join(dest_dir,backup_name)))
        os.system("cd {0}".format(os.path.join(dest_dir,backup_name)))
        #print('''for f in `find {0}/ -iname "*\.qp"`; do qpress -dT4 $f  $(dirname $f) && rm -f $f; done '''.format(os.path.join(dest_dir,backup_name)))
        os.system('''for f in `find {0}/ -iname "*\.qp"`; do qpress -dT4 $f  $(dirname $f) && rm -f $f; done'''.format(os.path.join(dest_dir,backup_name)))

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8e81d74611b96e8bac99544080a10328.html