#!/bin/bash export PATH=/bin:/usr/bin:/usr/local/bin TIMESTAMP=`date +"%Y%d%m-%H%M"` DB_BACKUP_PATH='/backup/dbbackup' MYSQL_HOST='localhost' MYSQL_PORT='3306' MYSQL_USER='root' MYSQL_PASSWORD='mysecret' DATABASE_NAME='mydb' echo "Backup started for database - ${DATABASE_NAME}" mysqldump -h ${MYSQL_HOST} \ -P ${MYSQL_PORT} \ -u ${MYSQL_USER} \ -p${MYSQL_PASSWORD} \ ${DATABASE_NAME} | gzip --best > ${DB_BACKUP_PATH}/${DATABASE_NAME}-${TIMESTAMP}.sql.gz if [ $? -eq 0 ]; then echo "Database backup successfully completed" else echo "Error found during backup" exit 1 fi
After creating the script make sure to set execute permission to run properly.
chmod +x /<path_to>/backupscript.sh
Schedule Script in Crontab
Now schedule the script in crontab to run every quarter of an hour . Edit crontab on your system with crontab -e
command.
0,15,30,45 * * * * root /backup/mysql-backup.sh
Save your crontab file.