elixir-devcontainer/.devcontainer/snap_db.sh
2025-07-18 23:12:35 +00:00

33 lines
920 B
Bash
Executable file

#!/bin/bash
# Ensure the .snapshots directory exists
mkdir -p "$PROJECT_ROOT/.snapshots"
# Generate the base filename with timestamp
base_filename="$PROJECT_ROOT/.snapshots/snapshot_$(date +%Y%m%d_%H%M%S)"
# Initialize an empty string for sanitized arguments
sanitized_args=""
# Loop through all arguments
for arg in "$@"; do
# Sanitize the argument: remove spaces and special characters
sanitized=$(echo "$arg" | tr -cd '[:alnum:]_-')
sanitized_args="${sanitized_args}_${sanitized}"
done
# Remove leading underscore if present
sanitized_args=${sanitized_args#_}
# Create the final filename
if [ -n "$sanitized_args" ]; then
filename="${base_filename}_${sanitized_args}.sql"
else
filename="${base_filename}.sql"
fi
# Run pg_dump and save to the generated filename
pg_dump --no-tablespaces ${PGDATABASE} -f "$filename"
# Print the filename that was written
echo "Snapshot saved to: $filename"