This rsync backup tutorial walks through a setup many VPS owners end up needing: a second server that holds a usable copy of the first one. You will copy your website files, configs, and databases to another VPS, keep daily snapshots that only store changed files, and run the whole thing on a schedule. The first full copy takes the longest. After that, each run moves only what changed, so nightly backups stay fast and cheap on disk. The 3-2-1 backup rule for servers explains why one copy on the same machine is not a backup at all, and why the second copy should live somewhere else.
rsync backup tutorial setup: two servers and SSH access
You need two servers. The source is the live VPS you want to protect. The target is a second VPS with enough disk space for the first full copy plus several daily snapshots. A cheap storage VPS works well for the target because it needs little CPU or RAM. Both servers should run Linux, and you should have root or sudo access on both. The commands here use a dedicated backup user on the target server, not root, and they assume Ubuntu or Debian paths.
Decide what to back up before you run anything: the web root, usually /var/www, the Nginx or Apache config, and database dumps. Do not copy a live database directory while MySQL or MariaDB is running. Dump it first, then copy the dump file. The same rule applies to Docker volumes. Our guide to backing up Docker volumes automatically covers that case if your stack runs in containers.
Set up SSH key authentication for rsync
Password logins break automation, so set up ssh key authentication for rsync first. On the source server, create a key pair for the backup job if you do not have one yet. Do not reuse your personal login key. A separate key makes it easy to revoke backup access later without touching your own access.
ssh-keygen -t ed25519 -f ~/.ssh/id_backup -C "backup-to-vps2"
ssh-copy-id -i ~/.ssh/id_backup.pub [email protected]
Replace backup with your backup username and 203.0.113.10 with the target IP address. Test the login before you go further:
ssh -i ~/.ssh/id_backup [email protected] "mkdir -p /home/backup/backups && echo ok"
If the test prints ok, the key works. If you are new to this step, how to set up SSH keys on a new VPS covers key creation, permissions, and the common mistakes that lock people out. Keep the private key on the source server only, set its permissions to 600, and never copy it to the target.
Your first manual rsync backup
Run the first copy by hand so you can see what rsync does and catch errors early. The command below copies /var/www to the target server over SSH. The -a flag keeps permissions, timestamps, and ownership, -v prints what it copies, and -z compresses the transfer. Use --delete only when you are sure the target folder should mirror the source, because it removes files on the target that no longer exist on the source.
rsync -avz -e "ssh -i ~/.ssh/id_backup" /var/www/ [email protected]:/home/backup/backups/www/
The first run copies every file, so it can take a while on a slow connection. When it finishes, check that the files arrived:
ssh -i ~/.ssh/id_backup [email protected] "du -sh /home/backup/backups/www && ls /home/backup/backups/www | head"
If the sizes look right and the folder names match the source, the manual run works. Do not automate a command you have not watched succeed at least once.
Make backups incremental with –link-dest
A plain rsync mirror gives you one copy, and that copy changes every night. If a file gets corrupted or deleted on the source, the next backup overwrites the good copy before you notice. That is why rsync link-dest snapshots matter. Each daily run creates a new folder that looks like a full copy, but unchanged files are hard links to the previous day, so they take almost no extra disk space.
Create a backup script on the source server, for example /usr/local/bin/backup-to-vps2.sh:
#!/bin/bash
set -e
DATE=$(date +%Y-%m-%d)
SRC=/var/www/
[email protected]:/home/backup/backups/www
LATEST=$(ssh -i /root/.ssh/id_backup [email protected] "ls -d /home/backup/backups/www/*/ 2>/dev/null | sort | tail -n 1")
rsync -avz --delete
-e "ssh -i /root/.ssh/id_backup"
--link-dest="$LATEST"
"$SRC" "$DEST/$DATE/"
The first run has no previous snapshot, so rsync copies everything. The second run links unchanged files to the first folder and copies only the changes. After a week you have seven folders that each look complete, and the total disk use is close to one full copy plus a week of changes. This is the core of incremental rsync backups to a second vps: a full-looking snapshot every day, at the cost of one changed file at a time. Add a database dump to the same script before the rsync line if you run WordPress or another database-backed site.
Exclude files you should never copy
Not every file belongs in a backup. Cache folders, logs, and temporary files add size and slow the transfer for no benefit. Write an exclude file and pass it to rsync with --exclude-from. Create /etc/backup-excludes.txt:
cache/
*.log
tmp/
node_modules/
vendor/
*.tmp
Then add --exclude-from=/etc/backup-excludes.txt to your rsync command. Test the exclude list on a manual run and check that nothing you need went missing. A common mistake is excluding a folder that holds user uploads, so do not exclude /var/www/html/wp-content/uploads. If you would rather send backups to object storage instead of a second VPS, backing up a VPS to S3-compatible storage with restic shows a different tool for the same job.
Automate rsync backups with cron
Once the manual runs work, automate rsync backups with cron so they happen every night without you. Use a quiet time, for example 02:30. Edit the root crontab on the source server:
crontab -e
Add this line, adjusted to your script path:
30 2 * * * /usr/local/bin/backup-to-vps2.sh >> /var/log/backup-to-vps2.log 2>&1
The log line matters. A cron job that fails silently is worse than no job, because you only find out when you need the backup. Check the log the next morning for the new dated folder. After a week, add a cleanup step that removes snapshots older than 30 days:
ssh -i /root/.ssh/id_backup [email protected] "find /home/backup/backups/www -maxdepth 1 -type d -mtime +30 -exec rm -rf {} +"
Be careful with that cleanup line. Test it with -print instead of -exec rm -rf first, and confirm it lists only the old dated folders. A wrong path in a delete command can wipe the whole backup set.
Test a restore before you need one
A backup you have never restored is a guess. Pick a quiet time and restore one file to a test location on the source server. Do not restore over the live site for a test. Copy a file from yesterday snapshot back to /tmp/restore-test/ and compare it with the live version:
rsync -avz -e "ssh -i ~/.ssh/id_backup" [email protected]:/home/backup/backups/www/2026-11-09/index.html /tmp/restore-test/
diff /tmp/restore-test/index.html /var/www/index.html && echo "files match"
Then test a full-folder restore to a staging folder, and a database restore on a spare database, not the live one. Write down the steps that worked. Repeat the test every month or after any big change. If the restore test fails, fix the backup script now, while the live site still works.
Common rsync problems and fixes
Most rsync failures fall into a few groups. Permission denied usually means the SSH key is wrong, the key file has loose permissions, or the backup user cannot write to the target folder. Run the SSH test again and check the key path. A trailing slash changes what rsync copies: /var/www/ copies the contents of the folder, while /var/www without the slash copies the folder itself. Pick one form and use it every time so your snapshots line up.
Slow transfers often come from many small files rather than a slow link. Compression with -z helps for text and code, but it adds little for images and video that are already compressed. Disk full on the target is the other common failure, so check free space before the first run and keep the 30-day cleanup in place. If you outgrow a single VPS target, compare Backblaze B2 vs Hetzner Storage Box vs Wasabi, and check official pricing before you pick one.
Frequently asked questions
How much disk space does the target VPS need?
Start with at least twice the size of the data you back up, to leave room for the first full copy, a month of changed files, and a restore test.
Can I back up a database with rsync alone?
No. Copying a live database directory can give you a broken dump. Dump the database to a file first with mysqldump or pg_dump, then let rsync copy that file.
How often should I run the backup?
Nightly fits most sites. If you publish or take orders through the day, run it twice a day or add a midday run for the database dump only.