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.
- Docker Engine
- Bash shell
jq
for JSON parsing (for inspecting Docker volumes)
- Docker Desktop for Windows
- PowerShell (v5+)
jq
for JSON parsing (optional but recommended)
The backup script performs the following actions:
- Create a Docker image of the specified container.
- Backup named volumes associated with the container.
- Save the image and volumes to a user-specified backup directory for future restoration.
-
Save the script as
Backup-DockerContainer.sh
(for Linux) orBackup-DockerContainer.ps1
(for Windows). -
Run the script based on your operating system:
For Linux:
bash Backup-DockerContainer.sh
For Windows:
.\Backup-DockerContainer.ps1
-
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.
The restore script allows you to:
- Load a Docker image from a
.tar
backup file. - Restore named volumes from backup
.tar
files. - Create and run the restored container using the loaded image and restored volumes.
-
Save the script as
Restore-DockerContainer.sh
(for Linux) orRestore-DockerContainer.ps1
(for Windows). -
Run the script based on your operating system:
For Linux:
bash Restore-DockerContainer.sh
For Windows:
.\Restore-DockerContainer.ps1
-
Follow the interactive prompts to:
- Enter the backup directory.
- Specify a name for the restored container.
- Optionally, restore volumes.
- Optionally, start the container immediately.
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
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.
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.
-
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.
- The script creates an image of the container and saves it as a
-
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.
This repository is licensed under the MIT License. Feel free to modify and distribute the scripts as needed.