Skip to content

Instantly share code, notes, and snippets.

@Phate334
Last active November 5, 2024 02:08
Show Gist options
  • Save Phate334/284a854d8b66fc6d4d511a0460a2038e to your computer and use it in GitHub Desktop.
Save Phate334/284a854d8b66fc6d4d511a0460a2038e to your computer and use it in GitHub Desktop.
Simple script to backup volumes in docker compose env.
#!/bin/bash
# Function to get Docker API version
get_docker_api_version() {
docker version --format '{{.Server.APIVersion}}'
}
# Function to get volume paths from docker-compose and backup
backup_volumes() {
# Get the API version
API_VERSION=$(get_docker_api_version)
echo "Docker API Version: $API_VERSION"
# Create a backup directory with the current date
BACKUP_DIR="backup-$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"
echo "Backup directory created: $BACKUP_DIR"
# Get all container ids from docker-compose
container_ids=$(docker-compose ps -q)
# Loop through each container
for container_id in $container_ids; do
# Get container details
container_info=$(docker inspect $container_id)
# Get volumes from container info
volumes=$(echo $container_info | jq -r '.[].Mounts | .[] | select(.Type == "volume") | .Name')
# Backup each volume
for volume in $volumes; do
destination=$(docker inspect $container_id| jq -r --arg volume "$volume" '.[].Mounts[] | select(.Type == "volume" and .Name == $volume) | .Destination')
BACKUP_FILE="${volume}_$(date +%Y%m%d%H%M).tar"
docker run --rm --volumes-from $container_id -v "$(pwd)/$BACKUP_DIR":/backup ubuntu tar cvf /backup/"$BACKUP_FILE" "$destination"
echo "Volume $volume from container $container_id backed up as $BACKUP_FILE"
done
done
}
# Check if docker-compose is present
if ! command -v docker-compose &> /dev/null
then
echo "docker-compose could not be found, please install it and run this script again."
exit 1
fi
# Check if jq is present
if ! command -v jq &> /dev/null
then
echo "jq could not be found, please install it and run this script again."
exit 1
fi
# Start the backup process
backup_volumes
#!/bin/bash
# WIP
function restore_volumes() {
# 列出所有備份目錄
echo "Available backup directories:"
dirs=(backup-*)
if [ ${#dirs[@]} -eq 0 ]; then
echo "No backup directories found"
exit 1
fi
# 顯示選項
for i in "${!dirs[@]}"; do
echo "$((i+1))) ${dirs[$i]}"
done
# 讓使用者選擇
read -p "Select backup directory (1-${#dirs[@]}): " choice
BACKUP_DIR="${dirs[$((choice-1))]}"
if [ ! -d "$BACKUP_DIR" ]; then
echo "Invalid selection"
exit 1
}
echo "Using backup directory: $BACKUP_DIR"
# 處理每個備份文件
for backup_file in "$BACKUP_DIR"/*.tar; do
# 從文件名提取 volume 名稱
volume_name=$(basename "$backup_file" | cut -d'_' -f1)
echo "Restoring volume: $volume_name"
# 創建新的 docker volume
docker volume create "$volume_name"
# 使用臨時容器還原數據
docker run --rm -v "$volume_name":/restore \
-v "$(pwd)/$backup_file":/backup.tar \
ubuntu bash -c "cd /restore && tar xvf /backup.tar --strip-components=1"
echo "Volume $volume_name restored"
done
echo "Restore completed"
}
# 檢查 docker compose 是否存在
if ! docker compose version &> /dev/null
then
echo "docker compose could not be found, please install it and run this script again."
exit 1
fi
# 檢查 jq 是否存在
if ! command -v jq &> /dev/null
then
echo "jq could not be found, please install it and run this script again."
exit 1
fi
# 開始還原程序
restore_volumes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment