1. 首页 > 国产数据库教程 > TiDB教程 > 正文

tidb-096-TiDB定时任务与作业管理

3.1.2 备份任务

风哥提示:

# 使用BR执行备份
tiup br backup full –pd “192.168.1.10:2379” –storage “s3://backup-bucket/fgedudb/$(date +%Y%m%d)” –ratelimit 128

3.2 使用crontab管理定时任务

3.2.1 配置crontab

# 编辑crontab
crontab -e

# 添加定时任务
# 每天凌晨1点执行全量备份
0 1 * * * tiup br backup full --pd "192.168.1.10:2379" --storage "s3://backup-bucket/fgedudb/$(date +\%Y\%m\%d)" --ratelimit 128

# 每小时执行一次增量备份
0 * * * * tiup br backup inc --pd "192.168.1.10:2379" --storage "s3://backup-bucket/fgedudb/$(date +\%Y\%m\%d)" --last-backup "s3://backup-bucket/fgedudb/$(date +\%Y\%m\%d)" --ratelimit 64

# 每天凌晨3点执行统计信息收集
0 3 * * * mysql -h 192.168.1.20 -P 4000 -u root -p -e "ANALYZE TABLE test.table;"

3.2.2 查看crontab任务

crontab -l

3.3 使用Airflow管理复杂任务

3.3.1 安装Airflow

# 安装Airflow
pip install apache-airflow

# 初始化Airflow
airflow db init

# 创建Airflow用户
airflow users create –username admin –firstname Admin –lastname User –role Admin –email admin@example.com

# 启动Airflow Web服务器
airflow webserver –port 8080

# 启动Airflow调度器
airflow scheduler

3.3.2 配置Airflow DAG

# dags/tidb_backup.py
from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime, timedelta

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'email': ['admin@example.com'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5),
}

with DAG(
    'tidb_backup',
    default_args=default_args,
    description='TiDB backup workflow',
    schedule_interval=timedelta(days=1),
    start_date=datetime(2024, 1, 1),
    catchup=False,
) as dag:
    # 全量备份任务
    full_backup = BashOperator(
        task_id='full_backup',
        bash_command='tiup br backup full --pd "192.168.1.10:2379" --storage "s3://backup-bucket/fgedudb/{{ ds }}" --ratelimit 128',
    )
    
    # 统计信息收集任务
    analyze_table = BashOperator(
        task_id='analyze_table',
        bash_command='mysql -h 192.168.1.20 -P 4000 -u root -p -e "ANALYZE TABLE test.table;"',
    )
    
    # 任务依赖
    full_backup >> analyze_table

3.4 作业管理操作

3.4.1 查看作业状态

# 查看TiDB中的作业
SHOW PROCESSLIST;

# 查看备份作业状态
tiup br status –pd “192.168.1.10:2379”

3.4.2 暂停和恢复作业

学习交流加群风哥QQ113257174

# 暂停作业
KILL [process_id];

# 恢复作业(重新执行)
tiup br backup full –pd “192.168.1.10:2379” –storage “s3://backup-bucket/fgedudb/$(date +%Y%m%d)” –ratelimit 128

3.4.3 监控作业执行

# 查看作业日志
tail -f /tidb/app/tidb/logs/tidb.log

# 查看备份日志
tiup br backup full –pd “192.168.1.10:2379” –storage “s3://backup-bucket/fgedudb/$(date +%Y%m%d)” –ratelimit 128 –log-file /tidb/app/backup.log
tail -f /tidb/app/backup.log

更多视频教程www.fgedu.net.cn

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息