#!/bin/bash
# Path to the tools this scripts needs
MYSQLDUMP="/usr/bin/mysqldump"
GZIP="/bin/gzip"
FIND="/usr/bin/find"
# Check to see if this script has access to all the commands it needs
for CMD in $MYSQLDUMP $GZIP $FIND; do
type $CMD &> /dev/null
if [ $? -ne 0 ]; then
echo
echo "ERROR: The script cannot find the required command \"${CMD}\"."
echo
exit 1
fi
done
# Enter SQL database login credentials
USER="root"
PASS="mypass"
# Specify which databases to backup
DB="--all-databases"
# Specify where the databases will be saved to
BKUP_PATH="/path/to/your/backup/folder"
# Format for the timestamp
timestamp="$(date +%Y-%m-%d_%a)"
# Run the backup and compress it
error=0
${MYSQLDUMP} -u${USER} -p${PASS} --events --single-transaction --quick ${DB} | ${GZIP} -9 > ${BKUP_PATH}/Nightly_dB_Backup_${timestamp}.sql.gz
if [ "$?" -ne 0 ]; then
echo
echo "WARNING: Mysqldump returned an error!"
echo
error=1
fi
# Now find & delete old backups we no longer need
${FIND} ${BKUP_PATH} -maxdepth 1 -type f -mtime +30 -delete
if [ "$?" -ne 0 ]; then
echo
echo "WARNING: Find returned an error!"
echo
error=1
fi
if [ "$error" -eq 0 ]; then
echo "Mysql backup script completed successfully!"
fi