How to Automate Linux Server Backups with Restic and Cron

Backups only count if you can restore from them. Too many servers have a backup script that has never been tested, a disk that filled up months ago, or a cron job that silently stopped running after an update. This guide shows how to automate Linux server backups with Restic and Cron in a way you can trust: a simple script, a schedule, monitoring for failures, and a restore test you actually run.

Restic is a good fit for this job because it encrypts by default, deduplicates data so repeated backups stay small, and works with local disks, SFTP servers, and object storage. Cron is the scheduler already on your server. Together they need no extra services.

Automate Linux Server Backups: Set Up the Repository

Install Restic from your package manager or the official binary, then create a repository. Pick a location separate from the data you back up: a second disk, another server over SFTP, or an S3-compatible bucket. A backup on the same disk as the data protects against accidental deletion, not against disk failure.

export RESTIC_REPOSITORY=/mnt/backup/restic-repo
export RESTIC_PASSWORD_FILE=/root/.restic-password
restic init

Store the password in a root-only file, not in the script itself, and keep a copy somewhere safe outside the server. An encrypted backup you cannot unlock is no backup at all. Your restic backup repository is now ready to receive snapshots.

Decide what to back up before writing the script. A typical web server list includes /etc, /var/www, home directories, and database dumps. Databases need a dump step first: backing up a live database directory directly can produce a corrupt copy. Dump with mysqldump or pg_dump into a staging folder, then let Restic back up the dump.

Write the Backup Script

Keep the script short and explicit. Here is a solid starting point:

#!/bin/bash
set -euo pipefail
export RESTIC_REPOSITORY=/mnt/backup/restic-repo
export RESTIC_PASSWORD_FILE=/root/.restic-password

DB_DUMP=/var/backups/db.sql
mysqldump --single-transaction --all-databases > "$DB_DUMP"

restic backup /etc /var/www /home /var/backups 
  --tag daily 
  --exclude-caches

restic forget --prune 
  --keep-daily 7 --keep-weekly 4 --keep-monthly 6

The forget --prune step is retention policy, and it matters more than people think. Without it, snapshots pile up until the disk fills and the next backup fails. Seven daily, four weekly, and six monthly snapshots give you about half a year of history with bounded disk use.

Make the script executable, run it once by hand, and read the output. A script you have never seen succeed is a script you cannot trust. If you are comparing tools before committing, our comparison of Linux backup tools including Restic, Borg, and Duplicacy covers where each one fits.

Schedule It with a Cron Backup Schedule

A cron backup schedule is one line in the root crontab. Run nightly when load is low, and redirect output to a log file so failures leave evidence:

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

That line runs the script at 3 AM daily. Do not rely on “cron will email me” unless you have verified mail delivery from the server, which many servers lack. Instead, check the log in your weekly routine, or add a simple check that alerts you when the log shows a failure or when no new snapshot appears for 48 hours.

Two cron gotchas catch beginners. First, cron runs with a minimal environment, so use absolute paths everywhere and set any variables inside the script. Second, overlapping runs can corrupt a backup or double the load, so add a lock with flock if a run could ever outlast the interval. Our guide to running cron jobs reliably covers the same reliability ideas in a container setting, and the principles transfer directly.

Test the Restore: Restic Restore Testing

This is the step most setups skip, and it is the whole point. A restic restore testing routine means picking a file, restoring it to a temporary location, and comparing it with the original. Do it monthly:

restic snapshots
restic restore latest --target /tmp/restore-test --include /etc/nginx/nginx.conf
diff /tmp/restore-test/etc/nginx/nginx.conf /etc/nginx/nginx.conf

If the diff is empty, the restore works. Also practice a full-directory restore at least once, and time it. A restore that takes twelve hours changes your disaster plan, and you want to learn that on a quiet Tuesday, not during an outage.

For stateful systems like Kubernetes, the same discipline applies to cluster state. Our etcd backup and disaster recovery guide shows how the restore-first mindset works for control-plane data.

Monitor, Rotate, and Keep It Boring

A backup system should be boring. Check three things on a schedule: the latest snapshot timestamp, the repository size trend, and the restore test result. If snapshots stop appearing, treat it as an incident, not a chore to get to later. A backup gap discovered during an outage is the worst kind of surprise.

Rotate credentials and repository passwords when team members leave, and document the restore steps where someone else can find them. The person restoring the backup during a real incident may be you at 3 AM, and future-you will be grateful for clear notes.

Quick Checklist

  • Repository on separate storage, password stored safely outside the server
  • Script backs up configs, web roots, home dirs, and database dumps
  • Retention policy prunes old snapshots automatically
  • Cron schedule logged, failures visible, lock prevents overlaps
  • Restore tested monthly, full restore practiced at least once

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.