From ad40aa40b66667fb71166a316d54d4c631159507 Mon Sep 17 00:00:00 2001 From: Fuhu Xia Date: Fri, 14 Jul 2023 11:49:06 -0400 Subject: [PATCH] update db backup and restore --- docs/backup_restore/README.md | 24 +++++++++++++++ ...nventory-db-backup.sh => app-db-backup.sh} | 13 ++++----- ...entory-db-restore.sh => app-db-restore.sh} | 13 ++++----- docs/backup_restore/catalog-db-backup.sh | 29 ------------------- 4 files changed, 35 insertions(+), 44 deletions(-) create mode 100644 docs/backup_restore/README.md rename docs/backup_restore/{inventory-db-backup.sh => app-db-backup.sh} (50%) rename docs/backup_restore/{inventory-db-restore.sh => app-db-restore.sh} (56%) delete mode 100755 docs/backup_restore/catalog-db-backup.sh diff --git a/docs/backup_restore/README.md b/docs/backup_restore/README.md new file mode 100644 index 0000000..ff4157b --- /dev/null +++ b/docs/backup_restore/README.md @@ -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 +``` \ No newline at end of file diff --git a/docs/backup_restore/inventory-db-backup.sh b/docs/backup_restore/app-db-backup.sh similarity index 50% rename from docs/backup_restore/inventory-db-backup.sh rename to docs/backup_restore/app-db-backup.sh index 95957bb..780f8ff 100755 --- a/docs/backup_restore/inventory-db-backup.sh +++ b/docs/backup_restore/app-db-backup.sh @@ -5,22 +5,21 @@ 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 + echo "$1 .*SUCCEEDED" + 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" diff --git a/docs/backup_restore/inventory-db-restore.sh b/docs/backup_restore/app-db-restore.sh similarity index 56% rename from docs/backup_restore/inventory-db-restore.sh rename to docs/backup_restore/app-db-restore.sh index de055f5..ccba090 100755 --- a/docs/backup_restore/inventory-db-restore.sh +++ b/docs/backup_restore/app-db-restore.sh @@ -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 @@ -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" diff --git a/docs/backup_restore/catalog-db-backup.sh b/docs/backup_restore/catalog-db-backup.sh deleted file mode 100755 index 5d95877..0000000 --- a/docs/backup_restore/catalog-db-backup.sh +++ /dev/null @@ -1,29 +0,0 @@ -# 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 -# 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 "Space name> " space_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 - -# Go to the correct space -cf target -s $space_name - -backup_id=$$ -backup_path="catalog-db-$(date +%Y%m%d-%H%M%S)-$space_name.gz" - -cf run-task backup-manager --name "catalog-db-backup-$backup_id" --command "backup psql catalog-db $backup_path" - -wait_for "catalog-db-backup-$backup_id"