Database wide search and replace script

Aug 21st, 2012 | By | Category: Linux / Freebsd

Just something I thought that I would post as I needed a quick solution to do a search and replace of an entire Mysql Database.  It’s actually dumping the DB, searching and replacing the dump file and then re-importing it.  But it does the trick

 

#!/bin/bash

if (($# >= 1)) ; then
if (($# >= 2)) ; then
if (($# >= 3)) ; then

Database=$1
searchTerm=$2
replaceTerm=$3

read -s -p “What’s the mysql root Password? ” mysqlPass

mysqldump -uroot -p${mysqlPass} $Database > $Database.sql
sed -i “s/$searchTerm/$replaceTerm/g” $Database.sql
mysql -uroot -p${mysqlPass} $Database < $Database.sql

else
echo “U forgot the replace term. Syntax mysqlSearchReplace.sh Database search Term replaceTerm”
fi
else
echo “No Search Term. Syntax mysqlSearchReplace.sh Database search Term replaceTerm”
fi
else
echo “Enter a database please. Syntax mysqlSearchReplace.sh Database search Term replaceTerm”
fi

 

Tags: , , , , ,

Leave a Comment