Home arrow Web Authoring arrow Backup and Restore a Drupal Website and Database

Backup and Restore a Drupal Website and Database

This web development article discusses how to backup and restore a Drupal website and its related database.

Author Info:
By: Codex-M
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
July 22, 2011

print this article

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:


Step 2: Issue the command to connect to your Drupal website:

ssh -p [portnumber] username@hostname

Step 3: Navigate to your Drupal directory. This is the directory where you can see your Drupal files. You will be using the CD (change directory) command: http://www.computerhope.com/unix/ucd.htm

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:

cp -rp /path/to/drupal_web_site /path/to/intended/backupdirectory

So using the above paths, it will be:

cp -rp /home/codex-m/public_html/codexdrupal /home/codex-m/drupal_backup

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:

scp -P [portnumber] username@hostname:/home/codex-m/drupal-backup-july-2011.tar.gz /home/codex-m/Desktop

The above command will download the compressed drupal backup file: drupal-backup-july-2011.tar.gz to your Linux desktop.

If you are using Windows, you can also use the PSCP (Putty secure copy) command using the same syntax. You can read this tutorial for details:


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:

mkdir drupal_DB_backup

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:

mysqldump -uUSERNAME -pPASSWORD -hHOSTNAME DATABASENAME > /home/codex-m/drupal_DB_backup/drupaldatabasebackup.sql

3: The SQL backup file will be placed in this path (with the folder you created earlier):


4: You can then download the backup database to your local computer using the SCP command illustrated earlier.

Using the Command Line to Restore the Drupal Website from Backup

Supposing you encounter a problem either from an upgrade, update or any changes that prevents your website from working properly, you can restore the website from its backup, using the steps below:

1: Make sure you know the path to the Drupal website backup, which is placed in your server (e.g. this is the temporary/quick backup created in the previous section).


As well as the path to your current Drupal directory, e.g:


If you also have accidentally deleted the backup in the server, you can upload the backup folder from your local computer to the path above your public_html.

2:  Go inside the Drupal backup folder, for example:

cd drupal_backup

3: Confirm that you are really in the Drupal backup folder:


For example it should provide the correct path:


3: Enable the copying of hidden files; this will make it possible to copy hidden files such as .htaccess:

shopt -s dotglob

4: Now copy all the files inside the Drupal backup folder to the current Drupal directory, including hidden files as well as retaining file permissions:

cp -rp * /home/codex-m/public_html/codexdrupal

Do not close the command line window if the copying is still ongoing; it will be completed once you see the bash prompt again.

In some instances, you might encounter these warnings:

cp: cannot create regular file `/sites/default/settings.php': Permission denied

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):


You can restore it by using this command:

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.

All Web Authoring Tutorials
More By Codex-M

blog comments powered by Disqus

- Top WordPress Plugins for Social Media
- How to Install WordPress on WAMPP
- More of the Best Wordpress Plugins for Comme...
- Top Wordpress Plugins to Fight Spam
- More of the top Drupal modules for SEO
- Top Drupal Modules for SEO
- More Top Wordpress Plugins for SEO
- Website Optimization Tips
- Backup and Restore a Drupal Website and Data...
- 5 Best Web Font Services
- Web Font Embedding Services
- Joomla Site Administration
- Create PHP Pages that Use WordPress Templates
- Top Joomla Extensions for Social Networking
- Drupal Modules for Site Navigation

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials