diff --git a/devops/backup.sh b/devops/backup.sh index a805fc7e29..371ebd7fa0 100644 --- a/devops/backup.sh +++ b/devops/backup.sh @@ -7,7 +7,7 @@ HELM_RELEASE_NAME='fi' FYIPE_DB_USERNAME='fyipe' FYIPE_DB_PASSWORD='password' -FYIPE_DB_NAME='fyipe' +FYIPE_DB_NAME='fyipedb' CURRENT_DATE=$(date +%s) CURRENT_USER=$(whoami) BACKUP_PATH="/Users/$CURRENT_USER/Documents" @@ -20,12 +20,12 @@ function HELP (){ echo "" echo "all arguments are optional and have a default value when not set" echo "" - echo " -c Database username. Default value 'fyipe'." - echo " -l Backup path on local system where backup file will be stored. Default value - Users/$CURRENT_USER/Documents" - echo " -n Database name. Default value 'fyipe'" + echo " -l Backup path on local system where backup file will be stored. Default value - $BACKUP_PATH" + echo " -n Database name. Default value 'fyipedb'" echo " -p Database password. Default value 'password'" echo " -r Helm release name. Default value 'fi'" echo " -t Backup retain days. Set the number of days backup is kept before it is deleted. Default value '14'" + echo " -u Set database username. Default value 'fyipe'." echo "" echo " -h Help." echo "" @@ -33,8 +33,8 @@ function HELP (){ } -# PASS IN PROPS -while getopts ":r:u:p:n:c:l:t:h" opt; do +# PASS IN ARGUMENTS +while getopts ":r:u:p:n:l:t:h" opt; do case $opt in r) HELM_RELEASE_NAME="$OPTARG" ;; @@ -44,8 +44,6 @@ while getopts ":r:u:p:n:c:l:t:h" opt; do ;; n) FYIPE_DB_NAME="$OPTARG" ;; - c) CURRENT_USER="$OPTARG" - ;; l) BACKUP_PATH="$OPTARG" ;; t) BACKUP_RETAIN_DAYS="$OPTARG" @@ -60,40 +58,101 @@ while getopts ":r:u:p:n:c:l:t:h" opt; do esac done +function BACKUP_SUCCESS(){ + curl -X POST -H 'Content-type: application/json' --data '{ + "blocks": [ + { + "type": "divider" + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "*Backup complete*\n Date:'${TODAY}'\nPath: '$BACKUP_PATH'/fyipe-backup-'$CURRENT_DATE'.archive" + }, + "accessory": { + "type": "image", + "image_url":"https://www.fonedog.com/images/backup-restore/ios/what-does-restore-from-backup-mean.jpg", + "alt_text": "alt text for image" + } + }, + { + "type": "divider" + } + ] + }' https://hooks.slack.com/services/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 +} + +function BACKUP_FAIL_SERVER(){ + curl -X POST -H 'Content-type: application/json' --data '{ + "blocks": [ + { + "type": "divider" + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "*Backup Failed*\n Date:'${TODAY}'\nReason: Could not create backup on container.\nPath: '$BACKUP_PATH'/fyipe-backup-'$CURRENT_DATE'.archive" + }, + "accessory": { + "type": "image", + "image_url":"https://www.fonedog.com/images/backup-restore/ios/what-does-restore-from-backup-mean.jpg", + "alt_text": "alt text for image" + } + }, + { + "type": "divider" + } + ] + }' https://hooks.slack.com/services/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 +} + +function BACKUP_FAIL_LOCAL(){ + curl -X POST -H 'Content-type: application/json' --data '{ + "blocks": [ + { + "type": "divider" + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "*Backup failed*\n Date:'${TODAY}'\nReason: Could not create backup on local path.\nPath: '$BACKUP_PATH'/fyipe-backup-'$CURRENT_DATE'.archive" + }, + "accessory": { + "type": "image", + "image_url":"https://www.fonedog.com/images/backup-restore/ios/what-does-restore-from-backup-mean.jpg", + "alt_text": "alt text for image" + } + }, + { + "type": "divider" + } + ] + }' https://hooks.slack.com/services/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 +} + + echo "Taking a backup on the server" -sudo kubectl exec fi-mongodb-primary-0 -- mongodump --uri="mongodb://$FYIPE_DB_USERNAME:$FYIPE_DB_PASSWORD@localhost:27017/$FYIPE_DB_NAME" --archive="/bitnami/mongodb/fyipedata.archive" echo "" +if sudo kubectl exec fyipe-766b74d759-ncbg7 -- mongodump --uri="mongodb://$FYIPE_DB_USERNAME:$FYIPE_DB_PASSWORD@localhost:27017/$FYIPE_DB_NAME" --archive="/tmp/fyipedata.archive"; then + echo "Copying backup from server to local computer. This will take some time...." + echo "" + if sudo kubectl cp fyipe-766b74d759-ncbg7:tmp/fyipedata.archive "$BACKUP_PATH/fyipe-backup-$CURRENT_DATE.archive"; then + echo "File Saved: $BACKUP_PATH/fyipe-backup-$CURRENT_DATE.archive" + echo "" + BACKUP_SUCCESS + + else + echo "Failure, exit status: $?" + BACKUP_FAIL_LOCAL + fi +else + echo "Failure, exit status: $" + BACKUP_FAIL_SERVER +fi -echo "Copying backup from server to local computer. This will take some time...." -sudo kubectl cp fi-mongodb-primary-0:/bitnami/mongodb/fyipedata.archive "$BACKUP_PATH/fyipe-backup-$CURRENT_DATE.archive" - - -echo "File Saved: $BACKUP_PATH/fyipe-backup-$CURRENT_DATE.archive" -echo "" - - # Send Backup succss message to slack - curl -X POST -H 'Content-type: application/json' --data '{ - "blocks": [ - { - "type": "divider" - }, - { - "type": "section", - "text": { - "type": "mrkdwn", - "text": "*Backup complete*\n Date:'${TODAY}'\nFolder Name: '${CURRENT_DATE}'" - }, - "accessory": { - "type": "image", - "image_url":"https://www.fonedog.com/images/backup-restore/ios/what-does-restore-from-backup-mean.jpg", - "alt_text": "alt text for image" - } - }, - { - "type": "divider" - } - ] -}' https://hooks.slack.com/services/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 ####### Remove backups older than {BACKUP_RETAIN_DAYS} days ######## @@ -105,29 +164,29 @@ if [ ! -z ${BACKUP_PATH} ]; then for backupFile in `ls $BACKUP_PATH`; do if [ $backupFile -lt ${DBDELDATE} ]; then rm -rf $backupFile - Send delete message - curl -X POST -H 'Content-type: application/json' --data '{ - "blocks": [ - { - "type": "divider" - }, - { - "type": "section", - "text": { - "type": "mrkdwn", - "text": "*Backup Deleted*\nFolder Name: '$backupFile'" - }, - "accessory": { - "type": "image", - "image_url":"https://icon-library.com/images/delete-icon/delete-icon-13.jpg", - "alt_text": "alt text for image" - } - }, - { - "type": "divider" - } - ] - }' https://hooks.slack.com/services/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 + # Send delete message + # curl -X POST -H 'Content-type: application/json' --data '{ + # "blocks": [ + # { + # "type": "divider" + # }, + # { + # "type": "section", + # "text": { + # "type": "mrkdwn", + # "text": "*Backup Deleted*\nPath: '$backupFile'" + # }, + # "accessory": { + # "type": "image", + # "image_url":"https://icon-library.com/images/delete-icon/delete-icon-13.jpg", + # "alt_text": "alt text for image" + # } + # }, + # { + # "type": "divider" + # } + # ] + # }' https://hooks.slack.com/services/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 fi done fi diff --git a/devops/restore.sh b/devops/restore.sh index cb6fa2ce9a..b4295a3817 100644 --- a/devops/restore.sh +++ b/devops/restore.sh @@ -13,13 +13,156 @@ FYIPE_DB_PASSWORD='password' FYIPE_DB_NAME='fyipedb' CURRENT_DATE=$(date +%s) CURRENT_USER=$(whoami) -FILE_NAME="fyipe-backup-1604889147.archive" -FILE_PATH="/Users/$CURRENT_USER/Documents/$FILE_NAME" +FILE_NAME="fyipe-backup-1613551425" +FILE_PATH="/mnt/c/Users/ok/Documents/backup" +TODAY=`date +"%d%b%Y"` + +function HELP (){ + echo "" + echo "Fyipe 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" + echo " -n Database name. Default value 'fyipe'" + echo " -p Database password. Default value 'password'" + echo " -r Helm release name. Default value 'fi'" + echo " -u Set database username. Default value 'fyipe'." + echo "" + echo " -h Help." + echo "" + exit 1 +} + +# PASS IN ARGUMENTS +while getopts ":r:u:p:n:l:f:h" opt; do + case $opt in + r) HELM_RELEASE_NAME="$OPTARG" + ;; + u) FYIPE_DB_USERNAME="$OPTARG" + ;; + p) FYIPE_DB_PASSWORD="$OPTARG" + ;; + n) FYIPE_DB_NAME="$OPTARG" + ;; + l) FILE_PATH="$OPTARG" + ;; + f) FILE_NAME="$OPTARG" + ;; + h) HELP + ;; + \?) echo "Invalid option -$OPTARG" >&2 + HELP + echo -e "Use -h to see the help documentation." + exit 2 + ;; + esac +done + +function RESTORE_SUCCESS (){ + echo " Send Backup success message to slack" + + curl -X POST -H 'Content-type: application/json' --data '{ + "blocks": [ + { + "type": "divider" + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "*Restore complete*\n Date:'${TODAY}'\nFile Name: '${FILE_NAME}'" + }, + "accessory": { + "type": "image", + "image_url":"https://www.fonedog.com/images/backup-restore/ios/what-does-restore-from-backup-mean.jpg", + "alt_text": "alt text for image" + } + }, + { + "type": "divider" + } + ] +}' https://hooks.slack.com/services/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 + exit 1 +} + +function RESTORE_FAIL_SERVER (){ + echo "Send failure message to slack" + + curl -X POST -H 'Content-type: application/json' --data '{ + "blocks": [ + { + "type": "divider" + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "*Restore Failed*\n Date:'${TODAY}'\nReason: Could not restore database.\nFile Name: '${FILE_NAME}'" + }, + "accessory": { + "type": "image", + "image_url":"https://www.fonedog.com/images/backup-restore/ios/what-does-restore-from-backup-mean.jpg", + "alt_text": "alt text for image" + } + }, + { + "type": "divider" + } + ] +}' https://hooks.slack.com/services/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 + exit 1 +} + + +function RESTORE_FAIL_LOCAL (){ + echo "Send failure message to slack" + + curl -X POST -H 'Content-type: application/json' --data '{ + "blocks": [ + { + "type": "divider" + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "*Restore Failed*\n Date:'${TODAY}'\nReason: Could not copy backup to container.\nFile Name: '${FILE_NAME}'" + }, + "accessory": { + "type": "image", + "image_url":"https://www.fonedog.com/images/backup-restore/ios/what-does-restore-from-backup-mean.jpg", + "alt_text": "alt text for image" + } + }, + { + "type": "divider" + } + ] +}' https://hooks.slack.com/services/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 + exit 1 +} + echo "Copying backup from local to server. This will take some time...." -sudo kubectl cp "$FILE_PATH" fi-mongodb-primary-0:/bitnami/mongodb/fyipedata.archive +echo "" +if sudo kubectl cp "$FILE_PATH/$FILE_NAME" fyipe-766b74d759-ncbg7:/tmp/fyipedata.archive; then + echo "Restoring a backup on the server." + echo "" + if kubectl exec fyipe-766b74d759-ncbg7 -- mongorestore --uri="mongodb://$FYIPE_DB_USERNAME:$FYIPE_DB_PASSWORD@localhost:27017/$FYIPE_DB_NAME" --archive="/tmp/fyipedata.archive"; then + echo "Restore success" + RESTORE_SUCCESS + else + echo "Restore Failed, exit status: $?" + RESTORE_FAIL_SERVER + fi +else + echo "Restore Failed, exit status: $?" + RESTORE_FAIL_LOCAL +fi -echo "Restoring a backup on the server." -sudo kubectl exec fi-mongodb-primary-0 -- mongorestore --uri="mongodb://$FYIPE_DB_USERNAME:$FYIPE_DB_PASSWORD@localhost:27017/$FYIPE_DB_NAME" --archive="/bitnami/mongodb/fyipedata.archive" -echo "Restore Complete" \ No newline at end of file + + \ No newline at end of file