Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #129 from cisagov/AL-db-backup
Browse files Browse the repository at this point in the history
Postgres backup and restore scripts
  • Loading branch information
cduhn17 committed Jun 29, 2022
2 parents 7b1fb83 + abc3756 commit 1620c63
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ Options:
"warning", "error", and "critical". [default: info]
```

## Database backup/restore ##

Follow the instructions below to backup the P&E database instance and restore locally.

In the P&E database environment:

- Pull the latest repository
- If necessary, edit ./src/pe_reports/pe_db/pg_backup.sh and replace the
default output path ($PWD) with your preferred output path.
- Open terminal and run:
`bash ./src/pe_reports/pe_db/pg_backup.sh`
- Export resulting .zip file

In your local environment:

- Pull the latest repository
- If necessary, edit ./src/pe_reports/pe_db/pg_restore.sh and replace
the default path to the backup files ($PWD) with your preferred path.
- Start local postgres
- Open terminal and run:
`bash ./src/pe_reports/pe_db/pg_restore.sh`

## Collect P&E Source Data ##

- Add database and data source credentials to src/pe_reports/data/config.ini
Expand Down
35 changes: 35 additions & 0 deletions src/pe_reports/pe_db/pg_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail

# Set path to save backup files
path=$PWD
backup_folder=$path/backups_$(date +%m-%d-%Y)

globalsqlfile=$backup_folder/pedb_globals.sql
sqlfile=$backup_folder/pedb_dump.sql
errfile=$backup_folder/stderr.txt
zipfile=$path/pedb_dump_$(date +%m-%d-%Y).zip

# Create backup folder
mkdir -p "$backup_folder"

# Create globals backup
if pg_dumpall --globals-only --no-role-passwords --database "$PE_DB_NAME" --port "$PE_DB_PORT" --username "$PE_DB_USER" --host "$DATABASE_HOST" --no-password 2> "$errfile" > "$globalsqlfile"; then
echo 'Globals dump created'
else
echo 'Globals pg_dump return non-zero code'
exit
fi

# Create backup
if pg_dump --dbname "$PE_DB_NAME" --port "$PE_DB_PORT" --username "$PE_DB_USER" --host "$DATABASE_HOST" --format custom --no-password 2> "$errfile" > "$sqlfile"; then
echo 'PG dump created'
else
echo 'pg_dump return non-zero code'
exit
fi

# Zip folder
zip --recurse-paths "$zipfile" "$backup_folder"
21 changes: 21 additions & 0 deletions src/pe_reports/pe_db/pg_restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail

# Set path to pg dump and globals backup files. ex: /Users/user/Desktop/backups
path=$PWD

dropdb pe ---host localhost --username postgres --if-exists

globalsql=$path/pedb_globals.sql
dumpsql=$path/pedb_dump.sql

psql --username postgres --host localhost --command "CREATE DATABASE pe;"

# rdsadmin database (even if empty) is required for the next script
psql --username postgres --host localhost --command "CREATE DATABASE rdsadmin;"

psql --username postgres --host localhost pe < "$globalsql"

pg_restore --username postgres --host localhost --dbname pe "$dumpsql"

0 comments on commit 1620c63

Please sign in to comment.