How To Backup and Restore a Drupal Installation

My current project is to learn to set up the whole Drupal installation again from scratch It is vital that anyone running a Drupal installation knows how to do this. I am currently learning it now, so will blog my progress.

There are four main reasons why you should know how to restore a Drupal installation:

  1. If you upgrade your Drupal CMS to the latest version, it is advisable to create a copy that you can use if the upgrade goes disastrously wrong – i.e. you lose your database and old files.
  2. If your server dies, or you decide to change webhost, then you will have to know how to do this, otherwise you will be paying someone a lot of money to install your Drupal CMS site for you.
  3. If hackers destroy your site, you will need to be able to quickly revert to a previous saved version.
  4. If you wish to create a test site of your Drupal installation, for testing new modules and templates, then making a copy is essential. Really your should never test in a live environment.

I seen several examples of one of the above happening at some point during my short time playing about on the web. I have seen a well respected SEO Forum disappear due to lack of backup. I have seen a Drupal install destroyed by hackers – at least the database was taken down, and with such a CMS, the database is everything.

So, how do you backup Drupal and then install?

Currently for backup, I am using a module which backs up all the files and the database into one tar.gz file. The easiest way to restore the files, is simply to FTP the gar.tz file to the server, then SSH using PuTTy to the command directory, and type in the command line:

tar -xvvzf drupalbackup.tar.gz

This will extract all the files, and also the drupal-backup.sql.gz

If you are only moving the location of you site, i.e. moving from domain1 to domain2, or from www.domain to subdomain.domain, then this is all that is actually required, as the same database will be used. Go to the new domain, and your site should be working as normal, just under a different name. Remember that now you have two domains using the same database.

Things to consider:

  1. Ensure that you do not link to the site anywhere, and if you have Google Adsense on the site, remove it for now. If the search engines find the new site, then you will be penalised for having duplicate content. When not testing / using the site, put it into Site maintenance. If you are restoring a whole site after a host move, this is obviously not required.
  2. Also remember that both domains are usign the same database at the moment, so don’t get excited and start installing and testing new modules yet!

However, if you lost your database, or have moved to a new host, then you will need to now extract and import the database files also. I have used phpmyadmin for importing database files in the past, but there are often problems for larger files. So I plan to master the mysql command line also, but first, I wish to see if I can do it with phpmyadmin.

First, in phpmyadmin, create a new database. Then, edit the Drupal config files to read the new database.

\sites\default\settings.php

IMPORTANT NOTE:
This file may have been set to read-only by the Drupal installation program. If you make changes to this file, be sure to protect it again after making your modifications. Failure to remove write permissions to this file is a security risk. i.e. it should be read–read–read, or 444.

The file only has two lines (apart from the comments, which make up most of the file).

$db_url = ‘mysql://account:password@localhost/dbname’;
$db_prefix = ”;

All you need to do is change he dbname to the newly created database that you have just made in phpmyadmin. The account and password will obviously be your MySQL account and password.

Be sure that you have uploaded your new settings.php file. As the file is set to read only, then you may not be able to simply FTP the new one over and replace. First, delete the file on the server. If you refresh your test/new website at this point, you will see “Unsupported database type” in the browser. On uploading the new settings.php file, and refreshing the browser, you should now see a blank page. This is proof that the database is not being read.

So, now we are ready to import the new database (the backup). Once this is done, you have a playground for testing, or a new permanent home for your site.


5 Comments on “How To Backup and Restore a Drupal Installation”

  1. It’s all good and well, but your very vague when saying what you use. I quote: “Currently for backup, I am using a module which backs up all the files and the database into one tar.gz file.” Which module!?

  2. I have actually used two backup modules in Drupal;

    http://drupal.org/project/backup

    and more recently

    http://drupal.org/project/backup_migrate

    At the time of writing I was using backup, but that was not supported for the most recent Drupal update, so I started using backup_migrate.

    Both work well, and do the job, but I recommend the backup_migrate option as the author of the script is still maintaining it as far as I can tell. Always one of the downfalls of using open source software – sometimes people just get bored of a project and no-one else takes the reins.

  3. Hi, thanks for your efficient blog 🙂
    I am not sure if backup_migrate module do the job cause as far as i can test it, it only backup database.

    I hope I am wrong due to a lack of nice settings…

    Could you tell me how to set this module in order to save files please ?

    Thanks

  4. Hi, its been a long time since I tried to be honest, do not do much Drupal stuff now. As I recall, it should allow you to backup the files somewhere, as it is designed to migrate the whole lot.

Leave a Reply

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