Skip to content

Commit

Permalink
Script to dump db by schema
Browse files Browse the repository at this point in the history
  • Loading branch information
aprilrieger committed Jul 18, 2024
1 parent a4aa654 commit bf422d9
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions dump_schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# run this against the old database you are dumping from
# after you execute shell in db run this script

DB_NAME="pals-production-hyku" # Replace with your database name
DB_USER="postgres" # Replace with your database username
DUMP_DIR="/tmp/postgres" # Replace with your desired directory to store dump files

# Create dump directory if it doesn't exist
mkdir -p $DUMP_DIR

# Get the list of schemas excluding system schemas
SCHEMAS=$(psql -U $DB_USER -d $DB_NAME -t -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('pg_catalog', 'information_schema', 'pg_toast', 'public');")

# Loop through each schema and dump it
for SCHEMA in $SCHEMAS; do
# Remove leading/trailing whitespace from schema name
SCHEMA=$(echo $SCHEMA | xargs)

# Output message
echo "Dumping schema: $SCHEMA"

# Dump the schema
pg_dump -U $DB_USER -d $DB_NAME -n $SCHEMA -F c -f "$DUMP_DIR/${SCHEMA}.dump"

# Check if the dump was successful
if [ $? -eq 0 ]; then
echo "Successfully dumped schema: $SCHEMA"
else
echo "Failed to dump schema: $SCHEMA" >&2
fi
done

0 comments on commit bf422d9

Please sign in to comment.