Skip to content

Commit

Permalink
update db backup restore
Browse files Browse the repository at this point in the history
  • Loading branch information
FuhuXia committed Jul 14, 2023
1 parent b598780 commit 49a6490
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/backup_restore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
To backup an application database run:
$ sh app-db-backup.sh
App name (inventory|catalog)> catalog


It will create catalog-db-20230714-110218-development.gz


catalog-db-restore.sh

To restore a database backup to a new DB, first you need to get the NEW DB set up

cf create-service aws-rds medium-gp-psql catalog-new-db -c "{\"storage\": 250, \"version\": \"12\"}"
refer to existing DB for correct plan and storage size

then run:
$ sh app-db-restore.sh
S3 Backup path> catalog-db-20230714-110218-development.gz
New Service name> catalog-new-db
25 changes: 25 additions & 0 deletions docs/backup_restore/app-db-backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# backup database to a dump zip file in S3 bucket cg-2bd85037-4eb6-450a-87f4-460d932a6c40

set -o errexit
set -o pipefail
set -o nounset

# Get input params
space_name=$(cf t | grep "space" | cut -d ':' -f 2 | awk '{$1=$1};1')
read -rp "App name (inventory|catalog)> " app_name

function wait_for () {
while ! (cf tasks backup-manager | grep -q "$1 .*SUCCEEDED"); do
echo "$1 .*SUCCEEDED"
sleep 30
done
}

cf set-env backup-manager DATASTORE_S3_SERVICE_NAME backup-manager-s3

backup_id=$$
backup_path="$app_name-db-$(date +%Y%m%d-%H%M%S)-$space_name.gz"

cf run-task backup-manager --name "$app_name-db-backup-$backup_id" --command "backup psql $app_name-db $backup_path"

wait_for "$app_name-db-backup-$backup_id"
28 changes: 28 additions & 0 deletions docs/backup_restore/app-db-restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# restore catalog-db from a dump zip file in S3 bucket cg-2bd85037-4eb6-450a-87f4-460d932a6c40

set -o errexit
set -o pipefail
set -o nounset

# Service name: the name of the service that is hosting the S3 Backup
# Backup path: the path in S3 that is the backup location
read -rp "S3 Backup path> " backup_path
read -rp "New Service name> " service_name

function wait_for () {
while ! (cf tasks backup-manager | grep -q "$1 .*SUCCEEDED"); do
sleep 5
done
}

cf set-env backup-manager DATASTORE_S3_SERVICE_NAME backup-manager-s3
cf bind-service backup-manager "$service_name"
cf restart backup-manager

# # Restore backup
restore_id=$$
cf run-task backup-manager --name "db-restore-$restore_id" --command "PG_RESTORE_OPTIONS='--no-acl' restore psql $service_name $backup_path"

# # This job may return "FAILED", and may not return successfully
wait_for "db-restore-$restore_id"

0 comments on commit 49a6490

Please sign in to comment.