Linux VPS

Back up and sync files with rsync on Linux

Use rsync to copy, mirror and back up files between folders and servers over SSH, with dry runs, excludes, deletes and a scheduled backup example.

IntermediateLinux VPS3 min read

Rsync copies files locally or between servers and only transfers what changed, which makes it ideal for backups, migrations and keeping two folders in sync. It runs over SSH, so you do not need any extra service. This rsync backup guide shows the commands you will actually use.

Before you start

  • rsync installed on both ends: apt install rsync or dnf install rsync.
  • SSH access to the remote server, ideally with an SSH key so scheduled jobs need no password.
  • Enough free space on the destination (check with df -h).

The basic rsync command

rsync -avh --progress /var/www/site/ /backup/site/
  • -a archive mode: recursive, keeps permissions, owners, timestamps and symlinks.
  • -v verbose, -h human-readable sizes, --progress shows each file.

Note: the trailing slash matters. site/ copies the contents of site; site without a slash copies the folder itself, creating /backup/site/site.

Sync to or from a remote server

Push a local folder to a remote server:

rsync -avz -e "ssh -p 22" /var/www/site/ user@203.0.113.10:/home/user/site-backup/

Pull from a remote server to your machine:

rsync -avz user@203.0.113.10:/var/www/site/ ~/site-backup/

-z compresses data in transit, useful over slow links but not needed on a fast LAN. Change the port in -e "ssh -p 2222" if SSH is not on 22.

Always do a dry run first

Add -n (--dry-run) to see what would change without touching anything:

rsync -avhn --delete /var/www/site/ /backup/site/

This is essential before using --delete.

Mirror with --delete and excludes

rsync -avh --delete --exclude 'cache/' --exclude '*.log' /var/www/site/ /backup/site/
  • --delete removes files from the destination that no longer exist in the source, making an exact mirror.
  • --exclude skips patterns; use --exclude-from=list.txt for many.
  • --partial keeps half-transferred large files so a retry resumes them.
  • --bwlimit=5M caps bandwidth so a backup does not saturate the link.

A plain mirror cannot recover a file deleted yesterday. With --link-dest each day gets its own folder, but unchanged files are hard links, so they use almost no extra space:

#!/bin/bash
SRC=/var/www/
DEST=/backup/snapshots
TODAY=$(date +%F)
rsync -a --delete --link-dest="$DEST/latest" "$SRC" "$DEST/$TODAY" \
  && ln -sfn "$DEST/$TODAY" "$DEST/latest"
find "$DEST" -maxdepth 1 -type d -name '20*' -mtime +14 -exec rm -rf {} +

Save it as /usr/local/bin/snapshot.sh, run chmod +x on it, and schedule it with cron:

30 2 * * * /usr/local/bin/snapshot.sh >> /var/log/snapshot.log 2>&1

Check the backup

  • Compare sizes: du -sh /var/www /backup/snapshots/latest.
  • Run the same rsync again with -n; it should list no files if everything is in sync.
  • Restore a test file now and then. A backup you have never restored is a guess.

Troubleshooting

  • rsync: command not found on the remote: install rsync there, or point to it with --rsync-path=/usr/bin/rsync.
  • Permission denied: the remote user cannot read or write the path. Use a user with access, or --rsync-path="sudo rsync" with a suitable sudo rule.
  • Ownership changes: only root can preserve owners. Run as root or add --no-owner --no-group.
  • Everything re-copies each time: timestamps differ (e.g. FAT/NTFS targets). Try --modify-window=2 or -c (checksum, slower).

Rsync FAQ

Is rsync a full backup solution?

It is a great building block. Combined with snapshots and an off-site destination it covers most VPS needs, but it does not dump running databases consistently; dump them first with mysqldump or pg_dump.

rsync vs scp: which should I use?

Use scp for a quick single copy. Use rsync for anything repeated or large, because it resumes and only sends changes.

Can I rsync to my cPanel hosting account?

Yes, if SSH access is enabled on the account. The destination path is under your home directory, for example ~/backups/.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.