Created
June 21, 2019 16:17
-
-
Save kadekjayak/0a7784f76c03c490d451253de014f35e to your computer and use it in GitHub Desktop.
Backup File & DB to AWS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| AWS_ACCESS_KEY_ID= | |
| AWS_SECRET_ACCESS_KEY= | |
| S3_BUCKET= | |
| SITE_NAME=kadekjayak.web.id | |
| WEB_ROOT=/var/www/html | |
| # Database Config | |
| MYSQL_USER=kadekjayak | |
| MYSQL_HOST=127.0.0.1 | |
| MYSQL_PORT=3306 | |
| MYSQL_PASS= | |
| MYSQL_DB= | |
| # Set Configuration | |
| aws configure set aws_access_key_id ${AWS_ACCESS_KEY_ID} | |
| aws configure set aws_secret_access_key ${AWS_SECRET_ACCESS_KEY} | |
| # Backup Database | |
| DB_FILE=db-$(date +"%d-%b-%Y").sql | |
| cd /tmp | |
| mysqldump \ | |
| --host ${MYSQL_HOST} \ | |
| --port ${MYSQL_PORT} \ | |
| -u ${MYSQL_USER} \ | |
| --password="${MYSQL_PASS}" \ | |
| ${MYSQL_DB} > ${DB_FILE} | |
| if [ "${?}" -eq 0 ]; then | |
| gzip -f ${DB_FILE} | |
| else | |
| echo ${SITE_NAME} Error backing up Database | |
| exit 255 | |
| fi | |
| # Kompress File | |
| OUTPUT_FILE=${SITE_NAME}-$(date +"%d-%b-%Y").tar.gz | |
| cd /tmp | |
| tar -zcf \ | |
| ${OUTPUT_FILE} \ | |
| ${WEB_ROOT} \ | |
| ${DB_FILE}.gz \ | |
| --ignore-failed-read | |
| if [ "${?}" -eq 0 ]; then | |
| aws s3 cp ${OUTPUT_FILE} s3://${S3_BUCKET}/${SITE_NAME}/ --storage-class STANDARD | |
| rm ${OUTPUT_FILE} | |
| rm ${DB_FILE}.gz | |
| echo ${SITE_NAME} WEB Backup Complete | |
| else | |
| echo ${SITE_NAME} Error backing up Web Data | |
| rm ${OUTPUT_FILE} | |
| rm ${DB_FILE}.gz | |
| exit 255 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment