Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update db backup and restore #26

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

24 changes: 7 additions & 17 deletions docs/backup_restore/catalog-db-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ set -o nounset
read -p "Space name> " space_name
read -p "S3 Backup path> " backup_path
read -p "Storage size for new db> " storage_size
read -p "Service plan> " db_plan
read -p "DB Version> " db_version

function wait_for () {
while ! (cf tasks backup-manager | grep -q "$1 .*SUCCEEDED"); do
Expand All @@ -23,16 +25,12 @@ cf set-env backup-manager DATASTORE_S3_SERVICE_NAME backup-manager-s3
# Go to the correct space
cf target -s $space_name

# # create temp Database
if [[ "${space_name}" == 'prod' ]]; then
db_plan=large-gp-psql-redundant
else
db_plan=large-gp-psql
fi
cf create-service aws-rds ${db_plan} catalog-db-new -c "{\"storage\": ${storage_size}, \"version\": \"12\"}" --wait
cf create-service aws-rds ${db_plan} catalog-db-new -c "{\"storage\": ${storage_size}, \"version\": \"${db_version}\"}" --wait
cf bind-service backup-manager catalog-db-new
cf restart backup-manager

cf scale backup-manager -i 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was getting an ssh error with the app when 0 instances are up so i scale up and down


# # Restore backup
restore_id=$$
cf run-task backup-manager --name "catalog-db-restore-$restore_id" --command "PG_RESTORE_OPTIONS='--no-acl' restore psql catalog-db-new $backup_path"
Expand All @@ -56,12 +54,6 @@ fi
cf rename-service catalog-db catalog-db-venerable
cf rename-service catalog-db-new catalog-db

# clear solr indexes
clear_id=$$
cf run-task catalog-admin --name "clear-solr-index-$clear_id" -c "ckan search-index clear"

wait_for "clear-solr-index-$clear_id"

# bind to new database
cf unbind-service catalog-admin catalog-db-venerable
cf bind-service catalog-admin catalog-db
Expand All @@ -85,8 +77,6 @@ cf unbind-service catalog-fetch catalog-db-venerable
cf bind-service catalog-fetch catalog-db
cf restart catalog-fetch

# reindex solr
cf run-task catalog-admin -c "ckan search-index rebuild -i -o" --name search-index-rebuild -k 2G -m 2G

# cleanup
cf delete-service catalog-db-venerable
cf delete-service catalog-db-venerable
cf scale backup-manager -i 0
Loading