How To Back Up a Web Server Doc Root?

LPIC-2 [ Yaser Rahmati | یاسر رحمتی ]

It’s important to make frequent automated backups of your web server’s document root should you ever accidentally delete files or suffer a hack.

1. Prepare Backup

There is no particular recommended folder to back up to in Linux so you can choose this yourself. In this guide, we are saving backups to /var/www_backups/. Ideally, you would store these on an external drive or an offsite server, but in this guide, we will focus on creating backups locally.

Begin by creating your backup folder.

[root@rahmatiserver ~]# mkdir /var/www_backups/

Before initiating any backups, make sure you have sufficient disk space to store your backups. To check for available disk space on your system, run:

[root@rahmatiserver ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        909M     0  909M   0% /dev
tmpfs           919M     0  919M   0% /dev/shm
tmpfs           919M   97M  823M  11% /run
tmpfs           919M     0  919M   0% /sys/fs/cgroup
/dev/sda1        33G   12G   20G  38% /
tmpfs           184M     0  184M   0% /run/user/0
[root@rahmatiserver ~]#

To check the size of the directory you want to back up, in this example, the web document root:

[root@rahmatiserver ~]# du -s -h /var/www/html 
642M    /var/www/html
[root@rahmatiserver ~]#

The du command is a standard Linux/Unix command that allows a user to gain disk usage information quickly.

2. Understanding tar

The tar command in Linux is used to create .tar.gz compressed archive files (called “tarballs”) suitable for storing backups. The archive filename and extension can be whatever you want, but we recommend using your domain name followed by .tar.gz. Here is an example of a typical tar command backing up this website’s web document root in /var/www/html to an archive /var/www_backups/yaser-rahmati.ir.tar.gz .

[root@rahmatiserver ~]# tar -cvpzf /var/www_backups/yaser-rahmati.ir.tar.gz -C /var/www/ html

It’s important you get this command right so it might help to break down each part:

Item

Explanation

tar

execute tar as superuser

c

create a new file (overwrites old file)

v

verbose (will show backup progress on screen)

p

preserve permissions (755, 777, etc)

z

compress (compress the archive into a “tarball”)

f

filename

/var/www_backups/yaser-rahmati.ir.tar.gz

the path and name of the backup archive that will be created.

-C = (uppercase C)

change to this directory

/var/www/

the path to the folder above your document root

SPACE (Important!)

html

the name of the actual document root folder

The last four parts here -C /var/www/ html are required to keep your tarball directory structure concise and space before the doc root folder is important.

3. Test Backup

Now, we are backing up this website’s doc root.

[root@rahmatiserver ~]# tar -cvpzf /var/www_backups/yaser-rahmati.ir.tar.gz -C /var/www/ html

Once your command has executed, you should see a list of files being processed by tar.

When done, list the backup folder to make sure your tarball is there. In this example, we’ll check the backup for this website. (the-la option here shows us file permissions and file sizes in human-readable format).

[root@rahmatiserver ~]# ls -lh /var/www_backups/

4. Test Restore

Create a folder /var/www_backups/restore/ to restore the backup to.

[root@rahmatiserver ~]# mkdir -p /var/www_backups/restore/

The command to extract and restore a tarball is very similar to the one used to create one, the only difference being the -x and z parameters, which mean extract and uncompress.

In the example below, we are going to extract yaser-rahmati.ir.tar.gz to our restore folder.

[root@rahmatiserver ~]# tar -xvpzf /var/www_backups/yaser-rahmati.ir.tar.gz -C /var/www_backups/restore/

Item

Explanation

x

extract tarball

Once the tarball has extracted, list the restore folder.

[root@rahmatiserver ~]# ls -l /var/www_backups/restore/

Once you’ve verified your restore, you can delete the restore folder and continue to the next step.

[root@rahmatiserver ~]# rm -r /var/www_backups/restore/

Website : www.yaser-rahmati.ir

Instagram : https://www.instagram.com/yaser.rahmati/

Linkedin : https://www.linkedin.com/in/yaserrahmati/

Last updated

Was this helpful?