Skip to content

This repository provides scripts for backing up and restoring Docker containers and their associated volumes. The scripts are designed to work on both Windows and Linux environments, allowing users to easily create backups of Docker images and volumes, and restore them when needed.

License

Notifications You must be signed in to change notification settings

Poagesh/docker-backup-restore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Docker Container Backup & Restore Scripts

This repository contains scripts designed to back up and restore Docker containers and their associated volumes. These scripts are compatible with both Windows and Linux operating systems.

  • Backup Script: Creates backups of Docker containers and their volumes, saving them to a specified directory.
  • Restore Script: Restores a Docker container and its volumes from a backup directory.

🛠️ Requirements

For Linux:

  • Docker Engine
  • Bash shell
  • jq for JSON parsing (for inspecting Docker volumes)

For Windows:

  • Docker Desktop for Windows
  • PowerShell (v5+)
  • jq for JSON parsing (optional but recommended)

📋 Backup Script Overview

The backup script performs the following actions:

  1. Create a Docker image of the specified container.
  2. Backup named volumes associated with the container.
  3. Save the image and volumes to a user-specified backup directory for future restoration.

How to Use:

  1. Save the script as Backup-DockerContainer.sh (for Linux) or Backup-DockerContainer.ps1 (for Windows).

  2. Run the script based on your operating system:

    For Linux:

    bash Backup-DockerContainer.sh

    For Windows:

    .\Backup-DockerContainer.ps1
  3. Follow the interactive prompts to:

    • Enter the container name to back up.
    • Provide a directory where the backup will be saved.
    • Optionally, back up associated volumes.

🔄 Restore Script Overview

The restore script allows you to:

  1. Load a Docker image from a .tar backup file.
  2. Restore named volumes from backup .tar files.
  3. Create and run the restored container using the loaded image and restored volumes.

How to Use:

  1. Save the script as Restore-DockerContainer.sh (for Linux) or Restore-DockerContainer.ps1 (for Windows).

  2. Run the script based on your operating system:

    For Linux:

    bash Restore-DockerContainer.sh

    For Windows:

    .\Restore-DockerContainer.ps1
  3. Follow the interactive prompts to:

    • Enter the backup directory.
    • Specify a name for the restored container.
    • Optionally, restore volumes.
    • Optionally, start the container immediately.

📁 Backup Directory Structure (Expected)

For both scripts, the backup directory should have the following structure:

C:\Backups\mycontainer\2025-04-30_15-23-00\   (Windows)
or
/home/user/Backups/mycontainer/2025-04-30_15-23-00/   (Linux)
├── mycontainer-image-2025-04-30_15-23-00.tar
├── myvolume1-backup-2025-04-30_15-23-00.tar
├── myvolume2-backup-2025-04-30_15-23-00.tar

💬 Sample Interaction

Backup Script:

Linux:

$ bash Backup-DockerContainer.sh
Enter the Docker container name to back up: mycontainer
Enter the full path where you want the backup to be stored: /home/user/Backups/mycontainer/2025-04-30_15-23-00
Starting backup for container: mycontainer
Creating image from container...
Container image saved to: /home/user/Backups/mycontainer/2025-04-30_15-23-00/mycontainer-image-2025-04-30_15-23-00.tar
...
Backup completed successfully.

Windows (PowerShell):

Enter the Docker container name to back up: mycontainer
Enter the full path where you want the backup to be stored: C:\Backups\mycontainer\2025-04-30_15-23-00
Starting backup for container: mycontainer
Creating image from container...
Container image saved to: C:\Backups\mycontainer\2025-04-30_15-23-00\mycontainer-image-2025-04-30_15-23-00.tar
...
Backup completed successfully.

Restore Script:

Linux:

$ bash Restore-DockerContainer.sh
Enter the full path to the backup folder (e.g., /home/user/Backups/mycontainer/2025-04-30_15-23-00): /home/user/Backups/mycontainer/2025-04-30_15-23-00
Loading Docker image from: mycontainer-image-2025-04-30_15-23-00.tar
Do you want to restore volumes as well? (yes/no): yes
Restoring volume: myvolume1 from myvolume1-backup-2025-04-30_15-23-00.tar
Do you want to create and run the container now? (yes/no): yes
Container 'mycontainer' has been created and is running.

Windows (PowerShell):

Enter the full path to the backup folder (e.g., C:\Backups\mycontainer\2025-04-30_15-23-00): C:\Backups\mycontainer\2025-04-30_15-23-00
Loading Docker image from: mycontainer-image-2025-04-30_15-23-00.tar
Do you want to restore volumes as well? (yes/no): yes
Restoring volume: myvolume1 from myvolume1-backup-2025-04-30_15-23-00.tar
Do you want to create and run the container now? (yes/no): yes
Container 'mycontainer' has been created and is running.

⚠️ Notes

  • Backup:

    • The script creates an image of the container and saves it as a .tar file.
    • Volumes associated with the container are backed up as .tar files.
    • If no volumes are attached to the container, the script skips volume backup.
  • Restore:

    • The script restores the container image and volumes.
    • Volumes are restored to Docker volumes or specific host paths.
    • The restored container can be started immediately after restoration.
  • Volume Restoration:

    • Ensure that the application inside the volumes is capable of handling restored data.
    • Volumes are restored to Docker volumes or specific host paths.

🧾 License

This repository is licensed under the MIT License. Feel free to modify and distribute the scripts as needed.

About

This repository provides scripts for backing up and restoring Docker containers and their associated volumes. The scripts are designed to work on both Windows and Linux environments, allowing users to easily create backups of Docker images and volumes, and restore them when needed.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published