Skip to content

Instantly share code, notes, and snippets.

@faceleg
Created May 13, 2013 21:32
Show Gist options
  • Save faceleg/5571703 to your computer and use it in GitHub Desktop.
Save faceleg/5571703 to your computer and use it in GitHub Desktop.
Export a database and change the name used in all tables, triggers and routines
#!/bin/bash
if [ $# != 4 ]; then
echo "Usage: $0 <database_name> <new_database_name> <mysql_username> <mysql_password>"
exit 1;
fi
DATABASE_NAME=$1
NEW_DATABASE_NAME=$2
USERNAME=$3
PASSWORD=$4
# Dump database to an sql file
mysqldump --no-data \
--triggers \
--routines \
-u$USERNAME \
-p$PASSWORD \
--skip-comments \
--databases $DATABASE_NAME > $NEW_DATABASE_NAME.sql
# Replace database name with new database name
sed -i s/$DATABASE_NAME/$NEW_DATABASE_NAME/g $NEW_DATABASE_NAME.sql
echo "mysql -u$USERNAME -p$PASSWORD < $NEW_DATABASE_NAME.sql"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment