Codeistry

Uploading, downloading & backing up MODx sites

MODx sites consist of two basic bits - a collection of files and a MySQL database. Each of these needs backing up and restoring differently. The files are quite simple - you can just copy them - but the database is slightly more complex. In this article, we'll cover the whole process, including backing up and restoring both the database and files and the related job of moving a MODx site from one server to another.

Backing up a MODx site

Database

Screenshot of the MODx Manager, with mouse cursor clicking the link to download the backup file.There's a tool built into the MODx manager which allows you to save a backup script of your sites database. This script contains a complete copy of your data, along with the SQL instructions to re-create the database structure.

To backup, just log into the MODx manager, click the Tools tab at the top, then click 'Backup', on the left. Next, just click the top two check-boxes on the page - the 'Generate DROP TABLE statements' one, and the 'Table name' one at the top of the list; this should tick all the boxes. You can now click the 'Click here' link at the top and save the downloadable file somewhere. This file is your database backup - keep it safe.

Files

Screenshot showing the folders which make up a MODx install - assets & manager.How you backup your site's files differs for sites where you have physical or remote shell access and sites where you can only talk to the server via FTP.

The files which make up a MODx site are split into two folders - /assets and /manager. The assets folder contains all the files specific to your site - your images, media, stylesheets etc... and the manager folder contains the gubbins which run MODx. Generally, you'll only need to backup the whole folder tree (including the manager folder and anything else you might've added) once, after the site's up and running. After that, you can just backup the assets folder.

The only site specific thing the manager folder contains is the MODx configuration file (/manager/includes/config.inc.php) - more on this below.

Local

If you have local or shell access to your server, you can just zip up the assets folder, or the whole MODx folder tree, to backup your files.

If you're using Linux (or a Mac with OSX, probably), you can just run this command to make a time-stamped compressed (.tar.bz) archive of your assets folder; just copy and paste this into a command prompt:

tar --totals -C ~/www/modx/sitename/ -cjf `date +%Y%m%d-%H%M%S`-sitename-assets.tar.bz2 assets

This assumes that your site is running from ~/www/modx/sitename; just amend the path to match your setup. Running this on my ~/www/modx/codeistry folder produces a file called 20090109-175949-codeistry-assets.tar.bz2 which contains a compressed copy of the assets folder.

If you're using Windows, my condolences; you'll need to install a file compression program first. I strongly recommend the free and open source 7-zip. Once you have 7-zip installed, you can run this command to make a time-stamped compressed (.zip) archive of your assets folder. You'll need to open the command prompt and change directory to the sites root folder first, then run this:

c:\Program Files\7-Zip\7z.exe a -tzip %date:~-4,4%%date:~-7,2%%date:~-10,2%-%time:~0,2%%time:~3,2%%time:~6,2%-sitename-assets.zip assets

This assumes that you're in the site's root folder. Just replace sitename with the name of your actual site. Running this in a c:\Program Files\Apache Software Foundation\htdocs\codeistry\ folder would produce a file called 20090109-185623-codeistry-assets.zip which contains a compressed copy of the assets folder.

Both these commands produce time-stamped archives which look like this: YYYYMMDD-HHMMSS-sitename-assets.ext, so you can re-run the commands at any time to produce a time-stamped backup, or use them in automated backup scripts which you could run on a schedule.

Remote

Screenshot of Filezilla, showing the toolbar and log pane.For remote servers, if you have remote shell or remote desktop access to your server, you can basically follow the local instructions above. If not, then you can do this over FTP quite easily. If you don't already have FTP client software, I recommend the free and open source FileZilla, which runs on Windows, Linux and Mac. Then just connect to your server using FileZilla and download a copy of your assets folder. You can use the commands above to make a compressed archive of this folder once you've downloaded it, if you like.

Restoring a MODx site

Database

There are GUI tools for MySQL which you can download and use for all this, but I'm going to use the command line tools - they're quicker, just as easy, come installed with MySQL - and you can just copy and paste these commands into a command prompt to use them.

To start the MySQL command shell and connect to your database server, open a command prompt and run this:

mysql -uUsername -pPassword -hServeraddress

Check the 'Include Bin Directory in Windows PATH' during MySQL install.If you're on Windows and didn't check this box when installing MySQL, you'll either need to add the MySQL bin folder to your path, launch the 'MySQL Command Line Client' from the Start Menu, or use the full path to the command, something like this:

"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe" -uUsername -pPassword -hServeraddress

You'll need to replace the bold bits with the appropriate values for your setup. The -h bit is only needed if you're connecting to a non-localhost server. Once you're connected and are at a mysql> prompt type \s and press enter. This will print some server status info. Mine looks like this:

mysql Ver 14.12 Distrib 5.0.67, for debian-linux-gnu (x86_64) using readline 5.2 Connection id: 9930 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.0.67-0ubuntu6 (Ubuntu) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: latin1 Conn. characterset: latin1 UNIX socket: /var/run/mysqld/mysqld.sock Uptime: 1 day 10 hours 3 min 23 sec Threads: 1 Questions: 60954 Slow queries: 0 Opens: 2670 Flush tables: 1 Open tables: 64 Queries per second avg: 0.497

Note that 'Current database:' is blank. We need to be using a database before we run our script. If you're restoring from scratch, without overwriting an existing site, you'll need to create a new blank database to restore into, like this:

create database modx_sitename;

Now you need to change to your site's database before running the script:

use modx_sitename;

If you run \s again, you should see that it now says 'Current database: modx_sitename'. Now we can run our script to re-create the tables and data inside our database:

\. /path/to/wherever/you/put/the/script/10_jan_09_database_backup.sql

At this point a load of text should scroll past talking about rows and you're done. If you started by creating a new blank database, you can check that the restore worked by typing:

show tables;

This should print a list of the tables your database now contains. You can quit the MySQL shell by typing \q.

Files

To restore your site's files, you just need to unpack the archive you made earlier into the sites root folder. I suggest that you rename the existing assets folder to assets_old before you do this, just in case. If the restore works OK, you can remove assets_old later. To restore from scratch, rather than overwriting an existing site, you'll need a copy of the whole MODx folder tree, or install a new blank MODx (make sure it's the same version as your backup), then restore your backup over the top of that.

Moving a MODx site from one server to another

You now know how to do this - just do a backup on one server and a restore on the other one. As the new server will probably have a different name and address than the old one, you will have to make a couple of changes to the configuration of your newly restored MODx site.

config.inc.php

The first changes need to be made to the config.inc.php file. This can be found in /manager/includes/config.inc.php. Open this file in a text editor and change the bold values in the first few lines to match your setup:

/** * MODx Configuration file */ $database_type = 'mysql'; $database_server = 'localhost'; $database_user = 'db_username'; $database_password = 'db_password'; $database_connection_charset = 'latin1'; $database_connection_method = 'SET CHARACTER SET'; $dbase = '`modx_sitename`'; $table_prefix = 'tableprefix_';

Be careful to keep all the quotes and back-ticks and whatnot - just change the bold bits.

Tools > Configuration

Screenshot of the File Manager Path in the MODx manager Configuration page.The second change needs to be made in the MODx manager, in the Tools tab, then Configuration. In order for the File Manager to work properly, you need to set the File Manager Path to match your new server's setup.

Screenshot of the MODx Manager showing part of the System Info report.If you don't know what this needs to be set to, you can find out by clicking the Reports tab, then System Info, then click the 'View' link next to phpInfo(). Scroll down this page until you get to 'include_path' and copy this value, minus the 'manager/includes/' part and put it into the File Manager Path box.

That's it, you're done! Hopefully you found this article useful. If you have any questions or suggestions, please feel free to get in touch.