How to create a compact manual backup of a Magento installation?

You need to back up the files and the database.

(1) Preparing the database

To back up the database, empty the core_session table first as this will else be the largest chunk of unnecessary data in the database. However, empty that table only if it is ok for you to force a log-out and cart reset for all your users.

Else, there is a way to remove session data while keeping most recent sessions: In short, ask yourself what sessions you want to keep: those started in the last k hours. Convert these k hours to seconds j, and add the session lifetime in seconds i as configured in Magento. Then execute a command like this (in this example we use 300000 s session lifetime and keep the sessions started during the last 8 hours):

DELETE FROM core_session
  WHERE session_expires < (unix_timestamp(now()) + 300000 - 28800);

Note that tables with type MEMORY will not export their data when exporting a database, so do not worry about them (often containing also a big portion of the whole database size). This is just temporary data that would be lost when re-starting the database server anyway [source].

Also, disable the Magento caches. This setting is stored in the database, and without them being enabled, there’s one thing less to care about when re-installing a Magento installation, potentially including changes in database access credentials and other stuff that would else be cached.

(2) Backing up the database

To export the database, you can use phpMyAdmin (or similar) or the command line via SSH on the server. To do it with the command line, use something like this:

mysqldump --opt --no-create-db --default-character-set=utf8 --user="your-username" --password="xxx" --database your-database > com.example.yyyy-mm-dd.Database.sql;
gzip com.example.yyyy-mm-dd.Database.sql;

Note that utf8 charset option in the mysqldump command. Without it, earlier versions default to latin1, so you might lose characters.

(3) Preparing the files for backup

To reduce the size of the file backup, do the following (commands relative to Magento document root directory):

  • empty the Magento cache
  • empty the Magento cache storage
  • empty the Magento image cache
  • disable the Magento compiler
  • rm -R media/import/*
  • rm -R var/log/*
  • rm downloader/pearlib/download/* (The files in here are not needed by Magento Connect Manager after successful installation, yet can be as large as the rest of Magento [source].)
  • rm downloader/pearlib/cache/*
  • rm -R includes/src/*

The “rm -R includes/src/*” command will remove the currently compiled files. They can be re-created by re-running the compiler, after the backup has been done. Normally, the command

php -f shell/compiler.php clear

should also do that cleaning of compiled files, and also disable the compiler, but in my Magneto 1.5.0.1 installation, failed to clean files.

(4) Backing up the files

Now, you can create a file backup with a command like this via SSH on the server:

tar -cf com.example.yyyy-mm-dd.tar httpdocs/

Or if you don’t care for server load at the moment, do the gzip part at the same time:

tar -czf com.example.yyyy-mm-dd.tar.gz httpdocs/

(5) Restoring the database

Now that you need your backup restored (like when migrating your Magento installation to a new server), use this command:

mysql --default-character-set=utf8 --user="user-name"  --password database-name < dump.sql

Note again the explicit option to set the UTF-8 character set [source]. Some (or all?) versions of mysql assume Latin1 instead, and reading your UTF-8 data as Latin1 will result in nasty, nasty UTF-8 double encoding. But there is a solution to that, working best for me; and some alternative solutions.

(6) Restoring the files

This is pretty simple, you’ll know how:

tar -czf com.example.yyyy-mm-dd.tar.gz httpdocs/

(7) Preparing Magento for use

Do not forget to re-enable Magneto caching and Magento compiler, for speed.


Posted

in

,

by

Tags:

Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.