Skip to content

Instantly share code, notes, and snippets.

@virullius
Created August 27, 2014 21:29
Show Gist options
  • Save virullius/39d0ad81ca5c09c504fc to your computer and use it in GitHub Desktop.
Save virullius/39d0ad81ca5c09c504fc to your computer and use it in GitHub Desktop.
Copy and rename MySQL database
#!/usr/bin/env bash
# transfer mysql database to different server with a new name
# such as production database to test database
read -p "source host:" SRC_HOST
read -p "user for host $SRC_HOST:" SRC_USER
read -p "password for $SRC_USER@$SRC_HOST:" -s SRC_PW; echo
read -p "source database name:" SRC_DB
read -p "target host:" TGT_HOST
read -p "user for host $TGT_HOST:" TGT_USER
read -p "password for $TGT_USER@$TGT_HOST:" -s TGT_PW; echo
read -p "target database name:" TGT_DB
read -p "Transfer database $SRC_DB from host $SRC_HOST as user $SRC_USER to database $TGT_DB on host $TGT_HOST as user $TGT_USER? Enter y to continue:" DO_IT
if [ "x$DO_IT" = "xy" ]
then
echo "working..."
mysql --host=$TGT_HOST --user=$TGT_USER --password=$TGT_PW --execute="create database $TGT_DB;"
mysqldump --host=$SRC_HOST --user=$SRC_USER --password=$SRC_PW $SRC_DB | mysql --host=$TGT_HOST --user=$TGT_USER --password=$TGT_PW $TGT_DB
echo "finished"
else
echo "canceled"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment