Skip to content

Commit

Permalink
Backup and Restore a Rosalution DB to and from LTS (#169)
Browse files Browse the repository at this point in the history
* Updated the scripts to use the expected cgdslts remote source for rclone for the s3 bucket

* Update etc/database/restore-database-lts.sh

Co-authored-by: James Scherer <[email protected]>
Signed-off-by: Angelina Uno-Antonison <[email protected]>

---------

Signed-off-by: Angelina Uno-Antonison <[email protected]>
Co-authored-by: James Scherer <[email protected]>
  • Loading branch information
SeriousHorncat and JmScherer authored Apr 19, 2024
1 parent 9e20c29 commit 7e2892d
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ media/

# SSL/TLS Certificates
**/.certificates/**
*.pem
*.pem

etc/.rosalution-db-backups
62 changes: 62 additions & 0 deletions etc/database/backup-database-lts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#! /bin/bash
# ./backup-database-lts.sh <docker-container>
usage() {
echo "usage: $0 <mongodb-archive>"
echo " Backups a local MongoDB dump archive into UAB Long Term Storage remote"
echo " source 'cgdslts'."
echo " "
echo " Visit https://docs.rc.uab.edu/data_management/transfer/rclone/#setting-up-an-s3-lts-remote"
echo " for setup instructions for interacting with UAB LTS using rclone. Use 'cgdslts'"
echo " instead of 'uablts' for the remote configured in the setup instructions."
}

fail=false

if ! rclone --version &> /dev/null
then
echo "Error: rclone could not be found."
fail=true
fi

if [[ $# -ne 1 ]]
then
echo "Error: required input missing."
fail=true
fi

if $fail
then
echo "Exiting script ..."
usage
exit 1
fi

TARGET_LOCAL_SOURCE_DATABASE_DUMP_FILEPATH=$1
TARGET_S3_REMOTE="cgdslts"

if [ ! -f "$TARGET_LOCAL_SOURCE_DATABASE_DUMP_FILEPATH" ]
then
echo "Error: $TARGET_LOCAL_SOURCE_DATABASE_DUMP_FILEPATH does not exist"
usage
exit 1
fi

if ! rclone listremotes | grep -q "$TARGET_S3_REMOTE"
then
echo "Missing expected '$TARGET_S3_REMOTE' rclone remote; aborting operation"
usage
exit 1
fi

if ! rclone lsf "$TARGET_S3_REMOTE:" | grep -q rosalution
then
echo "Missing expected root path 'rosalution/' in bucket"
usage
exit 1
fi


destination_backup_path="$TARGET_S3_REMOTE:rosalution/db-backup/"
rclone copy -P "$TARGET_LOCAL_SOURCE_DATABASE_DUMP_FILEPATH" "$destination_backup_path"

echo "Backup operation complete..."
85 changes: 85 additions & 0 deletions etc/database/restore-database-lts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#! /bin/bash
# ./restore-database-lts.sh <docker-container> clean
usage() {
echo "usage: $0 <docker-container> clean(optional)"
echo " Restores an archived 'rosalution_db' MongoDB Database for Rosalution, into the specified"
echo " MongoDB docker container from the CGDS UAB Long Term Storage remote source 'cgdslts'."
echo " "
echo " Visit https://docs.rc.uab.edu/data_management/transfer/rclone/#setting-up-an-s3-lts-remote"
echo " for setup instructions for interacting with UAB LTS using rclone. Use 'cgdslts'"
echo " instead of 'uablts' for the remote configured in the setup instructions."
}

OPTIONAL_CLEAN=false

fail=false

if ! rclone --version &> /dev/null
then
echo "Error: rclone could not be found."
fail=true
fi

if [[ $# -lt 1 ]]
then
echo "Error: required input missing."
fail=true
fi

if [[ $# -ge 2 && "$2" == "clean" ]]
then
OPTIONAL_CLEAN=true
fi

if $fail
then
echo "Exiting script ..."
usage
exit 1
fi

DOCKER_CONTAINER=$1
TARGET_S3_REMOTE="cgdslts"

if ! rclone listremotes | grep -q "$TARGET_S3_REMOTE"
then
echo "Missing expected '$TARGET_S3_REMOTE' rclone remote; aborting operation"
usage
exit 1
fi

if ! rclone lsf "$TARGET_S3_REMOTE:" | grep -q rosalution
then
echo "Missing expected root path 'rosalution/' in bucket"
exit 1
fi

echo "Available Rosalution Backups"
echo "------------------------------------------------------"
rclone lsf cgdslts:rosalution/db-backup --files-only | sort
echo "------------------------------------------------------"

echo "Which backup would you like to restore?"
read -r rosalution_db_backup

backup_absolute_path="$TARGET_S3_REMOTE:rosalution/db-backup/$rosalution_db_backup"
local_destination_directory=".rosalution-db-backups"
local_relative_backup_path="$local_destination_directory/$rosalution_db_backup"
if ! rclone lsf "$backup_absolute_path" | grep -q "$rosalution_db_backup"
then
echo "Rosalution Backup '$backup_absolute_path' does not exist in CGDS' UAB LTS."
fi

mkdir -p .rosalution-db-backups

rclone copy -P "$backup_absolute_path" "$local_destination_directory"

./restore-database.sh "$DOCKER_CONTAINER" "$local_relative_backup_path"

if [ "$OPTIONAL_CLEAN" = true ]
then
echo "Removing local backup copy..."
rm "$local_relative_backup_path"
fi

echo "Restore operation complete..."

0 comments on commit 7e2892d

Please sign in to comment.