Syncing a Magento instance from production to development

September 08, 2011   ·   2 min read  ·   Edit on GitHub

UPDATE! Code moved to bitbucket:

Often times, you need to pull down an up-to-date version of Magento from your production server to your development or staging servers. This involves:

  • Executing a mysqldump on production.
  • Zipping it up.
  • Secure copying the zipped file from production to development (or staging).
  • Unzipping the file locally.
  • Updating the base URL’s in the coreconfigdata table to your local URL’s.
  • Executing the SQL into your local database instance.
  • Deleting all the temp files from your production and development machines.

What a pain. Wouldn’t it be nice if you just executed a script that did all of this for you?


if [[ `ssh $REMOTE_HOST 'test -e ~/'$REMOTE_MYSQL_DB'.tmp.sql && echo exists'` == *exists* ]]; then
  echo "Backup is currently being executed by another process. Please try again in a few moments."
  exit 1

echo "Creating backup of production database"
ssh $REMOTE_HOST 'mysqldump -u '$REMOTE_MYSQL_USER' -p'$REMOTE_MYSQL_PASS' '$REMOTE_MYSQL_DB' > ~/'$REMOTE_MYSQL_DB'.tmp.sql' &> /dev/null
ssh $REMOTE_HOST 'tar -czf '$REMOTE_MYSQL_DB'.tmp.sql.tar.gz '$REMOTE_MYSQL_DB'.tmp.sql' &> /dev/null

echo "Transferring database backup to localhost"
scp $REMOTE_HOST:~/$REMOTE_MYSQL_DB.tmp.sql.tar.gz ~/
ssh $REMOTE_HOST 'rm ~/'$REMOTE_MYSQL_DB'.tmp*'

echo "Extracting backup" 
tar -xzf ~/$REMOTE_MYSQL_DB.tmp.sql.tar.gz -C ~/
echo "Updating config" 
sed "s/https/http/g" ~/$REMOTE_MYSQL_DB.tmp.2.sql > ~/$
echo "Reloading localhost database (may take few minutes)"
mysql -u $LOCAL_MYSQL_USER -p$LOCAL_MYSQL_PASS $LOCAL_MYSQL_DB < ~/$ &> /dev/null

# Clean local temp files
rm ~/$REMOTE_MYSQL_DB.tmp*

echo "Complete!"

You’re welcome :)

Just save the file as somewhere on your Mac or Linux machine (No love for Windows people, sorry. Come on and get a Mac already.), update the constants at the top of the file, make it executable (chmod +x), and execute it as so: ~/ You can easily set this up on a cron for automated syncing down of a production database to staging.

NOTE: Updated NOV-4-2011 with more configurability between remote and local mysql database names, usernames, passwords, etc.

NOTE: Updated NOV-18-2011 with multi-store support on same root domain ( and nixing https from localhost.

NOTE: Updated AUG-13-2012 with db hostname configurability. Also, moved to bitbucket: