April 29, 2025 - 09:14
Data Synchronization and Backup with rsync in Linux Image
Linux

Data Synchronization and Backup with rsync in Linux

Comments

In Linux, rsync is one of the most effective tools for data backup and synchronization.

Fast, secure, and efficient in file transfer, rsync facilitates data transfer between local and remote servers. In this article, I will explain the basic usage of the rsync command, backup strategies, and automatic backup methods.


1. What is rsync and Why is it Used?

rsync (remote sync) is a Linux tool that provides fast and efficient file synchronization between two locations. It detects file changes and only transfers the modified parts, thus saving bandwidth and processing time.

Uses of rsync

  • File synchronization between local and remote servers
  • Automated backups on daily, weekly, or specific intervals
  • Optimizing large data transfers
  • Secure backup of system and project files

2. Basic File and Directory Synchronization with rsync

1️⃣ Local Directory Synchronization

Copy a directory to another location on the same machine:

BASH
rsync -av /source_directory/ /destination_directory/

This command copies the contents of /source_directory/ to /destination_directory/.

Parameter Explanations:
  • -aArchive mode (preserves permissions, timestamps, and symbolic links)
  • -vGives verbose output

To synchronize only modified files:

BASH
rsync -avu /source_directory/ /destination_directory/

This method detects modified files and performs synchronization efficiently.

2️⃣ Synchronizing Files to a Remote Server

Transfer files from one server to another:

BASH
rsync -avz /source_directory/ user@remote_server:/backup_directory/

This command synchronizes the files to the remote server's directory.

Additional Parameters:
  • -zCompresses data for faster transfer.
  • -e sshSecures the connection over SSH.

Use rsync without a password prompt using SSH key authentication:

BASH
rsync -avz -e 'ssh -i /home/user/.ssh/id_rsa' /source_directory/ user@remote_server:/backup_directory/

This method automates the login process and improves security.


3. Automatic Backup with rsync (Crontab Integration)

For automatic backups, you can use rsync with crontab.

1️⃣ Daily Automatic Backup

To back up the /var/www/html directory every night at 02:00:

BASH
0 2 * * * rsync -avz /var/www/html/ backup_user@server:/backup/html/

This task will run automatically at the scheduled time.

2️⃣ Weekly Backup of a Specific Folder

To back up the /home/user directory every Sunday at 01:00:

BASH
0 1 * * 0 rsync -avz /home/user/ backup@server:/backup/home/

This command will back up the specified directory every Sunday at 01:00.


4. Security and Optimization in rsync Usage

1️⃣ Encrypting Data Transfer

To make rsync transfers secure, transfer over SSH is required:

BASH
rsync -avz -e ssh /source/ user@server:/destination/

This method encrypts the file transfer, enhancing security.

2️⃣ Optimizing Large Data Transfers

To send only new or changed files:

BASH
rsync -avz --progress --ignore-existing /source/ user@server:/destination/

This command skips existing files, reducing processing time.

3️⃣ Synchronizing Deleted Files

To delete files at the destination that have been removed from the source:

BASH
rsync -avz --delete /source/ user@server:/destination/

This method ensures that deleted files from the source are also removed from the destination.


5. Example Usage Scenarios

Usage Scenario Command
Local directory backuprsync -av /home/user/ /backup/
SSH backup to remote serverrsync -avz -e ssh /data/ backup@remote:/backup/
Daily automatic backup0 2 * * * rsync -av /home/ backup@server:/backup/
Optimizing large data transferrsync -avz --progress --ignore-existing /source/ /dest/
Synchronizing deleted filesrsync -avz --delete /source/ /dest/

Related Articles

Comments ()

No comments yet. Be the first to comment!

Leave a Comment