The following article will outline the steps and commands involved to successfully transfer Magento from one server to another. This article assumes you
have a basic understanding of SSH, and that you have SSH access on both servers.
Overview
Here is an overview of the steps we’ll take to do this:
Make a MySQL dump file of the database
Archive the media and theme directories
Copy the files to the other server
Install a clean version of Magento
Import the dump file to a blank database
Move our copied data to the correct places
TO DO:
Include commands for saving app/code/local/ folder
Copy extensions and plugins
Then copy all to one handy script (would be great if mage supplied it)
Old Server
The first thing you’ll want to do is login to the server where Magento currently resides. You will need to go to the directory where Magento is installed. For the sake of this article, I’m going to assume your directory structure on both servers looks like this:
/home/username/public_html/
The public_html directory is considered the root directory for your domain, so if we had a file called test.html in there, it would be accessible like so:
http://www.example.com/test.html
With that in mind, we need to go to the directory where Magento is installed. In our case, it’s installed in the root of the domain, so once we are logged in we will run the following:
cd public_html
By default, when you login to SSH it will place you in the very root directory that you have access rights to, which is typically one directory above the public_html directory.
We will start by making a folder called backup, so we can place all of the files we want to transfer over to the new server in:
mkdir backup
MySQL Dump
Now we need to create a MySQL dump file of our database:
You can create a dump file and import it into your new database using one line in your terminal. You need to have remote access the the old server.
mysqldump -h oldhost -u oldusername -poldpassword --single-transaction olddbname | mysql -h newhost -u newusername -pnewpassword newdbname
Otherwise, you may do the following
mysqldump -h DBHOST -u DBUSER -pDBPASS --single-transaction DBNAME > data.sql
You will need to replace the values with the correct information:
DBHOST - Database host name, usually just localhost
DBUSER - Database user with access rights to the database
DBPASS - The password for the database user
DBNAME - The name of the database that Magento is using
NOTE: If you’re running MySQL 4.x, you should include the following option:
--default-character-set=utf8
NOTE: If your password contains special characters or punctuation marks—for example, if your password was U!re+Ha_A?r2anp—you’ll want to use this command instead:
mysqldump -h DBHOST -u DBUSER -p --single-transaction DBNAME > data.sql
After you run this command, it will ask you to enter the database password, and you can safely enter it.
Depending on the size of your database, this can take a couple seconds to a couple minutes. If no errors are reported and you’re left with a new command line, then the dump file was created successfully. We can safely move this to the backup directory we created earlier:
mv data.sql backup/
Media Directory
Now let’s compress the contents of the media directory:
tar -cvf media.tar media/*
And move it over to our backup directory:
mv media.tar backup/
Theme Files
Next, we’re going to copy the theme directory from both the app and skin directories. For the sake of this article, I’m going to assume you have not created a new interface, and that we’re using the default interface instead. We’ll start with the theme in the app directory:
cd app/design/frontend/default/
If you were wondering what I meant by interface, it’s the last directory in the previous command. Most people do not create a new interface, but if you did, you’ll want to change the last directory name with the interface name you’re using.
The directory we’re in now is where all the themes for the default interface reside. There should already be a folder in here called default. I hope you’re not modifying the files in this directory to customize the theme for your website. If you have, you’ll want to rename this directory to something more appropriate so it does not get overwritten when you upgrade Magento.
For the sake of this article, we’re going to assume that our theme is called mytheme, so let’s compress it:
tar -cvf app.tar mytheme/
We need to move this tar file to our backup directory now:
mv app.tar ../../../../backup/
We need to do the same thing for the theme in our skin directory, so let’s change in to that directory:
cd ../../../../skin/frontend/default/
This works the same way as the interface and theme in the app directory. If you’re using another interface, change the last directory name out with your interface name. Just like the app directory, we should have a directory in here called mytheme, so let’s compress that as well:
tar -cvf skin.tar mytheme/
And move it to our backup directory:
mv skin.tar ../../../backup/
Now let’s head back to the main directory:
cd ../../../
Config File
Only thing left is our config file. This file contains the encryption key, the user name and password to connect to the database. If the connection parameters are different between your old and new servers, then you need to edit this file to reflect the changes. The editing is straight forward as the password for the database connection is not encrypted. Now, let’s copy that over as well:
cp app/etc/local.xml backup/
Important note about caching! If you are using apc caching (
apc in local.xml) and are creating a copy of the installation on the same server, you MUST change the setting to a new value. The reason for this is that if apc cache is used, magento will save all cache entries in the apc itself instead of /var/cache directory. And the apc cache storage is the same for the whole server, it knows nothing about installations, and setting is the only thing that can tell it which website each cache entry belongs to. If you have two sites running with the same prefix, some very strange and hard-to-find errors may occur like data from one shop appearing in another shop’s menus, spontaneous redirects to the other shops’ url in back-end, changing of settings in backend affecting the other installation etc, etc.
Alright, so we have all of the data we need!
If you omit this file in the new server, you will have to go through the setting up process the first time you browse your site in the new server. Here, Magento will make a new encryption key which will cause problems with the credit card information and passwords in the database.
NOTE: If you want to copy your .htaccess or php.ini (assuming you have one) in to the backup directory, now is the time to do it. You don’t need the .htaccess or php.ini file unless you’ve made changes to them. However, if you do want to move them over, run the following command:
cp .htaccess php.ini backup/
Our backup directory will now have the following files in it:
local.xml
app.tar
data.sql
media.tar
skin.tar
New Server
NOTE: Before continuing to move your site make sure that your server has all the necessary software components installed. Do the Magento Server Compatibility Check as described here: http://www.magentocommerce.com/knowledge-base/entry/how-do-i-know-if-my-server-is-compatible-with-magento.
A couple of tips:
1) PDO for mysql needs to be installed (ubuntu: apt-get install php5-mysql)
2) The curl extension for PHP5 needs to be installed. (ubuntu: apt-get install php5-curl). Without this when attempting to access the admin login you will see a blank page on the server you have transferred the Magento install to.
IMPORTANT NOTE FOR MAMP USERS: You must select PHP 5.2 in the PHP preferences!
We’re ready to set Magento up on the other server now, so SSH in to your account and go to your public_html directory:
cd public_html
We now want to grab all of the files from the other server, but let’s create a backup directory here so we have somewhere to move the files:
mkdir backup
And let’s go in to that directory:
cd backup/
Copy Files
Time to grab the files from the remote server:
wget http://www.example.com/backup/local.xml
wget http://www.example.com/backup/app.tar
wget http://www.example.com/backup/data.sql
wget http://www.example.com/backup/media.tar
wget http://www.example.com/backup/skin.tar
You’ll want to replace the above URLs with the IP address or domain name from the old server. Usually whatever domain you had setup on the old server will be the same one you have on the one.
NOTE: If you’ve already updated the DNS nameservers for your domain and it’s pointing to the server we’re moving the files over to, you cannot use the domain name. Use the IP address instead. If you haven’t updated the nameservers for your domain yet, then you can use that.
IMPORTANT SECURITY NOTE: Using the method above you are putting all of your data (including credit card data if you are storing it), your website files and the config file containing your database password on the public internet for anyone to download. If you must use this method, you may want to use something other than /backup as your path, and you will certainly want to delete the files immediately when you have copied them. Consider putting the files in a private folder and copying via SFTP instead.
Alternatively you can do a secure copy (SCP) as below:
scp -r username@example.com:/home/username/public_html/backup .
Sure this copy will be really secure only if /backup folder is out of /public_html as below:
scp -r username@example.com:/home/username/backup .
Now we have all of our files over here. So let’s do a clean install of Magento. We need to go back to the public_html directory, because right now we’re still in the backup directory:
cd ..
Install Magento
Ok, time to install Magento:
wget http://www.magentocommerce.com/downloads/assets/1.6.0.0/magento-1.6.0.0.tar.gz
tar -zxvf magento-1.6.0.0.tar.gz
mv magento/* magento/.htaccess .
mv php.ini.sample php.ini
chmod o+w var var/.htaccess app/etc
chmod -R o+w media
Import Database
If you haven’t setup a blank database on this server yet, do so now. We’re going to import the data from the dump file in to this database:
mysql -h DBHOST -u DBUSER -pDBPASS DBNAME < backup/data.sql
Replace the values like you did when we created a dump file.
PEAR Downloader
Now we want to initialize the PEAR registry and let Magento update anything in our database that needs to be updated:
chmod 550 ./mage
./mage mage-setup .
./mage install community Mage_All_Latest
Lastly, a little bit of cleanup:
rm -rf downloader/pearlib/cache/* downloader/pearlib/download/*
rm -rf magento/ magento-1.6.0.0.tar.gz
rm -rf index.php.sample .htaccess.sample STATUS.txt
Restore Media & Theme
Almost finished! We just have to move the media directory back in place and the themes:
cp backup/app.tar app/design/frontend/default/
cp backup/skin.tar skin/frontend/default/
cp backup/media.tar .
And we need to extract the data:
cd app/design/frontend/default/
tar -xvf app.tar
rm -rf app.tar
cd ../../../../skin/frontend/default/
tar -xvf skin.tar
rm -rf skin.tar
cd ../../../
tar -xvf media.tar
rm -rf media.tar
cd ..
Restore Config File
If your database details have changed from what you had on the old server, you’ll need to edit the local.xml file and update the appropriate information. It should be pretty clear what you need to update in there, all the fields are plain and not encrypted.
So the last thing left to do is to move the local.xml file where it belongs:
mv backup/local.xml app/etc/
Magento Connect Extensions & configurations
To migrate your extensions you need to copy these folders:
app/code/community
app/code/local
app/etc/modules
js
Update 1
You need to edit a few things in the database if your changing domain names or if you’re using a test server that’s just an IP address. Using PHPMyAdmin, Navicat or something similar, connect to the database. Table core_config_data has rows that need to be changed and updated with the new url. For a single store installation you need to just update:
path: value:
web/unsecure/base_url http://[you_domain_here]/
web/secure/base_url https://[your_secure_domain_here]/
with multiple stores you need also change:
path: value:
web/unsecure/base_url http://[you_domain_here]/
web/secure/base_url https://[your_secure_domain_here]/
web/unsecure/base_link_url http://[your_domain_here]/
web/unsecure/base_skin_url http://[your_domain_here]/skin/
web/unsecure/base_media_url http://[your_domain_here]/media/
web/unsecure/base_js_url http://[your_domain_here]/js/
web/secure/base_link_url https://[your_secure_domain_here]/
web/secure/base_skin_url https://[your_secure_domain_here]/skin/
web/secure/base_media_url https://[your_secure_domain_here]/media/
web/secure/base_js_url https://[your_secure_domain_here]/js/
~Fastmover
evisboy on forums provide example of code for MySQL:
UPDATE core_config_data SET value="unsecure.domain.com" WHERE path=’web/unsecure/host’;
Propose: I think ‘web/unsecure/host’ should be changed to ‘web/unsecure/base_url’ in the last line above here.
~ bdugan
Have A Beer (STZBR!)
That’s it! Open up your browser and your site should be working. If it is, you can delete the backup directory on the new server:
rm -rf backup/
The data on the older server is safe to delete as well.
Alternative Method
The above method starts you out with a fresh, clean installation of the latest version of Magento. If you experience problems using this method, there’s another way to copy the data.
Follow the instructions above for creating a MySQL dump file, and leave that in the directory where Magento is installed. Make sure you Refresh or Disable the cache before exporting the database. You can also do an “Export” via phpMyAdmin.
You’re then going to archive the entire public_html directory, so you’ll want to be one directory above the directory where Magento is installed:
tar -cvf backup.tar public_html/
You will then need to use wget like we did before to transfer this file from the old server to the new one. Extract the data from the backup archive we created:
tar -xvf backup.tar
And move it out of it’s directory:
mv public_html/* public_html/.htaccess .
You can remove the extracted public_html directory now. Do not confuse this with the public_html directory for your site, this is only the name of the directory that was in our backup file!
rm -rf public_html/
Now you need to create a blank database and import the MySQL dump file to it. The instructions are listed in the previous method.
Open the new database with phpMyAdmin (or however you prefer), go to the [mage]core_config_data table, and edit the web/unsecure/base_url and web/secure/base_url (config_id’s 2 and 3) rows to reflect the new URL of the new server.
(Note: some seem to avoid this step by changing the paths to { {base_url} } in the Admin section [System?Configuration?General?Web?Secure and Unsecure] before attempting to move the site, and then changing it in the Admin after the move.)
(Another quick way to deal with the URL changes is to load the SQL dump into Editplus or something similar and search and replace the URLs before importing)
Edit the local.xml (app/etc/local.xml) file and update the database variables if they’ve changed (but keep the same security/encryption key).
Lastly, in order to make magento connect to work correctly edit the pear.ini (downloader/pearlib/pear.ini) and update al the paths that you see in this file. Otherwise magentoconnect will try to upgrade your old site when you use it.
(the files downloader/pearlib/pear downloader/pearlib/peardev downloader/pearlib/pecl may also need the paths updating if you have changed the path)
(Note: Updating this file -pear.ini- is a tedious job, because when you change the paths, you also need the update the field before it which starts with “s:”. You need to type in the length of characters of the path field that you have updated.)
A simple helper script to generate a new the pear.ini file: drop your old pear.ini in the same directory as the PHP script, adjust the paths in the script, open it in a browser and paste the contents into your new pear.ini.
header('Content-type: text/plain');
$contents = explode("n", file_get_contents('pear.ini'));
$data = unserialize($contents[1]);
foreach($data as $key => $value) {
if(is_string($value)) {
$data[$key] = str_replace('/old/path', '/new/path', $value);
}
}
echo $contents[0] . "n";
echo serialize($data);
?>
Note: The code above splits the ini file into lines by detecting the “n” character - depending on the operating system you are running on the correct new line character to split your file may vary.
Make sure there is nothing in the cache directory (var/cache and var/session):
rm -rf var/*
Finally make sure the permissions are still correct on the new server:
chmod o+w var var/.htaccess app/etc
chmod -R o+w media
If you find any errors, make sure folder permissions are set to 755 and file permissions to 644. If your administration submenu pulldowns don’t work, make sure /js/index.php is 644.
That’s it, you’re all done!