LOGO

rsync Backup: Securely Back Up Your Linux Data

February 5, 2013
rsync Backup: Securely Back Up Your Linux Data

Understanding Rsync for Data Backup and Synchronization

Rsync is a powerful protocol designed for Unix-like operating systems. It offers exceptional flexibility when it comes to data backup and synchronization tasks.

This utility can be employed locally to create backups of files to alternative directories. Alternatively, it can be configured to synchronize data across networks to remote hosts.

Rsync Installation on Linux

While rsync can be utilized on Windows, it requires the use of ports like Cygwin. This guide will focus on setting up rsync on Linux systems.

The initial step involves installing or updating the rsync client. The specific command depends on your Linux distribution.

  • On Red Hat-based distributions, use: yum install rsync.
  • On Debian-based distributions, use: sudo apt-get install rsync.

Installation Commands

The following image illustrates the command used on Red Hat/CentOS systems. Access as the root user is required, though newer Red Hat distributions may support sudo.

how-to-use-rsync-to-backup-your-data-on-linux-1.jpgThe image below shows the corresponding command for Debian/Ubuntu systems.

how-to-use-rsync-to-backup-your-data-on-linux-2.jpgSuccessfully executing one of these commands will ensure that the rsync utility is available for use on your Linux system.

Leveraging rsync for Local Data Backups

This tutorial begins with a demonstration of backing up files from Directory1 to Directory2. While both directories reside on the same physical drive in this example, the process remains identical when the directories are located on separate drives.

The approach to backups can be tailored to specific requirements. However, for many scenarios, the following command provides a sufficient solution:

$ rsync -av --delete /Directory1/ /Directory2/

This command synchronizes the contents of Directory1 with Directory2, ensuring complete consistency between them. Any files present in Directory2 but absent in Directory1 will be removed. Conversely, any changes – creations, modifications, or deletions – made in Directory1 will be mirrored in Directory2.

rsync offers a wide array of switches to customize its behavior. Let's break down the components of the command used above:

1. -a = This option enables archive mode, encompassing recursive directory traversal, preservation of symbolic links, permissions, modification times, group ownership, owner, device files, and special files.

2. -v = The verbose flag is crucial for monitoring the backup process. It allows you to observe precisely which files are being backed up. Consider the potential risk of a failing hard drive silently deleting files; running rsync with verbose mode can prevent propagating these unwanted deletions to your backups.

3. --delete = This instructs rsync to remove files from Directory2 that do not exist in Directory1. Using this option necessitates the verbose flag to ensure awareness of any deletions.

The following output illustrates the results of backing up Directory1 to Directory2 using the described script. Without the verbose switch, this level of detail would not be available.

how-to-use-rsync-to-backup-your-data-on-linux-3.jpg

As shown in the screenshot, File1.txt and File2.jpg were identified as either new or modified compared to their counterparts in Directory2, and were subsequently backed up. A helpful reminder: the trailing slashes at the end of the directory paths in the rsync command are essential – don't forget to include them.

Further useful switches will be discussed later in this guide. For a comprehensive list of available options, consult the rsync manual page by typing "man rsync" in your terminal.

This concludes the section on local backups. As demonstrated, rsync is a straightforward tool to utilize. Synchronizing data with a remote host over the internet introduces some added complexity, but a simple, secure, and efficient method will be presented shortly.

Leveraging rsync for Offsite Backups

Several configurations are possible when employing rsync for external backups. This guide details a practical, secure, and straightforward method: tunneling rsync connections through SSH. SSH is pre-installed on the majority of servers and numerous client systems, making it ideal for your backup procedures.

We will demonstrate the process of backing up data from one Linux machine to another within a local network. This same methodology applies to scenarios where one host resides on the internet, provided that the appropriate port forwarding is configured on the server-side network devices.

Server Preparation

On the server – the destination for your backups – ensure both SSH and rsync are installed and operational.

# yum -y install ssh rsync

# sudo apt-get install ssh rsync

Beyond the installation of these core utilities, focus on establishing the backup repositories on the server. Also, prioritize the security of your SSH configuration.

Implement a strong, complex password for the user account designated for backups. Consider altering the default SSH listening port (typically 22) as an additional security measure.

Initiating the Backup

The command structure for initiating a backup remains largely consistent with local rsync usage, with additions to facilitate the SSH tunnel.

For instance, to connect as user "geek" to the server at IP address "192.168.235.137" while utilizing the standard switches (-av --delete), execute the following:

$ rsync -av --delete -e ssh /Directory1/ geek@192.168.235.137:/Directory2/

If your SSH server is configured to listen on a non-standard port, you must explicitly specify it within the rsync command. The following example demonstrates this using port 12345:

$ rsync -av --delete -e 'ssh -p 12345' /Directory1/ geek@192.168.235.137:/Directory2/
how-to-use-rsync-to-backup-your-data-on-linux-4.jpgAs illustrated in the image above, the output generated during a network backup closely mirrors that of a local backup. The primary difference lies in the command syntax employed.

Note that the system prompts for a password during the connection. To streamline and automate rsync operations, consider implementing RSA keys to bypass password authentication.

Automated Backups with rsync and Cron

On Linux systems, the Cron utility provides a powerful method for automating tasks, including running rsync for regular backups. This allows for scheduled data replication, such as nightly backups, tailored to your specific needs.

To modify the cron table for the current user, execute the following command:

$ crontab -e

Proficiency with the vi text editor is necessary for editing this file. Initiate insert mode by pressing "I", then proceed with your modifications to the cron table.

Understanding Cron Syntax

Cron employs a specific syntax to define scheduling: minute, hour, day of the month, month, day of the week, and the command to execute.

This structure can initially seem complex. Consider this example, which demonstrates running an rsync command each night at 10:00 PM:

0 22 * * * rsync -av --delete /Directory1/ /Directory2/

Here, "0" indicates the top of the hour, and "22" represents 10 PM. The asterisks in the remaining fields signify that the command should run every day. Following this, the rsync command itself is specified.

Once the configuration is complete, exit insert mode by pressing the Escape key. Then, type ":wq" (without the quotes) and press Enter to save the changes made within vi.

While Cron offers extensive capabilities, this tutorial focuses on basic scheduling. A simple daily or weekly backup routine can be readily implemented using the method described. For comprehensive information, consult the Cron man pages.

  • rsync is a versatile tool for synchronizing files and directories.
  • Cron enables automated task scheduling on Linux.
  • The vi editor is commonly used to manage cron tables.

Implementing automated backups with rsync and Cron provides a robust solution for data protection and recovery.

Additional Capabilities of Rsync

A further advantageous function involves compressing your backups into a zip archive. You'll need to designate the desired location for this zip file, and subsequently utilize rsync to transfer that directory to your designated backup destination. Consider the following example:

$ zip /ZippedFiles/archive.zip /Directory1/ && rsync -av --delete /ZippedFiles/ /Directory2/

This command first gathers the files from Directory1, packages them into /ZippedFiles/archive.zip, and then employs rsync to synchronize that directory with Directory2. Initially, one might anticipate this approach to be inefficient for substantial backups, given that the zip file will be modified with each even minor file alteration.

However, rsync’s core functionality lies in transferring only the altered data. Therefore, if your zip file is 10 GB in size, and a small text file is subsequently added to Directory1, rsync will recognize this as the sole addition—even within the zip archive—and transfer only the few kilobytes of changed data.

Backup Encryption Methods

Several methods exist for securing your rsync backups through encryption. The simplest approach involves implementing encryption directly on the storage device itself – the one receiving your backup files. Alternatively, you can encrypt your files prior to transmission to a remote server or another storage drive, regardless of your backup destination.

Detailed explanations of these encryption techniques will be provided in subsequent articles.

Regardless of the features and options you select, rsync remains a remarkably efficient and adaptable backup solution. Even a basic rsync script can effectively safeguard your data against loss.

Linux Command Reference

File Management

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Process Control

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Network Utilities

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld

FURTHER READING: Recommended Linux Laptops for Developers and Tech Enthusiasts

#rsync#backup#linux#data backup#command line#data protection