How to Use rsync to Sync Files Between Your VPS and Home Computer
If you run a VPS and a home computer, you end up moving files between them all the time. Site backups, photos, project folders, config files. The usual way, dragging files through an SFTP client or zipping and downloading them, works once. It gets old fast. This guide shows how to sync files between VPS and computer with rsync, a command line tool that copies only what changed, works over SSH, and can run on a schedule. Once it is set up, a sync takes one command and a few seconds.
What rsync Does That Plain Copying Cannot
rsync compares the source and the destination before it copies anything. Files that already match stay untouched, so the second sync of a large folder takes seconds instead of minutes. It also keeps file permissions, timestamps, and ownership intact, which matters when you move website files or scripts that need to stay executable.
Another difference is direction. A single rsync command can push files from your computer to the server or pull files from the server to your computer. You decide the direction each time by choosing which side is the source and which is the destination. That sounds simple, and it is, but getting the direction wrong is the most common way people overwrite the wrong copy. Check the command twice before you press Enter, and run it with --dry-run first whenever you are unsure.
Most Linux distributions ship rsync already installed. Check with rsync --version. On Debian and Ubuntu, install it with sudo apt install rsync on both machines if it is missing. macOS includes rsync by default, and Windows users can get it through WSL. The examples in this guide use a Linux home computer, but the commands are the same on macOS.
Set Up SSH Keys Before Your First Sync
rsync talks to your VPS over SSH, so the first step is passwordless SSH access. Typing a password every time a sync runs defeats the point, and it makes scheduled syncs impossible. If you have not set up key login yet, follow our guide on how to set up SSH keys on a new VPS first, then come back here.
The short version: generate a key pair on your home computer with ssh-keygen -t ed25519, then copy the public key to the server with ssh-copy-id user@your-server-ip. After that, ssh user@your-server-ip should log you in without asking for a password. Test this before you try rsync. If SSH key login does not work on its own, rsync over SSH will not work either, and you will be debugging two things at once.
One more detail: rsync needs to know which SSH key to use if you keep several. Add -e "ssh -i ~/.ssh/id_ed25519" to the command, or set the key in ~/.ssh/config for the server so every connection picks it up automatically.
Push Files to Your VPS: The Basic Command
The basic push command looks like this:
rsync -avz ~/projects/my-site/ user@your-server-ip:/var/www/my-site/
Here is what the flags do:
-a(archive) keeps permissions, timestamps, ownership, and symlinks. This is the flag you will use in almost every sync.-v(verbose) prints each file as it copies, so you can see what is happening.-z(compress) compresses data during transfer. It helps on slow connections and for text files, but it adds CPU work, so skip it on a fast local network or for files that are already compressed.
The user@your-server-ip:/var/www/my-site/ part is the remote destination. The colon separates the server from the path. Without the colon, rsync treats the target as a local folder, so a missing colon is a classic typo that copies files to a weird local directory instead of the server.
Before any real sync, add -n or --dry-run and look at the list of files rsync plans to copy. Nothing changes on either side during a dry run. This one habit prevents most accidents:
rsync -avzn ~/projects/my-site/ user@your-server-ip:/var/www/my-site/
When the list looks right, run the same command without -n. The first sync copies everything. Every sync after that copies only new and changed files.
Pull Files Back: Sync Files Between VPS and Computer in Reverse
Pulling files down works the same way, with the source and destination swapped:
rsync -avz user@your-server-ip:/var/www/my-site/ ~/backups/my-site/
This is how you grab a fresh copy of a site before you edit it, or pull server logs and backups to your home computer for safekeeping. The same flags apply, and the same dry run rule applies. A pull with --delete removes files from your local folder that no longer exist on the server, so never run a pull with --delete on a folder that holds files you want to keep. A local folder you use as a backup target should contain only copies from the server, nothing else.
Decide the direction before every sync and say it out loud if it helps: push means local to server, pull means server to local. If you sync the same folder in both directions at different times, you can overwrite newer files with older ones. rsync on its own is a one-way mirror, not a two-way merge. For folders you edit on both sides, read our comparison of Nextcloud vs Syncthing vs Seafile for self-hosted file sync, which covers tools built for that job.
The Trailing Slash Trap and Other Mistakes That Cost Files
The trailing slash on the source path changes what rsync copies, and this trips up almost everyone at least once:
rsync -avz ~/projects/my-site/ user@your-server-ip:/var/www/my-site/
rsync -avz ~/projects/my-site user@your-server-ip:/var/www/my-site/
The first command copies the contents of my-site into /var/www/my-site/. The second command copies the folder itself, creating /var/www/my-site/my-site/. One slash is the difference between a clean deploy and a nested duplicate. The rsync trailing slash rule is simple: a trailing slash on the source means copy the contents, no slash means copy the folder.
The other mistakes worth knowing about:
--deletewithout a dry run. This flag deletes files on the destination that are not in the source. It is what makes a true mirror, and it is also what wipes a folder when the source path is wrong or empty. Always dry run first when--deleteis in the command.- Syncing the wrong direction. Double-check which side is the source. If you are not sure, dry run and read the file list.
- Forgetting
--exclude. Syncing a project folder usually means skippingnode_modules,.git, and cache folders. Add--exclude 'node_modules' --exclude '.git'to keep them out. - Running rsync as root when you do not need to. Files copied as root land on the server owned by root, and your web server may not be able to read them. Sync as the user that owns the files on the destination.
For a fuller treatment of rsync as a backup tool, including incremental backups to a second server, see our rsync backup tutorial for incremental server backups.
Automate It With Cron
A manual sync works, but the real payoff comes when the sync runs on its own. On your home computer or on the VPS, add a cron job that runs rsync on a schedule. Open the crontab with crontab -e and add a line like this:
0 2 * * * rsync -az --delete ~/projects/my-site/ user@your-server-ip:/var/www/my-site/ >> ~/rsync.log 2>&1
This syncs the folder every night at 2 a.m. and appends the output to ~/rsync.log so you can check what happened. The log matters. A cron job that fails silently is worse than no cron job, because you only find out it stopped working when you need the files. Check the log after the first scheduled run, and glance at it once a week after that. This is the simplest form of an rsync cron backup: one line, one folder, one log file.
A few notes on scheduling. Use full paths in cron, since cron runs with a minimal environment and may not find rsync or your SSH key otherwise. If you use a non-default key, add -e "ssh -i /home/your-user/.ssh/id_ed25519" with the full path. Keep --delete out of automated jobs until the manual version has run clean for a while. And if you want the same scheduling habit applied to server checks, our guide on running a cron job that sends a daily server health report shows the pattern with email alerts.
That is the whole setup. One command for a manual sync, one cron line for an automatic one, and a dry run habit that keeps both safe. Start with a test folder, confirm the direction and the trailing slash behave the way you expect, then point rsync at the folders you actually care about.