Jenkins Backup: ThinBackup with S3 bucket script


Manual Script vs. Jenkins Backup Plugin
Factor | Manual Script | Jenkins Plugin (ThinBackup) |
Ease of Setup | Requires writing & maintaining a shell script | GUI-based setup in Jenkins (no coding needed) |
Scheduling | Requires cron or external scheduler | Built-in scheduler in Jenkins |
Backup Granularity | Full backup only (unless scripted otherwise) | Can selectively backup configs, jobs, or plugins |
Restore Process | Manual extraction + file placement | One-click restore from Jenkins UI |
Monitoring | Must set up logging manually | Backup history is visible in Jenkins |
Dependencies | Needs AWS CLI on the server | Plugin handles compression & archiving |
Maintenance | Must update the script if Jenkins paths change | Plugin 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
Delta Backups
Some plugins (like ThinBackup) can perform incremental backups, saving only changes since the last backup. This reduces storage costs.Pre/Post Backup Hooks
Plugins often let you run scripts before or after backup (e.g., to notify Slack).Built-in Retention Policies
Auto-delete old backups after X days (no need to scriptfind -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)
Download from S3:
aws s3 cp s3://your-jenkins-backup-bucket/filename /tmp/
Stop Jenkins:
sudo systemctl stop jenkins
Extract backup:
sudo tar -xzf /tmp/jenkins_backup_YYYYMMDD_HHMMSS.tar.gz -C /
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
Open crontab:
sudo crontab -e
Add this line for daily backups at 2 AM:
0 2 * * * /usr/local/bin/jenkins_s3_backup.sh
Verify cron job:
sudo crontab -l
🚀 Advanced Improvements
Enable S3 Versioning (protect against accidental deletions):
aws s3api put-bucket-versioning --bucket your-jenkins-backup-bucket --versioning-configuration Status=Enabled
Encrypt Backups (server-side encryption):
aws s3 cp --sse AES256 "$BACKUP_FILE" "s3://$S3_BUCKET/"
Email Notifications (add to script):
echo "Jenkins backup completed" | mail -s "Backup Status" admin@example.com
Subscribe to my newsletter
Read articles from Khalid shaikh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
