Mark Ku's Blog

Problem

Docker on Windows often breaks after an automatic upgrade. To deal with this, I wrote a PowerShell script that pairs with Windows Task Scheduler to back up Docker containers on a regular basis.

Backup shell script

# 定義備份的路徑
$backupPath = "C:\DockerBackup"
if (-Not (Test-Path -Path $backupPath)) {
    New-Item -ItemType Directory -Path $backupPath
}

# 獲取所有容器的ID

$containerIds = docker ps -aq

foreach ($id in $containerIds) {
    # 獲取容器的名稱
    $containerName = docker ps --filter "id=$id" --format "{{.Names}}"

    # 為每個容器創建一個備份目錄
    $containerBackupPath = Join-Path -Path $backupPath -ChildPath $containerName
    if (-Not (Test-Path -Path $containerBackupPath)) {
        New-Item -ItemType Directory -Path $containerBackupPath
    }

    # 備份容器的映像為tar文件
    $image = docker container inspect $id --format "{{.Image}}"
    $imageName = $image -replace '[:/]', '-'
    $imageBackupPath = Join-Path -Path $containerBackupPath -ChildPath "$imageName.tar"
    docker save -o $imageBackupPath $image
    
    # 備份容器的設定(JSON格式)
    $configBackupPath = Join-Path -Path $containerBackupPath -ChildPath "config.json"
    docker container inspect $id | Out-File -FilePath $configBackupPath
}

Write-Output "Backup Completed!"

Restore shell script

# 定義備份的路徑
$backupPath = "C:\DockerBackup"

# 確認備份路徑是否存在
if (-Not (Test-Path -Path $backupPath)) {
    Write-Error "Backup path does not exist!"
    exit
}

# 遍歷備份目錄中的每一個容器備份
Get-ChildItem -Path $backupPath | ForEach-Object {
    $containerBackupPath = $_.FullName

    # 還原映像
    $imageBackupPath = Get-ChildItem -Path $containerBackupPath -Filter "*.tar" | Select-Object -First 1
    if ($null -eq $imageBackupPath) {
        Write-Error "No image backup found for $($_.Name)!"
        return
    }
    docker load -i $imageBackupPath.FullName

    # 獲取備份的配置信息
    $configBackupPath = Join-Path -Path $containerBackupPath -ChildPath "config.json"
    if (-Not (Test-Path -Path $configBackupPath)) {
        Write-Error "No config backup found for $($_.Name)!"
        return
    }
    $config = Get-Content -Path $configBackupPath | ConvertFrom-Json
    
    # 創建新的容器使用還原的映像和配置
    # 注意:這可能需要基於實際的配置和用例進行調整
    docker run -d --name $config.Name $config.Image
}

Write-Output "Restore Completed!"

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
··490

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
··333

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
··264

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
··221

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
··215

Setting Up Samba on Ubuntu to Share Folders with Windows 11

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