Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Backup & Restoring Jenkins

The purpose of the backup is to create a copy of data that can be restored in the event of a primary data failure which can be the result of hardware or software failure, data corruption, or a human-caused event, or accidental deletion of data.

Backup copies allow data to be restored from an earlier point in time to help the business recover from an unplanned event.

Why Jenkins needs B & R ?

In Jenkins, all the settings, build logs and archives of the artifacts are stored under the JENKINS_HOME directory as Jenkins don't use any database . Setting access rights, selecting the necessary plugins and jobs configuration is quite a laborious process, so it’s a good idea to organize regular backups of all the necessary settings and parameters.

In this post we will be exploring 2 ways to perform B & R

  • By creating a freestyle project
  • By using ThinBackup plugin

How to take backups in Jenkins ?

The easy way is to just keep this folder separately as a new back and whenever the same needs to be used, just copy it back.

As the build jobs created under this directory contains all the details of each and every individual jobs configured in the Jenkins install. Any job can be moved from one installation directory to another just by copying the jobs.

But again its a manual task to copy the files from one location to another so instead lets leverage the Jenkins for B & R process by automating it.

Creating a freestyle project to take regular backups

Before going with the steps make sure your initiate git repository is initiated and you are also connected with it.

Now the following steps can be followed to configure a freestyle project that takes regular backup .

  • Create a new item in Jenkins and choose Freestyle project

  • In general section provide description if required and omit everything else.

  • Choose None in Source Code Management section

  • In Build Triggers section choose Build periodically and provide a cron expression to trigger your backup .

    • Examples
     0 12 * * *   // will trigger at 12:00pm
     45 12 * * *  // will trigger at 12:45pm
        
    
  • Then choose Execute shell from the Build section and write a shell script ( or you can use the script provided in extra section at the bottom of this post ) to push the contents of your JENKINS_HOME to GITHUB as a backup which can be pulled whenever required from the same .

  • Finally save the job and you have automated your backup process.

  • The point here can be noted that you can also make this job run after every job you build by selecting build other project option from the post-build section of other project builds.

To restore Jenkins

  • Go to JENKINS_HOME directory and initiate a new git repository
  • Cleans the working tree by recursively removing files that are not under version control
  • Add a new remote and you just have Pull all data from GitHub
  • Now all you have to do is restart Jenkins and its restored.

Using ThinBackup

Since, Jenkins can be enormously extended by the usage of several plugins, there comes a plugin which can be used for the backup management in Jenkins - ThinBackup plugin.

This plugin backs up the job specific configuration files.

To use this plugin follow the steps defined below :

  • From the Jenkins home page, click on Manage Jenkins and in the next page click on Manage plugins .
  • Now download and install the ThinBackup plugin.
  • After downloading you must be able to see 3 options as:
    • Backup Now
    • Restore
    • Settings
  • Click on Settings link to configure the backup options.
  • Just provide the configuration details as required and save the settings.
  • Provide a persistent location for Backup directory
  • Then write a cron expression to schedule backup in Backup schedule for full backups
  • Check the required options and save it.
  • Now from this point ThinBackup plugin will automatically take backup for your Jenkins and store it in some persistent location provided.

Restore

If your Jenkins fail for any of the reason then :

  • Restart Jenkins
  • Download the ThinBackup plugin again and specify the same location persistent location for Backup directory as specified earlier .
  • Now this time select restore option
  • Select the desired backup from Restore Configuration page.
  • Its done

Extra Section

Script to push the content of your JENKINS_HOME

cd $JENKINS_HOME
BACKUP_TIME=${date}
git add .
git commit -m "backup-$BACKUP_TIME"
git push -u origin master
echo "backup pushed successfully"

Thanks for keeping up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment