Skip to content

Instantly share code, notes, and snippets.

@nicholasrq
Last active August 29, 2015 14:06
Show Gist options
  • Save nicholasrq/026e0f0c3c7fadd837f5 to your computer and use it in GitHub Desktop.
Save nicholasrq/026e0f0c3c7fadd837f5 to your computer and use it in GitHub Desktop.
simple wrapper over the mysqldump command
#!/bin/sh
# you also can pass an argument which
# will be the name of a table in remote DB
# mysql_load users
mysql_load(){
if ! type "pv" > /dev/null
then
echo "You should install Pipe View first"
else
user="your_user_here"
pass="your_password_here"
host="your_remote_host_here"
local_db="local_database_name" # database name where we import data
remote_db="remote_database_name" # database name from where we take dump
tmp_path="/tmp/mysqldump.sql" # you can replace this location with whatever you want
import="mysql -uroot $local_db"
md_options="--single-transaction --verbose -C"
base_command="mysqldump -h$host -u$user -p$pass $remote_db"
if [ "$1" ]
then
if [ "$2" ]
then
echo "Dump remote DB from $host [$remote_db] => $local_db [table: $1, id: $2]"
cmd="$base_command --tables $1 --where='id=$2' --opt='OFF' $md_options"
else
echo "Dump remote DB from $host [$remote_db] => $local_db [table: $1]"
cmd="$base_command --tables $1 $md_options"
fi
else
echo "Dump remote DB from $host [$remote_db] => $local_db [table: all]"
cmd="$base_command $md_options"
fi
echo "Downloading..."
if eval "$cmd > $tmp_path"
then
if [ ! -f "$tmp_path" ]
then
echo "Couldn't find file $tmp_path"
else
echo "Importing..."
eval "pv $tmp_path | mysql -uroot $local_db"
echo "Cleanup"
eval "rm $tmp_path"
echo "Done"
fi
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment