
# VPS Backup: How to Back Up Your Server the Right Way

**A reliable VPS backup has two layers: automatic daily copies from your host, and your own script that ships the data that matters (databases, files, configs) somewhere else.** The first layer covers you when the server breaks; the second keeps your data safe even if something happens to the server or the account. Below are both, with ready-to-use scripts for Ubuntu 24.04 and Debian 12, and how to test a restore.


## The 3-2-1 Rule

- **3 copies** of your data: the live one plus two backups;
- **2 different storage locations or media;**
- **1 copy off-site** (another provider, object storage, a NAS at home).

On a VPS that usually means provider backups plus restic to S3-compatible storage or a second server.

## What to Back Up

| Data | Typical location | How to copy it |
|---|---|---|
| App code and files | `/root/app`, `/opt`, `/srv` | archive or restic |
| Configs | `/etc/nginx`, `/etc/systemd/system`, `.env` | archive or restic |
| PostgreSQL | `/var/lib/postgresql` | `pg_dump` only |
| MySQL / MariaDB | `/var/lib/mysql` | `mysqldump` only |
| Docker volumes | `/var/lib/docker/volumes` | stop the container or dump from inside |
| User uploads | app folder | restic |

Never copy a running database's files directly; the copy may be corrupt. Always dump.

## Option 1. Automatic VPS Backup from Your Host

The set-and-forget option. On 0x.cloud, backups keep the last 7 daily copies of your server and cost +20% of the plan price:

| Plan | Plan price / month | Backups / month |
|---|---|---|
| s1-1 | $4.49 | ≈ $0.90 |
| s1-2 | $7.99 | ≈ $1.60 |
| s2-4 | $15.99 | ≈ $3.20 |
| s4-8 | $31.99 | ≈ $6.40 |

Before a one-off risky change, also take a snapshot: see [VPS snapshots](https://0x.cloud/kb/servers/vps-snapshot).

## Option 2. A tar and cron Backup Script

Good for a small project. The script dumps the database, archives files and deletes copies older than 7 days.

```bash
sudo nano /usr/local/bin/backup.sh
```

```bash
#!/bin/bash
# stop on any error
set -euo pipefail

DEST=/var/backups/app
DATE=$(date +%F_%H-%M)
mkdir -p "$DEST"

# compressed PostgreSQL dump
sudo -u postgres pg_dump -Fc mydb > "$DEST/mydb_$DATE.dump"

# archive code and configs
tar -czf "$DEST/files_$DATE.tar.gz" /root/app /etc/nginx

# delete copies older than 7 days
find "$DEST" -type f -mtime +7 -delete
```

```bash
# make it executable and test it by hand
sudo chmod +x /usr/local/bin/backup.sh
sudo /usr/local/bin/backup.sh && ls -lh /var/backups/app
```

Run it nightly at 03:30 via `sudo crontab -e`:

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

Schedule syntax is covered in [cron on Linux](https://0x.cloud/kb/servers/cron). The catch: these copies live on the same server. Push them out with `rsync` or use option 3.

## Option 3. Restic: Encrypted Off-Site Backups

[Restic](https://restic.readthedocs.io/) makes incremental, encrypted backups to S3-compatible storage, another server over SFTP and more. After the first run, only changes are sent.

```bash
# install restic
sudo apt update && sudo apt install -y restic

# repository password (keep a copy off the server too!)
echo 'a-strong-password' | sudo tee /root/.restic-pass && sudo chmod 600 /root/.restic-pass

# repository on a second server over SFTP
export RESTIC_REPOSITORY=sftp:backup@198.51.100.20:/srv/restic
export RESTIC_PASSWORD_FILE=/root/.restic-pass

# create the repository (once)
restic init

# back up
restic backup /root/app /etc /var/backups/app

# list backups
restic snapshots

# keep 7 daily, 4 weekly, 6 monthly; delete the rest
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
```

Append the `restic backup` and `restic forget` lines to the script from option 2, and cron ships an off-site copy every night. For an app where backups are critical, see self-hosting Vaultwarden.

### Backup VPS to Local

To keep a copy at home or on another machine you control, push it from the server on a schedule. 0x.cloud servers have no public IP address, so your computer cannot pull from them:

```bash
# on the server: mirror the backup folder to your own machine
rsync -avz /var/backups/app/ you@home.example.com:vps-backups/
```

## Test Your Restores

A backup you have never restored is a guess. Once a month:

```bash
# restore the latest restic backup to a temp folder
restic restore latest --target /tmp/restore-test

# restore a dump into a test database
sudo -u postgres createdb mydb_test
sudo -u postgres pg_restore -d mydb_test /var/backups/app/mydb_2026-11-20_03-30.dump
```

An hourly-billed scratch server is perfect for this: create, restore, verify, delete. On s1-2 (about $0.054725 an hour) a one-hour restore test costs about $0.05, and the unused amount returns to your balance when you delete it. The same routine works for moving hosts; see how to migrate a VPS.


```text
Set up a daily backup on this server: dump the PostgreSQL database mydb and the /root/app folder at 03:30 via cron, keep 7 days, and send copies with restic to my server backup@198.51.100.20. Then restore the latest copy to a temp folder and show me the files are there.
```


## Common Problems and Fixes

**The script works by hand but not from cron.** Cron has a minimal `PATH`. Use full paths to commands or set `PATH` at the top of the crontab.

**The disk filled up with backups.** Old copies are not being deleted. Check the `find ... -delete` line or `restic forget --prune`.

**`pg_dump: error: connection failed`.** Run it as the `postgres` user (`sudo -u postgres`) or pass connection options.

**Lost the restic password.** Without it the data cannot be restored. Store it in a password manager off the server.

Backup, snapshot and plan prices are on the [pricing page](https://0x.cloud/pricing).


[Create server: s1-2, $7.99/mo](https://0x.cloud/cabinet/create?plan=s1-2)


## FAQ

**How often should I back up my VPS?**
Daily is enough for most projects. If data changes constantly (orders, messages), dump the database every hour or more often.

**How many backups should I keep?**
A solid default is 7 daily, 4 weekly and a few monthly copies. That lets you go back to yesterday or to last month.

**What do 0x.cloud backups include?**
The last 7 daily copies of your server are kept automatically. The add-on costs +20% of the plan price, about $1.60/month on s1-2.

**What is the difference between a backup and a snapshot?**
Backups run on a schedule and keep several days of history. A snapshot is a manual copy you take right before a risky change so you can roll back fast.

**Do I need an off-site copy if my host already backs up the server?**
For important data, yes. A copy with another provider or at home protects you from account mistakes, an accidentally deleted server and single-provider issues.
