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

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:

A2 Hosting
AU$4.61 /mo
Starting price
Visit A2 Hosting
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.8
  • Pricing
    4.0
Hostinger
AU$4.61 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7
HostArmada
AU$3.84 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0

How To Set Up SSH for an Ubuntu 16.04 VPS From a Linux Client

How to set up ssh private key authentication on an Ubuntu 16.04 server with a Li
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Set Up SSH on an Ubuntu 16.04 VPS or Dedicated Server

This tutorial shows you how to set up secure shell (SSH) on an Ubuntu 16.04 virt
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Set Up SSH for your Ubuntu 18.04 VPS or Dedicated Server

This how-to article shows you how to create a public and a private key for acces
4 min read
Arvind Singh
Arvind Singh
Hosting Expert

How To Transfer Files From a Remote Server to another Remote Server Using SSH

A relatively common situation is requiring the move of a website from one server
4 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top