Friday, September 23, 2011

Shell script to taking MySql Dump/Backup.

[root@17_johnson]# cat /usr/bin/mybackupsql

#!/bin/bash
# USER VARIABLES
TIMESTAMP=$(date +%Y-%m-%d)
MYSQLUSER=root
MYSQLPWD=im#secure
MYSQLHOST=localhost
# PATH VARIABLES
MK=/bin/mkdir
GREP=/bin/grep
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
# CREATE MYSQL BACKUP
# Create new backup dir
$MK /backup/mysqlback_$TIMESTAMP
#Dump new files
for i in $(echo 'SHOW DATABASES;' | $MYSQL -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST|$GREP -v '^Database$'); do
  $MYSQLDUMP -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST $i >/backup/mysqlback_$TIMESTAMP/$i.sql
sleep 30
echo "$i"
done

1 comments:

Renu said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
Devops Training in electronic city