How to make a backup of a Magento store?

Like any other dynamic website a Magento web store is made of a database and files. To create a full backup of the store (either for security reasons or to migrate to another server) you will need to export your database and save all files under the magento root directly. There are many ways to accomplish this but my personal favorite is just to run some commands at the cli 🙂

Backup the database

So, how can you backup the database used the magento store? Well, first you will need to determine where that database is and how to access it.

If you are running a Magento 2 web store this information is stored in the env.php file that exists in the app/etc folder. Then env.php returns an associative array that as a db key in it. This is where you can find all the information needed by magento to locate the database that hold the data of your store. The content of this file will look something like this:

<?php
return [
    'backend' => [
        'frontName' => 'admin'
    ],
    'crypt' => [
        'key' => 'kmj09mf8mfsdnf89shfs223'
    ],
...
    'db' => [
        'table_prefix' => 'mg_',
        'connection' => [
            'default' => [
                'host' => 'localhost',
                'dbname' => 'mage_db',
                'username' => 'mage_user',
                'password' => 'db_password',
                'active' => '1',
                'model' => 'mysql4',
                'engine' => 'innodb',
                'initStatements' => 'SET NAMES utf8;'
            ]
        ]
    ],
...

In the above example you can see that the credentials to access the database are:

host => localhost
dbname => mage_db
username => mage_usr
password => db_password

If you are running a Magento 1 web store you can find this information in the local.xml file that exists in the app/etc folder. The structure of this file is totally different from the env.php because this is actually an xml file. It will be something like this:

<config>
    <global>
        <install>
            <date><![CDATA[Wed, 19 Mar 2014 09:31:51 +0000]]></date>
        </install>
        <crypt>
            <key><![CDATA[98sjhfsdkf7jh343234addasd]]></key>
        </crypt>
...
        <resources>
            <db>
                <table_prefix><![CDATA[]]></table_prefix>
            </db>
            <default_setup>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[mage_usr]]></username>
                    <password><![CDATA[db_password]]></password>
                    <dbname><![CDATA[mage_db]]></dbname>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                    <model><![CDATA[mysql4]]></model>
                    <type><![CDATA[pdo_mysql]]></type>
                    <pdoType><![CDATA[]]></pdoType>
                    <active>1</active>
                </connection>
            </default_setup>
        </resources>
...        
    </global>
...    
</config>

The relevant part here is the connection section where you can get the host, username, password and dbname information for the database:

<host><![CDATA[localhost]]></host>
<username><![CDATA[mage_usr]]></username>
<password><![CDATA[db_password]]></password>
<dbname><![CDATA[mage_db]]></dbname>

Using this information you can now run the following command in your terminal:

$ mysqldump -u mage_usr -p mage_db > database-$(date +%F).sql

You will be requested the password and the command will save the database sql dump into a file named database-<date>.sql in your current directory.

If your database is not hosted in your server (localhost) you will also need to specify the -h option in the command and even the -P option (for a port other than the default 3306). It would look something like

$ mysqldump -u mage_usr -h domain.com -P 3307 -p mage_db > database-$(date +%F).sql

where domain.com is where your database is hosted and 3307 is the port that the mysql service is listening to on that server.

Backup the files

To backup the site’s files run the following command on the directory above the magento root directory, where magento_root_folder is the folder containing your magento files:

$ tar -czvf backup-$(date +%F).tar.gz magento_root_folder/

The command create a tar.gz archive named backup-<date>.tar.gz in your current directory.

If you what me to do it for you hire me on Fiverr.

Leave a Reply

Your email address will not be published.