How to Use Rsync to Make a Remote Linux Backup – JumpCloud

Go to tutorial

We can’t stress enough the importance of having a backup. Natural disasters, cyberattacks, or other devastating events can happen when you least expect them. To be on the safe side, it is always recommended to have system backups twenty-four hours a day to ensure business continuity in the event of a service interruption.

One of the most useful backup utilities for Linux systems is the rsync utility. Rsync, short for remote synchronization, is a data transfer and synchronization tool that intelligently transfers and synchronizes files between directories or between networked computer systems. This is achieved by comparing file sizes and modification times. If the file size and modification times are different, transfer the files from the directory or system hosting the files to another remote directory or system.

Rsync is configured to transfer and synchronize data securely via the SSH protocol. File synchronization is done immediately, and with the proper backup test process, you can rest assured that you have a safe and accurate backup.

In a previous tutorial, we covered how to make local backups using rsync. In this guide, we will go a step further and demonstrate how to make a remote backup, i.e. your data is stored on a separate machine, using the rsync utility.

Prerequisites

When you get started, make sure you have the following:

  1. SSH is installed and running on both the local and target servers. Most likely, the SSH daemon is already installed and no further action is required.

To check the version of SSH you are running, run the following command

:

$ ssh -V

Also
  1. , you need two Linux servers: the source or local server and the remote server. Here’s the lab setup we’ll use to demonstrate how rsync works

: Local server IP: 173.82.120.115 (

Ubuntu 20.04) Remote server IP: 173.82.255.207

(CentOS 8)

  1. Finally, make sure you have a local user configured with sudo privileges.

Step 1: Install Rsync on the local

server To get started, make sure rsync is installed. In this example, we will install rsync on the local server (Ubuntu 20.04) as follows

: $ sudo apt install rsync

Once installed, start and enable the rsync service. $ sudo systemctl start rsync $

sudo systemctl enable

rsync

To confirm that rsync is installed, run the command:

$ rsync -version

The following result confirms that we have

rsync installed:

Step 2: Install and configure

rsync on the destination server

In addition to installing rsync on the source or local server, we also need to install it on the destination server or in the cloud. To install rsync on CentOS 8, use the DNF package manager as follows

: $

sudo dnf install rsync rsync-daemon

Once installed, confirm that it is installed with the following:

$ rpm -qi rsync

<img src

=”https://jumpcloud.com//wp-content/uploads/2022/02/remote-rsync-5.png”

alt=”” />

Next, you must configure rsync to allow remote connections from the source or local server. To do this, create a configuration file as follows:

$ sudo vim /etc/rsync.conf

Then paste the following lines into the configuration file. The path directive specifies the path to the destination backup directory, while the hosts allow directive indicates the IP address of the source server.

# add to final

pid file = /var/run/rsyncd.pid log file = /var/log/

rsyncd.log

max connections = 4

#

log transfer results or not transfer

logging = yes

#

any name you like

[backup]

#

target directory to copy

path = /home/

user/backup

# hosts you allow to

access hosts allow = 173.82.120.115 hosts

deny = *

list = true

uid

= root

gid

=

root

read only = false

Save and exit.

Then, start and enable the rsync service.

$ sudo systemctl start rsyncd $ sudo systemctl enable rsyncd

And confirm that the rsync daemon is running.

$

sudo systemctl status rsyncd

If

SELinux is enabled, configure the Boolean option as follows:

$

setsebool -P rsync_full_ access on

Next, configure the firewall to allow the rsync service

: $ sudo firewall-cmd –

add-service=rsyncd -permanent $ sudo

firewall-cmd -reload

Now let’s put our configuration to the test and see if we can successfully back up data from the source server to the remote server.

Step 3: Test the configuration

To test the file backup process, log back in to the source server. We already have a directory in our home folder that contains some files that need to be backed up.

To save and synchronize files remotely, rsync takes the following syntax

: $ sudo rsync -avz -e ssh SOURCE_DIRECTORY DESTINATION_IP::backup

Where

:

SOURCE_DIRECTORYis the directory to be backed up

.

DESTINATION_IPis the IP address of the remote or destination server.

In our example, the full command will be

:

$ sudo rsync -avz -e ssh

/home/jumpcloud/data/

[email protected]:

/home/user/backup

The directory to be backed up to the local or source server is the /home/jumpcloud/data/ folder and the destination backup directory is the /home/user/backup folder. You can create your own source and destination directories in different paths as you see fit.

Since the output of the command execution, the files were successfully transferred to the remote server. This is proof that the file transfer worked.

Step 4: Automate the

backup process

By default, rsync does not have a built-in automation process for backing up and synchronizing files without user intervention. Fortunately, we can automate the backup process by creating a shell script with the backup command and automating the script to run at specific times using a cron job.

But first, we need to set up passwordless SSH authentication between the local and remote server, as rsync uses SSH to initiate a connection between the two and transfer files securely.

Therefore, generate an SSH key pair as follows:

$ssh-keygen

This generates a public and private key which are cryptographic keys that are stored in the .ssh directory.

Now, we need to copy the public key to the remote server to enable passwordless SSH authentication. To do so, we will use the

ssh-copy-id command as follows:

$ ssh-copy-id [email protected]

When prompted, authenticate with the remote user’s password. The public key is saved on the authorized_files in the .ssh directory of the home folder of the remote server.

To verify that password authentication has been disabled, we will attempt to log in to the remote server normally as shown.

$ ssh [email protected]

<img src

=”https://jumpcloud.com//wp-content/uploads/2022/02/remote-rsync-11.png” alt=”” />

Now, let’s create a shell script that will contain the

backup command.

$ sudo vim backup.sh

The first line begins with a shebang header, a signature of all shell scripts, followed by the backup command

.

#!/bin/bash

rsync -avz -e ssh /home/jumpcloud/data/ [email protected]’173.82.255.207′:/home/user/backup

Save the script and exit. Next, make the shell script executable:

$ chmod +x backup.sh

The last step is to automate the execution of the script using a cron job. To create a cron job, run the command:

$ crontab

-e

This opens the crontab file. At the bottom of the file, add the following

line: 05

22 * * * /home/jumpcloud/backup.sh

The line stipulates that the script will run exactly at 10:05 p.m.

Save the script and exit. You should see output indicating that a new crontab is being installed.

You can list cron jobs using the command:

$ crontab -l

To simulate file synchronization, we added two additional files in our data directory on our source server. When the clock struck 10:05 p.m., synchronization occurred as evidenced in the output when we listed the contents of the backup directory.

This time, you can see that additional files were added to the backup directory.

We even verified that the synchronization occurred by viewing the /

var/log/syslog log file. $ cat /var/log/

syslog log

Conclusion

We have successfully enabled remote file backup using rsync. With rsync, you can back up an entire home directory or any directory of your choice to a remote server. If you are working on a system that has constantly changing data, it is advisable to configure the backup to be performed at shorter time intervals, as long as this does not impede network speed and interrupt working users during business hours.

Interested in other strategies you can use to ensure the security of your Linux system? Check out one of the recommended tutorials here.

How to Enable Full Disk Encryption on an Ubuntu 20.04 Desktop How to

  • Create and Manage SSH Keys
  • on Linux Machines How to Manage

  • Sudo Access in Ubuntu 20.04 How to Manage User
  • Passwords on Linux Machines