Linux Directory Mirroring: Securely Sync Files with Rsync and SSH
Need reliable Linux directory mirroring? Learn how to use the rsync command over SSH for secure data transfer and automated remote backups.
When you need to keep two directories in sync across different servers, the rsync command is the absolute standard. It’s efficient, preserves file permissions, and—most importantly—it plays perfectly with SSH to ensure your data stays encrypted in transit.
I remember the first time I set up a production sync. I tried using scp in a loop, which was a disaster; it didn't handle partial transfers or deletions, and I ended up with a bloated mess of duplicate files. rsync solved that by only moving the bits that actually changed.
The Power of Rsync and SSH
Using rsync over SSH gives you the best of both worlds: the delta-transfer algorithm of rsync for performance and the security of SSH for transport. You aren't just copying files; you’re mirroring them.
To get started, you’ll want to use a command structure like this:
Bashrsync -avz -e ssh /local/path/ user@remote-host:/remote/path/
Here’s why those flags matter:
-a(Archive mode): This is a shortcut for a bunch of flags that preserve permissions, symlinks, timestamps, and ownership. It’s essential for system-level mirroring.-v(Verbose): You’ll want to see what’s happening, especially when you’re debugging a sync script.-z(Compress): This compresses data during the transfer. It’s a lifesaver if you have slow network links or are syncing text-heavy logs.-e ssh: This tellsrsyncto use SSH as the transport layer.
Mirroring Directories with Deletions
The real magic of linux directory mirroring happens when you add the --delete flag. Without it, rsync only adds or updates files. If you delete a file on your source, it stays forever on the destination.
Bashrsync -avz --delete -e ssh /local/data/ user@remote-host:/backup/data/
Warning: Always use a trailing slash (e.g., /local/data/) when you mean the contents of the directory. If you omit the slash, rsync will create the directory itself inside the destination, which often leads to nested folder headaches.
Handling Production Risks
I’ve learned the hard way that running sync commands without a dry run is a recipe for an on-call nightmare. Before you run a command that includes --delete, always test it with the -n or --dry-run flag:
Bashrsync -avzn --delete -e ssh /local/data/ user@remote-host:/backup/data/
This shows you exactly what will be deleted or transferred without actually touching a single file. It’s the single most important safety habit for rsync remote backup workflows.
Comparison: Rsync vs. Alternatives
| Feature | Rsync | SCP | SFTP |
|---|---|---|---|
| Delta Transfer | Yes | No | No |
| Delete Support | Yes | No | No |
| Preserve Metadata | Yes | Partial | Partial |
| Speed | High | Low | Low |
Automating with SSH Keys
If you’re running this as a cron job, you don't want to type your password every time. You should set up SSH key-based authentication so your secure data transfer process runs headless.
- Generate your key:
ssh-keygen -t ed25519 - Copy it to the remote server:
ssh-copy-id user@remote-host
Once that’s done, your rsync commands will execute without manual intervention. If you're managing multiple servers, you might find my guide on Linux SSH Tunneling: A Practical Guide to Port Forwarding useful for securing connections through jump hosts.
FAQ
What happens if the connection drops mid-sync?
rsync is robust. If the transfer fails, simply run the same command again. It will check the checksums of existing files and resume exactly where it left off.
How do I exclude specific files?
Use the --exclude flag. For example, to skip .git directories, add --exclude='.git'.
Is rsync safe for production databases?
Generally, no. Never rsync a live database file (like a MySQL ibdata file) while the service is running, as you'll likely end up with a corrupted backup. Always dump your database to a SQL file first, then rsync that dump.
If you’re struggling with server configuration or need help setting up a robust, hardened environment for these tasks, I offer VPS Server Setup, Deployment & Hardening to take the guesswork out of the infrastructure side.
Next time, I’d probably look into rsync’s --partial and --append flags for even larger file transfers, as they can save hours on massive datasets. It’s all about finding that balance between speed and reliability.