Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Created April 1, 2024 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmarreck/b8de3cac9218cc801484e33ab934d91e to your computer and use it in GitHub Desktop.
Save pmarreck/b8de3cac9218cc801484e33ab934d91e to your computer and use it in GitHub Desktop.
Dumping an entire postgres database
#!/usr/bin/env bash
datetimestamp() {
local datebin="date";
$datebin --resolution > /dev/null 2>&1 || datebin="gdate";
$datebin --resolution > /dev/null 2>&1 || datebin="date";
local format=${DATETIMESTAMPFORMAT:-'+%Y%m%d%H%M%S'};
case "$1" in
--date=* | -d=*)
$datebin --date="${1#*=}" "$format"
;;
--date | -d)
$datebin --date="$2" "$format"
;;
--help | -h)
echo "Usage: datetimestamp [--date|-d[=| ]'date']";
echo " --date|-d [date] date to use, defaults to now, see man date for format details";
echo " --help|-h show this help";
return 0
;;
-*)
echo "Unknown option: $1" 1>&2;
datetimestamp -h;
return 2
;;
*)
$datebin "$format"
;;
esac
}
pg_dump "$DATABASE_URL" -F c -Z 8 -b -v > ~/database_name-$(datetimestamp).sql
# -F c = format internally-compressed (custom structured binary output format for postgres that also includes additional features)
# -Z 8 = set zlib compression level (1-9)
# -b = use custom binary format that also compresses via zlib
# -v = be verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment