This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from cisagov/AL-db-backup
Postgres backup and restore scripts
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |