VPSWala Blog

How to Rsync Files Between Two Linux Servers Automatically

May 22, 2025, Written by 0 comment

Imagine making one update on a server, and it magically appears on another one—no extra work, no stress. That’s what file syncing does. When you’re managing multiple Linux servers, keeping files up to date across them is not just helpful—it’s critical. It saves time, reduces errors, and keeps everything running smooth as butter.

Table of Contents

Real-Life Scenarios Where Automatic Rsync Can Save the Day

Picture this: you’re running a website hosted on two servers. One crashes. No problem—your second server has the exact same files thanks to rsync. Or maybe you’re a developer pushing updates from a test server to production. Rsync makes it automatic, quick, and drama-free.


Getting to Know Rsync

What Is Rsync and Why Is It So Popular?

Rsync stands for “remote sync.” It’s a tool built into Linux that helps move files from one place to another, fast. But here’s the cool part—it only copies what’s changed. That means less data transfer and faster updates. People love it because it’s reliable, fast, and doesn’t eat up your system resources.

The Perks of Using Rsync for Server Syncing

  • It’s super efficient—only the changed parts of a file are copied.
  • You can sync files locally, over a network, or over the internet.
  • It works well with automation tools like cron.
  • It’s built into most Linux systems already. No need to install heavy stuff.

Prepping the Ground

What You’ll Need Before You Start

  • Two Linux servers (of course)
  • Basic command line access (don’t worry, nothing too wild)
  • SSH access between the servers
  • Rsync installed on both servers

Checking for Rsync on Both Servers

Run this command on both servers:

rsync --version

If you get a version number, you’re good. If not, install it:

sudo apt install rsync # For Debian/Ubuntu
sudo yum install rsync # For CentOS/RHEL

Making Sure You Have SSH Access Set Up

You need to be able to log into the second server from the first one using SSH. Try:

ssh username@remote_server_ip

If it logs you in, you’re ready for the next step.

Also read: How To Backup and Restore Data On VPS Instances?


Setting Up Passwordless SSH Login

Why Passwordless SSH Makes Automation Easier

Automation and typing passwords don’t mix. Passwordless SSH lets your server log into another one without needing your input each time. That’s perfect for cron jobs and scripts.

Generating SSH Keys the Easy Way

On your first server, type:

ssh-keygen -t rsa

Just press Enter through the prompts unless you need special settings.

Copying Your SSH Key to the Remote Server

Send your public key to the remote server:

ssh-copy-id username@remote_server_ip

This step connects the two servers so they can trust each other.

Testing the Connection Like a Pro

Try logging in again:

ssh username@remote_server_ip

No password? Success.


Basic Rsync Commands to Get You Comfortable

Your First Rsync Command Explained

Here’s a simple one:

rsync -avz /home/user/files/ username@remote:/home/user/backup/
  • -a = archive mode (keeps file permissions and timestamps)
  • -v = verbose (shows you what’s happening)
  • -z = compress (makes things faster)

Common Flags and What They Actually Do

  • --delete: deletes files on destination not in source
  • --progress: shows progress bar
  • --exclude: skips certain files or folders

A Quick Peek at Local to Remote and Remote to Local Syncing

Local to remote:

rsync -av file.txt user@server:/path/

Remote to local:

rsync -av user@server:/path/file.txt /local/path/

Structuring Your File Sync Workflow

Choosing What to Sync (And What Not To)

Don’t sync everything. Just the stuff that matters. Config files, logs, web files—yes. Temp folders and cache—nope.

Setting Up Source and Destination Paths Smartly

Use absolute paths when possible. Double-check them. A wrong slash can copy things into the wrong folder or wipe stuff out.

Avoiding Common Mistakes When Syncing Sensitive Files

Don’t forget .env files or secret keys. Use --exclude to skip those. Also, check file permissions to avoid security risks.


Automating Rsync with Cron Jobs

What’s a Cron Job and Why You’ll Love It

Cron is like a robot that runs your command on a schedule. Once set, it just works.

Creating Your First Cron Job for Rsync

Edit the cron file:

crontab -e

Add a line like this:

0 * * * * rsync -az /source/ user@remote:/dest/

This syncs files every hour.

Setting Up Different Frequencies: Hourly, Daily, Weekly

  • Every hour: 0 * * * *
  • Daily at midnight: 0 0 * * *
  • Weekly: 0 0 * * 0

Making Sure Your Cron Job Is Running Smoothly

Check logs:

cat /var/log/syslog | grep CRON

Or add >> /var/log/rsync.log 2>&1 at the end of your cron command to log everything.


Using Shell Scripts for More Control

Writing a Simple Rsync Shell Script

#!/bin/bash
rsync -az /source/ user@remote:/dest/

Save it as sync.sh, then make it executable:

chmod +x sync.sh

Adding Logging and Notifications

rsync -az /source/ user@remote:/dest/ >> /var/log/rsync.log
mail -s "Rsync Complete" [email protected] < /var/log/rsync.log

Combining Cron + Script for a More Powerful Setup

Set your cron to run the script:

0 * * * * /path/to/sync.sh

Now you’ve got logs, email alerts, and automation. Sweet.


Handling Errors Like a Pro

How to Know When Something Goes Wrong

Use the --log-file option to keep a record:

rsync -az --log-file=/var/log/rsync.log /source/ user@remote:/dest/

Using --log-file to Track Rsync Activity

Check that file anytime to see what synced and what didn’t.

What to Do When Files Aren’t Syncing Properly

  • Check your paths
  • Look for permission errors
  • Try running the command manually with -vv for more detail

Syncing Specific File Types or Directories

Only Syncing Images, Documents, or Code

Use --include:

rsync -av --include='*.jpg' --include='*.png' --exclude='*' /source/ user@remote:/dest/

Excluding Folders You Don’t Want to Touch

rsync -av --exclude='node_modules/' /source/ user@remote:/dest/

Using Include/Exclude Filters to Fine-Tune Syncing

Get specific:

rsync -av --exclude='*.tmp' --exclude='backup/' /source/ user@remote:/dest/

Keeping Things Secure

Making Sure Your Data Is Safe in Transit

Rsync over SSH encrypts your data. Always use -e ssh.

Best Practices for SSH Key Management

  • Don’t share private keys
  • Use a strong passphrase (or none if using automation—but store safely)
  • Restrict SSH access to specific IPs

Hardening Your Rsync and SSH Setup

  • Disable root login on the server
  • Use firewalls
  • Monitor login attempts with fail2ban

Bi-Directional Syncing: Is It Worth It?

Understanding One-Way vs Two-Way Sync

One-way: Source to destination only.
Two-way: Both servers send updates to each other. Tricky business.

Tools and Scripts That Help With Two-Way Sync

Rsync isn’t great at two-way. Try tools like:

  • Unison
  • Syncthing

Risks and Conflicts to Watch Out For

What if the same file changes on both servers? You could lose data. Always back up before trying two-way syncing.


When Rsync Isn’t Enough

Limitations of Rsync in Larger Environments

Rsync is great for small to medium tasks. But if you have thousands of files changing all the time—it can get slow.

Introducing Rsync Alternatives Like Unison or Syncthing

  • Unison does two-way sync with conflict management
  • Syncthing is real-time, decentralized, and works even on different OSes

How to Know When It’s Time to Upgrade

If rsync becomes a bottleneck, you’re troubleshooting too much, or need real-time syncing—it might be time to move on.


Real-World Use Cases

Syncing Web Server Files Across Regions

Keep your website fast for everyone by syncing assets across regional servers.

Keeping Backup Servers Up to Date

Nightly rsyncs can ensure your backup is always current—no surprises when you actually need it.

Using Rsync for Development and Deployment Workflows

Push code from staging to production without breaking a sweat. Version-controlled and reliable.


Troubleshooting Common Rsync Issues

SSH Permission Denied? Here’s What to Do

  • Check your SSH keys
  • Make sure the user has access
  • Look at the server’s /var/log/auth.log

Rsync Is Skipping Files – Let’s Fix That

  • Look for --exclude or typo in path
  • Check if files are hidden (use -a to catch them)

Dealing with Timezone or Timestamp Conflicts

Use --times to preserve timestamps. Also, sync clocks using NTP.


Advanced Tips for Power Users

Speeding Things Up with --compress and --partial

Use -z for compressing files in transfer, and --partial to resume broken syncs.

Avoiding Disk Overwrites with --backup and --suffix

rsync -av --backup --suffix='.bak' /source/ user@remote:/dest/

Running Rsync Over a VPN or SSH Tunnel

Extra layer of security. Just route your SSH over a VPN connection and sync like normal.


Final Thoughts

Keeping Your Setup Maintainable

Label your cron jobs. Keep a copy of your scripts. Use version control.

Why Monitoring and Logging Are Just as Important

Logs tell the story. Without them, you’re guessing. Always log your syncs.

What’s Next After Mastering Rsync Automation

Try Unison or Syncthing. Or move into container syncing with Docker volumes. There’s always a new level to reach.

vpswala Admin

Savita Sathe is an experienced writer and editor with over 10+ years of professional experience creating engaging content across industries. Whether it's blogs, whitepapers, website copy, emails, social media posts, or more. She develops effective hosting and tech content that helps brands connect with their audiences and achieve business goals.

Leave a reply

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