Tip: Start typing to get instant search results.
How to Create and Restore a public_html Backup via SSH
The public_html folder, as you may already know, is the main folder of your hosting account, where all the files of your site are located. There are cases where, before making significant changes to your website, you may want to first take a backup, so that you can restore your files to the state they were in before your changes.
Below we present some ready-made examples for creating a compressed backup of the public_html folder via SSH, using the tar command. Note that this backup will contain only the files of your site. For a complete backup you also need a copy of the database.
Checking available space before the backup
Before creating a backup, it is important to make sure there is enough free space on your account/server. If you start creating a backup and your disk fills up, this can cause your website to malfunction.
To see the available space, enter the following command in SSH:
df -h
To see how much space the public_html folder takes up, you can run:
du -hs public_html/
Since we will be using compression, the size of the backup will certainly be smaller than the size of public_html, but it is good to have at least an equal amount of free space for safety.
The examples assume that you are in your user’s home directory. You simply need to run the command below to move to your home directory.
cd
Full backup of public_html
To create a compressed backup of the entire public_html:
tar -czf public_html-backup.tar.gz public_html/
The file public_html-backup.tar.gz will be created inside your home directory.
Excluding a folder from the backup (exclude)
In many cases there is no need to include cache or temporary files in the backup.
For example, to exclude the folder public_html/var/cache, we simply modify the above command as follows:
tar -czf public_html-backup.tar.gz --exclude='public_html/var/cache' public_html/
Excluding multiple folders
Similarly, we can exclude more folders by adding multiple --exclude parameters. This is particularly useful in Magento, where the cache and logs folders can be quite large:
tar -czf public_html-backup.tar.gz --exclude='public_html/var/cache' --exclude='public_html/var/log' public_html/
Checking & Verifying the backup
It is a good idea to confirm that the backup was created correctly. To see the size of the file:
ls -lh public_html-backup.tar.gz
To see a list of the contents of the backup:
tar -tzf public_html-backup.tar.gz | head
Restoring the backup
To restore your files from the backup, first move to your home directory:
cd
Caution: Extracting the tar overwrites existing files with the same name, but does not delete files that already exist in public_html and are not included in the backup.
If you want to restore to exactly the state of the backup, we recommend, for safety, first renaming the existing public_html folder rather than deleting it (renaming will cause the website to be temporarily unavailable until the extraction that follows is completed):
mv public_html public_html-old
Then extract the backup:
tar -xzf public_html-backup.tar.gz
Since the backup was created from the home directory and contains the public_html/ folder, the command will recreate the public_html folder with the files from the backup.
Once you have confirmed that the site is working correctly, you can delete the old folder:
rm -rf public_html-old
Restoring a specific file or folder
If you need to restore only one file (e.g. wp-config.php) without touching the rest, you can extract it individually:
tar -xzf public_html-backup.tar.gz public_html/wp-config.php
Similarly, for an entire subfolder:
tar -xzf public_html-backup.tar.gz public_html/wp-content/themes/
Verification after restoring
After restoring, check that the files exist and that the site loads normally:
ls -lh public_html/
Deleting the backup from the server
Once you are finished using the backup and no longer need it, it is a good idea to delete it from the server, so that it does not take up space unnecessarily:
rm public_html-backup.tar.gz