Jenkins Backup: ThinBackup with S3 bucket script

Khalid shaikhKhalid shaikh
4 min read

Manual Script vs. Jenkins Backup Plugin

FactorManual ScriptJenkins Plugin (ThinBackup)
Ease of SetupRequires writing & maintaining a shell scriptGUI-based setup in Jenkins (no coding needed)
SchedulingRequires cron or external schedulerBuilt-in scheduler in Jenkins
Backup GranularityFull backup only (unless scripted otherwise)Can selectively backup configs, jobs, or plugins
Restore ProcessManual extraction + file placementOne-click restore from Jenkins UI
MonitoringMust set up logging manuallyBackup history is visible in Jenkins
DependenciesNeeds AWS CLI on the serverPlugin handles compression & archiving
MaintenanceMust update the script if Jenkins paths changePlugin auto-adapts to Jenkins changes

When to Use the Manual Script

You want minimal cost & overhead
You're comfortable with shell scripting & cron
You need full control over backup logic
Your Jenkins setup is simple (single server)

Example Use Case:

  • A small team with one Jenkins server where you can afford to manually verify backups occasionally.

When to Use the Jenkins Plugin

You prefer a GUI over CLI
You want built-in restore functionality
You need to exclude certain components (e.g., only backup jobs)
Non-technical team members might manage backups

Example Use Case:

  • A larger team where multiple admins might need to restore backups without SSH access.

Key Advantages of the Plugin You Might Overlook

  1. Delta Backups
    Some plugins (like ThinBackup) can perform incremental backups, saving only changes since the last backup. This reduces storage costs.

  2. Pre/Post Backup Hooks
    Plugins often let you run scripts before or after backup (e.g., to notify Slack).

  3. Built-in Retention Policies
    Auto-delete old backups after X days (no need to script find -mtime +30 -delete logic).

Install ThinkBackup Plugin from the available plugins in Jenkins.

Under Manage Jenkins > Tools and Actions.

You will find the ThinkBackup option.

Make settings in the Global Configuration option

  • Go to Manage Jenkins → System → ThinBackup

  • Scroll down to the ThinBackup option

  • Configure the following:

    • Backup directory: /var/lib/jenkins/backups (or another valid path)

    • Full backup schedule: 0 2 * * * (runs daily at 2 AM, cron syntax)

    • Enable:

      • Backup build results

      • Backup next build number

      • Backup user content

      • Backup config history

    • Save settings

For testing, create some random builds…

Go to Manage Jenkins → ThinBackup, click Backup Now to verify it works. Check the backup directory to confirm files were created.

Now for uploading the backup to S3, you should either give the EC2 instance IAM permissions for s3:PutObject on the desired S3 bucket or configure aws secret and access keys via aws configure command

Post that create a script to upload the backup to S3

/var/lib/jenkins/scripts/upload_to_s3.sh

#!/bin/bash

# Set your variables
BACKUP_DIR="/var/lib/jenkins/backups"
S3_BUCKET="s3://your_bucket_name/jenkins-backups"

# Find the latest backup folder
LATEST_BACKUP=$(ls -td "$BACKUP_DIR"/*/ | head -1)

# Zip the backup folder
ZIP_FILE="/tmp/jenkins-backup-$(date +%F-%H%M%S).zip"
zip -r "$ZIP_FILE" "$LATEST_BACKUP"

# Upload to S3
aws s3 cp "$ZIP_FILE" "$S3_BUCKET/"

# Optional: remove old zip
rm -f "$ZIP_FILE"

Run the script manually to test it

Now let’s say some pipeline gets deleted, or the whole server gets crashed, or any mishap happens

Then you can

  • Go to Jenkins Dashboard → Manage Jenkins → ThinBackup

  • Click on "Restore"

  • You'll see a list of backup folders (by timestamp)

  • Select the backup you want to restore from

  • Click "Restore Now"

Step 4: Restore from Backup (If Needed)

  1. Download from S3:

     aws s3 cp s3://your-jenkins-backup-bucket/filename /tmp/
    
  2. Stop Jenkins:

     sudo systemctl stop jenkins
    
  3. Extract backup:

     sudo tar -xzf /tmp/jenkins_backup_YYYYMMDD_HHMMSS.tar.gz -C /
    
  4. Restart Jenkins:

     sudo systemctl start jenkins
    

⚠️ Important Notes Before Restoring

  • Jenkins will shut down temporarily while restoring

  • It restores:

    • Job configs

    • Global config

    • Plugins and versions (if included)

    • User content and credentials

  • After restore, Jenkins automatically restarts

  • You may lose any changes made since the backup (so double-check the timestamp)

🧪 After Restore

Once Jenkins is up again:

  • Check if all jobs, views, plugins, credentials, etc., are restored

  • Run a sample job to validate

⏰ Step : Schedule with Cron

  1. Open crontab:

     sudo crontab -e
    
  2. Add this line for daily backups at 2 AM:

     0 2 * * * /usr/local/bin/jenkins_s3_backup.sh
    
  3. Verify cron job:

     sudo crontab -l
    

🚀 Advanced Improvements

  1. Enable S3 Versioning (protect against accidental deletions):

     aws s3api put-bucket-versioning --bucket your-jenkins-backup-bucket --versioning-configuration Status=Enabled
    
  2. Encrypt Backups (server-side encryption):

     aws s3 cp --sse AES256 "$BACKUP_FILE" "s3://$S3_BUCKET/"
    
  3. Email Notifications (add to script):

     echo "Jenkins backup completed" | mail -s "Backup Status" admin@example.com
    
0
Subscribe to my newsletter

Read articles from Khalid shaikh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Khalid shaikh
Khalid shaikh