How to Back Up Your VPS to an External Drive Automatically Every Night

How to Back Up Your VPS to an External Drive Automatically Every Night

When your VPS goes down, the backup sitting on that same server goes down with it. That is why it pays to back up VPS to an external drive every night. A copy stored somewhere else turns a dead server into an annoying evening, not a lost business. This guide covers the full setup on Ubuntu 24.04: mounting the drive, writing a backup script with rsync, and scheduling it with cron so it runs while you sleep.

Why an External Drive Beats Backups on the Same VPS

Most VPS owners start with backups that live on the same disk as the website. That feels safe until the disk fails, the provider has an outage, or a bad command wipes the wrong directory. A backup on the same disk shares the fate of the thing it is meant to protect. An external drive breaks that link. It sits outside the server, so a disk failure, a hacked account, or a provider-side problem does not take your copies with it.

An external drive also costs nothing per month once you own it. Cloud backup storage charges by the gigabyte every month, and the bill grows as your site grows. A 2 TB USB drive attached to a home server or a small NAS handles nightly copies of most small VPS setups for years. If you want a second copy somewhere even further away, the 3-2-1 backup rule for a single VPS explains how to layer an offsite copy on top of this setup.

What You Need Before You Start

You need three things: a VPS running Ubuntu 22.04 or 24.04, an external drive you can reach from that server, and about 30 minutes. The drive can be a USB drive attached to a home machine you reach over the network, a NAS share, or a second server with a big disk. A directly attached USB drive on a VPS is rare, so most people mount a network share instead. The rest of this guide uses an NFS or SMB share mounted at /mnt/backup.

Check two numbers before you begin. First, how much data are you backing up? Run du -sh /var/www /etc /home and add the results. Second, how much free space does the drive have? It should hold at least three full copies of your data, because a single copy is not a retention plan. If the numbers do not fit, delete old logs and caches before you start.

Connect and Mount the External Drive

Mount the drive at a fixed path and make the mount survive reboots. For an NFS share from a home NAS, add a line to /etc/fstab like this:

192.168.1.50:/volume1/backup /mnt/backup nfs defaults,nofail 0 0

The nofail option matters. Without it, a missing network drive can stop your server from booting. After editing fstab, create the mount point with mkdir -p /mnt/backup and run mount -a. Then confirm the mount with df -h /mnt/backup. If the command shows nothing, check that the NAS allows your VPS IP address and that your firewall lets NFS traffic through.

For an SMB share, install cifs-utils and use a credentials file in /root/.smbcredentials with mode 600, so the password is not sitting in a world-readable file. Test the mount now, not at 3 a.m. when the cron job runs and nobody is watching.

Back Up VPS to an External Drive With Rsync

rsync is the right tool for nightly copies. This setup gives you an automatic VPS backup with rsync that only moves files that changed, so the first run takes a while and every run after that finishes in minutes. It also keeps permissions and timestamps, which matters when you restore a web server and expect the files to behave the same way.

Create a script at /usr/local/bin/vps-backup.sh:

#!/bin/bash
set -e
SRC="/"
DEST="/mnt/backup/vps-nightly"
LOG="/var/log/vps-backup.log"
EXCLUDES="/root/backup-excludes.txt"

rsync -aAX --delete 
  --exclude-from="$EXCLUDES" 
  "$SRC" "$DEST" >> "$LOG" 2>&1
echo "$(date): backup finished" >> "$LOG"

The --delete flag removes files from the backup that you deleted on the server, so the copy stays a mirror of the live system. The excludes file is where you skip things that should never be backed up: /proc, /sys, /dev, /tmp, the mount point itself (/mnt/backup), and cache directories. Forgetting to exclude the mount point is the classic mistake. rsync will happily try to copy the backup into itself until the disk fills. If you want a deeper look at rsync options, our rsync backup tutorial for incremental server backups covers the flags in detail.

Schedule the Nightly Run With Cron

A backup script that only runs when you remember to run it is not a backup plan. Cron handles the schedule. Open the root crontab with crontab -e and add this line to run the script at 2 a.m. every night:

0 2 * * * /usr/local/bin/vps-backup.sh

Pick a quiet hour. For most small sites, 2 a.m. server time is fine. If your audience is in another timezone, check your access logs for the lowest-traffic hour and use that instead. Make the script executable first with chmod +x /usr/local/bin/vps-backup.sh, then test it by hand once before trusting the schedule. Run it, watch the log at /var/log/vps-backup.log, and confirm the files land on the drive.

Your VPS backup cron job is only useful if you know when it fails. Add a check that emails you on failure, or at least read the log once a week. A silent failure is the worst outcome. You discover it months later, on the day you need the backup.

Keep Database Dumps in the Nightly Backup

rsync copies files, but a live MySQL or PostgreSQL database is not a set of files you can safely copy while the server is running. Dump the database to a file first, then let rsync pick up the dump. Add this to the top of your backup script for MySQL:

mysqldump --all-databases --single-transaction > /root/db-dump.sql

The --single-transaction flag keeps the dump consistent without locking your tables, so your site stays up while the dump runs. For PostgreSQL, use pg_dumpall > /root/db-dump.sql. Store the dump inside a directory that rsync copies, and keep it out of the web root so it is never served to visitors. Our guide to automatic MySQL backups with mysqldump and cron covers the details, including per-database dumps and keeping the credentials out of the script.

One more caution: a database dump contains everything, including user emails and password hashes. The external drive holds a full copy of your data, so treat it like the server. Encrypt the drive if it ever leaves your house, and never plug it into a machine you do not trust.

Retention: How Many Nights to Keep

One nightly copy is not enough. If a file gets corrupted on Monday and you only notice on Thursday, a single mirror from Wednesday night already holds the corrupted version. Keep several dated copies instead. Seven daily copies plus four weekly ones covers about a month of history.

rsync with --link-dest makes this cheap. Each night creates a new folder that links unchanged files to the previous night instead of copying them again, so seven copies of a 20 GB server might only take 25 GB on disk. A simpler option is BorgBackup, which handles deduplication and retention in one tool. Our BorgBackup setup guide for a budget VPS walks through that route. Whichever method you pick, automate the cleanup. A backup drive that fills up stops backing up, and a full drive at 2 a.m. is a silent failure.

Frequently Asked Questions

How long does the first backup take?

It depends on how much data you have and how fast the link to the drive is. A 20 GB server over a home network usually takes under an hour. Later runs only move changed files, so they often finish in a few minutes. Run the first copy during the day, when you can watch it, and let cron handle the nights after that.

Can I use a USB drive plugged into my home computer instead of a NAS?

Yes. Share the drive over your network from that computer and mount it the same way. The catch is that the computer must be on when the backup runs. A NAS or a small always-on machine is more reliable, but a shared folder on a desktop works if the machine stays awake at 2 a.m.

Is an external drive enough, or do I need cloud storage too?

An external drive protects you from server failure, but not from a fire, theft, or flood at home. If your site matters to your income, add one offsite copy as well. Cheap offsite VPS backup storage, such as an object storage bucket or a second VPS in another region, works fine. The drive is the fast restore; the offsite copy is the last resort.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.