互联网笔记

Mydumper 数据备份恢复

2024-02-07 · 2 min read

Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具。

Mydumper主要特性:

轻量级C语言写的
多线程备份,备份后会生成多个备份文件
事务性和非事务性表一致的快照(适用于0.2.2以上版本)
快速的文件压缩
支持导出binlog
多线程恢复(适用于0.2.1以上版本)
以守护进程的工作方式,定时快照和连续二进制日志(适用于0.5.0以上版本)
开源 (GNU GPLv3)

/opt/sync.sh

#!/usr/bin/env bash

# Origin Database Credentials

origin_host=""
origin_port=3306
origin_user=""
origin_password=""
origin_database=""

# Target Database Credentials
target_host=""
target_port=3306
target_user=""
target_password=""
target_database=""

# Task ID
task_id=$(openssl rand -hex 8)

# Backup Database
function DatabaseBackup() {
    echo "Backup Database Start";
    mkdir -p /tmp/backup/$task_id
    mydumper -h $origin_host -P $origin_port -u $origin_user -p $origin_password -B $origin_database -o /tmp/backup/$task_id -v 3 -t 8
    echo "Backup Database End";
}

# Restore Database
function DatabaseRestore() {
    echo "Restore Database Start";
    myloader -h $target_host -P $target_port -u $target_user -p $target_password -B $target_database -d /tmp/backup/$task_id -v 3 -t 2
    echo "Restore Database End";
}

function main {
    echo "Sync Start";
    startTime=`date +%Y%m%d-%H:%M:%S`
    startTime_s=`date +%s`

    DatabaseBackup;
    DatabaseRestore;

    endTime=`date +%Y%m%d-%H:%M:%S`
    endTime_s=`date +%s`

    sumTime=$[ $endTime_s - $startTime_s ]
    echo "$startTime ---> $endTime" "Total:$sumTime seconds"

    echo "Sync End";
}

main