Simple Mysql Backup Script can be used with Crontab

By | May 12, 2013

This script backs up all databases on the server it’s running on. Usually ran as a cron job.

Hope someone else can use this as well.

# Backup ALL mysql databases
# by crouse -
x=`mysql --user=root --password=REMOVED -B -e "show databases"  | wc -l `; y=$((x-2));
for i in `mysql --user=root --password=REMOVED -B -e "show databases"  | tail -${y}`; do
mysqldump --opt --user=root --password=REMOVED $i | gzip -9 > /root/MYSQLBACKUPS/$i.`date --iso-8601`-mysqlbackup.gz
ls -t /root/MYSQLBACKUPS/$i.* | awk 'NR>7' | xargs rm -f
# Mail updates
touch sendthis
echo "" > sendthis
echo "Nightly Mysql Backup Information" >> sendthis
echo "---------------------------------------------" >> sendthis
echo " " >> sendthis
ls -la /root/MYSQLBACKUPS/ >> sendthis
cat sendthis | mail -s mail -s "Nightly Mysql Backup Notice"
rm sendthis