volumes
  1. #!/bin/bash
  2.  
  3. VOLUMES=("postgres-iam-data" "postgres-sxs-data")
  4.  
  5. FORCE=false
  6. while getopts "f" opt; do
  7. case $opt in
  8. f)
  9. FORCE=true
  10. ;;
  11. \?)
  12. echo "Invalid option: -$OPTARG" >&2
  13. exit 1
  14. ;;
  15. esac
  16. done
  17.  
  18. for VOLUME in "${VOLUMES[@]}"; do
  19. if docker volume inspect "$VOLUME" &>/dev/null; then
  20. if [ "$FORCE" = true ]; then
  21. echo "Volume $VOLUME exists. Force flag detected. Removing and recreating..."
  22. docker volume rm "$VOLUME"
  23. docker volume create "$VOLUME"
  24. echo "Volume $VOLUME recreated successfully."
  25. else
  26. echo "Volume $VOLUME already exists. Skipping..."
  27. fi
  28. else
  29. echo "Volume $VOLUME does not exist. Creating..."
  30. docker volume create "$VOLUME"
  31. echo "Volume $VOLUME created successfully."
  32. fi
  33. done
  34.  
  35. echo "Operation completed."
Pasted 2026-10-07 18:43:14

Short link: