Magento Migration

By Paulus, 19 May, 2009

One of the things I do at my job is launch websites. We use three different content management systems. One of them being Magento for the clients that just want a ecommerce site. When I launched a Magento site for the first time I found that it was a little more involved than exporting the database using PHPMyAdmin, copying the files, and re-importing the database.

In order to follow this tutorial, you will need to be in the root web folder. Depending on who your host is, this path will vary. For example, I have virtual hosts set up on my home machine. The path to where I have a Magento install is: /var/www/vhosts/magentotest.com/htdocs/ In order to make things easier, create a directory called 'backup'

$ cd /var/www/vhosts/magentotest.com/htdocs/
$ mkdir backup

Dump the Database

The best way to dump the database is by doing a MySQL dump at the command line.

$ mysqldump -u yourusername -p yourdatabasename > yourdatabasename.sql

If you are using MySQL 4.x then you will need to add the '--default-character-set=utf8' flag.

There are some hosts out there that will not give you access to the command line so the only way to import a Magento database is to modify the sql dump file. The main problem when importing a Magento database are the checks that are done on the records. At the beginning of the MySQL add the following lines:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT;
SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS;
SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;
SET NAMES utf8;
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0;

At the end of the SQL dump file add these lines:

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT;
SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS;
SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION;
SET SQL_NOTES=@OLD_SQL_NOTES;

SQL_MODE="NO_AUTO_VALUE_ON_ZERO" is used to prevent MySQL from changing the values of any autoincremented columns in the database. Foreign key checks are also disabled. if checks are not disabled, importing of the database will throw numerious 'Foreign key' errors because the table that is being imported has data that is referencing data in another table which does not contain anything yet.

Backing up Media

$ tar cjvf media.tar.bz2 media/*

Backing up Theme & Skin

If you have created a theme for your site, then you will need to back it up as well:

$ cd app/design/frontend/default/
$ tar -cjvf ../../../../backup/app.tar.bz2 yourtheme
$ cd ../../../../skin/frontend/default
$ tar -cjvf ../../../backup/skin.tar.bz2 yourskin

Backup Configuration

Depending on how your new host sets up databases, you may need to modify this file to reflect the changes. There's not need to worry. The data will still be the same, the database and possibly the user name will be a bit different. Let's go back to the web root directory:

$ cd ../../../ $ cp app/etc/local.xml backup/

Moving the Contents

If you have shell access and the wget command is available to you, then you can ssh into your new server and run the wget commands:

$ wget http://www.example.com/backup/local.xml
$ wget http://www.example.com/backup/app.tar.bz2
$ wget http://www.example.com/backup/data.sql
$ wget http://www.example.com/backup/media.tar.bz2
$ wget http://www.example.com/backup/skin.tar.bz2

In addition to all the backup files you will want to get upload or 'wget' the version of Magento you were using.

$ tar -zxvf magento-x.y.z.tar.gz
$ mv magento/* magento/.htaccess.

This step may not be necessary, depending on your host. Some hosts allow you to upload a php.ini file while others allow you to edit a php.ini file through their preferred control panel.

$ mv php.ini.sample php.ini
$ chmod o+w var var/.htaccess app/etc
$ chmod -R o+w media

Don't forget to change the owner and group of the files if you are moving it from one account to another on your personal web server.

Re-importing the Database

$ mysql -u yourusername -pyourpassword newdatabase < yourdatabasename.sql

The attached shell scripts will automate the process above.

package.sh -- will package the files needed. For the script to run correctly you must supply several parameters:

sh package.sh <dbuser> <dbpass> <dbname>

<dbuser> - MySQL user

<dbpass> - MySQL pass

<dbname> - MySQL database to dump.

package.sh should be placed in the directory of the site you wish to migrate.

The package.sh will create a file called backup.tar.gz for you to download and upload to a different server. If the site is being migrated from one directory to another, then you can simply login to the server and move it to the httpdocs of the domain in which it will be installed on. Be sure to include the tarball of magento that was used when the site was initially set up.

Before running the unpackage.sh script, make sure you create the database and database user.

run unpackage.sh -- This will untar all the files and install magento:

sh unpackage.sh <magento_ver> <dbuser> <dbpass> <dbname> <http_url> <https_url>

<magento_ver> - Ther version in magento-x.y.z.tar.gz. For magento 1.3.1 the first parameter will be 1.3.1

<dbuser> - MySQL user

<dbpass> - MySQL pass

<dbname> - MySQL database to import.

<http_url> - unsecure URL - http://www.example.com

<https_url> - secure URL - https://www.example.com

Issues After Migration

Two issues that I've seen after migrating a site is:

  1. Magento Connect gives an invalid file permission error. The reason why this happened to me was that php was being executed as apache so therefore Magento could not write to the directories it needed to. If your host is using suPHP then you won't have an issue. Otherwise, you can 777 just the directories:




    $ find . -type d -exec chmod 777 {} \;

     

  2. Anything that was installed via Magento Connect will need to be reinstalled.