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

Advertisements

March 4, 2011 - Posted by | MySQL, Shell Script | , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: