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 [email protected]
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.
The remote server needs a copy of the SSH key.
$ sudo ssh-copy-id [email protected]
Now connect to remote Linux server without a password SSH access.
$ sudo ssh [email protected]
You should automatically gain access to the remote server via SSH.
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
On the server/remote machine:
$ pwd $ ls
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 [email protected]:/home/ubuntu/LinuxShellTips_Backup
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 [email protected]:/home/ubuntu/LinuxShellTips_Backup /home/dnyce/LinuxShellTips
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.
Remove ‘sudo‘ from every command because it is not necessary and will place all of the keys under the root account.
I would rather recommend using rsync for remote (and local) backups.
Yes, rsync is very useful in syncing files across Linux servers, here is the guide for the same: How to Rsync Files Between Two Linux Servers Automatically