Today we’re introducing a new type of post at Lateral Code: the simple snippet. These articles are meant to provide you with concise, meaningful snippets of code for use on your website. To kickoff off the simple snippet, we begin with an important topic for any site: backups.
Have you ever lost important files? If you have, you know the importance of keeping backups.
Backups could mean the difference between life and death for your site. It’s vital to constantly generate backups and even store them on a remote machine in order to ensure full protection. All it takes is a determined attacker to possibly corrupt all of your files. As a result, this simple snippet is focused on getting you started with the backup process.
Generating backups through secure-shell (SSH) can be quite a simple task. The tar
command is all you’ll need:
$ tar czvf backup.tar.gz path/to/directory
Let’s break down this snippet into it’s constituents:
tar - the command to create a tar file
c - the option that creates a tar ball
z - add gzip compression
v - verbose option to display files that are added to the tar ball
f - used to specify the archive file (in this case backup.tar.gz)
backup.tar.gz - the name of the archive file for the f option
path/to/directory - recursively search this directory for files to place in the tar ball
After running this command on the directory of your choice (generally the public or public_html directory for a full backup), you should have an archive of files which you can easily download to your own computer (or keep on your web server).
You can read more about the tar
command here.