How to use SSH to Back Up Your WordPress Website Hosted on a Linux VPS

The default directory path for websites in Linux Servers is usually./var/www/html/ We use this path for demonstration purposes on how to backup websites by archiving/compressing using tools like tar, gzip and copying to a remote server using SCP (Secure Copy) and/or SSH (Secure Shell).

In this tutorial, we back up WordPress site hosted on Centos 7 VPS Server and copy to remote Ubuntu 16.04 Server using scp and ssh.

Special note: consult HostAdvice's  Best Linux hosting services page to find the leading web hosts in this category, including expert and user reviews.

The site’s files are located in the /var/www/html/ directory.

We archive the files to the user’s home directory. There are several ways to go about this.

Option One: Archive and Copy to Remote Server in Two Steps

Create the tar archive file from the site directory

$ tar –czvf demositebackup.tar.gz /var/www/html/*

Check and confirm the backed up files:

$ tar –tf demositebackup.tar.gz

Copy to remote Ubuntu 16.04 Server.

$ scp –i ~/.ssh/id_rsa demositebackup.tar.gz linuxuser@ubuntu16-hostadvice:/backups

In case you don’t have ssh keys configured but can log in via password, use the command below:

$ scp demositebackup.tar.gz linuxuser@ubuntu16-hostadvice:/backups

[Enter the password for the remote user when the prompt appears]

The demositebackup.tar.gz has been copied to directory /backups in the remote ubuntu16 server. Ensure the following before running the command:

  1. The remote user, in this case, “linuxuser” being logged in via SSH has write permissions to the /backups directory, otherwise it will throw a “Permission denied” error.
  2. There should be no file in the destination directory with a similar name to the archived website backup file, otherwise the destination file will be automatically overridden.

Option Two: Archive and Copy to Remote Server in One Step

$ tar -czvf - /var/www/html/* | ssh linuxuser@ubuntu16-hostadvice "cd /home/linuxuser/backups 
; cat > demositebackup.tar.gz"

Log into the remote server and confirm that the backup file demositebackup.tar.gz has indeed been copied successfully.

In case you need the backup to be copied and extracted at a go, use the command below:

$ tar -czvf - /var/www/html/* | ssh linuxuser@ubuntu16-hostadvice "cd /home/linuxuser/backups ; tar -xzvf -"

You will see folder var created in the remote server’s

/home/linuxuser/

Check out the top 3 WordPress hosting services:

Was this article helpful?