Mark Ku's Blog
首頁 關於我
使用Powershell 備份及還原 Mysql 容器
PowerShell
使用Powershell 備份及還原 Mysql 容器
Mark Ku
Mark Ku
November 18, 2023
1 min

解決問題

企業使用越來越多開源服務,像是 headless cms 、wordpress,一旦資料損毀時,要補就很困難,很多都是 Mysql ,寫了個使用Powershell腳本搭配 Windows 排程備份,並將其備份至 One Drive。

撰寫備份 Powershell腳本 - backup-mysql-contrainer.ps1

# MariaDB資料庫備份腳本,帶有參數化的憑證

# 數據帳密
$dbUser = "your_username" # 用您的資料庫用戶名替換
$dbPassword = "your_password" # 用您的資料庫密碼替換

# 設置備份文件夾路徑
$backupDir = "E:\mysql-backup\de-cms-maria-db"
$containerName = "de-cms-maria-db"

# 確保備份目錄存在
if (-not (Test-Path -Path $backupDir)) {
    New-Item -ItemType Directory -Path $backupDir
}

# 獲取當前日期
$date = Get-Date -Format "yyyy-MM-dd"

# 初始化流水號
$serial = 1

# 定義備份文件名
do {
    $serialPadded = "{0:D2}" -f $serial
    $backupFileName = "$date-$serialPadded-db.dump"
    $backupFilePath = Join-Path -Path $backupDir -ChildPath $backupFileName
    $serial++
} while (Test-Path -Path $backupFilePath)

# 執行Docker命令以備份資料庫
$dockerCommand = "docker exec $containerName /bin/bash -c ""mysqldump -u $dbUser -p$dbPassword --routines --triggers strapi"""
Invoke-Expression "$dockerCommand > `"$backupFilePath`""

# 刪除舊的備份,只保留最新的20個
$allBackups = Get-ChildItem -Path $backupDir -Filter "*.dump" | Sort-Object LastWriteTime -Descending
if ($allBackups.Count -gt 20) {
    $backupsToDelete = $allBackups[20..($allBackups.Count-1)]
    foreach ($backup in $backupsToDelete) {
        Remove-Item $backup.FullName
    }
}

robocopy C:\mysql-backup "C:\Users\Administrator\OneDrive - ABC Technology Corp\Database\mysql-backup\us-strapi-db" /MIR

撰寫資料庫還原 Powershell腳本 - restore-mysql-contrainer.ps1


# 資料庫憑證
$dbUser = "your_username" # 用您實際的資料庫用戶名替換
$dbPassword = "your_password" # 用您實際的資料庫密碼替換

# 設置備份目錄路徑
$backupDir = "E:\mysql-backup\de-cms-maria-db"
$containerName = "de-cms-maria-db"

# 確保備份目錄存在
if (-not (Test-Path -Path $backupDir)) {
    Write-Host "備份目錄不存在。退出腳本。"
    exit
}

# 選擇要恢復的備份文件
$allBackups = Get-ChildItem -Path $backupDir -Filter "*.dump" | Sort-Object LastWriteTime -Descending
if ($allBackups.Count -eq 0) {
    Write-Host "未找到備份文件。退出腳本。"
    exit
}

# 可選操作,列出所有備份並讓用戶選擇一個
Write-Host "可用的備份文件:"
$allBackups | ForEach-Object { Write-Host $_.Name }

# 假設恢復最新的備份,但這可以修改
$backupToRestore = $allBackups[0].FullName
Write-Host "從此文件恢復:$backupToRestore"

# 執行Docker命令以恢復資料庫
$dockerCommand = "docker exec -i $containerName /bin/bash -c ""mysql -u $dbUser -p$dbPassword strapi"""
Invoke-Expression "`"$dockerCommand`" < `"$backupToRestore`""

Write-Host "資料庫恢復完成。"

Tags

Mark Ku

Mark Ku

Software Developer

8年以上豐富網站開發經驗,直播系統、POS系統、電子商務、平台網站、SEO、金流串接、DevOps、Infra 出身,帶過幾次團隊,目前專注於北美及德國市場電商網站開發團隊。

Expertise

前端(React)
後端(C#)
網路管理
DevOps
溝通
領導

Social Media

facebook github website

Related Posts

使用Powershell 備份及還原 Windows docker 容器
使用Powershell 備份及還原 Windows docker 容器
November 18, 2023
1 min

Quick Links

關於我

Social Media