How to compress your website htdocs folder into a ZIP file for backup and migrations

Backing up your website periodically is a very important procedure to ensure stability and ability to roll back your site to a previous snapshot in case something goes wrong. Backing up your website root directory (In Apache setups its called htdocs) into a zip file will make it easy to do so.

To Backup you website root directory, navigate to the webserver folder, this is often located at one of these locations:

Once inside the root foler you can use the zip command to compress the all files and folders using the following command:

zip -r backup.zip *

This method will output a zip file with a backup of all your website assets. To download the file access www.yoursite.com/backup.zip or use an SFTP client like FileZilla to download the zip file to your local storage.

Certain configurations like Bitnami images use symlinks in the root directory to reach other directories in other paths, take the following example:

lrwxrwxrwx  1 bitnami bitnami    29 Jul 18 00:10 wp-content -> /bitnami/wordpres
lrwxrwxrwx  1 bitnami daemon     32 Jul  7 15:59 wp-config.php -> /bitnami/wordpress/wp-config.php

When running zip commands in a folder with symlinks, it will automatically copy the destination file/folder to your directory, which leads to a variety of 404 errors and server issues reaching files added to the destination folder post migration. To zip files and respect original symlinks use this syntax instead:

sudo zip -r backup.zip * --symlinks

Now when you try to unzip this folder it will have the symlinks or shortcuts as-is with no modification.

To Unzip the zip file into local development environment or to a new server, upload the zip file to the new web server root directory, the use this command:

unzip backup.zip