No description
Find a file
2026-11-24 15:19:52 +00:00
.gitattributes Generalise the backup scripts for multiple servers 2026-09-11 10:40:18 +02:00
backup-common.sh Move backup schedule into config and add installer scripts 2026-10-14 21:48:23 +02:00
backup-excludes-docker-volumes.txt first commit 2026-09-05 20:42:21 +02:00
backup-excludes-filepile.txt media/downloads uit filepile backup 2026-11-23 17:01:33 +01:00
backup-excludes-home.txt settings for bellatrix 2026-11-04 17:03:23 +01:00
backup-excludes-services.txt update 2026-11-03 22:04:38 +01:00
backup-retention.sh Report retention counts per job, and totals across jobs 2026-09-11 12:45:10 +02:00
backup.conf.bellatrix update 2026-11-03 22:07:14 +01:00
backup.conf.example Move backup schedule into config and add installer scripts 2026-10-14 21:48:23 +02:00
backup.conf.hetzner-arm2 changed retention days all servers 2026-10-14 21:56:26 +02:00
backup.conf.rigel update 2026-11-03 22:04:38 +01:00
backup.conf.saiph update 2026-11-03 22:04:38 +01:00
backup.conf.silverstone Move backup schedule into config and add installer scripts 2026-10-14 21:48:23 +02:00
backup.service Made systemd configuration generic by removing server-specific mount dependencies; server-specific settings remain in backup.conf.<hostname>. 2026-09-17 08:47:40 +02:00
backup.sh Trap SIGHUP too, and prefer systemd over a terminal for manual runs 2026-09-11 13:53:59 +02:00
backup.timer Move backup schedule into config and add installer scripts 2026-10-14 21:48:23 +02:00
IDEAS.md Move backup schedule into config and add installer scripts 2026-10-14 21:48:23 +02:00
install-timer.sh Move backup schedule into config and add installer scripts 2026-10-14 21:48:23 +02:00
install-units.sh test real paths, not simlinked paths 2026-10-14 22:07:48 +02:00
README.md refactored readme 2026-10-15 10:23:01 +02:00
WORKLOG.md worklog update 2026-11-24 15:19:52 +00:00

Backup

Nightly backups for my Linux servers. It runs on any distro that uses systemd, such as Debian, Ubuntu, Fedora or RHEL.

Every night each server copies its important folders to a backup disk. Each night's backup is a complete, dated folder you can just open and browse — no special tool needed to get a file back. Files that haven't changed since the previous night aren't copied again, so a new backup takes up only as much space as what actually changed.

Backups end up here:

<backup disk>/<server name>/<what was backed up>/<date>/

For example /mnt/katz/backup/rigel/services/2026-10-14/.

What happens during a backup

  1. Safety checks. The script makes sure the backup disk is mounted and that every folder it's about to back up exists and isn't empty. If anything looks wrong, it stops before touching anything.
  2. Back up the things that can be copied while running, such as a file share.
  3. Stop Docker (optional), so databases and other container data are in a consistent state.
  4. Back up the Docker stuff: the compose files and the volumes.
  5. Start Docker again. Docker is only down for as long as step 4 takes.
  6. Clean up old backups according to the retention rules (see How long backups are kept).

Steps 3 to 5 only matter on servers that run Docker. You choose per folder whether Docker has to be stopped for it, and you can switch the stopping off entirely with MANAGE_DOCKER=false (see The config file). Without Docker, it's simply: check, back up, clean up.

Setting up a new server

  1. Get the code onto the server, in the same place as on the other servers:

    git clone https://git.swill.be/rob/backup-snapshots.git /home/rob/services/scripts/backup
    cd /home/rob/services/scripts/backup
    

    If you put it somewhere else, change the ExecStart line in backup.service to point to the new location. The installer will refuse to run until they match.

  2. Create a config file for this server and fill it in:

    cp backup.conf.example backup.conf.$(hostname -s)
    nano backup.conf.$(hostname -s)
    

    See The config file below for what to fill in.

  3. Install the nightly schedule:

    sudo ./install-units.sh
    
  4. Do a test run and watch it:

    sudo systemctl start backup.service
    journalctl -t backup -f
    
  5. Only on Fedora/RHEL-style servers with SELinux: do the one-time SELinux fix before relying on the backups.

Everyday use

I want to… Command
see when the next backup runs, and when the last one ran systemctl list-timers backup.timer
see whether the last backup succeeded systemctl status backup.service
watch a backup while it runs journalctl -t backup -f
read the log since the last reboot journalctl -t backup -b
run a backup right now sudo systemctl start backup.service
restore a file just copy it out of the dated folder on the backup disk

Start backups with systemctl start, not by running ./backup.sh in a terminal. If you run the script by hand and your SSH or Cockpit session drops, the backup gets killed halfway through. The script will restart Docker, but that night's backup is lost. Started through systemd, it keeps running no matter what happens to your session.

The config file

Each server has its own config file, named after the server: backup.conf.rigel, backup.conf.saiph, and so on. The script picks the right one automatically. backup.conf.rigel has comments explaining every setting.

The settings you'll actually touch:

  • BACKUP_BASE — where the backups go, e.g. /mnt/katz/backup. This folder must already exist; the script never creates it (see Safety).

  • REQUIRE_MOUNTS — disks that must be mounted before a backup is allowed to start. Normally the backup disk.

  • BACKUP_JOBS — what to back up. One line per folder:

    BACKUP_JOBS=(
        "filepile|/mnt/courage/filepile|no"
        "services|/home/rob/services|yes"
        "docker-volumes|/var/lib/docker/volumes|yes"
    )
    

    Each line is name | folder | does Docker need to be stopped?. The name is what the folder on the backup disk is called. To skip certain files in a job, list them in backup-excludes-<name>.txt (optional).

  • MANAGE_DOCKER — whether the script stops and starts Docker at all. Set it to false on a server without Docker, or if you'd rather back up while your containers keep running. Jobs marked yes are then backed up without stopping anything.

  • BACKUP_SCHEDULE — when the backup runs. The default is every night at 02:00:

    BACKUP_SCHEDULE="*-*-* 02:00:00"     # every day at 02:00
    BACKUP_SCHEDULE="Mon..Fri 03:30"     # weekdays at 03:30
    

    After changing the schedule, run sudo ./install-timer.sh, otherwise the change does nothing. You can check a schedule first with systemd-analyze calendar "<schedule>".

  • Retention settings — see the next section.

How long backups are kept

When RUN_RETENTION=true, old backups are cleaned up after each successful backup. A backup is kept if it matches any of these rules:

  1. It's one of the last KEEP_DAILY_DAYS backups (14 on the servers).
  2. It was made on a Sunday last month (WEEKLY_DAY=0 means Sunday).
  3. It's the first backup of one of the last KEEP_MONTHLY full months (2 on the servers).

Everything else is deleted. So you always have the last two weeks day by day, last month week by week, and one backup for each of the two months before that. The rules overlap, so the total is smaller than the numbers added up.

The rules apply to each job separately. The summary at the end of the log adds them all up, so three jobs with 14 backups each reports 42.

To see what would be deleted without actually deleting anything:

./backup-retention.sh --dry-run

Without --dry-run it really deletes, so preview first when you change these settings.

Safety

The scripts are built so that a problem never makes things worse:

  • No backup disk, no backup. If the backup disk isn't mounted, the script stops. It won't quietly fill up the server's own disk instead.
  • No empty backups. If a folder to back up is missing or empty (for example because its disk didn't mount), the script stops. Otherwise it would save an empty backup, and the cleanup could then throw away the last good one.
  • Docker always comes back. When the script stops Docker, whatever goes wrong, Docker is started again at the end. If Docker refuses to stop, nothing is backed up and Docker is left as it was.
  • One problem doesn't stop the rest. If one job fails, the others still run, and all failures are reported together at the end.
  • No half backups. A backup that fails halfway is deleted, so every dated folder you see is complete.
  • No cleanup after a failure. Old backups are only removed after a backup that fully succeeded.
  • Only one at a time. A second backup can't start while one is running.

Good to know

Updating after a git pull

The install script copies the schedule files into systemd's own folder, so a git pull doesn't change them automatically. After pulling, just run the installer again:

sudo ./install-units.sh

Changes to the scripts themselves (backup.sh and friends) or to the config file take effect immediately — except the schedule, which needs sudo ./install-timer.sh.

Copy versus link install

sudo ./install-units.sh --link links the systemd files to the repo instead of copying them. That saves a step after a git pull, but not much: systemd still needs a sudo systemctl daemon-reload before it notices the change. And if /home is on a separate disk that mounts late during boot, the link can point to nothing at boot time and the backup silently never runs. Check with findmnt /home: no output means /home isn't separate and linking is safe. Copying has neither problem, which is why it's the default.

How the schedule works under the hood

The backup is run by systemd. backup.service describes what to run; backup.timer describes when. systemd can't read the config file, so install-timer.sh copies BACKUP_SCHEDULE into /etc/systemd/system/backup.timer.d/schedule.conf, which overrides the timer's built-in 02:00 schedule. ./install-timer.sh --print shows what it would write without changing anything.

To go back to the built-in schedule:

sudo rm /etc/systemd/system/backup.timer.d/schedule.conf
sudo systemctl daemon-reload
sudo systemctl restart backup.timer

Only ever enable the timer, never backup.service itself — that would run a backup at every boot.

To install by hand instead of with the installer:

sudo cp backup.service backup.timer /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
sudo ./install-timer.sh

SELinux (Fedora / RHEL only)

On servers with SELinux, rsync (the copy tool) isn't allowed to read some folders or write to the backup disk by default. The backups still work, but the logs fill up with tens of thousands of "SELinux is preventing…" warnings per night. Check with:

getenforce                                                     # is SELinux on?
journalctl --since "-1 day" | grep -c "SELinux is preventing"  # how many warnings?

To fix it, run this once as root, with your own BACKUP_BASE instead of /mnt/second/backup:

setsebool -P rsync_full_access on
semanage fcontext -a -t rsync_data_t '/mnt/second/backup(/.*)?'
restorecon -Rv /mnt/second/backup

(semanage is in the policycoreutils-python-utils package.) Then do a test run and count the warnings again; it should be close to zero. Don't just hide the notifications — the problem would still be there.

What's in this repo

File What it is
backup.sh The backup itself.
backup-retention.sh Cleans up old backups. Runs after backup.sh, or on its own.
backup-common.sh Shared code used by the other scripts.
backup.conf.<server> The config for each server.
backup.conf.example Starting point for a new server.
backup-excludes-<job>.txt Files to skip for that job. Optional.
backup.service, backup.timer Tell systemd what to run and when.
install-units.sh One-step install on a new server.
install-timer.sh Applies BACKUP_SCHEDULE after you change it.