revert c1e242816b
revert Actualiser backup_and_restore/backup_docker_container.sh
This commit is contained in:
@@ -1,27 +1,26 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# This script backs up all running Docker containers and their associated volumes.
|
# This script backs up all running Docker containers.
|
||||||
# For each container, it creates a compressed archive file (.tar.gz) containing
|
# 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.
|
# the container's state at the time of execution.
|
||||||
# Backups are stored in a directory called "backup_docker_container" and "backup_docker_volumes".
|
# Backups are stored in a directory called "backup_docker_container".
|
||||||
# Each backup file is named with the container name and the current date and time,
|
# 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.
|
# making it easy to identify when the backup was taken.
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# 1. Save this script to a file, for example: backup_containers_volumes.sh
|
# 1. Save this script to a file, for example: backup_containers.sh
|
||||||
# 2. Make the script executable: chmod +x backup_containers_volumes.sh
|
# 2. Make the script executable: chmod +x backup_containers.sh
|
||||||
# 3. Run the script: ./backup_containers_volumes.sh
|
# 3. Run the script: ./backup_containers.sh
|
||||||
#
|
#
|
||||||
# Automation with Cron:
|
# Automation with Cron:
|
||||||
# You can automate the execution of this script and manage old backups with a Cron job:
|
# 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:
|
# 1. To back up all Docker containers every Sunday at midnight:
|
||||||
# 0 0 * * 0 /root/backup_containers_volumes.sh
|
# 0 0 * * 0 /root/backup_docker_container.sh
|
||||||
#
|
#
|
||||||
# 2. To delete backup files older than 14 days every day at 1 AM:
|
# 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_container/* -mtime +14 -exec rm {} \;
|
||||||
# 0 1 * * * find /root/backup_docker_volumes/* -mtime +14 -exec rm {} \;
|
#
|
||||||
#
|
|
||||||
# Restoring a backup:
|
# Restoring a backup:
|
||||||
# To restore a Docker container from a backup, follow these steps:
|
# To restore a Docker container from a backup, follow these steps:
|
||||||
#
|
#
|
||||||
@@ -36,16 +35,20 @@
|
|||||||
# 3. Start a new container from the restored image:
|
# 3. Start a new container from the restored image:
|
||||||
# docker run -d --name new_container_name new_image_name
|
# docker run -d --name new_container_name new_image_name
|
||||||
#
|
#
|
||||||
# 4. Restore the Docker volumes using the backed-up volume files.
|
# This will launch a new container based on the restored image.
|
||||||
# Use `docker volume create` and `docker run` to copy data back to the restored volumes.
|
#
|
||||||
|
# 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.
|
||||||
|
|
||||||
# Backup directories
|
# Backup directory
|
||||||
backup_container_dir="backup_docker_container"
|
backup_dir="backup_docker_container"
|
||||||
backup_volume_dir="backup_docker_volumes"
|
|
||||||
|
|
||||||
# Create backup directories if they don't exist
|
# Create backup directory if it doesn't exist
|
||||||
mkdir -p "$backup_container_dir"
|
mkdir -p "$backup_dir"
|
||||||
mkdir -p "$backup_volume_dir"
|
|
||||||
|
|
||||||
# Get current date and time
|
# Get current date and time
|
||||||
current_datetime=$(date +"%Y-%m-%d_%H-%M-%S")
|
current_datetime=$(date +"%Y-%m-%d_%H-%M-%S")
|
||||||
@@ -57,21 +60,9 @@ containers=$(docker ps --format '{{.Names}}')
|
|||||||
for container in $containers
|
for container in $containers
|
||||||
do
|
do
|
||||||
echo "Backing up container $container..."
|
echo "Backing up container $container..."
|
||||||
docker export "$container" | gzip > "$backup_container_dir/${container}_${current_datetime}.tar.gz"
|
docker export "$container" | gzip > "$backup_dir/${container}_${current_datetime}.tar.gz"
|
||||||
echo "Backup of container $container completed."
|
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
|
done
|
||||||
|
|
||||||
# Indicate that the backup process is completed
|
# Indicate that the backup process is completed
|
||||||
echo "Backup process completed."
|
echo "Backup process completed."
|
||||||
|
|||||||
Reference in New Issue
Block a user