Incremental backups on a VPS using rsync and cron | INTROSERV
EUR
european

EUR

usa

USD

English En
Ex. VAT Ex. VAT 0%

Incremental backups on a VPS using rsync and cron

Introduction

Backups are essential for any VPS. They protect you from accidental deletions, configuration mistakes, data corruption, or server compromise.

This manual explains how to set up incremental backups using:

  • rsync — efficient file-copying tool
  • hard links — to avoid duplicate data in each backup
  • cron — to schedule automatic backups

Result: each snapshot looks like a full backup, but only changed files take extra space. This approach is simple, reliable, and suitable for most common VPS setups.

How incremental backups with rsync work

rsync provides two key features:

  1. Synchronization of only changed files
  2. --link-dest option: creates hard links for unchanged files, allowing multiple "full" backup directories without consuming extra space.

Directory structure example:

/backups/ daily.0/ - today's backup daily.1/ - yesterday's backup daily.2/ - backup from 2 days ago

Each folder appears as a full backup, but only differences take space.

Prerequisites

On the VPS:

  • Linux installation
  • Access as root or user with sudo
  • Enough disk space for backup snapshots
  • Optional: remote storage if backing up to another server

Install rsync if needed:

sudo apt update

sudo apt install rsync

Info

Warning: Stop your database servers or exclude their data directories from backup. Online database backup via rsync may cause corrupted database backup.

Creating the backup directories

Choose a location for backups. Usually:

  • Local filesystem: /backups/
  • External disk: /mnt/storage/backups/
  • Remote server via SSH: user@backup-server:/data/backups/

Example (local):

sudo mkdir -p /backups

sudo chmod 700 /backups

Basic rsync command for incremental backups

Below is the key rsync command:

rsync -a --delete \ --link-dest=/backups/daily.1 \ /source/data/ \ /backups/daily.0/

Explanation:

  • -a — archive mode (preserves permissions, owners, timestamps, symlinks)
  • --delete — remove files deleted on the source
  • --link-dest — create hard links to unchanged files in previous snapshot
  • /source/data/ — directory you want to back up
  • /backups/daily.0/ — today's snapshot

Rotation script for daily snapshots

Create a script to:

  1. Rotate existing backups
  2. Create a new incremental backup
  3. Keep N days (example: 7)

Create file:

sudo nano /usr/local/bin/backup_daily.sh

Paste:

#!/bin/bash BACKUP_DIR="/backups/" SOURCE="/" EXCLUDE="/etc/backup-exclude.txt" DAYS="7" #REMOTE_USER="<YOUR_REMOTE_USER>" #REMOTE_SERVER="<YOUR_REMOTE_SERVER>" REMOTE_DIR="/backups/" # Rotate for ((i=DAYS-1; i>=0; i--)); do if [ -d "$BACKUP_DIR/daily.$i" ]; then rm -rf "$BACKUP_DIR/daily.$((i+1))" mv "$BACKUP_DIR/daily.$i" "$BACKUP_DIR/daily.$((i+1))" fi done # Prepare backup directories mkdir -p "$BACKUP_DIR/daily.0" if [ ! -d "$BACKUP_DIR/daily.1" ]; then mkdir -p "$BACKUP_DIR/daily.1" fi # Perform incremental backup rsync -a --delete \ --link-dest="$BACKUP_DIR/daily.1" \ --exclude-from="$EXCLUDE" \ "$SOURCE" "$BACKUP_DIR/daily.0" if [ -n "$REMOTE_USER" ] && [ -n "$REMOTE_SERVER" ] && [ -n "$REMOTE_DIR" ]; then rsync -azv --delete "$BACKUP_DIR" "$REMOTE_USER@$REMOTE_SERVER:$REMOTE_DIR" fi

Save and exit. Make it executable:

sudo chmod +x /usr/local/bin/backup_daily.sh

You can change variables on top of the script to tweak it for your needs. Change SOURCE to a specific directory if you don't need full root filesystem backup for example.

Excluding unneeded directories

Create an exclude file:

sudo nano /etc/backup-exclude.txt

Typical excludes:

  • /proc
  • /sys
  • /tmp
  • /run
  • /dev
  • /mnt
  • /media
  • /var/cache
  • /backups

Scheduling backups with cron

Edit crontab:

sudo crontab -e

Add (backup at 03:00 daily):

0 3 * * * /usr/local/bin/backup_daily.sh >/var/log/backup.log 2>&1

This ensures regular automatic snapshots.

Backing up to a remote server (optional)

If you want off-site backup follow next steps. Create destination folder on target server:

mkdir -p /backups

Ensure SSH keys are configured. Execute next command to generate your SSH key:

ssh-keygen

Replace <YOUR_REMOTE_USER> with your username on remote server, <YOUR_REMOTE_SERVER> with your remote server domain name and execute following command to copy your SSH key to the remote server:

ssh-copy-id <YOUR_REMOTE_USER>@<YOUR_REMOTE_SERVER>

Uncomment REMOTE_USER, REMOTE_SERVER variables at the top of your backup_daily.sh, replace <YOUR_REMOTE_USER> with your username on remote server, <YOUR_REMOTE_SERVER> with your remote server domain name and save backup_daily.sh to enable remote backup.

Uploading backups to INTROSERV CloudBox

INTROSERV provides online storage that lets you automatically or manually save secure copies of your important files and data on remote servers. To use it as your remote backup storage log in to CloudBox web-interface and add your public ssh key to profile. Profile settings are located in the top right corner.

CloudBox profile settings

Insert your public SSH key in the "Public keys" section to upload your key to the server.

CloudBox public SSH key section

Uncomment REMOTE_USER, REMOTE_SERVER variables at the top of your backup_daily.sh, replace <YOUR_REMOTE_USER> with your username on CloudBox server, <YOUR_REMOTE_SERVER> with your CloudBox server domain name and save backup_daily.sh to enable remote backup.

Testing backups

Before relying on your system test backups.

  1. Run the script manually: sudo /usr/local/bin/backup_daily.sh
  2. Check sizes: du -sh /backups/*. There must be only one large backup.
  3. Verify file integrity: ls -l /backups/daily.0/etc/

Restoring data

You can restore any individual file:

cp /backups/daily.2/etc/nginx/nginx.conf /etc/nginx/

Or restore everything:

sudo rsync -a /backups/daily.0/ /

Info

Warning: Restoring system directories overwrites current files.

Security considerations

  • Use strict permissions on backup directories (chmod 700).
  • If using SSH, disable password login, use keys only.
  • Do not store backups on the same disk as the server if possible.
  • Encrypt remote backups using LUKS or encrypted storage if needed.

Monitoring & logging

Logging is already added in cron: /var/log/backup.log

Check regularly:

tail -f /var/log/backup.log

Conclusion

Using rsync with cron offers:

  • Fast incremental backups
  • Efficient disk usage via hard links
  • Simple restore process
  • Fully automated scheduling

This method is reliable and easy for novice administrators and works perfectly on linux VPS setups.

VAT

  • Other

    Ex. VAT

    0%
  • austria

    Austria

    20%
  • Belgium

    Belgium

    21%
  • Bulgaria

    Bulgaria

    20%
  • Croatia

    Croatia

    25%
  • Cyprus

    Cyprus

    19%
  • Czech Republic

    Czech Republic

    21%
  • Denmark

    Denmark

    25%
  • Estonia

    Estonia

    22%
  • France

    France

    20%
  • Finland

    Finland

    24%
  • Germany

    Germany

    19%
  • Greece

    Greece

    24%
  • Hungary

    Hungary

    27%
  • Ireland

    Ireland

    23%
  • Italy

    Italy

    22%
  • Latvia

    Latvia

    21%
  • Lithuania

    Lithuania

    21%
  • Luxembourg

    Luxembourg

    17%
  • Malta

    Malta

    18%
  • Netherlands

    Netherlands

    21%
  • Poland

    Poland

    23%
  • Portugal

    Portugal

    23%
  • Romania

    Romania

    19%
  • Slovakia

    Slovakia

    20%
  • Slovenia

    Slovenia

    22%
  • Spain

    Spain

    21%
  • Sweden

    Sweden

    25%
  • USA

    USA

    0%
european
states
  • germany
  • Español
  • Italiano
  • Poland
  • Русский
  • Slovenski
  • Türkçe
  • ukraine
  • kingdom
  • French
  • Hrvatska
  • Other
  • Austria
  • Belgium
  • Bulgaria
  • Croatia
  • Cyprus
  • Czech Republic
  • Denmark
  • Estonia
  • Finland
  • France
  • Germany
  • Greece
  • Hungary
  • Ireland
  • Italy
  • Latvia
  • Lithuania
  • Luxembourg
  • Malta
  • Netherlands
  • Poland
  • Portugal
  • Romania
  • Slovakia
  • Slovenia
  • Spain
  • Sweden
  • USA