77 lines
3.2 KiB
Bash
77 lines
3.2 KiB
Bash
#!/bin/bash
|
|
|
|
# This script backs up all running Docker containers and their associated volumes.
|
|
# For each container, it creates a compressed archive file (.tar.gz) containing
|
|
# the container's state and any named volumes used by the container at the time of execution.
|
|
# Backups are stored in a directory called "backup_docker_container" and "backup_docker_volumes".
|
|
# Each backup file is named with the container name and the current date and time,
|
|
# making it easy to identify when the backup was taken.
|
|
#
|
|
# Usage:
|
|
# 1. Save this script to a file, for example: backup_containers_volumes.sh
|
|
# 2. Make the script executable: chmod +x backup_containers_volumes.sh
|
|
# 3. Run the script: ./backup_containers_volumes.sh
|
|
#
|
|
# Automation with Cron:
|
|
# You can automate the execution of this script and manage old backups with a Cron job:
|
|
#
|
|
# 1. To back up all Docker containers every Sunday at midnight:
|
|
# 0 0 * * 0 /root/backup_containers_volumes.sh
|
|
#
|
|
# 2. To delete backup files older than 14 days every day at 1 AM:
|
|
# 0 1 * * * find /root/backup_docker_container/* -mtime +14 -exec rm {} \;
|
|
# 0 1 * * * find /root/backup_docker_volumes/* -mtime +14 -exec rm {} \;
|
|
#
|
|
# Restoring a backup:
|
|
# To restore a Docker container from a backup, follow these steps:
|
|
#
|
|
# 1. Uncompress the .tar.gz file:
|
|
# gunzip /path/to/backup_docker_container/container_name_date.tar.gz
|
|
#
|
|
# 2. Create a Docker image from the .tar file:
|
|
# cat /path/to/backup_docker_container/container_name_date.tar | docker import - new_image_name
|
|
#
|
|
# This will create a new Docker image from the container's backup.
|
|
#
|
|
# 3. Start a new container from the restored image:
|
|
# docker run -d --name new_container_name new_image_name
|
|
#
|
|
# 4. Restore the Docker volumes using the backed-up volume files.
|
|
# Use `docker volume create` and `docker run` to copy data back to the restored volumes.
|
|
|
|
# Backup directories
|
|
backup_container_dir="backup_docker_container"
|
|
backup_volume_dir="backup_docker_volumes"
|
|
|
|
# Create backup directories if they don't exist
|
|
mkdir -p "$backup_container_dir"
|
|
mkdir -p "$backup_volume_dir"
|
|
|
|
# Get current date and time
|
|
current_datetime=$(date +"%Y-%m-%d_%H-%M-%S")
|
|
|
|
# Get list of running Docker containers
|
|
containers=$(docker ps --format '{{.Names}}')
|
|
|
|
# Iterate through each container and create a compressed backup with container name and current date and time
|
|
for container in $containers
|
|
do
|
|
echo "Backing up container $container..."
|
|
docker export "$container" | gzip > "$backup_container_dir/${container}_${current_datetime}.tar.gz"
|
|
echo "Backup of container $container completed."
|
|
|
|
# Get the volumes associated with the container
|
|
volumes=$(docker inspect --format '{{ range .Mounts }}{{ if eq .Type "volume" }}{{ .Name }} {{ end }}{{ end }}' "$container")
|
|
|
|
# Iterate through each volume and create a backup
|
|
for volume in $volumes
|
|
do
|
|
echo "Backing up volume $volume for container $container..."
|
|
docker run --rm -v "$volume":/volume -v "$backup_volume_dir":/backup busybox tar czf "/backup/${volume}_${current_datetime}.tar.gz" -C /volume .
|
|
echo "Backup of volume $volume completed."
|
|
done
|
|
|
|
done
|
|
|
|
# Indicate that the backup process is completed
|
|
echo "Backup process completed." |