Skip to content

Instantly share code, notes, and snippets.

@oxyc
Created May 30, 2012 15:59
Show Gist options
  • Select an option

  • Save oxyc/2837210 to your computer and use it in GitHub Desktop.

Select an option

Save oxyc/2837210 to your computer and use it in GitHub Desktop.
#!/bin/bash
get_value() { drush core-status --pipe --show-passwords "$1"; }
e_arrow() { echo -e " \033[1;33m➜\033[0m $@"; }
host="$@"
read -d '' dump_cmd <<'EOF'
tmp_file="sql_transfer_db_dump.sql.tar.gz"
if [ ! -f $tmp_file ]; then
cd public_html 2>/dev/null || cd httpdoc 2>/dev/null
get_value() { drush core-status --pipe --show-passwords "$1" 2>/dev/null; }
mysqldump -u$(get_value database_username) \
-p$(get_value database_password) \
$(get_value database_name) \
| gzip --rsyncable > ../$tmp_file
fi
EOF
e_arrow "Dumping database on remote host..."
ssh $host "$dump_cmd"
e_arrow "Transfering dump to localhost..."
for ((i=1; i<4; i++)); do
rsync --partial --progress --timeout=30 --rsh=ssh --archive $host:sql_transfer_db_dump.sql.tar.gz .
if [[ $? -ne 0 ]]; then
echo "Transfer complete"
break
else
echo "Attempt ${i} failed. Trying again"
fi
done
if [ $i -gt 3 ]; then
echo "Transfer failed. Exiting..."
exit 0
fi
gunzip < sql_transfer_db_dump.sql.tar.gz \
| mysql -u$(get_value "database_username") \
-p$(get_value "database_password") \
$(get_value "database_name")
e_arrow "Cleaning up..."
ssh $host "rm sql_transfer_db_dump.sql.tar.gz"
rm sql_transfer_db_dump.sql.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment