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:


echo dumping sql
ssh root@ -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@ /home/user/domains/

echo rsync db
/usr/bin/rsync -a --delete -e "ssh -p 7685" root@ /home/user/domains/

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

echo restoring permissions
chown -R user:user /home/user/domains/
chown user:user /home/user/domains/

echo done

This script is executed on the new server, which is loaded with DirectAdmin. The old server ( is plain CentOS 6.5. I added the 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!