oneuptime/Devops/backup/restore.sh

154 lines
3.5 KiB
Bash
Raw Normal View History

2020-11-09 01:43:21 +00:00
###
#
# Please make sure kubectl is installed nad context is pointed to the cluster you want to resotre to.
#
# RUN THIS BY:
2021-04-21 21:24:05 +00:00
# bash restore.sh -f <FILENAME>.archive
2020-11-09 01:43:21 +00:00
#
###
2020-11-09 02:41:06 +00:00
# Variables, please check these before you run the script.
2020-11-09 02:41:06 +00:00
2021-12-05 16:27:00 +00:00
MONGO_SERVER_HOST='a59a474aad89940889c1eb69b1a8f884-135820180.us-east-2.elb.amazonaws.com'
MONGO_SERVER_PORT="27017"
2021-04-21 21:24:05 +00:00
2022-01-28 22:34:06 +00:00
ONEUPTIME_DB_USERNAME='oneuptime'
ONEUPTIME_DB_PASSWORD='password'
2022-01-28 22:34:06 +00:00
ONEUPTIME_DB_NAME='oneuptimedb'
2020-11-09 02:41:06 +00:00
CURRENT_DATE=$(date +%s)
CURRENT_USER=$(whoami)
2021-12-03 15:45:14 +00:00
FILE_NAME="oneuptime-backup-1638534940.archive"
FILE_PATH=~/db-backup
TODAY=$(date +"%d-%b-%Y")
ENVIRONMENT='Staging'
2021-02-17 10:12:58 +00:00
function HELP() {
echo ""
echo "OneUptime DB restore command line documentation."
echo ""
echo "all arguments are optional and have a default value when not set"
echo ""
echo " -f Name of file to be restored"
echo " -l File path on local system where file will be restored from. Default value - $FILE_PATH"
2022-01-28 22:34:06 +00:00
echo " -n Database name. Default value 'oneuptimedb'"
echo " -p Database password. Default value 'password'"
2022-01-28 22:34:06 +00:00
echo " -u Set database username. Default value 'oneuptime'."
2021-12-03 15:45:14 +00:00
echo " -v Set database environment. Enums {Production, Staging}, defaults to 'Staging'."
echo ""
echo " -h Help."
echo ""
exit 1
2021-02-17 10:12:58 +00:00
}
# PASS IN ARGUMENTS
2021-12-03 15:45:14 +00:00
while getopts "u:p:n:l:f:v:h" opt; do
case $opt in
u)
ONEUPTIME_DB_USERNAME="$OPTARG"
;;
p)
ONEUPTIME_DB_PASSWORD="$OPTARG"
;;
n)
ONEUPTIME_DB_NAME="$OPTARG"
;;
l)
FILE_PATH="$OPTARG"
;;
f)
FILE_NAME="$OPTARG"
;;
2021-12-03 15:45:14 +00:00
v)
ENVIRONMENT="$OPTARG"
;;
h)
HELP
;;
\?)
echo "Invalid option -$OPTARG" >&2
HELP
echo -e "Use -h to see the help documentation."
exit 2
;;
esac
2021-02-17 10:12:58 +00:00
done
function RESTORE_SUCCESS() {
echo " Send Backup success message to slack"
curl -X POST -H 'Content-type: application/json' --data '{
2021-02-17 10:12:58 +00:00
"blocks": [
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
2021-12-03 15:45:14 +00:00
"text": "*'$ENVIRONMENT' Restore Complete*\n Date: '$TODAY'\nFile Name: '${FILE_NAME}'"
2021-02-17 10:12:58 +00:00
}
},
{
"type": "divider"
}
]
2021-12-02 18:38:07 +00:00
}' https://hooks.slack.com/services/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
exit 1
2021-02-17 10:12:58 +00:00
}
function RESTORE_FAIL_SERVER() {
echo "Send failure message to slack"
curl -X POST -H 'Content-type: application/json' --data '{
2021-02-17 10:12:58 +00:00
"blocks": [
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
2021-12-03 15:45:14 +00:00
"text": "*'$ENVIRONMENT' Restore Failed*\n Date: '$TODAY'\nReason: Could not restore database.\nFile Name: '${FILE_NAME}'"
2021-02-17 10:12:58 +00:00
}
},
{
"type": "divider"
}
]
2021-12-02 18:38:07 +00:00
}' https://hooks.slack.com/services/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
exit 1
2021-02-17 10:12:58 +00:00
}
function RESTORE_FAIL_LOCAL() {
echo "Send failure message to slack"
2021-02-17 10:12:58 +00:00
curl -X POST -H 'Content-type: application/json' --data '{
2021-02-17 10:12:58 +00:00
"blocks": [
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
2021-12-03 15:45:14 +00:00
"text": "*'$ENVIRONMENT' Restore Failed*\n Date: '$TODAY'\nReason: Could not copy backup to container.\nFile Name: '${FILE_NAME}'"
2021-02-17 10:12:58 +00:00
}
},
{
"type": "divider"
}
]
2021-12-02 18:38:07 +00:00
}' https://hooks.slack.com/services/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
exit 1
2021-02-17 10:12:58 +00:00
}
2021-04-21 21:24:05 +00:00
echo "Restoring Database. This will take some time...."
2021-02-17 10:12:58 +00:00
echo ""
2021-12-03 15:45:14 +00:00
if mongorestore --authenticationDatabase="${ONEUPTIME_DB_NAME}" --host="${MONGO_SERVER_HOST}" --port="${MONGO_SERVER_PORT}" --username="${ONEUPTIME_DB_USERNAME}" --password="${ONEUPTIME_DB_PASSWORD}" --archive="$FILE_PATH/$FILE_NAME"; then
2021-04-21 21:24:05 +00:00
echo "Restore success"
RESTORE_SUCCESS
2021-02-17 10:12:58 +00:00
else
2021-04-21 21:24:05 +00:00
echo "Restore Failed, exit status: $?"
RESTORE_FAIL_SERVER
2021-02-17 10:12:58 +00:00
fi