Guest User

Untitled

a guest
Sep 23rd, 2025
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. # PostgreSQL restore logic
  2. >&2 echo "Testing PostgreSQL connectivity..."
  3. local postgres_ready=false
  4.  
  5. for i in {1..10}; do
  6. if kubectl exec "$pod" -c "$restore_container" -n "$NAMESPACE" -- \
  7. timeout 10 pg_isready -U "$pg_user" -d "postgres" >/dev/null 2>&1; then
  8. postgres_ready=true
  9. break
  10. fi
  11. >&2 echo "PostgreSQL not ready yet, waiting... (attempt $i/10)"
  12. sleep 5
  13. done
  14.  
  15. if [[ "$postgres_ready" != "true" ]]; then
  16. >&2 echo "❌ PostgreSQL is not responding after multiple attempts"
  17. return 1
  18. fi
  19.  
  20. >&2 echo "PostgreSQL is ready! Proceeding with restore..."
  21. >&2 echo "Preparing database: $db_name"
  22.  
  23. # First, try to connect as the configured user to postgres database
  24. if kubectl exec "$pod" -c "$restore_container" -n "$NAMESPACE" -- \
  25. psql -U "$pg_user" -d "postgres" -c "SELECT 1;" >/dev/null 2>&1; then
  26. >&2 echo "✅ Successfully connected as user: $pg_user"
  27.  
  28. # Check if user has superuser privileges
  29. if kubectl exec "$pod" -c "$restore_container" -n "$NAMESPACE" -- \
  30. psql -U "$pg_user" -d "postgres" -t -c "SELECT rolsuper FROM pg_roles WHERE rolname='$pg_user';" | grep -q "t"; then
  31. >&2 echo "✅ User $pg_user has superuser privileges"
  32.  
  33. >&2 echo "Terminating connections to database: $db_name"
  34. kubectl exec "$pod" -c "$restore_container" -n "$NAMESPACE" -- \
  35. psql -U "$pg_user" -d "postgres" -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '$db_name';" 2>/dev/null || true
  36.  
  37. >&2 echo "Dropping database: $db_name"
  38. kubectl exec "$pod" -c "$restore_container" -n "$NAMESPACE" -- \
  39. psql -U "$pg_user" -d "postgres" -c "DROP DATABASE IF EXISTS \"$db_name\";" || {
  40. >&2 echo "❌ Failed to drop database $db_name"
  41. return 1
  42. }
  43.  
  44. >&2 echo "Creating database: $db_name using template0"
  45. kubectl exec "$pod" -c "$restore_container" -n "$NAMESPACE" -- \
  46. psql -U "$pg_user" -d "postgres" -c "CREATE DATABASE \"$db_name\" TEMPLATE template0;" || {
  47. >&2 echo "❌ Failed to create database $db_name"
  48. return 1
  49. }
  50. else
  51. >&2 echo "⚠ User $pg_user does not have superuser privileges, trying alternative approach"
  52. kubectl exec "$pod" -c "$restore_container" -n "$NAMESPACE" -- \
  53. psql -U "$pg_user" -d "postgres" -c "DROP DATABASE IF EXISTS \"$db_name\";" 2>/dev/null || true
  54. kubectl exec "$pod" -c "$restore_container" -n "$NAMESPACE" -- \
  55. psql -U "$pg_user" -d "postgres" -c "CREATE DATABASE \"$db_name\" TEMPLATE template0;" || {
  56. >&2 echo "❌ Failed to prepare PostgreSQL database $db_name"
  57. return 1
  58. }
  59. fi
  60. else
  61. >&2 echo "❌ Cannot connect as user: $pg_user"
  62. return 1
  63. fi
  64.  
  65. >&2 echo "Importing backup data..."
  66.  
  67. if gunzip -c "$backup_file" | \
  68. kubectl exec -i "$pod" -c "$restore_container" -n "$NAMESPACE" -- \
  69. psql -U "$pg_user" -d "$db_name"; then
  70. >&2 echo "✅ PostgreSQL restore completed successfully as user: $pg_user"
  71. else
  72. >&2 echo "❌ Failed to import PostgreSQL backup as user: $pg_user"
  73. return 1
  74. fi
  75.  
Advertisement
Add Comment
Please, Sign In to add comment