Automatically Backup all MySQL Database

#!/bin/bash
# sonia 16-nov-05
# backup each mysql db into a different file, rather than one big file
# as with –all-databases – will make restores easier

OUTPUTDIR=”/var/lib/backup”
MYSQLDUMP=”/usr/bin/mysqldump”
MYSQL=”/usr/bin/mysql”

# clean up any old backups – save space
rm “$OUTPUTDIR/*bak” > /dev/null 2>&1

# get a list of databases
databases=`$MYSQL
-e “SHOW DATABASES;” | tr -d “| ” | grep -v Database`

# dump each database in turn
for db in $databases; do
echo $db
$MYSQLDUMP –force –opt –user=$USER –password=$PASSWORD
–databases $db > “$OUTPUTDIR/$db.bak”
done

#http://bashexperts.com

Leave a Reply

Your email address will not be published. Required fields are marked *