Bash – mysql backup script

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.

#!/bin/bash
# Backup ALL mysql databases
# by crouse - http://archlinux.me/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
done
# Mail updates
touch sendthis
echo "" > sendthis
echo "Archlinux.me Nightly Mysql Backup Information" >> sendthis
echo "---------------------------------------------" >> sendthis
echo " " >> sendthis
ls -la /root/MYSQLBACKUPS/ >> sendthis
cat sendthis | mail -s mail SOMEEMAIL@archlinux.us -s "Archlinux.me - Nightly Mysql Backup Notice"
rm sendthis
exit

Comments are closed.