Skip to content

Commit

Permalink
[FEAT] add restore script to restore a database dump
Browse files Browse the repository at this point in the history
  • Loading branch information
dameyerdave committed Jan 10, 2025
1 parent 88d1991 commit 2fe6224
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ cython_debug/
.secret.yml
/data
/tmp
/dumps
/dumps/*
!/dumps/restore.sh
.DS_Store
/ena_upload_ng
/templates/**
Expand Down
50 changes: 50 additions & 0 deletions dumps/restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

# To dump the database use:
# pg_dump -U ena -Fc ena > /tmp/yyyy-mm-dd_ena.dump

function usage {
echo "USAGE: $(basename $0) dump-file"
exit 1
}

# Configuration
DB_USER="ena"
DB_NAME="ena"

if [ $# -lt 1 ]; then
usage
fi

# Disconnect all users
echo "Disconnecting all users from database '$DB_NAME'..."
psql -U "$DB_USER" -d postgres -c "
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '$DB_NAME'
AND pid <> pg_backend_pid();
"
if [ $? -ne 0 ]; then
echo "Failed to disconnect users."
exit 1
fi

# Drop the database
echo "Dropping database '$DB_NAME'..."
dropdb -U "$DB_USER" "$DB_NAME"
if [ $? -ne 0 ]; then
echo "Failed to drop the database."
exit 1
fi

# Recreate the database
echo "Recreating database '$DB_NAME'..."
createdb -U "$DB_USER" "$DB_NAME"
if [ $? -ne 0 ]; then
echo "Failed to recreate the database."
exit 1
fi

echo "Database '$DB_NAME' has been reset successfully."

pg_restore -U "$DB_USER" -d "$DB_NAME" "$1"

0 comments on commit 2fe6224

Please sign in to comment.