Skip to content

Commit

Permalink
update db backup and restore
Browse files Browse the repository at this point in the history
  • Loading branch information
FuhuXia committed Jul 14, 2023
1 parent ba0202e commit 1fd8c56
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 44 deletions.
24 changes: 24 additions & 0 deletions docs/backup_restore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Backup an application database
Run like this example:
```
$ sh app-db-backup.sh
App name (inventory|catalog)> catalog
```

It will create a db dump file such as `catalog-db-20230714-110218-development.gz`

## Restore a database backup
`catalog-db-restore.sh` is multi-purposed script and includes a lot of steps for specific catalog use case. Use `app-db-restore.sh` if you just want to restore a DB backup.

First you need to create a new database, such as
```
cf create-service aws-rds medium-gp-psql catalog-new-db -c "{\"storage\": 250, \"version\": \"12\"}"
```
Use the existing DB for correct db plan and storage size

then run this script, providing the backup file and new DB name:
```
$ sh app-db-restore.sh
S3 Backup path> catalog-db-20230714-110218-development.gz
New Service name> catalog-new-db
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@ set -o pipefail
set -o nounset

# Get input params
# Service name: the name of the service that is hosting the S3 Backup
# Backup path: the path in S3 that is the backup location
# Storage size (in GB): minimum 250 for catalog
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
sleep 5
sleep 30
done
}

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

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

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

wait_for "catalog-db-backup-$backup_id"
wait_for "$app_name-db-backup-$backup_id"
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ set -o errexit
set -o pipefail
set -o nounset

# Get input params
# Service name: the name of the service that is hosting the S3 Backup
# Backup path: the path in S3 that is the backup location
# Storage size (in GB): minimum 250 for catalog
read -p "S3 Backup path> " backup_path
read -p "New Service name> " service_name
space_name=$(cf t | grep "space" | cut -d ':' -f 2 | awk '{$1=$1};1')
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
Expand All @@ -19,13 +16,13 @@ function wait_for () {
}

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

# # Restore backup
restore_id=$$
cf run-task backup-manager --name "inventory-db-restore-$restore_id" --command "PG_RESTORE_OPTIONS='--no-acl' restore psql $service_name $backup_path"
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 "catalog-db-restore-$restore_id"
wait_for "db-restore-$restore_id"

29 changes: 0 additions & 29 deletions docs/backup_restore/catalog-db-backup.sh

This file was deleted.

0 comments on commit 1fd8c56

Please sign in to comment.