How To Use Rsync to Sync Local and Remote Directories
How To Use Rsync to Sync Local and Remote Directories

Synchronizing directories between the server and the local system rsync

File synchronization is one of the most important operations in data storage, backup, and server management. The rsync tool is a powerful, flexible, and optimized option for this task. This guide will show you how to synchronize local and remote directories (on the server) with rsync, use it for backups, and automate transfers.
0 Shares
0
0
0
0

1. Prerequisites

To execute rsync Between the local system and a remote server you need:

  • Two devices: one local (your computer) and one Linux server (VPS or dedicated server)

  • rsync Installed on both systems

  • SSH access between two systems (so you can communicate without constantly entering passwords)

  • SSH public key on the destination server in the file authorized_keys to be placed


2. The concept and function of rsync (specialized and understandable version for beginners and professionals)

rsync is a powerful command line tool in Linux that is used for Synchronization (Sync) and Data transfer It is used between two paths, whether these paths are on the same system or on two separate servers.

But why is rsync so popular?
Because of that Instead of transferring entire files, it transfers only the parts that have changed.

How exactly does rsync work? (Simple and precise understanding)

When you run rsync, the tool first:

  1. Structure and content of the two tracks (e.g., destination folder and source folder) compares.

  2. Checks Which files are new, which have changed, and which have been deleted or unchanged?.

  3. Instead of sending the entire file, just Part of the data that has changed Transfers.

This technique is called:

Delta Transfer Algorithm

This rsync algorithm Very fast and efficient It does, because:

  • The bandwidth consumed is reduced.

  • The transfer time is much shorter.

  • CPU resources on the server are less involved


Simple example to better understand rsync functionality

Let's say you have a 1GB file and only 5MB of it has changed.

  • Common tools such as SCP-1811 They re-upload the entire 1GB.

  • But rsync only sends the same 5MB that changed..

This means:

  • More speed

  • Less internet usage

  • Less pressure on the server


rsync and servers: Why it's the best tool for server management?

When on a Server Or VPS You work, you usually need:

  • Move backups between two servers

  • Move site files from the current server to the new server

  • Always keep important folders (like /var/www) synced.

  • Transfer large files without wasting bandwidth

In all these cases, rsync is the best choice because:

Secure transfer with SSH

rsync uses the SSH protocol for transfers, so:

  • Data is encrypted.

  • No need to open additional ports

  • Communication security is guaranteed.

Delete extra files at the destination

With flag --delete It can keep the destination exactly the same as the source.

Resume feature

If the internet connection drops during the transfer, rsync will transfer the rest of the file, not from scratch.

Suitable for Automation

It fits easily into cronjobs and creates automated backup processes.


Real example for better understanding:

If you want to sync the following path from server A to server B:

rsync -avz /var/www/ user@server-ip:/var/www/

This command:

  • Folder changes only /var/www Transfers

  • Updates it at the destination.

  • Ensures secure transmission via SSH


A simple summary for someone new to rsync:

FeatureExplanation
Main applicationData transfer and folder synchronization
Main advantageOnly changes are transferred, not the entire file.
SecurityUsing SSH for secure transfers
High speedBecause of the delta transfer algorithm
Professional and server-friendlyThe best tool for backup and migration between VPS

3. Basic rsync syntax

General structure rsync Much like other file tools like SCP-1811 is:

rsync [OPTIONS] منبع مقصد

Simple local example:

  • Create two test folders on the local system

  • Implementation:

    rsync -a dir1/ dir2

    In this case, only the content dir1 To dir2 It is moved, not the folder itself.

Option -a (archive) is recommended because:

  • Recursively synchronizes the directory

  • Preserves file ownership, modification time, and permissions

Important: If you put a “/” at the end of the source path, only the contents of the directory will be synced; if you don’t, the original folder itself will be transferred.


4. Run a secure preview with --dry-run

Before performing sensitive operations (especially when using options such as --delete you use), it's best to do a dry run first:

rsync -anv --delete /مسیر/منبع/ /مسیر/مقصد/
  • Option -n Or --dry-run Simulates operations without making changes

  • -v (verbose) Provides detailed output of files being synced or deleted.

This preview helps you see exactly what's going to happen before you delete files and avoid risky mistakes.


How to use Rsync to synchronize local and remote directories
How to use Rsync to synchronize local and remote directories

5. Push and Pull files between the local system and the server

Send to server (Push)

To sync a local directory to the server:

rsync -a ~/local-dir/ user@remote-server:/remote/path/
  • -a: Preserve file attributes

  • ~/local-dir/: Local route

  • user@remote-server: User and server address

  • :/remote/path/: Path on the server

If "/" is at the end of the source, only the contents of the folder are sent; otherwise, the folder itself is also transferred.

Receive from the server (Pull)

Reverse operation: Synchronization from server to local system:

rsync -a user@remote-server:/remote/path/ ~/local-dir/

Again, it's important to note the slash in the source path (here the server path) because it determines the synchronization behavior.


6. Advanced options rsync

  • -z: Compress data when sending to reduce network usage

  • -P: A combination of --progress and --partial Which allows displaying the progress of the transfer and allowing incomplete transfers to be continued

Example:

rsync -azP ~/local/dir/ user@remote:/remote/dir/
  • --delete: Delete files that exist in the destination but no longer exist in the source (suitable for mirroring)

  • --exclude and --include: Filter files/folders for more precise interaction

  • --backup and --backup-dir: Create a backup of deleted files in a separate path


7. Schedule synchronization with Cron

To run a periodic sync, you can rsync in cron Schedule:

  • Edit crontab:

    crontab -e
  • Example of a daily cronjob for synchronization:

    0 3 * * * /usr/bin/rsync -a --delete /var/www/html/ user@remote:/backups/html/

    This command runs every day at 3 AM and synchronizes the local directory with the server version.

Important recommendations for cron-job:

  • From absolute paths to rsync and use directories

  • Setting up passwordless SSH key login

  • Save cron output to a log file or handle errors


8. Troubleshooting common problems and best practices

Some common problems when using rsync And its solutions:

  • Error in source/destination path: Use of --dry-run For detailed review

  • Unwanted deletion of files: Only when we are absolutely sure of --delete Let's use

  • Permissions errors: with -a Maintain ownership and permissions.

  • Automation problems: Use full paths and passwordless SSH authentication


Conclusion

rsync It is a very powerful and optimized tool for synchronizing data between the local machine and the server. Thanks to the delta-transfer algorithm, this tool is a low-power, secure and accurate option for backup, data transfer and creating mirror environments. Using advanced options, experiment with --dry-run and scheduling with cronYou can optimize, secure, and automate the synchronization process.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like