Support Online
Skip to main content

Synchronizing Local and Remote Directories with Rsync: Detailed Guide

🎯 What You Will Learn in This Guide

In this guide, you'll learn how to synchronize files and folders between local and remote systems using the powerful command line tool called Rsync.
Thanks to the delta transfer algorithm, Rsync copies only the changed sections; This provides significant savings in both time and bandwidth.
You'll see step by step how to create a secure structure for backup, directory mirroring, and automated tasks.


🧠 Phase 1 – Content Summary

Main Technical Topic: Rsync (Remote Sync) command line tool.
Purpose: To synchronize files quickly, efficiently and securely between local and remote systems.
Highlighted Features:

  • Delta algorithm that transfers only changed blocks
  • Secure transfer via SSH
  • Automation (cron)
  • Safe testing with --dry-run, mirroring with --delete

Brief Summary:
File synchronization, backup and automatic task management operations are performed using Rsync on Linux-based systems.
This guide is optimized for both beginners and system administrators.


⚙️ Rsync Installation and Requirements

Rsync comes pre-installed on most Linux distributions. If not installed:

sudo apt install rsync

Requirements:

1 local system (running environment)

1 remote server (e.g. tr1-node01 on GenixNode)

Rsync and SSH must be enabled on both sides

Passwordless SSH key authentication must be performed


🧩 1. Understanding Rsync Syntax

The basic command structure of rsync is similar to other tools (ssh, scp, cp):


rsync [bayraklar] kaynak hedef

Creating a sample environment:


cd ~
mkdir dir1 dir2
touch dir1/dosya{1..100}
Arşiv Modu (-a)
Rsync’te en çok önerilen bayrak -a (archive)’dır.

This mode copies files recursively and preserves metadata such as permission, ownership, timestamp.


rsync -a dir1/ dir2

📝 Explanation: dir1/ carries only the content, while dir1 carries the directory itself.

This difference (trailing slash) determines how the file structure will be formed on the target.


🧪 2. Test Commands First (--dry-run)

Use --dry-run or -n to simulate rsync commands before actual execution.


rsync -anv dir1/ dir2

🧠 Why is it important?

It allows you to test before deleting operations such as --delete.

Shows how to copy files without modification.


🌐 3. Remote Server Operations (Push & Pull)

Push: Local to Remote


rsync -a ~/dir1 genixnode@tr1-node01:~/yedekler

💡 Result: The ~/backups/dir1/ directory is created on the remote server.

Pull: Remote to Local Transfer


rsync -a genixnode@tr1-node01:/var/log/nginx/ ~/yedekler/nginx-log

💡 Result: Files on the server are safely copied to the local directory.


⚡ 4. Improving Performance

To speed up transfer of large data:


rsync -azP kaynak hedef

-z: Enables compression (especially effective on text files)

-P: Shows transfer progress and continues in case of interruption


🧹 5. Mirroring (--delete)

To remove files from the target that are not in the source:


rsync -a --delete kaynak/ hedef/

⚠️ Warning: This command performs permanent deletion. Test with --dry-run first.


🚫 6. Exclude (--exclude)

To exclude specific directories or files from copying:


rsync -av --exclude='node_modules/' --exclude='*.tmp' proje/ yedek/

Or you can read the items to exclude from a file:


rsync -av --exclude-from='exclude.txt' kaynak/ hedef/

💾 7. Backup (--backup)

To keep older versions of updated or deleted files:


rsync -a --delete --backup --backup-dir=/yedek/arsiv/ /kaynak/ /hedef/

⏰ 8. Auto Rsync with Cron

Add Rsync command to cron task for regular backups:


crontab -e

Example cron line that will run every day at 03:00:


0 3 * * * /usr/bin/rsync -a --delete /var/www/ornek.com/ genixnode@tr1-node01:/srv/yedekler/genixnode-web/ > /dev/null 2>&1

💡 Tip:

Use full paths (/usr/bin/rsync)

Set up a passwordless SSH connection

Redirect output to log file


💡 Frequently Asked Questions (FAQ)

1. Why is rsync faster than scp?

Because Rsync copies only the changed sections with the delta algorithm. scp sends the entire file each time.

2. Is the directory or its content copied?

The trailing slash / if present only the content is copied, otherwise the directory is included.

3. How do I prevent accidental file deletion?

Simulate possible operations by running the command with --dry-run first.

4. Why does a permission error occur?

Usually there is no write permission on the target directory or read permission on the source. Use the -a flag.

5. How do I keep old files?

Move old versions to a separate folder with the --backup and --backup-dir parameters.


🚀 Result

Rsync; It is one of the most powerful tools for efficient synchronization, secure backup and automatic task scheduling. Now you can easily mirror your data, run error-free tests with --dry-run, and fully automate with cron.

💡 Instantly try Rsync synchronization on GenixNode infrastructure — for a safe, fast and hassle-free backup experience. 🚀