Skip to content

Commit

Permalink
Add psql-db-aurora-migration-job, updating dbbackup.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoaugustine committed Jul 10, 2024
1 parent e22797d commit e24c7da
Showing 1 changed file with 77 additions and 15 deletions.
92 changes: 77 additions & 15 deletions gen3/bin/dbbackup.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
#!/bin/bash

####################################################################################################
# Script: dbdump.sh
# Script: dbbackup.sh
#
# Description:
# This script facilitates the management of database backups within the gen3 environment. It is
# equipped to establish policies, service accounts, roles, and S3 buckets. Depending on the
# command provided, it will either initiate a database dump or perform a restore.
# equipped to establish policies, service accounts, roles, and S3 buckets. Depending on the
# command provided, it will either initiate a database dump, perform a restore, migrate to Aurora,
# or copy to Aurora.
#
# Usage:
# gen3 dbbackup [dump|restore]
# gen3 dbbackup [dump|restore|va-dump|create-sa|migrate-to-aurora|copy-to-aurora]
#
# dump - Initiates a database dump, creating the essential AWS resources if they are absent.
# The dump operation is intended to be executed from the namespace/commons that requires
# the backup.
# restore - Initiates a database restore, creating the essential AWS resources if they are absent.
# The restore operation is meant to be executed in the target namespace, where the backup
# needs to be restored.
# dump - Initiates a database dump, creating the essential AWS resources if they are absent.
# The dump operation is intended to be executed from the namespace/commons that requires
# the backup.
# restore - Initiates a database restore, creating the essential AWS resources if they are absent.
# The restore operation is meant to be executed in the target namespace, where the backup
# needs to be restored.
# va-dump - Runs a va-testing DB dump.
# create-sa - Creates the necessary service account and roles for DB copy.
# migrate-to-aurora - Triggers a service account creation and a job to migrate a Gen3 commons to an AWS RDS Aurora instance.
# copy-to-aurora - Triggers a service account creation and a job to copy the databases Indexd, Sheepdog & Metadata to new databases within an RDS Aurora cluster.
#
# Notes:
# This script extensively utilizes the AWS CLI and the gen3 CLI. Proper functioning demands a
# configured gen3 environment and the availability of the necessary CLI tools.
#
####################################################################################################

Expand Down Expand Up @@ -178,7 +180,55 @@ va_testing_db_dump() {
}


# main function to determine whether dump or restore
# Function to create the psql-db-copy service account and roles
create_db_copy_service_account() {
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: psql-db-copy-sa
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: psql-db-copy-role
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: psql-db-copy-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: psql-db-copy-role
subjects:
- kind: ServiceAccount
name: psql-db-copy-sa
namespace: ${namespace}
EOF
}

# Function to run the Aurora migration job
migrate_to_aurora() {
create_db_copy_service_account
sleep 30
gen3 job run psql-db-aurora-migration
}

# Function to run the Aurora copy job
copy_to_aurora() {
create_db_copy_service_account
sleep 30
gen3 job run psql-db-copy-aurora
}

# main function to determine whether dump, restore, or create service account
main() {
case "$1" in
dump)
Expand All @@ -202,8 +252,20 @@ main() {
create_s3_bucket
va_testing_db_dump
;;
create-sa)
gen3_log_info "Creating service account for DB copy..."
create_db_copy_service_account
;;
migrate-to-aurora)
gen3_log_info "Migrating Gen3 commons to Aurora..."
migrate_to_aurora
;;
copy-to-aurora)
gen3_log_info "Copying databases within Aurora..."
copy_to_aurora
;;
*)
echo "Invalid command. Usage: gen3 dbbackup [dump|restore|va-dump]"
echo "Invalid command. Usage: gen3 dbbackup [dump|restore|va-dump|create-sa|migrate-to-aurora|copy-to-aurora]"
return 1
;;
esac
Expand Down

0 comments on commit e24c7da

Please sign in to comment.