Created
August 3, 2020 17:30
-
-
Save AshishDhamalaAD/bb1ae110b1ecdd173aad756c3e871bfa to your computer and use it in GitHub Desktop.
Script to backup database and automatically delete after certain days
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 | |
export PATH=/bin:/usr/bin:/usr/local/bin | |
TODAY=`date +"%Y_%m_%d__%H_%M_%S"` | |
DB_BACKUP_PATH='/your/database/backup/folder' | |
MYSQL_HOST='localhost' | |
MYSQL_PORT='3306' | |
MYSQL_USER='db_user_name' | |
MYSQL_PASSWORD='db_user_password' | |
DATABASE_NAME='db_name' | |
BACKUP_RETAIN_DAYS=1 ## Number of days to keep local backup copy | |
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}/${DATABASE_NAME}-${TODAY}.sql.gz | |
if [ $? -eq 0 ]; then | |
echo "Database backup successfully completed" | |
else | |
echo "Error found during backup" | |
exit 1 | |
fi | |
##### Remove backups older than {BACKUP_RETAIN_DAYS} days ##### | |
find ${DB_BACKUP_PATH} -type f -mtime +${BACKUP_RETAIN_DAYS} -exec rm -f {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment