Skip to content

Instantly share code, notes, and snippets.

@marcw
Created October 23, 2010 12:35
Show Gist options
  • Save marcw/642158 to your computer and use it in GitHub Desktop.
Save marcw/642158 to your computer and use it in GitHub Desktop.
Automatically export a magento database and change all the base_url references
#!/bin/bash
# Export options
if [[ -z "$1" || -z $2 ]]
then
echo "export-database.sh"
echo " usage: ./export-database current_host distant_host"
echo " example: ./export-database my.local.host my.other.host"
exit 1;
fi
FROM_STORE=$1
TO_STORE=$2
# Dump Options
USERNAME=`cat app/etc/local.xml | grep username | sed 's/[ ]*<username><!\[CDATA\[\(.*\)\]\]><\/username>/\1/'`
PASSWORD=`cat app/etc/local.xml | grep password | sed 's/[ ]*<password><!\[CDATA\[\(.*\)\]\]><\/password>/\1/'`
DBNAME=`cat app/etc/local.xml | grep dbname | sed 's/[ ]*<dbname><!\[CDATA\[\(.*\)\]\]><\/dbname>/\1/'`
FILE_SUFFIX=`date +"%Y-%m-%d_%H%M%S"`
FILENAME="${DBNAME}_${FILE_SUFFIX}.sql"
MYSQLDUMP_OPTIONS="--compatible=mysql40"
if [ -n "$PASSWORD" ]
then
MYSQLDUMP_OPTIONS="-p${PASSWORD} ${MYSQLDUMP_OPTIONS}"
fi
echo ">> Dumping database ${DBNAME}"
mysqldump -u $USERNAME $MYSQLDUMP_OPTIONS $DBNAME > $FILENAME.tmp
echo ">> Preparing dump for other host"
cat $FILENAME.tmp | sed "s/$FROM_STORE/$TO_STORE/g" > $FILENAME
rm $FILENAME.tmp
echo ">> Dump available in $FILENAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment