We will consider the example of an imaginary web site created for a restaurant called Good Eatin' Bistro. Chef Wanyama is the owner of the Good Eatin' Bistro, a popular upscale restaurant. You can check this web site at http://goodeatin.drupalbyexample.com/.
Web site backups
A strong backup plan is critical for any successful web site. A good backup plan will protect against hardware failure, allow you to transfer your web site to another host, and allow you to recover from malicious hacking into your web site.
When you create a backup plan, you should also test the restoration from this backup to make sure that the backup works correctly.
In this section, we will explore ways of performing backups regardless of the host that you are using. Your hosting provider may also offer a solution that will back up files and databases either one time, or on a recurring basis. If your host does provide backup capabilities, you should review them to see if they suit your needs completely, or if you want to augment them or replace them with the techniques in this section.
Manually backing up a site
Good Eatin' Goal: Back up the web site without using a custom backup module.
Additional modules needed: None.
If you do not want to use a dedicated module to perform your backups, you can manually download the files and the database information that make up the site. However, this can be more time-intensive and error-prone than using a custom backup module.
A manual backup has two steps, in which you must first back up the files that make up the site and then back up the database information.
To back up the files for the web site, use the following procedure:
- Begin by opening the utility that you use to transfer files to the web site. This could be an FTP client, or an online file manager. My favorite FTP client is FileZilla, which is a freely-available open source client. The FileZilla client can be downloaded from http://filezilla-project.org/.
- Select the backup location on your local computer to which you want to copy the files, and select the root directory of your web server as the remote directory. You may want to date the backup folder so that you can maintain a history of the site.
- Next, download the files to your local directory. If you want, you can compress the files into a ZIP file or a compressed archive.
- To reduce the amount of data that you need to download, you should be able to download just the sites directory, because that folder contains all of the custom files, pictures, themes, and modules that you have added to the site.
To back up the database information, you can use your web site provider's database management utility. Many hosts provide phpMyAdmin for this purpose. If you are unsure whether or not your host gives you access to phpMyAdmin, you can contact their customer support group to check.
- Begin by opening phpMyAdmin and selecting the database that has your site information within it. The screen should be similar to the following:
- If you have multiple databases available on the host, you may need to select the database that you want to work with in the drop-down list at the upper left corner of the screen.
- Next, select the Export tab at the top of the screen. phpMyAdmin will prompt you to select the tables that you want to download and the format that you want to download in, as shown in the following screenshot:
- If you want to be able to rebuild the database at a later time, you should export all the tables in SQL format.
- Next, you will need to specify the name of the file to download to. You can use __DB__ as the database name.
- Then click Go to begin the download process. You will be prompted for the location to which you want to save the exported data.
You may want to zip the file to reduce storage space.
When you are ready to restore the web site from backup, you simply reverse the process.
- You should always import into a blank database, to avoid conflicts with existing data. You can either drop or delete all of the titles in the existing database, or you can create a new database to import the data into.
- After you have cleaned out your database, select the Import tab in phpMyAdmin.
- Now navigate to the file that you exported earlier, and click Go to begin the import. You may need to delete all of the tables in the database before you import the data, depending on the options you chose when you exported the data.
- To reload the files, simply open your FTP client, select the same directories that you used when creating the backup and then upload the files, rather than downloading them.
Automatic site backups
Good Eatin' Goal: Back up a web site so that it can be stored for easy recovery.
Additional modules needed: Backup and Migrate (http://drupal.org/project/backup_migrate).
Although you can manually back up your files and database, this process can be time-consuming and error prone. Luckily, the Backup and Migrate module makes this process easier, and optimizes the backups to exclude unnecessary data.
- Begin by downloading and installing the Back up and Migrate module.
- You can now back up your data by selecting Content management and then Backup and migrate, from the Administer menu.
- The Backup and Migrate module allows you to fully customize the backup files that are created. You can control which tables are included in the backup, and whether or not the data in the table is backed up. By default, the Backup and Migrate module does not back up cache information, session information, or watchdog information, because data in these tables is temporary and can easily be re-created.
- There are a variety of other options that you can choose from, which control how the resulting file is named, how it is compressed, and where it is compressed to.
- Once you have set the options as desired, click Backup Database to begin the backup process. If you have selected the Download option, the file will be sent to your computer so that you can store it. If you select the Save to Files Directory option, the backup file will be saved onto the server so that you can download it later, either directly from the server or using the Saved Backups tab.
- If you would like the Backup and Migrate module to back up your database automatically on a regular basis, you can schedule the back up to occur at specified intervals by clicking on the Backup Schedule tab, as shown here:
Please note that the backups created by the Backup and Migrate module do not include the files from the site, so you will still need to back up these files independently. You can minimize the backup file size by only backing up the files that the users can upload. These files are typically stored in the files directory. The process for backing up files is identical to the process used in the section on manual backups.
Restoring a site from a backup
Good Eatin' Goal: Restore information from a backup file created by the Backup and Migrate module.
Additional modules needed: Backup and Migrate (http://drupal.org/project/backup_migrate).
Restoring a backup created by the Backup and Migrate module is a simple process.
- Navigate to the Backup and Migrate manager by selecting Content management and then Backup and Migrate, from the Administer menu.
- Next, click on the Restore/Import DB tab.
- Navigate to the location of your backup file.
- After you have selected the backup file, click on Restore Database to begin the restore process. Please read all displayed warnings carefully, and make sure that you test the import on a test installation for your site before running it on your production site. If you are sure that you want to proceed with the import, agree to the confirmation and click restore.
- You may also need to import any saved files, if the server file system is not fully up-to-date. We discussed this previously in the section on manual backups.
Web site optimization
If your web site has a moderate to large number of visitors, you should tune your web site for maximum performance. This will ensure that all your visitors have a positive experience, and are not frustrated by delays in loading pages or accessing the web site.
We will discuss ways to optimize both the back end database, and the front end of the web site that displays the information to the visitor.
Optimizing the database tables
Good Eatin' Goal: Optimize the database where your site content is stored, to ensure the best possible performance.
Additional modules needed: DB Maintenance (http://drupal.org/project/db_maintenance).
When a database is used as frequently as the Drupal database is, it can become less optimized over time, because having a large number of records inserted and deleted from each table can result in the database having wasted space. All databases will allow you to run an optimization process that removes unused space and rebuilds the indexes to make the search for information faster.
The DB Maintenance module can automatically perform the optimization process for you when cron is run.
- Begin by downloading and installing the DB Maintenance module.
- You can edit the settings for the DB Maintenance module by selecting Site configuration and then DB maintenance, from the Administer menu.
- The settings allow you to control several options, including:
- Whether or not to write to the log when each optimization is performed.
- How often the tables are optimized, with options from hourly to bi-monthly, or at each cron run.
- Finally, you can control which tables are optimized. This allows you to not optimize tables that do not change frequently, which will allow the maintenance task to complete faster.
Once you have configured the settings properly, you simply save the settings, and the DB maintenance module will automatically perform the optimization at the specified intervals.
Using caching to improve performance
Good Eatin' Goal: Use caching to improve the performance of content display.
Additional modules needed: None.
Because dynamically-generated pages, such as those created with Drupal, can be time-consuming and resource-intensive to generate. Drupal uses a sophisticated caching mechanism to pre-load sections of content for display to users.
To set up the Drupal cache, carry out the following steps:
- Access the settings for the Drupal cache by selecting Site configuration and then Performance, from the Administer menu.
- The first section of settings relate to the Page cache, which is used to cache entire pages of data. The Page cache system will only cache pages for anonymous users, to prevent personal data for one user being made visible to other users, and also because it is simpler to ensure that the pages are successfully cached only if anonymous users are served cached content.
- If you choose to use Aggressive site caching, you need to be aware of any modules that may have problems. Drupal will identify these modules for you and highlight them in red. You should only enable the Aggressive cache if there are no potential incompatibilities, or if the module maintainer of a potentially incompatible module states that the module is acceptable for use with Aggressive caching. Some modules are not compatible with Aggressive mode due to the initialization or cleanup code that is run when a page is loaded normally, but not when it has been aggressively cached.
- If your site is exceptionally busy, you may want to enforce a minimum cache lifetime, which is the minimum amount of time that Drupal will wait for, before rebuilding cached pages. If your site receives infrequent updates, you may see a performance benefit by increasing this number to match your update schedule.
- The Page compression controls whether or not the cached pages are compressed. This can be left as Enabled unless your web server automatically caches all web pages.
- Drupal also provides a Block cache, which caches just the content of the blocks rather than the entire page. Unlike the page cache, the block cache works for both anonymous users as well as users who have logged into the site.
- Although Drupal caching makes your site faster and gives users a better experience, it can interfere with your development of the web site because the cache may prevent you from seeing the most recent changes to your site. If this is the case, you can clear the cached data by clicking on the Clear cached data button.
Most sites should enable Normal caching as this can significantly improve load times for your visitors without any possibility of adverse side effects.