How to Back Up Your VPS Automatically

How to Back Up Your VPS Automatically

Foreword Everybody agrees backups matter — right up until the moment they would have mattered. A misplaced rm -rf, a disk that dies without warning, a

Foreword

Everybody agrees backups matter — right up until the moment they would have mattered. A misplaced rm -rf, a disk that dies without warning, a provider that vanishes overnight with a farewell tweet: the stories all end the same way, with someone realizing their “backup” was a folder on the same machine. The fix is not more willpower. It is a system that runs without you. This article builds one from three standard tools: provider snapshots for fast full-machine restore, rsync for efficient file-level copies, and object storage as the offsite copy that survives even the worst day.

The 3-2-1 Rule, Sized for a VPS

The classic rule: 3 copies of the data, on 2 different media, with 1 offsite. On a single VPS, that translates to the live data, a snapshot or local copy on the provider’s infrastructure, and one copy that leaves the building entirely. Skip the last one and every risk you did not think of — account compromise, provider failure, a billing accident — takes out all copies at once.

VPS-BackUp

Level 1: Snapshots

Most VPS providers offer one-click snapshots or images. They capture the whole machine — system, config, data — in one operation, and restoring is a rebuild-from-image job measured in minutes. For “I broke the system while tweaking nginx at 2 a.m.”, nothing beats them.

Know the limits, though:

  • A snapshot stored by the same provider shares that provider’s fate. It is one copy, not insurance.

  • Retention is often limited or metered — old snapshots get rotated away, sometimes at the worst time.

  • Snapshots are all-or-nothing. You cannot pull out a single file, or a database from last Tuesday, without restoring the whole image.

Use snapshots as your rapid-recovery layer. Just do not call the job done there.

Level 2: rsync

rsync copies only what changed, which makes daily runs cheap even on modest bandwidth. A typical nightly job:

rsync -a --delete -e "ssh -p 22" /var/www/ user@backup-host:/backups/www/ 
  • -a preserves permissions, ownership, and timestamps;

  • --delete makes the destination mirror the source — powerful, and exactly as dangerous as it sounds: it also deletes files you removed on purpose, so point it at a dedicated backup directory, never at a live data store;

  • the trailing slashes matter: /var/www/ syncs the contents; without it you get a nested www/ directory.

Databases need one extra step — copying a live database file mid-write yields a corrupt copy. Dump first, then sync:

mysqldump --single-transaction --all-databases | gzip > /backups/db-$(date +%F).sql.gz 

Automate the pair with cron:

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

One line, one log file. If the log stops growing, you want to know — which is why the log deserves a glance (or a monitoring check) as much as the backup itself.

Level 3: Object Storage — The Offsite Copy

The final copy should live somewhere your VPS provider cannot touch. S3-compatible object storage fits this well: it is cheap, metered, and reachable from anywhere with standard tools. rclone is the standard client — configure once with the provider’s endpoint and keys, then:

rclone sync /backups remote:my-bucket/vps --transfers 4 

Run after the rsync step in the same script, this pushes your fresh backup offsite every night. Two honest notes:

  • Egress adds up. A full re-upload every night wastes the free tiers quickly; the dump-and-changed-files approach above keeps daily deltas small.

  • Encrypt before you upload. For anything sensitive, rclone crypt wraps the sync so the bucket only ever holds ciphertext.

For the destination itself, I use VMRack’s object storage — it includes 5 GB of storage and 10 GB of monthly transfer for free, which comfortably covers several generations of a small site’s files and database dumps. If your server lives with another provider, an object storage bucket like this is exactly the offsite copy the 3-2-1 rule asks for.

The Step Everyone Skips: Test the Restore

A backup you have never restored is a hope, not a backup. Once a month, on a throwaway machine or a fresh VPS: pull the latest snapshot, or download a database dump and load it somewhere disposable. Confirm the files open and the data is complete. Ten minutes, and it converts the entire chain above from theory into something you know works.

Conclusion

Snapshots for fast full-machine recovery, rsync for efficient incremental file copies, object storage for the offsite copy that survives provider-sized disasters — chained into one cron job with a log, it runs while you sleep and costs less than a coffee a month. Test the restore once a month, and the worst day becomes a bad hour instead of a closed site.

Thank you for reading all the way to the end!

Comment