Created
November 23, 2024 10:27
-
-
Save purplecandy/2b8e693244c82fa8ad4966c8ac56db34 to your computer and use it in GitHub Desktop.
Scirpt to easily backup and restore docker volumes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set backup directory | |
BACKUP_DIR="./docker_backups" | |
DATE=$(date +%Y%m%d_%H%M%S) | |
# Create backup directory if not exists | |
mkdir -p "$BACKUP_DIR" | |
# Loop through all running containers from docker-compose | |
for CONTAINER in $(docker-compose ps -q); do | |
CONTAINER_NAME=$(docker inspect --format='{{.Name}}' "$CONTAINER" | cut -c2-) # Remove leading "/" | |
echo "Backing up container: $CONTAINER_NAME" | |
# Get volume mounts for the container | |
MOUNTS=$(docker inspect --format='{{range .Mounts}}{{if eq .Type "volume"}}{{.Name}}:{{.Source}} {{end}}{{end}}' "$CONTAINER") | |
for MOUNT in $MOUNTS; do | |
VOLUME_NAME=$(echo "$MOUNT" | cut -d':' -f1) | |
VOLUME_PATH=$(echo "$MOUNT" | cut -d':' -f2) | |
echo "Backing up volume: $VOLUME_NAME at path: $VOLUME_PATH" | |
# Backup the volume | |
BACKUP_FILE="$BACKUP_DIR/${CONTAINER_NAME}_${VOLUME_NAME}_$DATE.tar.gz" | |
tar -czf "$BACKUP_FILE" -C "$VOLUME_PATH" . | |
echo "Backup saved: $BACKUP_FILE" | |
done | |
done | |
echo "All backups completed!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set backup directory | |
BACKUP_DIR="/absolute/path/to/docker_backups" # <-- Make sure this is the correct path! | |
# Check if the backup directory exists | |
if [ ! -d "$BACKUP_DIR" ]; then | |
echo "Backup directory not found: $BACKUP_DIR" | |
exit 1 | |
fi | |
# Loop through all containers managed by Docker Compose | |
for CONTAINER in $(docker-compose ps -q); do | |
CONTAINER_NAME=$(docker inspect --format='{{.Name}}' "$CONTAINER" | cut -c2-) # Remove leading "/" | |
echo "Restoring volumes for container: $CONTAINER_NAME" | |
# Get volume mounts for the container | |
MOUNTS=$(docker inspect --format='{{range .Mounts}}{{if eq .Type "volume"}}{{.Name}}:{{.Source}} {{end}}{{end}}' "$CONTAINER") | |
for MOUNT in $MOUNTS; do | |
VOLUME_NAME=$(echo "$MOUNT" | cut -d':' -f1) | |
VOLUME_PATH=$(echo "$MOUNT" | cut -d':' -f2) | |
# Find the most recent backup for the volume | |
BACKUP_FILE=$(ls -t "$BACKUP_DIR/${CONTAINER_NAME}_${VOLUME_NAME}_*.tar.gz" | head -n 1) | |
# Debugging: Check if the backup file exists | |
if [ ! -f "$BACKUP_FILE" ]; then | |
echo "Error: No backup file found for volume $VOLUME_NAME in container $CONTAINER_NAME at $BACKUP_FILE" | |
continue | |
fi | |
echo "Restoring volume: $VOLUME_NAME from backup: $BACKUP_FILE" | |
echo "Volume path: $VOLUME_PATH" | |
# Debugging: Check if the volume path exists | |
if [ ! -d "$VOLUME_PATH" ]; then | |
echo "Error: Volume path does not exist: $VOLUME_PATH" | |
continue | |
fi | |
# Stop the container to safely restore the volume | |
echo "Stopping container: $CONTAINER_NAME" | |
docker stop "$CONTAINER_NAME" | |
# Extract the backup to the volume's directory | |
tar -xzf "$BACKUP_FILE" -C "$VOLUME_PATH" | |
# Start the container after restoration | |
echo "Starting container: $CONTAINER_NAME" | |
docker start "$CONTAINER_NAME" | |
echo "Volume restored: $VOLUME_NAME" | |
done | |
done | |
echo "All volumes restored!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment