How to Use RSYNC to Back Up Your Data on Linux


The most terrible thing that can happen to any of us is losing important data and not being able to restore it. A backup copy of the data must be made to prevent this issue. Data backups are used to retrieve important data that has been lost and are copies of critical data that are saved on your devices, such as computers, phones, or tablets.

You can lose your data through variety of ways such as hard disc failure, ransomware. Whatever the catastrophe, having a data backup might provide you the peace of mind you need to recover the information on your devices. It is often stored in a secure location that is separate from the originating device, like the cloud.

Using “rsync” is one method we employ in Linux to back up our data. In this article on Ubuntu 22.04, we back up data using the rsync method.

Use rsync for Back up

Newer versions of Ubuntu now pre-install rsync; however, if it is not, execute the following command to install it:

$ sudo apt install rsync

Use rsync for Local Backups

We generate a backup between two folders on the same disc using this method. Although all these folders share a hard disc, they would still work just as well if they were on different drives.

$ rsync -av --delete linux_directory1 linux_directory2 

Use-Rsync-Backup-Data

With this command, Linux directory1 and Linux directory2 will be backed up. The contents of Linux directory1 and Linux directory2 will be in perfect sync. RSync will delete a file in Linux directory2 if it finds one there but not in Linux directory1. Rsync will duplicate changes made to a file in Directory1 in Linux directory2 if it finds that the file has been modified, added, or removed.

Verification

To determine if a data backup has been generated or not, run the command shown below to list the contents of the Linux directory2.

$ ls linux_directory2

Use-Rsync-Backup-Data

Use rsync to Automate Backup

Cron is a tool used on Linux for automating the use of commands like rsync. On our Linux system, we may use Cron to schedule backups to run every few hours or as often as you’d like. Run the command listed below to modify the cron table.

$ crontab -e

Use-Rsync-Backup-Data

I hit the Enter key and the number 1 on the keyboard to open the “nano” editor and edit a file. Minutes, hours, days of the week, months of the year, and commands make up Cron’s syntax. Every night at precisely 11 PM, the command below will execute rsync to automatically generate a backup of Linux directory1 in Linux directory2.

 0 23 * * * rsync -av --delete /home/linux/Documents/linux_directory1 /home/linux/Documents/linux_directory2

Use-Rsync-Backup-Data

Since we want this command to execute every day and the “0” represents minutes of 23, “23” represents 11, and “11” represents zero. Press escape, Ctrl+S to save your changes after making them, then Ctrl+X to close the file.

Conclusion

When utilizing computers, it’s crucial to create a backup copy of your crucial data. In this post, we explored using “rsync” to backup data on Ubuntu. Different tools are used for backing up data in Linux, particularly useful for transferring files from nearby and distant networks is the rsync program. Following the precise instructions in this article will help you avoid the problem of data loss.

Print Friendly, PDF & Email
Tags