tidb备份与恢复脚本


#!/usr/bin/python3
# encoding: utf-8
#filename: tidb-backup-restore.py
#author: gaohaixiang
#writetime:20250117


import re
import time
import subprocess
from datetime import datetime, timezone

import boto3
from botocore.exceptions import NoCredentialsError, PartialCredentialsError, ClientError
from urllib.parse import urlparse, parse_qs

"""
pip3 install boto3
"""

## br 命令行工具安装
def brCommandInstall():
    command = '''
    tiup install br:v8.5.0
    '''
    return command

## 集群的情况查看
def catStatusTidb():
    Command = '''
    /root/.tiup/bin/tiup cluster display tidb-cluster
    '''
    return Command

## 执行日志备份
def logBackupStart(pdInformation,s3ConInforLogbackup,secretInformation):
    command = '''
    tiup br log start --task-name=pitr --pd="{}" \
    --storage "{}"
    '''.format(pdInformation,s3ConInforLogbackup + secretInformation)
    return command

# 停止日志备份
def logBackupStop(pdInformation):
    command = '''
    tiup br log stop --task-name=pitr --pd="{}"
    '''.format(pdInformation)
    return command

# 启动日志备份任务后,可以查询日志备份任务状态:
def catLogBackupStatus(pdInformation):
    command = '''
    tiup br log status --task-name=pitr --pd="{}"
    '''.format(pdInformation)
    return command

## 执行快照备份
def snapshotBackup(pdInformation,s3ConInforSnapshot,secretInformation,formatted_date_with_timezone):
    command = '''
    tiup br backup full --pd="{}" \
    --storage "{}" \
    --backupts='{}'
    '''.format(pdInformation,s3ConInforSnapshot + secretInformation,formatted_date_with_timezone)
    return command

### 备份单个数据库的数据 backupDataSingleDatabase
def  backupDataSingleDatabase(pdInformation,database,s3ConInforSnapshot,secretInformation):
    command = '''
    tiup br backup db \
        --pd "{}" \
        --db {} \
        --storage "{}" \
        --ratelimit 128 \
        --log-file backuptable.log
    '''.format(pdInformation,database,s3ConInforSnapshot + secretInformation)
    return command


### 备份单张表的数据  backupDataSingleTable
def   backupDataSingleTable(pdInformation,database,table,s3ConInforSnapshot,secretInformation):
    command = '''
    tiup br backup table \
        --pd "{}" \
        --db {} \
        --table {} \
        --storage "{}" \
        --ratelimit 128 \
        --log-file backuptable.log
    '''.format(pdInformation,database,table,s3ConInforSnapshot + secretInformation)
    return command


### 使用表库过滤功能备份多张表的数据 backupDataSingleDatabaseTable
def backupDataSingleDatabaseTable(pdInformation,databaseOrTableFilter,s3ConInforSnapshot,secretInformation):
    command = '''
    def  backupDataSingleDatabaseTable():
    tiup br backup full \
        --pd "{}" \
        --filter '{}' \
        --storage "{}" \
        --ratelimit 128 \
        --log-file backupfull.log
    '''.format(pdInformation,databaseOrTableFilter,s3ConInforSnapshot + secretInformation)
    return command




## 执行 PITR
def restoreSpecifiedTimeData(pdInformation,s3ConInforLogbackup,s3ConInforSnapshot,secretInformation,formatted_date_with_timezone):
    command = '''
    tiup br restore point --pd="{}" \
    --storage "{}" \
    --full-backup-storage="{}" \
    --restored-ts '{}'
    '''.format(pdInformation,s3ConInforLogbackup + secretInformation,s3ConInforSnapshot + secretInformation,formatted_date_wit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值