This web development article discusses how to backup and restore a Drupal website and its related database.
It is very important to backup your Drupal website files and database. In fact, it is highly recommended that you do this on a periodic basis. Backing-up your data should be a requirement before you conduct any important activity on your Drupal website that could potentially alter its current state. For example, if you upgrade your Drupal installation from Drupal 6 to Drupal 7, you should make backups of your website before doing these changes; not only during upgrades but updates as well. If you update from version 6.2 to 6.7, you also need to backup your website files and database first.
The primary reason for doing these backups is to give yourself the ability to easily restore your website if you encounter a serious problem when implementing any changes.
This tutorial is written for a first time user of Drupal that wishes to learn how to backup and restore website files and databases using the SSH command line.
Using the Command Line to Backup Drupal Website Files
Command line methods such as SSH are one of the best ways to quickly perform a backup of your Drupal website. SSH is known as secure shell, since the communication between your computer and the server is encrypted. This tutorial assumes you already have:
1: SSH installed and running in your hosting server. 2: Your computer has the capability of issuing SSH commands. You can do this very easily in Linux using terminal command mode or Putty in Windows. 3: You have a fully running Drupal website on a remote server.
Below are the steps:
Step 1: Launch your SSH Client tool to connect to your Drupal remote server (which is also uses SSH). In Linux, you can simply use Terminal. In Windows you can use both PSCP (pscp.exe) and Putty (putty.exe) - references can be found here:
You will know that you are now in the Drupal directory because when you view the list of files and directories by issuing the command:
ls -l -a
You will see a list of files and folders such as this:
Step 4: Get the path of your current Drupal directory. Type:
For example, this will be the output:
Take note of this path as you will be needing it later.
Step 5: Decide where you will place your backups in your server. Below are some guidelines:
a.) For quick and temporary backups – you can put your Drupal backup files above the web root (inaccessible by browser or the public). In this way the backup files still reside on your web server.
b.) For permanent backups – once the backup files have been generated and placed above the web root or to its designated folder, you can download it to your local computer.
c.) For upgrades and updates- it is recommended you do both (the quick and the permanent method). This is because in some instances, a single mistake in issuing SSH commands (such as deleting files or directories) can wipe out all of your Drupal files and directories, including your backup! Downloading the backup (website files + database) is a wise move.
Supposing you will name your backup folder “drupal_backup” and put the Drupal backup files above the HTML web root:
Step 6: Now you have the path for your current Drupal website and the path where you would like to save the backup. Execute the command below to produce the quick backups of all of your Drupal website files:
The parameter -rp means to copy recursively and retain the permissions of the files and folders. Do not close the command line window until you see the command prompt again after entering the above command, because the copying process is ongoing. When you see the prompt, browse to the backup folder path to make sure you see all of your Drupal files in there.
Step 7: Now that the backup has been generated, you can download a copy of your Drupal website to your local computer. It is recommended that you compress your Drupal website folder to save some bandwidth, which will speed-up the downloading.
Go to the path where you can see the Drupal backup folder. Use the command below:
tar -zcvf backup_filename.tar.gz [Path to your Drupal backup folder]
For example, if I would like to use the filename drupal-backup-july-2011 for the compressed folder, below will be the command I would use:
tar -zcvf drupal-backup-july-2011.tar.gz /home/codex-m/drupal_backup
After entering the above command, it will be placed in the same path as the Drupal backup folder. For example:
Step 8: Now that the backup has been generated, you can simply download it to your local computer. Exit your current SSH session with your server by typing:
Step 9: If you are using Linux, you can use terminal and issue this SCP (Secure copy) command to download the compressed Drupal backup folder:
Using the Command Line to Backup a Drupal Database
Do not ever forget to backup your Drupal database. This is very important. Without this, your Drupal website won't work. Below are the steps to backup your Drupal database:
1: Before you backup your Drupal database using the command line, secure the following information:
a.) Drupal database username b.) Drupal database password c.) Drupal database hostname d.) Drupal database name e.) Path where you would like the SQL backup file to be saved and the SQL file name
a.) to d.) are easy to obtain. Let's talk about the path where you would like to save your database backup. As a recommendation, you should dump the database backup above the web root for security reasons; it's the same path as your Drupal backup folder illustrated earlier.
To make things easy, connect first to your Drupal server using SSH and go to the path where the Drupal backup folder is found. Create a new folder called “drupal_DB_backup” in the command line:
Set the permission to 755:
chmod 755 drupal_DB_backup
Finally, this is the path to this folder (example): /home/codex-m/drupal_DB_backup/
2: Suppose you want to name your Drupal backup database file "drupaldatabasebackup.sql". Execute the MySQLdump command:
This is okay, since the sites directory cannot be overwritten due to permission problems. Do not worry if nothing is changed inside your sites directory. Otherwise if you really need to overwrite it, change the permission of the sites folder first by going inside the current Drupal directory and execute this command:
chmod 777 -R sites
Then delete it:
rm -rf sites
After deleting the sites directory, copy the sites folder from your clean backup to your current Drupal path.
You can turn off the feature to exclude copying and displaying of hidden files by issuing this command:
shopt -u dotglob
5: Confirm that you have correctly copied all files by going to the current Drupal directory and launching your website in the browser. You should not see any errors in the browser and your website should operate normally like it was before.
Using the Command Line to Restore the Drupal Database from Backup
There are some instances in which your Drupal database is corrupted, which will prevent your website from working properly. Restoring a database is very quick and easy to do in the command line. Once you have your path to the SQL backup (for example):
mysql -uUSERNAME -pPASSWORD -hHOSTNAME DATABASENAME < /home/phpdevel/drupal_DB_backup/drupaldatabasebackup.sql
Your current corrupted Drupal database will be overwritten by the clean backup database and your website will be working properly again.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.