Join Tom Geller for an in-depth discussion in this video Backing up with the Backup and Migrate module, part of Advanced Drupal 7.
It's not a question of whether your site will some day run into problems. It's a question of when. It's not usually a matter of so- called computer error either. I found that catastrophic failures happen on my site most often when I'm doing routine maintenance. The best defense is a good offense, so this video will talk about how to be ready at all times to recover from problems you're sure to encounter. As you know, a Drupal site comprises a database and a set of files. You have to back up both of them. I talked about using the archive-dump command in Drush to back up both, but now I'd like to show you a little trick that uses a module called Backup and Migrate.
To get it, go to drupal. org/project/backup_migrate. We'll install this in the usual way. Copy the URL, go back to our site, and install it from the Modules page and paste it in. Then we enable it. Scroll down.
That's really all that you need. From here, it's quite a simple and elegant module to use. Just go up to Configuration and then down under System click Backup and Migrate. By default, it'll download your entire database to your computer. So I click Backup now. It takes a minute to package it up and then downloads. If I go to my desktop, I then see it right there, and of course, I could open that and take a look at it in any text editing program that I like. And there we go! But no backup program would be worth anything if it doesn't also restore.
And in fact, Backup and Migrate does exactly that. We go back to our site and click the Restore tab and then we can choose a file and just do exactly as you'd expect. I choose it, click Restore now; this may take a little while. But when it's done, we get a success message, and in fact our site is back as it was according to the time when we made that backup file. So that's the module's basic operation and you could download your Drupal files; that is, not the database separately using the techniques you saw in the video, transferring files to and from your web host.
But we can do better. We're going to change the way that Backup and Migrate operates so it'll save that file into a directory so that when we download our files we'll also have our database. The way we do that is instead of doing a backup to our desktop, we do it to the server. We change Download to Manual Backups Directory, and Backup now. But where did it save that backup? To find out, we click the Destinations tab and we see Manual Backups Directory. It's in private://backup_migrate/manual.
So the question is what is that private? Well, you can find that out by looking in your file system settings. In a previous video we set a private files location that's under Configuration and File system, and there it is. So it's sites/default/files/private, followed by the other things that were in that screen, backup_migrate/manual. We can determine that's true by going into our Terminal and looking there. I'm already logged in and looking at where my Drupal site lives.
We go into sites/default and see what's in there; make sure that I'm in right place, files/, private/, and you see we now have this backup_migrate directory that it created. And inside there, as you might imagine, is the manual folder. When we go in there, of course, we find the backup that we just made. Yup! And there it is.
One other thing that Backup and Migrate does is it creates this test.txt file. The reason for that is to make sure that you're not leaving that directory open so that anybody can grab your backups. We will do a pwd and just copy the end of this URL. I'll then back to my directory and add that on to the domain. In other words, I'm trying to go directly into that folder without using any links on the site or anything like that. If I can see that test.txt file in that folder, we'll know that it's not protected.
So go to that URL /test.txt. Good! We can't actually see what's in there. That means that we also can't see all of those manual backups we're saving to the server. So we can both download the database or we can store it in our Drupal installation site folder on the server. But what's great about that is we can set up schedules so that there are automatic backups to be stored on the server. To do that, you would go to the screen, which you can get to directly by going to Configuration and Backup and Migrate again, and then go to Schedules and I'll add a schedule.
I'll call it Daily. It'll be Default Settings; I'll get back to that in just a minute. Backup every day and I'm only going to keep a week's worth of files. The Destination will be the Scheduled Backups Directory, and then Save schedule. Now we won't wait around all day for that backup to occur, but as you might guess, it shows up in pretty much the same area as the manual backup that we just did. And if we go up to Destinations, we see the Scheduled Backups Directory and it's very similar.
It's just in a subdirectory called scheduled. So how does this help you? Well, because the files are being stored in that sites folder, you can simply download your entire Drupal installation, and you'll get your backups along with it whenever you do your backup of the entire site. So you now have a way to back up both the files and the databases, all in one go. I do have a few caveats though. First of all, although the files that Backup and Migrate creates are in the SQL language, you probably won't be able to import them directly into phpMyAdmin.
They're too specialized; they have a little bit of extra sauce to them that makes them incompatible with a lot of things that you do on phpMyAdmin. Second, it's worth exploring the options on that Advanced Backup page. Let's go back and take a quick look. Go back to Backup and Migrate and then see this tab up here, Advanced Backup. There are a lot of options having to do with the site name, whether it gets a timestamp, which tables it includes, and so on.
You might remember that I said I'd come back to that thing about this being the default setting. Well, you can actually create a new setting simply by going into Advanced Backup and then saving it down here under Save these settings. My third caveat is to be aware that these backups will take space. The backup strategy I outlined earlier means you only have to save seven database files, but a more likely scenario is that you'll save each year for the last 10 years or something like that, so you end up with 30, 40, or 50 files.
That doesn't hurt you very much when you're first building your site, but after it's been running for a while, your site might be as big as a gig, which means you're burning up 30, 40, or 50 gigs of space. Basically, you have to decide for yourself what balance of safety and convenience you need and set your backup strategy according to that. My fourth caveat is that you back up your entire Drupal directory, not just the sites folder, although your sites folder is what contains the database backups. You could just do that. But I've sometimes found it inconvenient to have to match up the sites directory to the right version of Drupal, especially if you haven't been completely on the ball about updating the version of Drupal that you have.
Whatever system you set along make it a part of your habit and be sure to test your backups once in a while. A backup that doesn't work is the same as no backup at all. Finally, check out the section in this course about Drush to learn a much faster way to back up and restore your site. It takes some preparation and learning, but ultimately you might find it a lot more convenient. The important thing is you now have two different ways to back up and no excuse not to follow one of them.
Drupal 7 Advanced Training was designed as a follow-up to Drupal 7 Essential Training and it also dovetails nicely with our other Drupal courses, such as Drupal 7 Reporting and Visualizing Data and Create Your First Online Store with Drupal Commerce.
- Moving a site from the development environment to production
- Hosting a Drupal site
- Moving databases with phpMyAdmin and Unix commands
- Making site administration more efficient with Drush
- Backing up site data
- Moderating comments
- Migrating from previous versions of Drupal
- Working with themes
- Creating variable layouts
- Enabling social features
- Creating an online store with Drupal Commerce