How to back up and restore Drupal 8 (Part 1)

This is the third chapter of the Drupal 8 Theming: Getting Started series. In this chapter you are going to learn how to back up your Drupal 8 site.

Before starting theming your site, it is of primary importance that you know how to back up the data you are working with. As with Drupal 7, in Drupal 8 it means that you have to save a copy of all the files that make up your site as well as another copy of the database your site is working with.

In the first chapter of this series, you can read about how to create a database for Drupal 8.

Backing up and restoring the Drupal 8 directory

Although there are a few different ways of backing up a directory (and all the files and directories within it), in this chapter I am going to address only two of them. The first one is about a piece of software named 7-zip (which is available for both Windows and Unix-like systems). And the second one is about the tar command, which is the one you will probably use with Unix-like systems. Let's begin!

7-zip

With 7-zip, first you create a tar archive and then you compress it with your selected format. For example, if you wanted to create a .tar.bz2 file (tar with bzip2) of your Drupal 8 directory, you would have to follow these steps:

1. Open a command line window and then change the directory to the 7-zip directory. In my case, the absolute path of the 7-zip directory is "C:\Program Files\7-zip".

cd "C:\Program Files\7-zip"

The quotation marks are required because Program and Files are separated with a blank space.

2. Run the following command:

7z a -ttar C:\Users\xus\Downloads\tmp\drupal8_backup.tar C:\xampp\htdocs\d8basics\

to create a .tar archive with all the files and subfolders of your Drupal 8 folder in the tmp directory. Note that the second absolute path is the location of your Drupal 8 folder.

3. Run the following command:

7z a -tbzip2 C:\Users\xus\Downloads\tmp\drupal8_backup.tar.bz2 C:\Users\xus\Downloads\tmp\drupal8_backup.tar

to create a .tar.bz2 archive with all the files and subfolders of your Drupal 8 folder in the tmp directory. Note that the second absolute path is the location of your .tar file.

If you want to use gzip instead of bzip2, replace -tbzip2 with tgzip and .tar.bz2 with .tar.gz

As you can see in the previous image, after using bzip2 compression, the file size has become much smaller (from 72 MB to 13 MB).

In order to extract your backup file, follow these steps:

1. Run the following command:

7z x C:\Users\xus\Downloads\tmp\drupal8_backup.tar.bz2 -oC:\Users\xus\Downloads

to extract (extract with full paths) all the files and subfolders from the .tar.bz2 file to the Downloads folder.

2. Run the following command:

7z x C:\Users\xus\Downloads\drupal8_backup.tar -oC:\Users\xus\Downloads

to extract (extract with full paths) all the files and subfolders from the .tar file to the Downloads folder.

3. If everything is OK, now there should be a subfolder in "Downloads" named d8basics containing all the files and subfolders of your Drupal 8 site. Let's check it out:

I am always running the 7z executable file from “C:\Program and Files\7-zip” because I haven’t added this path to the system environment variable PATH. If you want to run 7z from everywhere in the system, add “C:\Program and Files\7-zip” to the system environment variable PATH.

The previous commands can be rewritten in a more efficient manner by using the -si (Read Data from StdIn), -so (Write Data to StdOut) switches and the command redirection operator pipe (|). For example, you can also decompress the previous .tar.bz2 backup file with the following command:

7z x C:\Users\xus\Downloads\tmp\drupal8_backup.tar.bz2 -so | 7z x -si -ttar -oC:\Users\xus\Downloads

For more information about the 7-zip commands, please click me

Tar command

If you are using a UNIX-like system (like Linux), then you can take advantage of the tar command like this:

Say you are using Linux and you are located in your home folder. Within this folder, there is a folder named “web” which is your Drupal 8 site. If you wanted to backup this folder, that is, to create a .tar.bz2 file (tar with bzip2) and save the .tar.bz2 archive in “tmp”, which is another subfolder of your home directory, you would have to follow this step:

1. Be sure you are located in your home folder and then run the following command:

tar -jcvf ./tmp/FullCopy_web_`date + %d%b%y`.tar.bz2 ./web

In this example, the `date + %d%b%y` part is a Linux command that is contained in another command. It is enclosed between reverse quotation marks in order to use its output with the tar command. In this way you can add the current date to the name of the file of your backup.

If you want to use gzip instead of bzip2, replace -jcvf with -zcvf and .tar.bz2 with .tar.gz.

In order to extract your backup file, follow this step:

1. Run the following command:

tar -jxvf ./tmp/backupfile.tar.bz2 -C ./Downloads

to extract all the files and subfolders from the .tar.bz2 file to the Downloads folder of your home directory.

Replace -jxvf with -zxvf to extract a .tar.gz file.

For more information about the tar command, type man tar at your linux terminal.