Backup, restore and running in production

A restore drill, the green build a missing secrets folder gives you, and what operating a controller involves.

Advanced17 min · lesson 15 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (19 files, 8 KB): backup.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: 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: df68687147c0e166c74c36bc329a4d18aad3b13a1e319514cc04debe8e5d813a

A 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

ubuntu@secopslog-cicd:~/lab/backup · Jenkins 2.580.2
$ ./up.sh
agent lab-agent: online Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ echo lab-token-0123456789 | ./add-secret.sh api-token && ./seed.sh app app/ && ./newjob.sh app app && ./run-job.sh app | tail -3
credential api-token stored pushed app: 6e84de5 first commit job app reads Jenkinsfile from git://git/app.git token length: 20 Finished: SUCCESS build #1 result: SUCCESS
$ docker compose exec -T controller sh -c "cd /var/jenkins_home && du -sh plugins war jobs secrets credentials.xml workspace 2>/dev/null"
169M plugins 66M war 84K jobs 44K secrets 4.0K credentials.xml 208K workspace
In JENKINS_HOMEBack up?Why
jobs/, config.xml, users/YesJob configs, build history, accounts
credentials.xmlYesStored secrets, encrypted
secrets/Yes, in a separate archiveThe keys that decrypt them; with credentials.xml they give every secret in plain text
plugins/, war/NoThey come from the image; 235 MB of the home here
workspace/, caches/NoRebuilt by the next build

Because the image and YAML define the server, the backup only needs what happened on it:

backup.sh
#!/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 pipefail
img=alpine:3.24.4@sha256:93b41ef40b1677fce8fcc64ff4af09daddfacf5040afde618bc46c1e2d1cc7d4
(umask 077; mkdir -p backup)
docker compose --progress quiet stop -t 30 controller
trap 'docker compose --progress quiet up -d --wait controller' EXIT # restart Jenkins even if tar fails
docker run --rm -v lab-jenkins_lab-jenkins-home:/home:ro -v "$PWD/backup:/backup" "$img" sh -ec '
umask 077
tar -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 ./secrets
chown '"$(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)"
ubuntu@secopslog-cicd:~/lab/backup · Jenkins 2.580.2
$ ./backup.sh
backup: home 4.9M, secrets 4.0K
$ ls backup; tar -tzf backup/jenkins-home.tgz | grep -E "^\./(credentials.xml|jobs/app/config.xml|jobs/app/builds/1/build.xml|secrets/.*)$"; tar -tzf backup/jenkins-secrets.tgz | grep -E "^\./secrets/(master.key|hudson.util.Secret)$"
jenkins-home.tgz jenkins-secrets.tgz ./credentials.xml ./jobs/app/builds/1/build.xml ./jobs/app/config.xml ./secrets/hudson.util.Secret ./secrets/master.key

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:

ubuntu@secopslog-cicd:~/lab/backup · Jenkins 2.580.2
$ docker compose --progress quiet --profile agent rm -fsv controller agent; docker volume rm lab-jenkins_lab-jenkins-home >/dev/null; docker compose --progress quiet up -d --wait controller && ./jk.sh /api/json | jq -c "{jobs: [.jobs[].name]}"
Going to remove lab-jenkins-agent, lab-jenkins {"jobs":[]}

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:

restore.sh
#!/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 pipefail
img=alpine:3.24.4@sha256:93b41ef40b1677fce8fcc64ff4af09daddfacf5040afde618bc46c1e2d1cc7d4
with_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" ]; then
tar -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; }
fi
docker compose --profile agent rm -fsv agent controller >/dev/null 2>&1
docker volume rm lab-jenkins_lab-jenkins-home >/dev/null
docker 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/null
docker 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
ubuntu@secopslog-cicd:~/lab/backup · Jenkins 2.580.2
$ ./restore.sh && ./jk.sh /job/app/lastBuild/api/json | jq -r "\"last build #\(.number) \(.result)\""
agent lab-agent: online last build #1 SUCCESS
$ ./run-job.sh app | tail -3
token length: 20 Finished: SUCCESS build #2 result: SUCCESS

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:

ubuntu@secopslog-cicd:~/lab/backup · Jenkins 2.580.2
$ ./restore.sh --without-secrets 2>&1 | tail -1
agent lab-agent: online
$ ./run-job.sh app | tail -3
token length: 78 Finished: SUCCESS build #2 result: SUCCESS

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.

Watch out
Don't test this on a real server. And if you lose 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:

ubuntu@secopslog-cicd:~/lab/backup · Jenkins 2.580.2
$ docker compose --profile agent rm -fsv agent >/dev/null 2>&1; ./agent.sh; docker compose --profile agent exec -T agent sh -c "ls /home/jenkins/agent/workspace 2>/dev/null | wc -l"
agent lab-agent: online 0
$ ./down.sh
lab stopped and its data removed

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:

TopicWhat to know
No active-active HAA 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.
SizingSet 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.
MonitoringWatch 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.
UpgradesTake 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 proxyInbound 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.

Quick check
01After a restore, every job is present and builds go green, but deploys fail with "invalid token". Which restore mistake fits these symptoms?
Incorrect — Without it, jobs using withCredentials would fail with an obvious error rather than authenticating badly.
Correct — In the lab, the build stayed SUCCESS with a 78-character value instead of the real 20-character token.
Incorrect — Leaving workspaces out is correct, and they aren't where credentials are stored.
Incorrect — The stored token isn't tied to an agent's name.
02Which is the best evidence that your Jenkins backup can be restored?
Incorrect — That shows an archive was written. It says nothing about a restore.
Incorrect — That's necessary, but it doesn't show that Jenkins can use them.
Incorrect — The lab's bad restore looked just like this.
Correct — Only a real use of a secret separates the good restore from the bad one.
03What do disposable, one-build agents improve the most?
Correct — Each build starts from a known image, which is why the lab's new agent had zero workspaces.
Incorrect — New agents start with cold caches, so they're often slower without extra work.
Incorrect — The controller stores the same secrets as before.
Incorrect — Workspaces are excluded anyway, on controllers and agents alike.

Related