VPS/VDS Hosting

Back up a Linux VPS with rsync

Incremental Linux VPS backups to a remote host with rsync over SSH, cron scheduling and best practices.

intermediate~3 min read

rsync enables efficient incremental backups over SSH. Ideal for protecting /etc, websites, databases and app files on your Infrawire Linux VPS.

rsync compares the source and the destination and transfers only the files, or even the parts of files, that changed. The first run copies everything; the following runs are fast and use little bandwidth. It is a good fit for web files, configuration and dumps. It is not a complete backup strategy on its own: a plain mirror holds only one copy, so a deleted or corrupted file is propagated at the next run unless you keep dated snapshots (see below), and live database files must be dumped first.

📋 Prerequisites

  • Source Linux VPS, destination server/NAS with SSH and enough disk space.
  • rsync installed on both machines, and a dedicated user (here backup) on the destination that owns the target directory.
Bash
sudo apt update sudo apt install -y rsync openssh-client

On AlmaLinux and Rocky Linux use dnf; cronie provides cron.

Bash
sudo dnf install -y rsync openssh-clients cronie sudo systemctl enable --now crond

Set up passwordless SSH

On the source VPS, as the user running backups. The key has an empty passphrase (-N "") so that cron can use it without a prompt. That is a trade-off: anyone who reads the private key can log in to the destination, so keep it with chmod 600, use it only for backups, and give the destination account no more rights than it needs. Then check that the login works without a password.

Bash
ssh-keygen -t ed25519 -f ~/.ssh/id_backup -N "" ssh-copy-id -i ~/.ssh/id_backup.pub backup@IP_DEST
Bash
ssh -i ~/.ssh/id_backup backup@IP_DEST "echo ok"

First backup (test)

Example: copy /var/www and /etc to backup@DEST_IP:/backups/my-vps/. -a keeps permissions, dates and symbolic links, -v lists files, -z compresses during transfer, and -e selects the SSH key. --delete removes from the destination what no longer exists in the source, so the destination stays an exact mirror. The trailing slash matters: /var/www/ copies the content of the directory, while /var/www would create a www folder inside the destination.

Bash
rsync -avz --delete \ -e "ssh -i ~/.ssh/id_backup" \ /var/www/ backup@IP_DEST:/backups/my-vps/www/ rsync -avz \ -e "ssh -i ~/.ssh/id_backup" \ /etc/ backup@IP_DEST:/backups/my-vps/etc/

Before using --delete on a new path, add -n (dry run) to see what would happen without changing anything:

Bash
rsync -avzn --delete \ -e "ssh -i ~/.ssh/id_backup" \ /var/www/ backup@IP_DEST:/backups/my-vps/www/

Ownership is only preserved when the receiving side runs as root; as the backup user, files will belong to backup, which is fine for storage but must be corrected after a restore.

Exclude noise

Create an exclude file, e.g. /root/backup-excludes.txt. Patterns starting with / are relative to the transfer root, and *.log matches log files anywhere. Skip caches, temporary files and anything you can rebuild. The /proc/* line only matters if you back up / itself.

/tmp/*
*.log
/proc/*

Use: rsync -avz --exclude-from=/root/backup-excludes.txt ...

Automate with cron

Edit crontab (crontab -e) for a daily run at 3 AM. The output goes to a log file so you can see failures. Use absolute paths in cron, because its environment is minimal.

CRON
0 3 * * * rsync -avz -e "ssh -i /root/.ssh/id_backup" /var/www/ backup@IP_DEST:/backups/my-vps/www/ >> /var/log/backup-rsync.log 2>&1

If a run can last longer than a day, prefix it with flock -n so that two copies never run together:

CRON
0 3 * * * flock -n /var/lock/backup-rsync.lock rsync -avz -e "ssh -i /root/.ssh/id_backup" /var/www/ backup@IP_DEST:/backups/my-vps/www/ >> /var/log/backup-rsync.log 2>&1

Keep dated snapshots

A mirror cannot bring back yesterday's version of a file. With --link-dest, each day gets its own directory, but unchanged files are hard links to the previous copy, so they use no extra space. Update the latest link after a successful run; the first run prints a harmless warning because latest does not exist yet. Delete old directories with rm -rf when you no longer need them.

Bash
TODAY=$(date +%F) rsync -az --delete -e "ssh -i ~/.ssh/id_backup" \ --link-dest=/backups/my-vps/latest \ /var/www/ backup@IP_DEST:/backups/my-vps/$TODAY/ ssh -i ~/.ssh/id_backup backup@IP_DEST "ln -sfn /backups/my-vps/$TODAY /backups/my-vps/latest"

Databases and restore test

Do not copy live database files: dump them first, then let rsync copy the dump. Put the command in a script when you schedule it, because % has a special meaning in a crontab.

Bash
sudo mariadb-dump --single-transaction --all-databases | gzip > /var/backups/db-$(date +%F).sql.gz

A backup you never restored is only a hope. Restore into a temporary directory and compare:

Bash
mkdir -p /tmp/restore-test rsync -avz -e "ssh -i ~/.ssh/id_backup" backup@IP_DEST:/backups/my-vps/www/ /tmp/restore-test/ diff -r /var/www /tmp/restore-test | head

Troubleshooting

  • Permission denied (publickey): the public key is not in ~/.ssh/authorized_keys of the destination user; repeat ssh-copy-id.
  • rsync: command not found: install rsync on both machines.
  • rsync error: some files/attrs were not transferred (code 23): permissions on the source; run as root or exclude those paths.
  • No space left on device: free space on the destination or delete old snapshots.
  • Nothing happens from cron: read /var/log/backup-rsync.log and use absolute paths.

Infrawire offers a VPS for each major distribution: Ubuntu VPS, Debian VPS, AlmaLinux VPS, Rocky Linux VPS and Kali Linux VPS.

Tips

  • Test restore regularly, not only the copy job.
  • Encrypt sensitive backups (GPG or encrypted volume on destination).
  • Monitor destination disk space and cron logs.
  • Keep at least one copy outside the server you are protecting.

Protect your data! 🚀

Latest tutorials

Stay up to date with the latest offers, discounts and special promotions.

View promotions

We use cookies

We use cookies to measure and improve our site. Privacy policy