How to backup your website files using ssh. Avoid losing hours of work through website files getting trashed by hackers, server failure or user screw up. Here’s how to back up your site to a safe place.
Avoid losing hours of work through website files getting trashed by hackers, server failure or your own screw up. Perhaps you would like to make a clone of your website on your PC to try out some new plugins or scripts. Whatever the case, here’s how to back up your site to a single file.
This guide assumes
- Your sitename is mysite.com Replace that with your real site name whenever you see it further on.
- You have ssh access to your web hosting accounts. If you don’t have it, get it because it’s vital to achieve full control over the files of your website.
- The server address of the webhost of site to be backed up is: host.server1.com Replace that with your real host’s address whenever you see it further on.
- You have another hosting account where it’s server address is: host.server2.com Replace that with your real host’s address whenever you see it further on. If you don’t have a second hosting account then you’ll have to skip the step “Copy Backup to Another Server”
- Your site root folder is: ~/html/mysiterootfolder/ Replace that with your real site’s root folder path whenever you see it further on.
-
Start ssh
Start an ssh session to your website host and change to the user home directory.
ssh host.server.com
cd ~ -
Make a Backup file
Use tar on the source server to make a compressed copy of all your site files with the command…
tar -czvf ~/archive/mysiterootfolder-yyyy-mm-dd.tar.gz ~/html/mysiterootfolder/
A quick explanation of the above command…
tar runs the program which compresses files
-zcvf are the options selected when running the tar program
c = create a compressed file
z = use gunzip compression
v = verbose (output all the program’s actions while it’s running)
f = create a file
The first path (~/archive/……) is the path of the compressed file you wish to create
The second path (~/mysiterootfolder/……) is the path to the folder you want to make a compressed copy of. -
Duplicate the Backup File Elsewhere
Duplicate the backup file and then to be even safer, triplicate the file to different servers. It’s no good making a backup of your website files to just keep the backup file on the same server your website resides upon. If the server fails, then you potentially lose you website and the backup. Download the file to your local PC and if possible copy it to another server or the cloud using Dropbox or a similar service.
Copy Backup to PC
Use an ftp program such as FileZilla or setup a new network location in windows to copy the file to a local drive on your PC.
Copy Backup to Another Server Using rsync
Execute rsync on the source server to copy the compressed backup file from the original server to your second hosting server with the command
rsync -azv --partial-dir ~/rsync-partial ~/archive/mysiterootfolder-yyyy-mm-dd.tar.gz user@host.server2.com:~/archive/
Execute rsync on the destination server to copy the compressed backup file from the original server to your second hosting server with the command
rsync -azv --partial-dir ~/rsync-partial username@host.server.1.com:~/archive/mysiterootfolder-yyyy-mm-dd.tar.gz ~/archive/
A quick explanation of the above commands…
rsync runs the program which transfers the files
-azv –partial-dir are the options used to make rsync behave in the desired way
a = specifies that any sub-directories and nearly all files are included
z = compress file to speed up the transfer
v = verbose (output all the program’s actions while it’s running)
partial-dir = creates a temporary copy of the file being transferred in the specified directory which can be resumed from if the transfer is interrupted part way. The directory is automatically resumed once the transfer is complete. This saves have to start the transfer again from scratch if it has previously failed.
Create an archive folder.
Make a new folder to store your backup files with the command…
mkdir archive
And there you have it. Your website files should be safely backed up in multiple locations.