This commit is contained in:
Mauricio Siu 2026-09-11 13:11:54 -04:00 committed by GitHub
commit 773260a3ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -66,38 +66,98 @@ export const restoreVolume = async (
docker volume rm ${volumeName} --force
${baseRestoreCommand}
else
echo ""
echo "⚠️ WARNING: Cannot restore volume as it is currently in use!"
echo ""
echo "📋 The following containers are using volume '${volumeName}':"
echo ""
echo "$CONTAINERS_USING_VOLUME" | while IFS='|' read container_id container_name container_state labels; do
echo " 🐳 Container: $container_name ($container_id)"
echo " Status: $container_state"
# Determine container type
if echo "$labels" | grep -q "com.docker.swarm.service.name="; then
SERVICE_NAME=$(echo "$labels" | grep -o "com.docker.swarm.service.name=[^,]*" | cut -d'=' -f2)
echo " Type: Docker Swarm Service ($SERVICE_NAME)"
elif echo "$labels" | grep -q "com.docker.compose.project="; then
PROJECT_NAME=$(echo "$labels" | grep -o "com.docker.compose.project=[^,]*" | cut -d'=' -f2)
echo " Type: Docker Compose ($PROJECT_NAME)"
else
echo " Type: Regular Container"
fi
# Only "exited", "created" and "dead" are safe to remove. Anything else
# (running, restarting, paused, removing) is treated as still in use.
RUNNING_CONTAINERS=$(echo "$CONTAINERS_USING_VOLUME" | awk -F'|' '$3 != "exited" && $3 != "created" && $3 != "dead"')
STOPPED_CONTAINERS=$(echo "$CONTAINERS_USING_VOLUME" | awk -F'|' '$3 == "exited" || $3 == "created" || $3 == "dead"')
if [ -n "$RUNNING_CONTAINERS" ]; then
echo ""
done
echo "⚠️ WARNING: Cannot restore volume as it is currently in use!"
echo ""
echo "📋 The following containers are using volume '${volumeName}':"
echo ""
echo "$RUNNING_CONTAINERS" | while IFS='|' read container_id container_name container_state labels; do
echo " 🐳 Container: $container_name ($container_id)"
echo " Status: $container_state"
# Determine container type
if echo "$labels" | grep -q "com.docker.swarm.service.name="; then
SERVICE_NAME=$(echo "$labels" | grep -o "com.docker.swarm.service.name=[^,]*" | cut -d'=' -f2)
echo " Type: Docker Swarm Service ($SERVICE_NAME)"
elif echo "$labels" | grep -q "com.docker.compose.project="; then
PROJECT_NAME=$(echo "$labels" | grep -o "com.docker.compose.project=[^,]*" | cut -d'=' -f2)
echo " Type: Docker Compose ($PROJECT_NAME)"
else
echo " Type: Regular Container"
fi
echo ""
done
echo ""
echo "🔧 To restore this volume, please:"
echo " 1. Stop all containers/services using this volume"
echo " 2. Remove the existing volume: docker volume rm ${volumeName}"
echo " 3. Run the restore operation again"
echo ""
echo "❌ Volume restore aborted - volume is in use"
exit 1
fi
# Use a temp file instead of process substitution ("< <(...)") so this
# still works under a plain POSIX /bin/sh, not just bash.
STOPPED_CONTAINERS_FILE=$(mktemp)
printf '%s\\n' "$STOPPED_CONTAINERS" > "$STOPPED_CONTAINERS_FILE"
# Re-check each container's live state right before removing it: it may have
# started running again since the "docker ps -a" snapshot above.
ACTIVE_AGAIN=""
while IFS='|' read -r container_id container_name container_state labels; do
CURRENT_STATE=$(docker inspect -f '{{.State.Status}}' "$container_id" 2>/dev/null || echo "gone")
case "$CURRENT_STATE" in
running|restarting|paused|removing)
ACTIVE_AGAIN="$ACTIVE_AGAIN $container_name"
;;
esac
done < "$STOPPED_CONTAINERS_FILE"
if [ -n "$ACTIVE_AGAIN" ]; then
rm -f "$STOPPED_CONTAINERS_FILE"
echo ""
echo "⚠️ WARNING: Container(s)$ACTIVE_AGAIN became active again, aborting restore"
echo "❌ Volume restore aborted - volume is in use"
exit 1
fi
echo "Volume exists but is only referenced by stopped containers, removing them..."
echo ""
echo "🔧 To restore this volume, please:"
echo " 1. Stop all containers/services using this volume"
echo " 2. Remove the existing volume: docker volume rm ${volumeName}"
echo " 3. Run the restore operation again"
REMOVE_FAILED=""
while IFS='|' read -r container_id container_name container_state labels; do
echo " 🗑 Removing stopped container: $container_name ($container_id) [status: $container_state]"
if ! docker rm -f "$container_id" >/dev/null 2>&1; then
REMOVE_FAILED="$REMOVE_FAILED $container_name"
fi
done < "$STOPPED_CONTAINERS_FILE"
rm -f "$STOPPED_CONTAINERS_FILE"
if [ -n "$REMOVE_FAILED" ]; then
echo ""
echo "❌ Failed to remove container(s):$REMOVE_FAILED"
echo "❌ Volume restore aborted - could not free the volume"
exit 1
fi
echo ""
echo "❌ Volume restore aborted - volume is in use"
exit 1
echo "Removing existing volume and proceeding with restore"
if ! docker volume rm ${volumeName} --force; then
echo "❌ Volume restore aborted - failed to remove existing volume"
exit 1
fi
${baseRestoreCommand}
fi
fi
`;