Skip to content

Set up the GitLab off-site backup

Goal: Nightly automated backup of GitLab, shipped from the DO k3s cluster to the on-prem mac-mini for disaster recovery.

Environment: - GitLab pod: default namespace, node ubuntu-mashup-gitlab-haproxy (DO k3s cluster) - GitLab's data is NFS-backed by that same node at /data/gitlab/data2 (repos/DB/uploads) and /data/gitlab/config (secrets/config) - Backup target: mac-mini, Tailscale IP <MAC_MINI_TAILSCALE_IP>, user non, path /Users/non/backups/gitlab (macOS's / is read-only, can't use /backups)


Step 1 — Create a folder on mac-mini to receive backups

On mac-mini:

mkdir -p ~/backups/gitlab
df -h ~/backups          # sanity check free space

Step 2 — Create a dedicated SSH key on the gitlab node

On the gitlab node:

sudo ssh-keygen -t ed25519 -f /root/.ssh/gitlab_backup_key -N "" -C "gitlab-backup-to-macmini"
sudo cat /root/.ssh/gitlab_backup_key.pub
Copy the printed ssh-ed25519 ... line.

Step 3 — Authorize that key on mac-mini

On mac-mini:

mkdir -p ~/.ssh && chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
Paste the copied key on its own line. Save (Ctrl+O, Enter, Ctrl+X).

Step 4 — Test the connection

On the gitlab node:

sudo ssh -i /root/.ssh/gitlab_backup_key non@<MAC_MINI_TAILSCALE_IP> echo ok
Accept the host key prompt (yes) the first time. Must print ok.

Step 5 — Create a private staging folder

On the gitlab node:

sudo mkdir -p /var/backups/gitlab-staging
This is separate from GitLab's live NFS-shared backup folder — a private holding area so the (potentially large, slow-to-transfer) backup tar isn't sitting inside the live folder any longer than necessary.

Step 6 — Create the backup script

On the gitlab node:

sudo nano /usr/local/bin/gitlab-backup-offsite.sh
Paste in:

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

KUBECONFIG=/etc/rancher/k3s/k3s.yaml
NAMESPACE=default
REMOTE_USER="non"
REMOTE_HOST="<MAC_MINI_TAILSCALE_IP>"
REMOTE_DIR="/Users/non/backups/gitlab"
SSH_KEY="/root/.ssh/gitlab_backup_key"
STAGING_DIR="/var/backups/gitlab-staging"
KEEP_COPIES=2

export KUBECONFIG
mkdir -p "$STAGING_DIR"

echo "[$(date)] Finding gitlab pod..."
POD=$(kubectl get pods -n "$NAMESPACE" --no-headers -o custom-columns=":metadata.name" | grep -m1 gitlab)
[ -n "$POD" ] || { echo "No gitlab pod found"; exit 1; }
echo "  -> $POD"

echo "[$(date)] Creating backup inside pod (registry images skipped)..."
kubectl exec -n "$NAMESPACE" "$POD" -- gitlab-backup create STRATEGY=copy SKIP=registry

echo "[$(date)] Moving tar to private staging folder..."
mv /data/gitlab/data2/backups/*_gitlab_backup.tar "$STAGING_DIR"/

echo "[$(date)] Sending tar to mac-mini, deleting local copy once confirmed..."
rsync -avz --remove-source-files \
  -e "ssh -i $SSH_KEY -o StrictHostKeyChecking=accept-new" \
  "$STAGING_DIR"/*_gitlab_backup.tar \
  "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/"

echo "[$(date)] Sending a COPY of secrets/config (originals stay on this node)..."
rsync -avz \
  -e "ssh -i $SSH_KEY -o StrictHostKeyChecking=accept-new" \
  /data/gitlab/config/gitlab-secrets.json \
  /data/gitlab/config/gitlab.rb \
  "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/"

echo "[$(date)] Keeping only the ${KEEP_COPIES} newest backups on mac-mini..."
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new "${REMOTE_USER}@${REMOTE_HOST}" \
  "cd ${REMOTE_DIR} && ls -t *_gitlab_backup.tar | tail -n +$((KEEP_COPIES + 1)) | xargs -r rm --"

echo "[$(date)] Done."
Save (Ctrl+O, Enter, Ctrl+X).

Step 7 — Make it executable

sudo chmod +x /usr/local/bin/gitlab-backup-offsite.sh

Step 8 — Run it once by hand to verify

sudo /usr/local/bin/gitlab-backup-offsite.sh
Watch for each [date] ... line to print in order, ending with Done. and no errors. This step alone can take 10-40+ minutes depending on repo count and network speed — that's normal.

Then confirm on mac-mini:

ls -la ~/backups/gitlab
Expect: one *_gitlab_backup.tar, gitlab-secrets.json, gitlab.rb.

Step 9 — Schedule it to run automatically every night

On the gitlab node:

sudo crontab -e
Add this line at the bottom:
0 2 * * * /usr/local/bin/gitlab-backup-offsite.sh >> /var/log/gitlab-backup-offsite.log 2>&1
Save and exit. Confirm it saved:
sudo crontab -l

Step 10 — Check the log after the first automatic run

The next morning:

sudo cat /var/log/gitlab-backup-offsite.log
Should show the same [date] ... progress lines from Step 8, with no errors.


Why some steps are the way they are

  • SKIP=registry — container images inflate the backup a lot (in testing here, from ~7GB to 20GB+) and are regenerable from CI; excluded to avoid risking the gitlab node's disk filling up mid-backup.
  • Staging folder (Step 5) — the tar is large and slow to transfer; moving it out of GitLab's live NFS-shared folder first means that shared folder isn't tied up for the whole transfer.
  • --remove-source-files — deletes the tar from the gitlab node only after mac-mini confirms it received it intact. Never blind-deletes on a failed transfer.
  • gitlab-secrets.json / gitlab.rb are copied, never deleted from the gitlab node — these are the live files GitLab actively reads to run; only a safety-net copy goes to mac-mini.
  • Keep 2 copies, by count (Step 6's last line) — not a time window, per your preference; prunes anything past the 2 newest on mac-mini.
  • NAMESPACE=default — GitLab's pod lives in the default namespace on this cluster, not a dedicated gitlab one.

Known trade-offs

  • Registry (container) images are not backed up by this process at all.
  • The gitlab node's /data and / share one disk (310GB) — worth periodically checking df -h / doesn't get tight again.
  • Retention is by count (2), not time — if a nightly run fails silently for several days, check the log periodically rather than assuming it's always current.
  • Do a periodic restore test — an untested backup isn't a verified backup.