A server without an off-site backup is one bad command, failed disk or compromised account away from losing everything. S3-compatible object storage is one of the simplest places to keep that off-site copy: it is cheap per terabyte, every serious backup tool can talk to it, and it lives somewhere other than the machine you are protecting. This guide walks through a practical setup for a Linux server: what to back up, how to connect rclone and restic to a custom S3 endpoint, how to encrypt and prune snapshots, how to schedule it all, and how to prove that a restore actually works.
Start with the 3-2-1 rule
The 3-2-1 rule is the standard baseline for backups:
- 3 copies of your data: the live data plus two backups.
- 2 different types of storage, so one failure mode (a RAID controller, a provider outage, a filesystem bug) cannot take out every copy.
- 1 copy off-site, physically and administratively separate from the server.
For a typical VPS or dedicated server, that can look like this: the live data on the server, a local snapshot or a nightly archive on a second disk or volume, and an encrypted, versioned copy in S3 object storage in another location. The S3 copy is the one that saves you when the whole server is gone, so it is the one this guide focuses on.
One extra point people often miss: the server should not be able to destroy its own backups. If an attacker gets root, they can read your backup credentials. Use a dedicated S3 user with access only to the backup bucket, and keep pruning (deleting old snapshots) separate where you can. More on that below.
Decide what to back up
Backing up the entire disk is rarely the best approach. You can reinstall the operating system in minutes. What you cannot recreate is your data and configuration. A good list for most servers:
/etc- system and service configuration./homeand/root- user files, SSH keys, dotfiles and scripts./var/wwwor wherever your sites and applications live./optand/srvif you install software there.- Docker volumes and compose files, for example
/var/lib/docker/volumesplus your project directories. - Database dumps (not the raw database files, see the next section).
Exclude things that are large, change constantly and are easy to rebuild: /proc, /sys, /tmp, package caches, node_modules, build output and log files you do not need.
Dump databases first
Copying the data directory of a running MySQL, MariaDB or PostgreSQL server gives you files that may be in the middle of a write. The backup can look fine and still be unusable. Always create a consistent dump first, then back up the dump.
#!/bin/sh
# /usr/local/bin/db-dump.sh
set -eu
DUMP_DIR=/var/backups/db
mkdir -p "$DUMP_DIR"
chmod 700 "$DUMP_DIR"
# MySQL / MariaDB: --single-transaction gives a consistent InnoDB snapshot without locking tables
mysqldump --all-databases --single-transaction --routines --events \
| gzip > "$DUMP_DIR/mysql-all.sql.gz"
# PostgreSQL: one dump per database in the compressed custom format
for db in $(sudo -u postgres psql -Atc "SELECT datname FROM pg_database WHERE NOT datistemplate"); do
sudo -u postgres pg_dump -Fc "$db" > "$DUMP_DIR/pg-$db.dump"
doneStore the MySQL credentials in /root/.my.cnf (mode 600) so they are not on the command line. Overwriting the same file names every night is fine, because the backup tool keeps the history for you. If you use MongoDB or Redis, use mongodump or copy the Redis RDB file after a BGSAVE in the same way.
Prepare the bucket and credentials
From your object storage account you need four things: the endpoint URL, an access key, a secret key and a bucket. With an S3 service that supports IAM, create a dedicated user for each server rather than using your main keys, and give it access only to its own bucket. If that server is ever compromised, you revoke one key and nothing else is affected.
Option 1: rclone for simple file copies
rclone is a sync tool that supports dozens of storage providers, including any S3-compatible endpoint. It is ideal for copying finished archives or mirroring a directory. Install it from your distribution or with the official script, then create a remote. You can use rclone config interactively, or write the config file directly:
# ~/.config/rclone/rclone.conf
[s3backup]
type = s3
provider = Other
access_key_id = YOUR_ACCESS_KEY
secret_access_key = YOUR_SECRET_KEY
endpoint = https://your-s3-endpoint.example
acl = privateprovider = Other tells rclone to use plain S3 behaviour with your custom endpoint instead of AWS defaults. Test the connection and copy something:
rclone lsd s3backup:
rclone mkdir s3backup:server1-backups
rclone copy /var/backups/db s3backup:server1-backups/db --progressBe careful with rclone sync: it makes the destination match the source, which means a file deleted or encrypted by ransomware on the server is deleted or overwritten in the bucket too. For backups, prefer rclone copy, or use sync with --backup-dir pointing at a dated folder so replaced files are kept. rclone also has a crypt remote type if you want client-side encryption, but for real backup history restic is the better tool.
Option 2: restic for encrypted, versioned backups
restic creates deduplicated, encrypted snapshots. Only changed data is uploaded after the first run, every snapshot can be restored on its own, and nothing leaves the server unencrypted. It supports S3 directly, so you do not need rclone for this part.
Set up the environment
Put the credentials in a root-only file so scripts and cron can load them:
# /root/.restic.env (chmod 600)
export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY
export RESTIC_REPOSITORY=s3:https://your-s3-endpoint.example/server1-backups
export RESTIC_PASSWORD_FILE=/root/.restic.passopenssl rand -base64 32 > /root/.restic.pass
chmod 600 /root/.restic.pass /root/.restic.env
. /root/.restic.env
restic initThe repository password is the encryption key. Store a copy somewhere other than the server, such as a password manager. Without it, the backups cannot be decrypted by anyone, including you.
Run a backup
restic backup /etc /home /root /var/www /var/backups/db \
--exclude-caches \
--exclude '/home/*/.cache' \
--exclude 'node_modules' \
--tag nightlyList what you have with restic snapshots. The first run uploads everything. Later runs only upload new and changed chunks, so they are usually quick.
Retention with forget and prune
Snapshots are cheap, but not free. A retention policy keeps recent history in detail and older history more sparsely:
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --pruneforget removes snapshot references according to the policy, and --prune then deletes the data that no snapshot uses any more. The result is a week of daily restore points, a month of weekly ones and a year of monthly ones. Use --dry-run first to see what would be removed. Pruning rewrites repository data, so running it weekly rather than after every backup is usually enough.
If you are worried about a compromised server deleting its own backups, run forget --prune from a different machine with its own credentials, and limit the server's IAM user to what restic backup needs.
Check the repository
restic check # verify the repository structure
restic check --read-data-subset=5% # also download and verify 5% of the dataReading a different random subset each week gives you good confidence over time without downloading the whole repository every run.
Schedule it with cron
Wrap everything in one script so the order is always the same: dump databases, back up, then prune and check once a week.
#!/bin/sh
# /usr/local/bin/backup.sh
set -eu
. /root/.restic.env
/usr/local/bin/db-dump.sh
restic backup /etc /home /root /var/www /var/backups/db \
--exclude-caches --exclude 'node_modules' --tag nightly
# Prune and check on Sundays only
if [ "$(date +%u)" = "7" ]; then
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune
restic check --read-data-subset=5%
fichmod 700 /usr/local/bin/backup.sh /usr/local/bin/db-dump.sh
crontab -e
# Every night at 03:15, log output for review
15 3 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1Pick a quiet hour, and stagger the time if you have several servers. A backup that fails silently is worse than no backup, because you trust it. Send the log by mail, or ping a monitoring service at the end of a successful run so you are alerted when the ping stops arriving. A systemd timer works just as well if you prefer it to cron.
Test your restores
A backup is only proven when you have restored from it. Do a test restore when you first set things up, and again every few months.
# See what is in the latest snapshot
restic ls latest /etc/nginx
# Restore one directory to a scratch location
restic restore latest --target /tmp/restore --include /etc/nginx
# Restore an older snapshot by ID (from "restic snapshots")
restic restore 4f2a9c1e --target /tmp/restore-oldFor databases, load the dump into a throwaway database or a test server and run a few queries:
gunzip -c /tmp/restore/var/backups/db/mysql-all.sql.gz | mysql -h test-server
pg_restore -d restore_test /tmp/restore/var/backups/db/pg-mydb.dumpThe best test is a full rehearsal: build a fresh server, install restic, use only the credentials and password you stored off the server, and bring a service back up. If anything you needed was missing, such as the password file, a config directory or a list of installed packages, you found it during a test instead of during an outage.
rclone or restic?
| rclone | restic | |
|---|---|---|
| Main use | Copying and syncing files | Versioned backups |
| Encryption | Optional (crypt remote) | Always on |
| Deduplication | No | Yes |
| Point-in-time restore | Only with dated folders | Every snapshot |
| Files readable in bucket | Yes (without crypt) | No, restic only |
| Best for | Archives, media, mirrors | Server backups |
Many setups use both: restic for the server itself, and rclone to push large media or pre-built archives that do not benefit from deduplication.
Checklist
- Follow 3-2-1, with the off-site copy in object storage.
- Use a dedicated S3 user and bucket per server.
- Dump databases before every backup.
- Encrypt, and keep the repository password off the server.
- Set a retention policy and prune regularly.
- Schedule it and get alerted on failure.
- Test restores, including a full rebuild.
Need somewhere to put the off-site copy? Zettako S3 Object Storage is S3-compatible, so rclone and restic work with it as shown above. It is hosted in Frankfurt, supports IAM users and permissions, and uploads are free, so your nightly backups cost no traffic.