Skip to content

Instantly share code, notes, and snippets.

@danlamanna
Created January 29, 2012 01:05
Show Gist options
  • Save danlamanna/1696532 to your computer and use it in GitHub Desktop.
Save danlamanna/1696532 to your computer and use it in GitHub Desktop.
Example pull database script
#!/bin/bash
SLAVE_DB_USER="foo"
SLAVE_DB_PASS="bar"
SLAVE_DB_NAME="foobar"
SLAVE_DB_HOST="127.0.0.1"
MASTER_DB_USER="production_foo"
MASTER_DB_PASS="foobarbaz"
MASTER_DB_NAME="production"
MASTER_DB_HOST="realdomain.com"
REPLACE_FILE=$SLAVE_DB_NAME.sql
REPLACED_FILE=$SLAVE_DB_NAME.replace.sql
# Backup Current DB
mysqldump --user=$SLAVE_DB_USER --pass=$SLAVE_DB_PASS $SLAVE_DB_NAME > $BACKUP_FILE
# Get/Replace/Import new DB
mysqldump --max_allowed_packet=100M --user=$MASTER_DB_USER --pass=$MASTER_DB_PASS --host=$MASTER_DB_HOST $MASTER_DB_NAME > $REPLACE_\
FILE
sed -e "s/$MASTER_DB_HOST/$SLAVE_DB_HOST/g" $REPLACE_FILE > $REPLACED_FILE
mysql --user=$SLAVE_DB_USER --pass=$SLAVE_DB_PASS $SLAVE_DB_NAME -e 'drop database jhankle_accumera; create database jhankle_accumer\
a;'
mysql --user=$SLAVE_DB_USER --pass=$SLAVE_DB_PASS $SLAVE_DB_NAME < $REPLACED_FILE
# Clean up.
rm $REPLACE_FILE
rm $REPLACED_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment