{"id":59467,"date":"2022-02-18T09:45:00","date_gmt":"2022-02-18T14:45:00","guid":{"rendered":"https:\/\/jumpcloud.com\/?p=59467"},"modified":"2023-02-01T15:36:45","modified_gmt":"2023-02-01T20:36:45","slug":"how-to-use-rsync-remote-backup-linux-system","status":"publish","type":"post","link":"https:\/\/jumpcloud.com\/blog\/how-to-use-rsync-remote-backup-linux-system","title":{"rendered":"How to Use the Rsync Utility to Make a Remote Backup of a Linux System"},"content":{"rendered":"\n
Jump to Tutorial<\/a><\/p>\n\n\n\n We cannot emphasize 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 round-the-clock system backups to ensure business continuity in case of service interruption.<\/p>\n\n\n\n One of the more useful backup utilities for Linux systems is the rsync utility. Rsync, short for remote sync, is a data transfer and synchronization tool that intelligently transfers and synchronizes files between directories or across networked computer systems. It achieves this by comparing file sizes and modification times. If the file size and modification times are different, then it transfers the files from the directory or system that hosts the files to another directory or remote system. <\/p>\n\n\n\n Rsync is configured to securely transfer and synchronize data over the SSH protocol. The file synchronization happens immediately, and with the proper backup testing process in place, you can rest assured that you have a safe, accurate backup. <\/p>\n\n\n\n In an earlier tutorial, we covered how to make local backups using rsync<\/a>. In this guide, we will go a step further and demonstrate how to make a remote backup \u2014 i.e., your data is stored in a separate machine \u2014 using the rsync utility.<\/p>\n\n\n\n As you get started, ensure you have the following:<\/p>\n\n\n\n To check the version of SSH you are running, run the following command:<\/p>\n\n\n\n Local Server IP: 173.82.120.115 (Ubuntu 20.04)<\/strong><\/p>\n\n\n\n Remote Server IP: 173.82.255.207 (CentOS 8)<\/strong><\/p>\n\n\n\n To start off, ensure that rsync is installed. In this example, we will install rsync on the local server (Ubuntu 20.04) as follows:<\/p>\n\n\n\n Once installed, start and enable the rsync service.<\/p>\n\n\n\n To confirm that rsync is installed, run the command:<\/p>\n\n\n\n The output below confirms that we have rsync installed:<\/p>\n\n\n\n In addition to installing rsync on the source or local server, we also need to install it on the destination server or cloud server<\/a>. To install rsync on CentOS 8, use the DNF package manager as follows:<\/p>\n\n\n\n Once installed, confirm it is installed with the following:<\/p>\n\n\n\n Next, you need to configure rsync to allow remote connections from the source or local server. To do so, create a configuration file as follows:<\/p>\n\n\n\n Then paste the following lines in the configuration file. The <\/p>\n Save and exit.<\/p>\n\n\n\n Then start and enable the rsync service.<\/p>\n\n\n\n And confirm the rsync daemon is running.<\/p>\n\n\n\n If SELinux is enabled, configure the Boolean setting as follows:<\/p>\n\n\n\n Next, configure the firewall to allow rsync service:<\/p>\n\n\n\n Now let\u2019s put our setup to test and see if we can successfully back up data from the source server to the remote server.<\/p>\n\n\n\n To test the file backup process, log back into the source server. We already have a directory in our home folder containing a few files that need to be backed up.<\/p>\n\n\n\n To save and sync files remotely, rsync takes the following syntax:<\/p>\n\n\n\n Where:<\/p>\n\n\n\n In our example, the full command will be:<\/p>\n\n\n\n The directory to be backed up on the local or source server is the From the output of running the command, the files were transferred successfully to the remote server. This is proof that the file transfer worked.<\/p>\n\n\n\n By default, rsync does not have a built-in automation process for backing up and syncing 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.<\/p>\n\n\n\n But first, we need to configure passwordless SSH authentication between the local and remote server since rsync uses SSH to initiate a connection between the two and securely transfer files.<\/p>\n\n\n\n Therefore, generate an SSH keypair as follows:<\/p>\n\n\n\n This generates both a public and private key which are cryptographic keys that are saved in the Now, we need to copy the public key to the remote server to enable passwordless SSH authentication. To do so, we will use the When prompted, authenticate with the remote user\u2019s password. The public key is saved in the To verify that password authentication has been disabled, we will try to log in to the remote server normally as shown.<\/p>\n\n\n\n Now, we are going to create a shell script that will contain the backup command.<\/p>\n\n\n\n The first line starts with a shebang header \u2014 a signature of all shell scripts \u2014 followed by the backup command.<\/p>\n\n\n\n Save the script and exit. Next, make the shell script executable:<\/p>\n\n\n\n The last step is to automate the running of the script using a cron job. To create a cron job, issue the command:<\/p>\n\n\n\n This opens the crontab file. At the very bottom of the file, add the following line: <\/p>\n\n\n\n The line stipulates that the script will run at exactly 10:05 p.m. <\/p>\n\n\n\n Save the script and exit. You should see the output indicating that a new crontab is being installed.<\/p>\n\n\n\n You can list the cron jobs using the command:<\/p>\n\n\n\n To simulate the file synchronization, we added two additional files in our This time around, you can see that additional files were added to the backup directory.<\/p>\n\n\n\n We even verified the synchronization occurred by viewing the We have successfully enabled remote file backup using rsync. With rsync, you can back up an entire home directory or any directories of your choice to a remote server. If you are working on a system that has constantly changing data, it\u2019s advisable to set the backup to take place at shorter time intervals as long as this does not impede network speed and disrupt users working during business hours.<\/p>\n\n\n\n Interested in learning about other strategies you can use to ensure the security of your Linux system? Check out one of the recommended tutorials here.<\/p>\n\n\n\n Need to back up your Linux system? Learn how to make a remote backup using the rsync utility in this tutorial.<\/p>\n","protected":false},"author":150,"featured_media":59469,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_oasis_is_in_workflow":0,"_oasis_original":0,"_oasis_task_priority":"","inline_featured_image":false,"footnotes":""},"categories":[23],"tags":[],"collection":[2778],"platform":[],"funnel_stage":[3017],"coauthors":[2535],"acf":[],"yoast_head":"\nPrerequisites<\/h2>\n\n\n\n
\n
$ ssh -V<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
\n
\n
Step 1: Install Rsync on the Local Server<\/h2>\n\n\n\n
$ sudo apt install rsync <\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
$ sudo systemctl start rsync <\/code><\/p>\n\n\n\n
$ sudo systemctl enable rsync <\/code><\/p>\n\n\n\n
$ rsync –version<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
Step 2: Install and Configure Rsync on the Destination Server<\/h2>\n\n\n\n
$ sudo dnf install rsync rsync-daemon<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
$ rpm -qi rsync<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
$ sudo vim \/etc\/rsync.conf<\/code><\/p>\n\n\n\n
path<\/code> directive specifies the path to the destination backup directory while the
hosts allow<\/code> directive indicates the IP address of the source server.<\/p>\n\n\n
# add to the end<\/code><\/p>\n
pid file = \/var\/run\/rsyncd.pid<\/code><\/p>\n
log file = \/var\/log\/rsyncd.log<\/code><\/p>\n
max connections = 4<\/code><\/p>\n
# log transfer results or not<\/code><\/p>\n
transfer logging = yes<\/code><\/p>\n
<\/code>
# any name you like<\/code><\/p>\n
[backup]<\/code><\/p>\n
# target directory to copy<\/code><\/p>\n
path = \/home\/user\/backup<\/code><\/p>\n
# hosts you allow to access<\/code><\/p>\n
hosts allow = 173.82.120.115<\/code><\/p>\n
hosts deny = *<\/code><\/p>\n
list = true<\/code><\/p>\n
uid = root<\/code><\/p>\n
gid = root<\/code><\/p>\n
read only = false<\/code><\/p>\n\n\n
$ sudo systemctl start rsyncd<\/code><\/p>\n\n\n\n
$ sudo systemctl enable rsyncd<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
$ sudo systemctl status rsyncd<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
$ setsebool -P rsync_full_access on<\/code><\/p>\n\n\n\n
$ sudo firewall-cmd –add-service=rsyncd –permanent<\/code><\/p>\n\n\n\n
$ sudo firewall-cmd –reload<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
Step 3: Test the Configuration<\/h2>\n\n\n\n
$ sudo rsync -avz -e ssh SOURCE_DIRECTORY DESTINATION_IP::backup<\/code><\/p>\n\n\n\n
SOURCE_DIRECTORY<\/code>is the directory to be backed up.<\/p>\n\n\n\n
DESTINATION_IP<\/code>is the IP address of the remote or destination server.<\/p>\n\n\n\n
$ sudo rsync -avz -e ssh \/home\/jumpcloud\/data\/ user@173.82.255.207:\/home\/user\/backup<\/code><\/p>\n\n\n\n
\/home\/jumpcloud\/data\/<\/code> folder and the destination backup directory is the
\/home\/user\/backup<\/code> folder. You can create your own source and destination directories in different paths as you deem fit.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
Step 4: Automate the Backup Process<\/h2>\n\n\n\n
$ ssh-keygen<\/code><\/p>\n\n\n\n
.ssh<\/code> directory.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
ssh-copy-id<\/code> command as follows:<\/p>\n\n\n\n
$ ssh-copy-id user@173.82.255.207<\/code><\/p>\n\n\n\n
authorized_files<\/code> in the .
ssh<\/code> directory on the remote server\u2019s home folder.<\/p>\n\n\n\n
$ ssh user@173.82.255.207<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
$ sudo vim backup.sh<\/code><\/p>\n\n\n\n
#!\/bin\/bash<\/code><\/p>\n\n\n\n
rsync -avz -e ssh \/home\/jumpcloud\/data\/ user@’173.82.255.207′:\/home\/user\/backup<\/code><\/p>\n\n\n\n
$ chmod +x backup.sh<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
$ crontab -e<\/code><\/p>\n\n\n\n
05 22 * * * \/home\/jumpcloud\/backup.sh<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
$ crontab -l<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
data<\/code> directory in our source server. When the clock ticked 10:05 p.m., the synchronization happened as evidenced in the output when we listed the contents of the backup directory.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
\/var\/log\/syslog log<\/code> file.<\/p>\n\n\n\n
$ cat \/var\/log\/syslog log<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
Conclusion<\/strong><\/h2>\n\n\n\n
\n