Monday, June 21, 2010

Script to restore DB from backup

Here is a small snippet for speeding up the process of restoration of DB from backup gunzip files

db.sh

#!/bin/bash
FILE="$1"

MYSQL=$(which mysql)
MYSQLAD=$(which mysqladmin)
GZIP=$(which gzip)
MUSER="username"
MPASS="password"
MDB="database"

echo "Droping database..."
$MYSQLAD -u $MUSER -p$MPASS drop $MDB

echo "Recreating database..."
$MYSQLAD -u $MUSER -p$MPASS create $MDB

echo "Uncompressing data..."
$GZIP -d $FILE".gz"

echo "Inserting data..."
$MYSQL -u $MUSER -p$MPASS $MDB < $FILE



USAGE:
:~/ chmod u+x  db.sh
:~/ ./db.sh mysql-timestamp.sql (Remove the .gz extension while providing as a parameter)

I will add help and more parameterized functionality in due time.