Mark Ku's Blog

Solving the Problem

Enterprises are increasingly using open-source services like headless CMS and WordPress. If data corruption occurs, recovery can be very difficult. Many of these services use MySQL. I wrote a PowerShell script that works with Windows Task Scheduler to create backups and upload them to OneDrive.

Writing the Backup PowerShell Script - 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

Writing the Database Restore PowerShell Script - 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 "資料庫恢復完成。"

Author

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。Read More

Found this useful?

The author's free tools, daily podcasts and newsletter are all here.

Mark Ku · This article is licensed under CC BY 4.0. Credit the author and link back to the original when reusing it.

Comments

Subscribe to Newsletter

Subscribe to get new posts delivered instantly — never miss a tech share.

By submitting, you agree to receive emails. You can anytime.

Popular Posts

View all
Mark Ku
··602

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution
Mark Ku
··492

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.
Mark Ku
··334

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki
Mark Ku
··268

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning
Mark Ku
··218

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1
Mark Ku
··217

Setting Up Samba on Ubuntu to Share Folders with Windows 11

Setting Up Samba on Ubuntu to Share Folders with Windows 11
使用Powershell 備份及還原 Mysql 容器 - Mark Ku's Tech Notes