From 1fd8c561292cba8c1d5569b85d4aba203c0c913a Mon Sep 17 00:00:00 2001 From: Fuhu Xia Date: Fri, 14 Jul 2023 13:42:22 -0400 Subject: [PATCH 1/3] update db backup and restore --- docs/backup_restore/README.md | 24 +++++++++++++++ ...nventory-db-backup.sh => app-db-backup.sh} | 12 ++++---- ...entory-db-restore.sh => app-db-restore.sh} | 13 ++++----- docs/backup_restore/catalog-db-backup.sh | 29 ------------------- 4 files changed, 34 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..a210900 100755 --- a/docs/backup_restore/inventory-db-backup.sh +++ b/docs/backup_restore/app-db-backup.sh @@ -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" 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" From 230e5c088b81c7c9af3e778aee6dd9c0439779ba Mon Sep 17 00:00:00 2001 From: Reid Hewitt Date: Tue, 24 Sep 2024 13:47:32 -0600 Subject: [PATCH 2/3] add service plan arg; remove index processing; scale backup-manager app --- docs/backup_restore/catalog-db-restore.sh | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/docs/backup_restore/catalog-db-restore.sh b/docs/backup_restore/catalog-db-restore.sh index 9a10d89..10b84e4 100755 --- a/docs/backup_restore/catalog-db-restore.sh +++ b/docs/backup_restore/catalog-db-restore.sh @@ -11,6 +11,7 @@ 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 function wait_for () { while ! (cf tasks backup-manager | grep -q "$1 .*SUCCEEDED"); do @@ -23,16 +24,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\": \"15\"}" --wait cf bind-service backup-manager catalog-db-new cf restart backup-manager +cf scale backup-manager -i 1 + # # 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" @@ -56,12 +53,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 @@ -85,8 +76,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 \ No newline at end of file +cf delete-service catalog-db-venerable +cf scale backup-manager -i 0 \ No newline at end of file From 5dced73c0fb9d15aec5a17929b7ca0b8a50b4aa4 Mon Sep 17 00:00:00 2001 From: Reid Hewitt Date: Fri, 27 Sep 2024 13:18:05 -0500 Subject: [PATCH 3/3] add db version param --- docs/backup_restore/catalog-db-restore.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/backup_restore/catalog-db-restore.sh b/docs/backup_restore/catalog-db-restore.sh index 10b84e4..7619506 100755 --- a/docs/backup_restore/catalog-db-restore.sh +++ b/docs/backup_restore/catalog-db-restore.sh @@ -12,6 +12,7 @@ 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 @@ -24,7 +25,7 @@ cf set-env backup-manager DATASTORE_S3_SERVICE_NAME backup-manager-s3 # Go to the correct space cf target -s $space_name -cf create-service aws-rds ${db_plan} catalog-db-new -c "{\"storage\": ${storage_size}, \"version\": \"15\"}" --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