Auto-detects databases, has the temp file and backup file locations separated, purple colored real-time output, only logs important output, md5sums into a text file for databases, every filename uniquely hashed with month, day, year, hour, minute, and second, timestamps everything, uses bzip2 compression for smallest file sizes.
#!/bin/bash
# vars – no trailing slash please
THEDATE=$(date +%m%d%y%H%M%S)
BACKUPDIR=’/backups/mysql’
TEMPDIR=’/backups/mysql/temp’
LOGFILE=”$TEMPDIR/info-$THEDATE.txt”
USER=root
PASSWORD=password
date > $LOGFILEdate
echo -e “\e[1;35m[ " $(date +%H:%M:%S) "-- Starting MySQL Backup Script v0.3 by Seanp2k ]\e[00m"
# magic
DATABASES=$(mysql --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)echo -e "\e[1;35m[ " $(date +%H:%M:%S) "-- Found" $(echo $DATABASES | wc -w) "databases:" $DATABASES "]\e[00m"
# loop -- dump and hash
for DB in $DATABASES; doecho -en "\e[1;35m[ " $(date +%H:%M:%S)
echo -n " -- Processing " $DB ".."
mysqldump --user=$USER --password=$PASSWORD --databases $DB > $TEMPDIR/$DB-$THEDATE.sql
echo -n "..md5sum..."
md5sum $TEMPDIR/$DB-$THEDATE.sql >> $LOGFILE
echo -e "Done ]\e[00m"
done# compress and clean
echo -e "\e[1;35m[ " $(date +%H:%M:%S) "-- Creating Archive " $BACKUPDIR/mysql-THEDATE.tar.bz2 "]\e[00m"
time tar -cvvjf $BACKUPDIR/mysql-$THEDATE.tar.bz2 $TEMPDIR/*.sql $TEMPDIR/info-$THEDATE.txt
rm -rf $TEMPDIR/*.sql $TEMPDIR/info-$THEDATE.txt
date
echo -e "\e[1;35m[ " $(date +%H:%M:%S) "-- Finished ]\e[00m”