Skip to content

Deploying restic rest-server as a backup target on AlmaLinux 9 (production-ready)

Stand up a hardened restic rest-server on AlmaLinux 9 — append-only mode, htpasswd auth, nginx + Let's Encrypt, systemd unit, retention pruning cron.

You’ve been asked to provide a central backup target that the entire fleet’s restic jobs can push to. The README is small, you compile rest-server, you stick it behind nginx, you give every node the same shared password, you call it done. Eight weeks later, a compromised application server runs restic forget --prune against the central repo and wipes the backups of every other host that shared the credential — because the repo was not in append-only mode, the auth was a single shared account, and there were no per-host repositories. The data the backups were supposed to protect is now the only copy.

What’s happening

A rest-server deployment that is actually production-ready needs more than the binary plus a reverse proxy. Every client repository must be served in append-only mode (--append-only), so a compromised client can write new snapshots but cannot delete existing ones — pruning runs on the server, from a separate trusted path. Authentication must be htpasswd-based with one account per host (not a shared password), so a stolen credential reveals which client was compromised and what scope it had. Each client repository must be its own subdirectory under --private-repos so a client account only sees its own repo. nginx must terminate TLS with a Let’s Encrypt cert on an automatic renewal timer — restic refuses plain HTTP for repos that contain real data. The rest-server VM is itself the storage tier for backups, so its data volume needs XFS with noatime, capacity headroom with alerting at 80%, and the VM itself still gets VM-level snapshots even though the content is backups — because rest-server’s on-disk format is a directory tree, a corrupted filesystem on the host loses every client repo at once. Retention has to be a scheduled cron, not a manual exercise. None of these are individually critical. They compound — skip them and the backup target turns into a single point of catastrophic loss.

How to verify

After install, the listeners should look like this:

ss -lntp | grep -E ':(80|443|8000)\b'
# 127.0.0.1:8000   rest-server  (behind nginx)
# 0.0.0.0:443      nginx        (TLS, public)

The rest-server itself must bind to 127.0.0.1. Per-host repo isolation:

ls -la /srv/restic
# drwx------ rest-server rest-server  hostA/
# drwx------ rest-server rest-server  hostB/

A client-side check that the server accepts the credential and the repo answers:

restic -r rest:https://hostA:PASS@backup.example.com/hostA/ snapshots

Append-only sanity check — from a client, a restic forget --prune must fail:

restic -r rest:https://hostA:PASS@backup.example.com/hostA/ forget --prune --keep-last 1
# error: server in append-only mode

If forget --prune succeeds against the client account, append-only is not enabled and a compromised client can wipe its own backups.

The fix

Save the script below as /root/setup-restic-rest-server-almalinux9.sh, then run it. It performs the full installer flow on a fresh AlmaLinux 9.x VM: base update + EPEL, kernel tuning, file limits, SSH hardening with root key preserved, fail2ban for SSH, dnf-automatic.timer for security updates, XFS on the data drive with noatime, the rest-server binary from the upstream release, a rest-server system user, /etc/default/rest-server with --append-only and --private-repos, a systemd unit with LimitNOFILE=64000, an htpasswd file at /etc/rest-server/htpasswd ready to receive per-host entries, nginx in front of :8000 with Let’s Encrypt TLS and client_max_body_size 0, and a weekly retention prune cron that the server runs (not the clients) against each repo with a sensible ladder.

#!/usr/bin/env bash
set -euo pipefail

### AlmaLinux 9.x restic rest-server clean VM installer
### NOTE: The content IS backups, but the VM itself still gets VM-level snapshots.
### Keeps root SSH key login enabled
### Disables SSH password login
### Enables automatic security updates
### Installs and configures rest-server + nginx + Let's Encrypt
### Reboot remains manual

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root."
  exit 1
fi

: "${REST_DOMAIN:=backup.example.com}"
: "${LE_EMAIL:=ops@example.com}"
: "${REST_SERVER_VERSION:=0.13.0}"
: "${REST_DATA_DRIVE:=/dev/vdb}"

echo
echo "========================================"
echo "restic rest-server AlmaLinux 9.x Setup"
echo "========================================"
echo

echo "==> Updating base OS"
dnf -y update
dnf -y install epel-release
dnf -y update

echo "==> Installing base packages"
dnf -y install \
  vim curl wget ca-certificates gnupg2 chrony fail2ban \
  dnf-automatic policycoreutils-python-utils lsof tcpdump \
  bind-utils tar gzip unzip iproute net-tools xfsprogs \
  httpd-tools nginx restic

echo "==> Enabling time sync"
systemctl enable --now chronyd
timedatectl set-ntp true

echo "==> Applying kernel/network tuning"
cat > /etc/sysctl.d/99-rest-server.conf <<'SYSCTL'
net.core.somaxconn = 4096
net.core.netdev_max_backlog = 250000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.ip_local_port_range = 10240 65535

vm.swappiness = 10

kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
fs.file-max = 2097152
SYSCTL
sysctl --system

echo "==> Setting file limits"
cat > /etc/security/limits.d/99-rest-server.conf <<'LIMITS'
* soft nofile 64000
* hard nofile 64000
root soft nofile 64000
root hard nofile 64000
rest-server soft nofile 64000
rest-server hard nofile 64000
LIMITS

echo "==> Hardening SSH while keeping root SSH key login enabled"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%H%M%S)

set_sshd_option() {
  local key="$1" value="$2"
  if grep -qE "^[#[:space:]]*${key}[[:space:]]+" /etc/ssh/sshd_config; then
    sed -i "s|^[#[:space:]]*${key}[[:space:]].*|${key} ${value}|" /etc/ssh/sshd_config
  else
    echo "${key} ${value}" >> /etc/ssh/sshd_config
  fi
}

set_sshd_option PermitRootLogin prohibit-password
set_sshd_option PasswordAuthentication no
set_sshd_option PubkeyAuthentication yes
set_sshd_option X11Forwarding no
set_sshd_option ClientAliveInterval 300
set_sshd_option ClientAliveCountMax 2
set_sshd_option MaxAuthTries 4

sshd -t
systemctl restart sshd

echo "==> Configuring fail2ban for SSH"
mkdir -p /etc/fail2ban/jail.d
cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN
systemctl enable --now fail2ban

echo "==> Configuring automatic security updates"
cat > /etc/dnf/automatic.conf <<'DNFAUTO'
[commands]
upgrade_type = security
random_sleep = 900
network_online_timeout = 60
download_updates = yes
apply_updates = yes

[emitters]
emit_via = stdio

[base]
debuglevel = 1
DNFAUTO
systemctl enable --now dnf-automatic.timer

echo "==> Preparing data drive (XFS, noatime)"
if [[ ! -b "${REST_DATA_DRIVE}" ]]; then
  echo "Data drive ${REST_DATA_DRIVE} not present. Aborting."
  exit 1
fi
if ! blkid "${REST_DATA_DRIVE}" | grep -q 'TYPE="xfs"'; then
  mkfs.xfs -f -L "restic" "${REST_DATA_DRIVE}"
fi

UUID="$(blkid -s UUID -o value ${REST_DATA_DRIVE})"
mkdir -p /srv/restic
if ! grep -q "${UUID}" /etc/fstab; then
  echo "UUID=${UUID}  /srv/restic  xfs  defaults,noatime,nofail  0  0" >> /etc/fstab
fi
mount /srv/restic

echo "==> Creating rest-server user"
if ! id rest-server >/dev/null 2>&1; then
  useradd -r -M -d /srv/restic -s /sbin/nologin rest-server
fi
chown rest-server:rest-server /srv/restic
chmod 700 /srv/restic

echo "==> Installing rest-server ${REST_SERVER_VERSION}"
if [[ ! -f /usr/local/bin/rest-server ]]; then
  curl -fsSLo /tmp/rest-server.tar.gz \
    "https://github.com/restic/rest-server/releases/download/v${REST_SERVER_VERSION}/rest-server_${REST_SERVER_VERSION}_linux_amd64.tar.gz"
  tar -xzf /tmp/rest-server.tar.gz -C /tmp
  cp /tmp/rest-server_${REST_SERVER_VERSION}_linux_amd64/rest-server /usr/local/bin/rest-server
  chmod +x /usr/local/bin/rest-server
fi

echo "==> Writing rest-server environment"
mkdir -p /etc/rest-server
touch /etc/rest-server/htpasswd
chown rest-server:rest-server /etc/rest-server/htpasswd
chmod 600 /etc/rest-server/htpasswd

cat > /etc/default/rest-server <<'RESTENV'
REST_SERVER_OPTS="--path /srv/restic \
  --listen 127.0.0.1:8000 \
  --htpasswd-file /etc/rest-server/htpasswd \
  --private-repos \
  --append-only \
  --prometheus \
  --log /var/log/rest-server.log"
RESTENV

touch /var/log/rest-server.log
chown rest-server:rest-server /var/log/rest-server.log

echo "==> Writing systemd unit"
cat > /etc/systemd/system/rest-server.service <<'UNIT'
[Unit]
Description=restic rest-server backup target
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=rest-server
Group=rest-server
EnvironmentFile=/etc/default/rest-server
ExecStart=/usr/local/bin/rest-server $REST_SERVER_OPTS
LimitNOFILE=64000
Restart=on-failure
RestartSec=10s
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/srv/restic /var/log/rest-server.log

[Install]
WantedBy=multi-user.target
UNIT

systemctl daemon-reload

echo "==> Issuing Let's Encrypt cert"
dnf -y install certbot python3-certbot-nginx

cat > /etc/nginx/conf.d/rest-server.conf <<NGINXCONF
server {
    listen 80;
    server_name ${REST_DOMAIN};
    location / { return 404; }
}
NGINXCONF
systemctl enable --now nginx
nginx -t && systemctl reload nginx

certbot certonly --nginx -n --agree-tos -m "${LE_EMAIL}" -d "${REST_DOMAIN}"

echo "==> Writing real nginx site"
cat > /etc/nginx/conf.d/rest-server.conf <<NGINXCONF
server {
    listen 80;
    server_name ${REST_DOMAIN};
    return 301 https://\$host\$request_uri;
}

server {
    listen 443 ssl http2;
    server_name ${REST_DOMAIN};

    ssl_certificate     /etc/letsencrypt/live/${REST_DOMAIN}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/${REST_DOMAIN}/privkey.pem;

    add_header Strict-Transport-Security "max-age=31536000" always;

    client_max_body_size 0;
    proxy_buffering off;
    proxy_request_buffering off;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_connect_timeout 600s;
        proxy_read_timeout    600s;
        proxy_send_timeout    600s;
    }
}
NGINXCONF

nginx -t && systemctl reload nginx

echo "==> Starting rest-server"
systemctl enable --now rest-server

echo "==> Waiting for rest-server on 127.0.0.1:8000"
for i in {1..30}; do
  if ss -lntp | grep -q '127.0.0.1:8000'; then break; fi
  sleep 1
done

echo "==> Writing server-side retention prune cron"
cat > /usr/local/sbin/restic-prune-all.sh <<'PRUNE'
#!/usr/bin/env bash
set -euo pipefail
# Server-side prune. Clients can only append; the server is the only path
# that can delete. Run as root, against each per-host repo directly on disk.
# A separate trusted password is used here, set in REPO_PASSWORDS_FILE.
REPO_ROOT="/srv/restic"
RETENTION="--keep-daily 7 --keep-weekly 4 --keep-monthly 12 --keep-yearly 3"

if [[ ! -f /etc/rest-server/repo-passwords ]]; then
  echo "Missing /etc/rest-server/repo-passwords (format: hostname:password)"
  exit 0
fi

while IFS=: read -r host pw; do
  repo="${REPO_ROOT}/${host}"
  [[ -d "${repo}" ]] || continue
  echo "=== forget+prune: ${host} ==="
  RESTIC_PASSWORD="${pw}" restic -r "${repo}" forget ${RETENTION} --prune || true
done < /etc/rest-server/repo-passwords
PRUNE
chmod +x /usr/local/sbin/restic-prune-all.sh

touch /etc/rest-server/repo-passwords
chmod 600 /etc/rest-server/repo-passwords

cat > /etc/cron.d/restic-prune <<'CRON'
30 3 * * 0 root /usr/local/sbin/restic-prune-all.sh >> /var/log/restic-prune.log 2>&1
CRON

echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Services:"
systemctl is-active rest-server nginx chronyd fail2ban || true
echo
echo "Listeners:"
ss -lntp | grep -E ':(80|443|8000)\b' || true
echo
echo "Backup target URL: https://${REST_DOMAIN}/"
echo
echo "Add a per-host account (one per backup client):"
echo "  htpasswd -B /etc/rest-server/htpasswd hostA"
echo "  chown rest-server:rest-server /etc/rest-server/htpasswd"
echo "  install -d -o rest-server -g rest-server -m 700 /srv/restic/hostA"
echo
echo "Initialise the repo from the client side:"
echo "  restic -r rest:https://hostA:PASS@${REST_DOMAIN}/hostA/ init"
echo
echo "Record the client repo password in /etc/rest-server/repo-passwords"
echo "(format: hostname:password) so the weekly prune cron can run server-side."

Make it executable and run it:

chmod 700 /root/setup-restic-rest-server-almalinux9.sh
REST_DOMAIN=backup.example.com \
LE_EMAIL=ops@example.com \
/root/setup-restic-rest-server-almalinux9.sh

After it finishes, on the server, add one htpasswd entry per backup client (htpasswd -B /etc/rest-server/htpasswd hostA), create the matching /srv/restic/hostA directory, and from the client run restic -r rest:https://hostA:PASS@backup.example.com/hostA/ init. Record the per-repo password in /etc/rest-server/repo-passwords so the server-side prune cron can apply retention without the client ever holding delete-capable credentials. The rest-server is in append-only mode by design — the server prunes, the clients do not.

Run the backup target inside a Stack Harbor-managed environment with capacity alerting, monthly restore drills, and the prune job under observation: Monitoring + Support →.