How to Use rsync for File Transfers on Linux Print

  • rsync, file transfer
  • 0

rsync is a powerful command-line tool for syncing and transferring files between local and remote systems. It’s efficient, reliable, and commonly used for backups, migrations, and server-to-server file transfers.

 

This guide will show you how to use rsync for various file transfer scenarios on your VPS.

 


 

Why Use rsync?

• Transfers only changed parts of files (saves bandwidth and time)

• Preserves file permissions, ownership, timestamps, and symbolic links

• Works over SSH for secure remote transfers

• Ideal for backups and scheduled syncs via cron

 


 

Basic Syntax

rsync [options] source destination

 

 


 

Example 1: Copy Files from Local to Remote VPS

rsync -avz /path/to/local/files/ root@your_server_ip:/path/to/remote/

-a = archive mode (preserves permissions, timestamps, etc.)

-v = verbose

-z = compress data during transfer

/ at the end of the source directory ensures contents are copied (not the folder itself)

 


 

Example 2: Copy Files from Remote VPS to Local Machine

rsync -avz root@your_server_ip:/path/to/remote/files/ /local/backup/

 

 


 

Example 3: Sync Between Two Remote Servers

rsync -avz -e ssh root@server1:/var/www/ root@server2:/var/www/

This command transfers files directly from one VPS to another over SSH.

 


 

Example 4: Exclude Files or Directories

 

To exclude specific files or folders during transfer:

rsync -avz --exclude 'node_modules/' /project/ root@your_server_ip:/project/

You can also use a file listing patterns:

rsync -avz --exclude-from='exclude.txt' /src/ root@your_server_ip:/dest/

 

 


 

Example 5: Delete Files on Destination That No Longer Exist on Source

 

To make an exact mirror:

rsync -avz --delete /src/ root@your_server_ip:/dest/

Be careful—this will delete files on the destination that are not present on the source.

 


 

Example 6: Dry Run Before Sync

 

Preview what would be transferred or deleted:

rsync -avz --delete --dry-run /src/ root@your_server_ip:/dest/

 

 


 

Common rsync Options

Option

Description

-a

Archive mode (preserves everything)

-v

Verbose output

-z

Compress data

--delete

Delete files at destination not in source

--progress

Show progress during transfer

-e ssh

Use SSH explicitly

 

 


 

Automate rsync with Cron

 

You can schedule regular syncs using cron. Example:

crontab -e

Add a line:

0 2 * * * rsync -az /data/ root@your_server_ip:/backup/ >> /var/log/rsync.log 2>&1

This syncs data daily at 2:00 AM.

 


 

Conclusion

 

rsync is a versatile tool ideal for backups, migrations, and syncing files between systems. With SSH integration and intelligent file comparison, it’s one of the most efficient ways to manage file transfers on a Linux VPS.

 

If you need help setting up automated rsync tasks or troubleshooting file sync issues, contact Hosteons Support or access your server via https://vps.hosteons.com.

 


Was this answer helpful?

« Back