Backup, restore and running in production
A restore drill, the green build a missing secrets folder gives you, and what operating a controller involves.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/backup.tar.gz && tar -xzf backup.tar.gz, which creates ~/lab/backup/. SHA-256: df68687147c0e166c74c36bc329a4d18aad3b13a1e319514cc04debe8e5d813aA disk fails, and luckily there's a nightly backup. You restore it, Jenkins starts, jobs are back, the build goes green. A week later your deploys start failing against an API with an "invalid token" error. In this lesson you'll see the exact way that can happen, because a backup that's never been restored is only a hope. You'll restore one properly, restore one badly, and read what a bad restore looks like when nothing reports an error.
What to back up
In JENKINS_HOME | Back up? | Why |
|---|---|---|
jobs/, config.xml, users/ | Yes | Job configs, build history, accounts |
credentials.xml | Yes | Stored secrets, encrypted |
secrets/ | Yes, in a separate archive | The keys that decrypt them; with credentials.xml they give every secret in plain text |
plugins/, war/ | No | They come from the image; 235 MB of the home here |
workspace/, caches/ | No | Rebuilt by the next build |
Because the image and YAML define the server, the backup only needs what happened on it:
#!/usr/bin/env bash# Archives JENKINS_HOME from a stopped controller, leaving out what the image or a build recreates.# secrets/ goes into its own archive: store it somewhere else, encrypted, because together with# jenkins-home.tgz it decrypts every stored credential.set -euo pipefailimg=alpine:3.24.4@sha256:93b41ef40b1677fce8fcc64ff4af09daddfacf5040afde618bc46c1e2d1cc7d4(umask 077; mkdir -p backup)docker compose --progress quiet stop -t 30 controllertrap 'docker compose --progress quiet up -d --wait controller' EXIT # restart Jenkins even if tar failsdocker run --rm -v lab-jenkins_lab-jenkins-home:/home:ro -v "$PWD/backup:/backup" "$img" sh -ec 'umask 077tar -czf /backup/jenkins-home.tgz -C /home --exclude=./workspace --exclude=./caches --exclude=./war \--exclude=./plugins --exclude=./logs --exclude=./secrets .tar -czf /backup/jenkins-secrets.tgz -C /home ./secretschown '"$(id -u):$(id -g)"' /backup/*.tgz'echo "backup: home $(du -h backup/jenkins-home.tgz | cut -f1), secrets $(du -h backup/jenkins-secrets.tgz | cut -f1)"
Two archives: a few megabytes of history and configuration, and 4 KB of keys. credentials.xml is in the first and secrets/master.key in the second. It stopped the controller first so the files were consistent, and the trap starts it again even if tar fails, so a broken backup can't leave Jenkins down. A real setup would pick a quiet window or snapshot the volume. secrets/ went into its own archive, because the two together decrypt every stored credential. Encrypt both, for example with age, and store the secrets archive somewhere different from the other one.
The drill
Destroy the volume and confirm everything's gone:
No jobs. Now restore and run the job, which proves more than a login page would. restore.sh checks both archives before it deletes anything, so a missing or wrong file stops it while the old data still exists:
#!/usr/bin/env bash# Replaces JENKINS_HOME with the backup. --without-secrets leaves out secrets/, to show why it matters.# Both archives are checked before anything is deleted.set -euo pipefailimg=alpine:3.24.4@sha256:93b41ef40b1677fce8fcc64ff4af09daddfacf5040afde618bc46c1e2d1cc7d4with_secrets=1; [ "${1:-}" = --without-secrets ] && with_secrets=tar -tzf backup/jenkins-home.tgz ./config.xml >/dev/null 2>&1 || { echo "backup/jenkins-home.tgz is not a JENKINS_HOME backup" >&2; exit 1; }if [ -n "$with_secrets" ]; thentar -tzf backup/jenkins-secrets.tgz ./secrets/master.key >/dev/null 2>&1 || { echo "backup/jenkins-secrets.tgz has no secrets/master.key" >&2; exit 1; }fidocker compose --profile agent rm -fsv agent controller >/dev/null 2>&1docker volume rm lab-jenkins_lab-jenkins-home >/dev/nulldocker volume create --label com.docker.compose.project=lab-jenkins --label com.docker.compose.volume=lab-jenkins-home \--label com.docker.compose.version="$(docker compose version --short)" lab-jenkins_lab-jenkins-home >/dev/nulldocker run --rm -v lab-jenkins_lab-jenkins-home:/home -v "$PWD/backup:/backup:ro" "$img" sh -ec "tar -xzf /backup/jenkins-home.tgz -C /home${with_secrets:+tar -xzf /backup/jenkins-secrets.tgz -C /home}chown -R 1000:1000 /home"docker compose --progress quiet up -d --wait controller./agent.sh
Build 1's history is back, the agent reconnected, and build 2 used the restored credential. token length: 20 is the correct length, as stored. This is what a passing drill looks like.
One more risk when you restore to a new host for real: if the old controller is still running, you now have two Jenkins servers with the same jobs, triggers and credentials, and both may build and deploy. Stop the old one first, or start the restored one in quiet mode (/quietDown stops new builds from starting) until you've switched traffic.
The restore that looks fine
Now restore without secrets/, as if someone had decided those files didn't matter, or stored them somewhere that was also lost:
SUCCESS, and that's the dangerous part. The token length is now 78 instead of 20. Jenkins generated new keys on startup, couldn't decrypt the stored value, and gave the build the encrypted text as though it were the secret. Nothing failed. The pipeline only printed its length, so it went green. A real pipeline would send that garbage to an API and get "invalid token", and you'd start by suspecting the API.
That's why a drill must exercise a credential and check the result, for example by calling something that requires it. "The UI loaded and the jobs are there" proves nothing about your secrets.
secrets/ for real, the stored credentials are gone. You re-enter every one, which is a good moment to rotate them, too. Also note what the build showed: the agent reconnected only because agent.sh fetched the new secret, since agent secrets also derive from those keys. Every agent would need new configuration.Disposable agents
Agents need no backup at all if nothing on them matters. Throw this one away and make a new one:
A new agent connected, with zero workspaces. Nothing from earlier builds is left to leak into the next one, and a compromised build can't persist. That's the model to aim for: start a fresh agent for each build. The Kubernetes and Docker plugins for Jenkins do this for you. Define them in JCasC, and agents become configuration, not machines someone has to look after. One-build agents also lose their caches, so the first builds after the switch get slower. The fix is a shared dependency cache or proxy, such as a package mirror, that builds read from but can't write their own files into.
Running the controller in production
Backups are one part of operating Jenkins. A few more points matter once other teams depend on it:
| Topic | What to know |
|---|---|
| No active-active HA | A Jenkins controller is a single server. Your availability is how fast you can restore or replace it, so time the drill. Large organisations split work across several controllers instead of growing one. |
| Sizing | Set the heap explicitly, as the lab image does, and size it from what the controller really uses. Pipeline Groovy runs on the controller, so many concurrent pipelines cost controller CPU and memory even with zero executors. |
| Monitoring | Watch queue length, executors in use, agent disconnects, heap, and free disk in JENKINS_HOME. The Metrics and Prometheus plugins export these; this lab installs neither. |
| Upgrades | Take each LTS release within a few weeks, and keep plugins current between them. Try the upgrade on a restored copy of production, then roll out with the previous image and a fresh backup ready. |
| Agents behind a proxy | Inbound agents use TCP port 50000 here. Agents started with -webSocket connect over the normal HTTPS port instead, which is easier to pass through a reverse proxy. |
Hands-on
In a fresh lab, write a restore check you'd trust. Store a credential whose value you can test, such as one a small script compares with a known hash in its environment, and make the job fail on a mismatch. Run it after a good restore and after ./restore.sh --without-secrets. Does it catch the bad one? Finish with ./down.sh. One version is in solutions/Jenkinsfile.check.
What you should see: after a good restore, the job prints api-token: decrypts correctly and passes. After --without-secrets, it prints api-token: WRONG VALUE after restore and fails. The build that used to stay green with a 78-character value now fails, which is the point of a drill.
Remember
Back up JENKINS_HOME without the parts your image rebuilds, keep secrets/ apart and encrypted, and restore on a schedule. The drill must use a credential for real, because the worst restore here finished SUCCESS with a 78-character token.
withCredentials would fail with an obvious error rather than authenticating badly.SUCCESS with a 78-character value instead of the real 20-character token.