Bash script to migrate Magento to DirectAdmin via rsync

Migrating Magento is pretty easy. Most of the times you only need to sync the files and export/import the database. But to automate such a process to test a lot, you can use a bash script to make things easier.

I often use this bash script (or similar) to migrate a Magento shop from one server to another:

#!/bin/bash

echo dumping sql
ssh root@10.0.0.1 -p 7685 "mysqldump -u magento_user -ppassword magento_db > /home/magento.sql"

echo rsync files
/usr/bin/rsync --exclude 'app/etc/local.xml' --exclude 'media/.htaccess' --exclude 'app/etc/config.xml' -a --delete -e "ssh -p 7685" root@10.0.0.1:/home/domain.com/. /home/user/domains/domain.com/public_html/.

echo rsync db
/usr/bin/rsync -a --delete -e "ssh -p 7685" root@10.0.0.1:/home/magento_db.sql /home/user/domains/domain.com/

echo importing db
source /usr/local/directadmin/conf/mysql.conf
mysql -u $user -p$passwd new_magentodb < /home/user/domains/domain.com/magento.sql

echo restoring permissions
chown -R user:user /home/user/domains/domain.com/public_html
chown user:user /home/user/domains/domain.com/magento.sql

echo done

This script is executed on the new server, which is loaded with DirectAdmin. The old server (10.0.0.1) is plain CentOS 6.5. I added the rsa-key.pub to the authorized keys on the old server, so this server is able to execute commands as root without entering a password.

The port on the old server is 7685, that’s why you find it in the ssh and rsync commands.

I exclude the local.xml because on the new server I had to change the MySQL credentials. The media/.htaccess file gave problems on the new DirectAdmin server as well, so I had to change it and exclude the file from future rsyncs.

After running this script the Magento webshop is migrated from the old server to the new DirectAdmin server and works perfectly!

 

Author: Maarten Keizer

Hosting guru. WordPress addict. n00b programmer.

Leave a Reply

Your email address will not be published. Required fields are marked *