UnixServerAdmin

Server Administration & Management

How to Backup MySQL database using mysqldump

#!/bin/bash
####################################################
####   MySQL Backup Script , Created By UnixServerAdmin    ####
####################################################

rm -rf /backup/error_mysqldump.txt 2> /dev/null
rm -rf /backup/error_Mysqlzip.txt 2> /dev/null
rm -rf /backup/dblist.txt 2> /dev/null

## MySQL Databse Backup ##

cd /backup/

mysql -u ‘root’ -p -e ‘show databases’ > dblisttemp.txt
sed ‘1d’ dblisttemp.txt > dblist.txt
rm -rf /backup/dblisttemp.txt 2> /dev/null

mkdir /backup/Mysql/`date +%d.%m.%Y`

for database in $(cat /backup/dblist.txt)
do
mysqldump -u ‘root’ -p’pass’ $database > /backup/MySQL/$database.sql 2>> /backup/error_mysqldump.txt
done

cd MySQL/
dumpsize=`du -csh *.sql| tail -1 | cut -f1`

for database in $(cat /backup/dblist.txt)
do
zip -r `date +%d.%m.%Y`/$database.zip $database.sql 2>> /backup/error_Mysqlzip.txt
rm -rf $database.sql
done

March 4, 2011 Posted by | MySQL, Shell Script | , , | Leave a comment