Backing up your Drupal site

  • Post author:
  • Post last modified:May 12, 2022
  • Reading time:5 mins read

Backing up a Drupal site involves copying the website files and database to a local server. We copy the entire site root directory tree to the local server. Also, we dump the website MySQL database at the site server and copy the database dump to the local server.

Let's start with the site's MySQL database. In this example, the website root directory is some-directory/website and the site's MySQL database dump is stored at some-directory/db-dump. For taking the database dump, first login to your website as administrator. From the administration menu, flush all caches. Next, give the command on the website's server,

$ cd some-directory/db-dump 
$ rm * 
$ mysqldump --user=website-drupal-user --password=“Drupal-user's-mysql-password” --result=some-directory/db-dump/my-website-db-`date +%F-%H%M`.sql website-drupal-database

The mysqldump command is shown here in multiple lines because of lack of space. It is actually a single line command and needs to be entered without a line break in between. The database dump has the current timestamp appended to it so that we can identify it easily from other database dumps later on.

The next step is to get the database dump and the site's directory tree to the local server. We try to have the same directory structure at the local server. That is, we copy the website files under some-directory/website and the site's database under some-directory/db-dump at the local server. As a safety measure, you may take a backup of the existing directories, some-directory/website and some-directory/db-dump on the local server and keep it at a safe place before getting the files from the remote server. We use the rsync command to get the files to the local server. The commands are,

$ rsync -avz  user@remote-host:some-directory/db-dump/     /home/local-user/some-directory/db-dump  
$ sudo rsync -avz --delete user@remote-host:some-directory/website/     /home/local-user/some-directory/website

The first rsync command gets the database dump. The second command synchronizes the local website directory with the site's root directory on the live server. sudo is required so that none of the files get missed on account of permissions discrepancy between the local server and the remote server. –delete option deletes the unnecessary files on the local server, so that, at the end, we get an exact replica of directory tree of the remote server on the local server.

The reason for using rsync is ensure that only the differences from the source files is transferred and the precious network bandwidth usage is optimized. However, once the two directory trees on the remote server and the local server are synchronized, we can take a backup of the local server and create a tarball of the server directory tree.

$ cd /home/local-user/some-directory/website 
$ tar -cvzf ../db-dump/website-`date +%F-%H%M`.tar.gz .

With this, we have two files in /home/local-user/some-directory/db-dump, website-timestamp.tar.gz and my-website-db-timestamp.sql. These two files have the website's backup. website-timestamp.tar.gz has the site's entire root directory structure and my-website-db-timestamp.sql has the MySQL database dump for the site. Both the files have an embedded timestamp in the file name, so that we can identify the backup later on.

In order to test the backup, we can load the downloaded database dump in MySQL database on the local server,

$ cd some-directory/db-dump 
$ mysql -u root -p 
mysql> drop database website-drupal-database;   
mysql> create database website-drupal-database;    
mysql> grant all on  website-drupal-database.* to 'website-drupal-user'@'localhost' identified by “Drupal-user's-mysql-password”; 
mysql> flush privileges; 
mysql> quit    
$     
$ mysql -u website-drupal-user -p   
mysql> use  website-drupal-database    
mysql> source my-website-db-timestamp.sql;      
mysql> quit    

You can locally define a domain local-mywebsite.com by modifying the localhost entry in the /etc/hosts,

127.0.0.1       localhost www.local-mywebsite.com local-mywebsite.com

Next you can copy the web server's configuration for your domain from the remote server to the local server, replacing the domain name with local-mywebsite.com. After restarting the web server, you can try to open local-mywebsite.com in the browser. This way, you can test whether the backed up database dump and the site's root directory tree work or not.

Lastly, open the page /admin/config/media/file-system in the browser. Correct the Private file system path, if necessary. Also, ensure that the package php5-mysql is installed.

See also

Share

Karunesh Johri

Software developer, working with C and Linux.