How to Perform a Remote Linux Backup Using SSH

Secure Shell or SSH is responsible for successful network communication between two remote computers. For a Linux system administrator, this networking tool is the perfect solution for remote server/machine access over unsecured networks. For you to initiate and complete data backup under any Linux platform, you need to achieve two things:

  • Secure network access to the remote machine with data you wish to backup.
  • Secure data transfer mechanism to move your targeted data to a specified remote machine or backup directory.

Since this article objectifies remote data backup using SSH, it is important that the data transfer mechanism we choose to go with has undisputed support for SSH network protocols.

SCP for Secure Data Transfer

SCP (Secure Copy) is a reputable data transfer mechanism between two remote machines. Before data transfer takes place between the two remote machines, a Linux administrator has to be able to comfortably use one machine (local) to access the other machine (remote).

[ You might also like: How to Rsync Files Between Two Linux Servers Automatically ]

SCP first accomplishes local to remote machine access through the SSH network protocol before initiating any data transfer. With SSH protocol, access to a remote machine requires system username and password authentication.

This remote server access can be accomplished with a command implementation similar to the following:

$ ssh ubuntu@18.118.208.XX 
Connect to Remote Linux
Connect to Remote Linux

From here, the user attempting remote access is required to key in a user password associated with the username (ubuntu) before remote server access is authenticated.

[ You might also like: How to Pass Password to SCP Command in Linux ]

However, this article recommends passwordless access to your remote machine/server through generated SSH key pairs that exist on both the local machine and remote machine.

Connect to Remote Linux Without Password

On the local computer, generate the needed SSH key with the following command:

$ sudo ssh-keygen -t rsa 

On the resulting prompt, remember to skip the Enter passphrase: step by hitting [Enter] on the keyboard.

Create SSH Key in Local Linux
Create SSH Key in Local Linux

The remote server needs a copy of the SSH key.

$ sudo ssh-copy-id ubuntu@18.118.208.XX
Copy SSH Key in Remote Linux
Copy SSH Key in Remote Linux

Now connect to remote Linux server without a password SSH access.

$ sudo ssh ubuntu@18.118.208.XX

You should automatically gain access to the remote server via SSH.

Connect to Remote Linux Without Password
Connect to Remote Linux Without Password

SCP Remote Linux Backup via SSH Protocol

Before you backup data to/from a remote server, make sure you are on the correct directory path on the local machine and that you are also familiar with the directory structure on the remote/server machine.

On the local machine:

$ pwd
$ ls 
Listing Local Linux Files
Listing Local Linux Files

On the server/remote machine:

$ pwd
$ ls 
Listing Remote Linux Files
Listing Remote Linux Files

To perform SCP remote Linux backup via the SSH protocol, we would implement the following command syntax:

Backup Local Directory to Remote Linux

$ sudo scp -r path/to/local/directory/with/useful/data user@remote_server_ip:/path/to/remote/backup/directory

The above command syntax translates to the following:

$ sudo scp -r /home/dnyce/LinuxShellTips ubuntu@18.118.208.79:/home/ubuntu/LinuxShellTips_Backup
Backup Local Directory to Remote Linux
Backup Local Directory to Remote Linux

From the above command, we have successfully backed up a local machine directory to a remote machine directory by implementing the SCP tool kit with SSH keys.

Backup Remote Directory to Local Linux

To create a backup from the remote server to your local machine, the syntax to use will look like the following:

$ sudo scp -r user@remote_server_ip:/path/to/remote/directory/with/useful/data path/to/local/backup/directory/ 

The implementation of the above syntax translates to the following:

$ sudo scp -r ubuntu@18.118.208.79:/home/ubuntu/LinuxShellTips_Backup /home/dnyce/LinuxShellTips 
Backup Remote Directory to Local Linux
Backup Remote Directory to Local Linux

Whether you are after local-to-remote or remote-to-local backup solutions, SCP’s inheritance of SSH keys and network access protocols makes remote data backup effortless.

2 thoughts on “How to Perform a Remote Linux Backup Using SSH”

Got something to say? Join the discussion.