Update backup_named_volumes.sh

This commit is contained in:
2025-07-18 12:25:41 +00:00
parent 55fdf6aee6
commit 47764ac1e7

View File

@@ -1,11 +1,5 @@
#!/bin/bash #!/bin/bash
# Auto-elevate if not root
if [[ "$EUID" -ne 0 ]]; then
echo "This script needs root to access Docker volumes. Re-running with sudo..."
exec sudo "$0" "$@"
fi
set -euo pipefail set -euo pipefail
WORKDIR="$(pwd)" WORKDIR="$(pwd)"
@@ -16,11 +10,6 @@ if [[ ! -f "$COMPOSE_FILE" ]]; then
exit 1 exit 1
fi fi
# Original user info (for restoring file ownership)
ORIGINAL_USER="${SUDO_USER:-$USER}"
ORIGINAL_UID=$(id -u "$ORIGINAL_USER")
ORIGINAL_GID=$(id -g "$ORIGINAL_USER")
# Determine the project name (same as docker-compose uses) # Determine the project name (same as docker-compose uses)
PROJECT_NAME=$(basename "$WORKDIR") PROJECT_NAME=$(basename "$WORKDIR")
@@ -65,11 +54,7 @@ for SERVICE in $SERVICE_NAMES; do
echo "📦 Backing up $VOLUME_PATH to $OUTPUT_FILE" echo "📦 Backing up $VOLUME_PATH to $OUTPUT_FILE"
# Create tar as original user tar -czf "$OUTPUT_FILE" -C "$VOLUME_PATH" .
sudo -u "$ORIGINAL_USER" tar -czf "$OUTPUT_FILE" -C "$VOLUME_PATH" .
# Ensure file is owned properly
chown "$ORIGINAL_UID:$ORIGINAL_GID" "$OUTPUT_FILE"
echo "✅ Backup complete: $OUTPUT_FILE" echo "✅ Backup complete: $OUTPUT_FILE"
else else