Actualiser backup_and_restore/backup_docker_container.sh
This commit is contained in:
@@ -1,25 +1,26 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script backs up all running Docker containers.
|
||||
# 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 at the time of execution.
|
||||
# Backups are stored in a directory called "backup_docker_container".
|
||||
# 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.sh
|
||||
# 2. Make the script executable: chmod +x backup_containers.sh
|
||||
# 3. Run the script: ./backup_containers.sh
|
||||
# 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_docker_container.sh
|
||||
# 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:
|
||||
@@ -35,20 +36,16 @@
|
||||
# 3. Start a new container from the restored image:
|
||||
# docker run -d --name new_container_name new_image_name
|
||||
#
|
||||
# This will launch a new container based on the restored image.
|
||||
#
|
||||
# Note: This process does not restore Docker volumes. If your container used volumes,
|
||||
# you will need to restore or recreate them separately.
|
||||
#
|
||||
# This script only backs up running containers at the time of execution.
|
||||
# It exports only the container's file system (container content) and does not back up
|
||||
# Docker volumes that may be mounted from the host.
|
||||
# 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 directory
|
||||
backup_dir="backup_docker_container"
|
||||
# Backup directories
|
||||
backup_container_dir="backup_docker_container"
|
||||
backup_volume_dir="backup_docker_volumes"
|
||||
|
||||
# Create backup directory if it doesn't exist
|
||||
mkdir -p "$backup_dir"
|
||||
# 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")
|
||||
@@ -60,8 +57,20 @@ containers=$(docker ps --format '{{.Names}}')
|
||||
for container in $containers
|
||||
do
|
||||
echo "Backing up container $container..."
|
||||
docker export "$container" | gzip > "$backup_dir/${container}_${current_datetime}.tar.gz"
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user