Skip to content

Instantly share code, notes, and snippets.

@EX3MP
Last active February 10, 2017 10:27
Show Gist options
  • Save EX3MP/d28afb8547d1bc9c63dadd6c1fea73ab to your computer and use it in GitHub Desktop.
Save EX3MP/d28afb8547d1bc9c63dadd6c1fea73ab to your computer and use it in GitHub Desktop.
Einfaches bash zum mysqldump zwischen 2 dbs (WIP! muss noch getestet werden!)
#!/bin/bash
# based on https://gist.github.com/Siteograf/09c1e1c5655105362e754aa6640e6ffd
# use with ./migradeDB.sh
# or like ./migradeDB.sh 1 y
#
# required mysql or mysqldump at HOST1 & HOST2!
# solltet ihr "bad pattern: --password=" bekommen, funktioniert euer passwort wegen eines Sonderzeichen nicht
HOST1_isREMOTE=false
HOST1_SSH_USER="root" # required if host 1 is remote
HOST1_SSH_IP="0.0.0.0" # required if host 1 is remote
HOST1_HOST="localhost"
HOST1_DB="nice_tv_shows"
HOST1_USER="rick"
HOST1_PASS="WubbaLubbaDubDub"
HOST2_isREMOTE=false
HOST2_SSH_USER="root" # required if host 2 is remote
HOST2_SSH_IP="0.0.0.0" # required if host 2 is remote
HOST2_HOST="127.0.0.1"
HOST2_DB="best_tv_shows"
HOST2_USER="morty"
HOST2_PASS="ÄääämRick"
#
# DO NOT EDIT!
#
printf "\e[32;1mSelect type of migrade \e[0m\n"
echo "1) ${HOST1_DB}@${HOST1_HOST} to ${HOST2_DB}@${HOST2_HOST}";
echo "2) ${HOST2_DB}@${HOST2_HOST} to ${HOST1_DB}@${HOST1_HOST}";
function buildFrom(){
result=""
if [[ $1 = true ]]
then
result="ssh $2@$3"
fi
result="$result mysqldump --host=\"$4\" --user=\"$6\" --password=\"$7\" $5"
}
function buildTo(){
result=""
if [[ $1 = true ]]
then
result="ssh $2@$3"
fi
result="$result mysql --host=\"$4\" --user=\"$6\" --password=\"$7\" $5"
}
QUERY1=""
QUERY2=""
if [[ "$1" ]]
then
response=$1
else
read -r -p "Are you sure? [1/2] " response
fi
if [[ "$response" = "1" ]]
then
printf "\e[32;1mFROM $HOST1_DB TO $HOST2_DB \e[0m\n"
buildFrom $HOST1_isREMOTE $HOST1_SSH_USER $HOST1_SSH_IP $HOST1_HOST $HOST1_DB $HOST1_USER $HOST1_PASS
QUERY1=$result
buildTo $HOST2_isREMOTE $HOST2_SSH_USER $HOST2_SSH_IP $HOST2_HOST $HOST2_DB $HOST2_USER $HOST2_PASS
QUERY2=$result
else
printf "\e[32;1mFROM $HOST2_DB TO $HOST1_DB \e[0m\n"
buildFrom $HOST2_isREMOTE $HOST2_SSH_USER $HOST2_SSH_IP $HOST2_HOST $HOST2_DB $HOST2_USER $HOST2_PASS
QUERY1=$result
buildTo $HOST1_isREMOTE $HOST1_SSH_USER $HOST1_SSH_IP $HOST1_HOST $HOST1_DB $HOST1_USER $HOST1_PASS
QUERY2=$result
fi
if [[ "$2" ]]
then
response=$2
else
read -r -p "Are you sure? [y/N] " response
fi
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
printf "\e[32;1mExport/Import running \e[0m\n"
eval "$QUERY1 | $QUERY2"
# mysqldump --host=$lclHost --user=$lclDBUser --password=$lclDBPass $lclDBName | ssh $RemoteSSHUser@$RemoteSSHIP mysql --user=$RemoteDBUser --password=$RemoteDBPass --host=$RemoteHost $RemoteDBName
printf "\e[32;1mFinished, have a nice day \e[0m\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment