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.
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:
To check the size of the directory you want to back up, in this example, the web document root:
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
.
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.
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).
4. Test Restore
Create a folder /var/www_backups/restore/
to restore the backup to.
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.
Item
Explanation
x
extract tarball
Once the tarball has extracted, list the restore folder.
Once you’ve verified your restore, you can delete the restore folder and continue to the next step.
Website : www.yaser-rahmati.ir
Instagram : https://www.instagram.com/yaser.rahmati/
Linkedin : https://www.linkedin.com/in/yaserrahmati/
Last updated
Was this helpful?