Skip to content

Set up the Nexus off-site backup

Nightly automated backup of Nexus, shipped to the on-prem mac-mini. A Longhorn failure on nuc-a (2026-06-26 and 2026-10-01) corrupted the H2 database both times. With this backup you restore a known-good copy instead of rebuilding by hand.

[[TOC]]

Environment

Item Value
Nexus pod nexus namespace, pinned to node nuc-a, sonatype/nexus3:3.71.0 (H2 database)
Data volume Longhorn PVC nexus-data-longhorn
Backup target mac-mini, Tailscale IP 100.64.0.14, path /Users/<USER>/backups/nexus

<USER> is the account on the mac-mini that receives the backup.

What is on the volume:

Folder Size Why it matters
db/ about 30 MB (nexus.mv.db) The part that gets corrupted
blobs/ about 20 GB Artifact and image bytes
etc/, keystores/ small Needed to restore

How it runs

CronJob nexus-offsite-backup (nexus/backup-cronjob.yml in playtelly-iac) is deployed by ArgoCD with the rest of nexus/. It runs nexus/backup/nexus-backup.sh at 03:30 Bangkok time, after nexus-docker-cleanup (02:00, see Nexus Docker registry cleanup) and the H2 backup task (03:00).

  • Same node as Nexus. The PVC is ReadWriteOnce, which limits it to one node, not one pod. podAffinity puts the Job on whichever node runs Nexus, so it can mount the same volume. If Nexus moves, the Job follows it.
  • Database: a consistent H2 export made by Nexus's own task. A file copy of a live nexus.mv.db can be as broken as the corrupt one.
  • Blobs: copied with rsync into dated snapshots. Unchanged blobs are hard-linked to the previous snapshot, so the 2 snapshots kept cost about 20 GB plus the changes between them.

Set it up

1. Schedule the H2 backup inside Nexus

In the Nexus UI go to Settings → System → Tasks → Create task → Admin - Backup H2 Database.

Field Value
Name Nightly H2 backup
Location /nexus-data/backup
Task frequency Daily, 03:00 (the UI uses your browser's time zone, for example GMT+0700, so this is 03:00 Bangkok time)

Click Run once, then confirm a zip exists:

kubectl -n nexus exec deploy/nexus -c nexus -- ls -la /nexus-data/backup

The output lists a nexus-*.zip file.

2. Create a folder on the mac-mini

On the mac-mini:

mkdir -p ~/backups/nexus
df -h ~/backups

You need about 30 GB free.

3. Create an SSH key and host key

On any machine with kubectl, for example NUC-B:

mkdir -p ~/nexus-backup-key
ssh-keygen -t ed25519 -f ~/nexus-backup-key/id_ed25519 -N "" -C "nexus-backup-to-macmini"
ssh-keyscan -t ed25519 100.64.0.14 > ~/nexus-backup-key/known_hosts
cat ~/nexus-backup-key/id_ed25519.pub

The last command prints the public key (ssh-ed25519 ...). A pod starts with an empty known_hosts on every run, so the mac-mini's host key is saved now and shipped with the key. That way the Job checks it is talking to the real mac-mini and does not trust whatever answers.

4. Authorize the key on the mac-mini

On the mac-mini, add the printed ssh-ed25519 ... line to ~/.ssh/authorized_keys on its own line.

5. Store the key in the cluster

Never put the key in git.

kubectl -n nexus create secret generic nexus-backup-ssh \
  --from-file=id_ed25519=$HOME/nexus-backup-key/id_ed25519 \
  --from-file=known_hosts=$HOME/nexus-backup-key/known_hosts
rm -rf ~/nexus-backup-key

The output is secret/nexus-backup-ssh created.

6. Deploy

Merge to main. ArgoCD (nexus-prod) creates the CronJob and the nexus-backup-script-<hash> ConfigMap. To apply by hand instead:

kubectl apply -k nexus

7. Run it once by hand

kubectl -n nexus create job --from=cronjob/nexus-offsite-backup nexus-offsite-backup-manual
kubectl -n nexus logs -f job/nexus-offsite-backup-manual

The first run copies the full 20 GB; later runs send only what changed. The log ends with Done.

Then on the mac-mini:

ls -la ~/backups/nexus ~/backups/nexus/latest/ ~/backups/nexus/latest/db
du -sh ~/backups/nexus

Expect one dated folder, latest -> <that folder>, and inside it db/nexus-*.zip, etc/, keystores/ and blobs/. Run the Job a second time: it is much faster and du -sh barely grows, which confirms hard-linking works.

Remove the manual Job:

kubectl -n nexus delete job nexus-offsite-backup-manual

8. Check the first scheduled run

kubectl -n nexus get jobs | grep offsite
kubectl -n nexus logs job/<NEWEST_JOB_NAME>

<NEWEST_JOB_NAME> is the newest nexus-offsite-backup-xxxxx in the first command's output. The log ends with Done.

Restore Nexus from the backup

Use this when the Nexus database is corrupted or the volume is lost.

Before you start:

  1. Set the Longhorn setting auto-cleanup-system-generated-snapshot to false.
  2. Pause ArgoCD auto-sync on nexus-prod. Self-heal undoes manual changes.
  3. Suspend both CronJobs, so a nightly run doesn't overwrite good snapshots with broken data:
kubectl -n nexus patch cronjob nexus-docker-cleanup -p '{"spec":{"suspend":true}}'
kubectl -n nexus patch cronjob nexus-offsite-backup -p '{"spec":{"suspend":true}}'

Then restore:

  1. Scale Nexus down:
kubectl -n nexus scale deploy/nexus --replicas=0
  1. Start a temporary pod on nuc-a that mounts nexus-data-longhorn at /nexus-data and the nexus-backup-ssh Secret at /ssh (defaultMode: 0400), the same volumes as in backup-cronjob.yml. Install rsync openssh-client in it (for example alpine plus apk add). Then copy the snapshot back:
SSH="ssh -i /ssh/id_ed25519 -o UserKnownHostsFile=/ssh/known_hosts"
rsync -a -e "$SSH" '<USER>@100.64.0.14:/Users/<USER>/backups/nexus/latest/db/' /tmp/restore-db/

Move the corrupt db/ aside (db.corrupt-<DATE>), then unzip the zip from /tmp/restore-db/ into a fresh /nexus-data/db/ so that db/nexus.mv.db exists.

If blobs were lost too:

rsync -a -e "$SSH" <USER>@100.64.0.14:/Users/<USER>/backups/nexus/latest/blobs/ /nexus-data/blobs/

Finally give the restored files back to the Nexus user:

chown -R 200:200 /nexus-data
  1. Scale Nexus back up:
kubectl -n nexus scale deploy/nexus --replicas=1
  1. In Nexus run Repair - Reconcile component database from blob store. It re-adds anything uploaded after the database snapshot. Blobs are copied after the database, so they are always a superset of it.
  2. Resume both CronJobs and ArgoCD auto-sync, and set the Longhorn setting back.

Trade-offs

  • Up to about 24 hours of database changes can be lost (users, roles, repository config). The reconcile step recovers uploaded artifacts.
  • The mac-mini is on-prem, close to the NUCs. This protects against Longhorn and nuc-a failures, not against losing the whole site.
  • The Job installs bash rsync openssh-client from the Alpine mirror on every run, so it needs internet access (the same as nexus-docker-cleanup's pip install).
  • The volume is mounted read-write only so old H2 zips in /nexus-data/backup can be pruned. The script deletes nothing else there.
  • Retention is by count (2), the same as the GitLab backup. A problem noticed more than about 2 days late may already be in both copies. If runs fail, latest stops moving, so a failed Job in kubectl get jobs is the signal.
  • Do a restore test now and then, for example into a throwaway Nexus pod. An untested backup is not a verified backup.