Created
May 27, 2021 01:58
-
-
Save culy247/2c5d0558e6e10765b972b3aac06487f9 to your computer and use it in GitHub Desktop.
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/bash | |
| ################################################################ | |
| ## | |
| ## MySQL Database Backup Script | |
| ## | |
| ################################################################ | |
| export PATH=/bin:/usr/bin:/usr/local/bin | |
| TODAY=`date +"%d%b%Y"` | |
| ################################################################ | |
| ################## Update below values ######################## | |
| DB_BACKUP_PATH='/backup/dbbackup' | |
| MYSQL_HOST='localhost' | |
| MYSQL_PORT='3306' | |
| MYSQL_USER='root' | |
| MYSQL_PASSWORD='mysecret' | |
| DATABASE_NAME='mydb' | |
| BACKUP_RETAIN_DAYS=30 ## Number of days to keep local backup copy | |
| ################################################################# | |
| mkdir -p ${DB_BACKUP_PATH}/${TODAY} | |
| echo "Backup started for database - ${DATABASE_NAME}" | |
| mysqldump -h ${MYSQL_HOST} \ | |
| -P ${MYSQL_PORT} \ | |
| -u ${MYSQL_USER} \ | |
| -p${MYSQL_PASSWORD} \ | |
| ${DATABASE_NAME} | gzip > ${DB_BACKUP_PATH}/${TODAY}/${DATABASE_NAME}-${TODAY}.sql.gz | |
| if [ $? -eq 0 ]; then | |
| echo "Database backup successfully completed" | |
| else | |
| echo "Error found during backup" | |
| fi | |
| ##### Remove backups older than {BACKUP_RETAIN_DAYS} days ##### | |
| DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"` | |
| if [ ! -z ${DB_BACKUP_PATH} ]; then | |
| cd ${DB_BACKUP_PATH} | |
| if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then | |
| rm -rf ${DBDELDATE} | |
| fi | |
| fi | |
| ### End of script #### | |
| ## 0 2 * * * root /backup/mysql-backup.sh ## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment