How to backup and recover Jenkins using AWS S3?
This tutorial will guide you through a practical, step-by-step approach to back up and recover Jenkins on Mac using scripts with AWS S3.
Table of Contents
- Create IAM to copy files to S3
- Setup IAM on Mac
- Backup Jenkins
- Recover Jenkins
Environment
- Mac
- Jenkins
Before we get started, you should have installed Jenkins on Mac.
Create IAM to copy files to S3
- Sign in to the AWS Management Consol
- Create a new IAM user
- Create an S3 bucket and assign an IAM user to have permission to access your bucket
- Create the IAM access key ID and secret key
Setup IAM on Mac
-
Install AWS CLI
brew install awscli
-
Run below to set up IAM with yours
aws configure
Backup Jenkins
Step1: Backup Jenkins
- Download the backup_jenkins.sh here
Step2: Upload Jenkins to AWS S3
Create upload_to_s3.sh
#!/bin/bash
if [ -z "$1" ]
then
echo "Input file name"
exit 1
fi
AWS_DEFAULT_PROFILE="<Your AWS Profile>"
AWS_DEFAULT_REGION="<Your AWS Region >"
S3_BUCKET="s3://<Your AWS Bucket Name>"
/usr/local/bin/aws s3 cp $1 --profile ${AWS_DEFAULT_PROFILE} --region ${AWS_DEFAULT_REGION} ${S3_BUCKET}
Create cronjob_backup.sh to combine the two scripts shown above.
#!/bin/bash
rm -rf jenkins.tar
rm -rf tmp
FILE_NAME="jenkins-backup.tar"
./backup_jenkins.sh /Users/<YourUserName>/.jenkins ${FILE_NAME}
./upload_to_s3.sh ${FILE_NAME}
Note
- backup_jenkins.sh can back up all the jobs and settings in Jenkins at /Users/
/.jenkins - Once the backup file is done, you can run upload_to_s3.sh to upload it to AWS S3.
Recover Jenkins
Step1: Download Jenkins backup form AWS S3
Create download_s3.sh
#!/bin/bash
if [ -z "$1" ]
then
echo "Input file name (ex: jenkins-backup.tar)"
exit 1
fi
AWS_DEFAULT_PROFILE="<Your AWS Profile>"
AWS_DEFAULT_REGION="<Your AWS Region >"
S3_BUCKET="s3://<Your AWS Bucket Name>"
FILE_NAME="$1"
/usr/local/bin/aws s3 cp ${S3_BUCKET}/medini9/${FILE_NAME} --profile ${AWS_DEFAULT_PROFILE} --region ${AWS_DEFAULT_REGION} .
Run script to download Jenkins backup and copy it to Jenkins folder
# Download job and backup files from AWS S3
./download_s3.sh jenkins-backup.tar
# Copy files to Jenkins
./tar xvf jenkins-backup.tar
cp -a jenkins-backup/. /Users/<YourUserName>/.jenkins/
Step2: Recover Jenkins
- Log in to Jenkins
- Log in to Jenkins → System Management
- Click Tools and Actions to reload
Then you need to approve backup pipelines
Summary
In this tutorial, we walked you through the process of creating an IAM role to copy files to S3, setting up IAM on a Mac, and finally, backing up and recovering Jenkins.
The scripts provided should give you a comprehensive understanding of the process, enabling you to safeguard your Jenkins data effectively.
If you found this article helpful, please follow us on Facebook to get the latest tutorials in the future.
Thank you for reading!