Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Last active April 3, 2020 08:33
Show Gist options
  • Save jasdeepkhalsa/e025e11e21373ed41dcd6c88c1350e7c to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/e025e11e21373ed41dcd6c88c1350e7c to your computer and use it in GitHub Desktop.
MySQL A->B - Pipe a MySQL database from a source server (A) into a target MySQL database on another server (B)
#!/usr/bin/env bash
# This script will pipe a MySQL database from a source server (A)
# into a target MySQL database on another server (B)
# From StackExchange.com (https://dba.stackexchange.com/a/180) - Authored by David Hall - Under the license: cc by-sa 4.0 with attribution required (https://creativecommons.org/licenses/by-sa/4.0/)
# Slight modifications may have been made to the original script, which are in no way endorsed by the original author(s) of this script
# Specific alterations include:
# - Adding both a source & target hostname & database
# - Updating the command-line options in line with the latest versions of the `mysqldump` & `mysql` command-line tools
# - Making the input parameters configurable and required before the script begins
# This script is for my personal use only, by using it you do so at your own risk
if [[ "$1" ]]; then
SOURCE_USER="$1"
else
echo "Please enter a source username"
exit 1
fi
if [[ "$2" ]]; then
SOURCE_PASSWORD="$2"
else
echo "Please enter a source password"
exit 1
fi
if [[ "$3" ]]; then
SOURCE_HOST="$3"
else
echo "Please enter a source hostname"
exit 1
fi
if [[ "$4" ]]; then
SOURCE_DB="$4"
else
echo "Please enter a source database name"
exit 1
fi
if [[ "$5" ]]; then
TARGET_USER="$5"
else
echo "Please enter a target username"
exit 1
fi
if [[ "$6" ]]; then
TARGET_PASSWORD="$6"
else
echo "Please enter a target password"
exit 1
fi
if [[ "$7" ]]; then
TARGET_HOST="$7"
else
echo "Please enter a target hostname"
exit 1
fi
if [[ "$8" ]]; then
TARGET_DB="$8"
else
echo "Please enter a target database name"
exit 1
fi
mysqldump -u $SOURCE_USER -p$SOURCE_PASSWORD -h $SOURCE_HOST $SOURCE_DB | mysql -u $TARGET_USER -p$TARGET_PASSWORD -h $TARGET_HOST -D $TARGET_DB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment