How to backup and clone your Jenkins Instance

Few steps on how to clone your Jenkins Instance to ensure you have your configurations always saved.

Xavier Alves
5 min readMar 15, 2022
Photo by Miguel Á. Padriñán from Pexels

Today I bring you some words regarding how to backup all your history and configurations of a particular Jenkins instance and spin a new instance of Jenkins. The scenario currently presented is using a Jenkins instance being managed by a docker-compose file. Bear in mind that this might not be the best practice for your case as this has issues in a more robust scenario. In my case, I just use this instance for some simple projects so it is enough(perhaps later I will automate part of this process). I will have a section exposing alternatives to this approach.

If you need some lights on how to setup a simple Jenkins instance check this here.

This story is organized in the following way:

  • Instructions
  • Alternatives
  • Conclusion

Instructions

This is the core section of the Story we will move through the different steps needed to accomplish the backup. Near the end we will get into how to replace the files from the backup so that we have a Jenkins instance with the previous configurations and data.

The first step is to know what is the container ID of the Jenkins instance we want to backup:

Execution of docker ps to retrive the Container ID.

To enter inside de container we use the docker exec command. Here is an example command:

docker exec -it c7e1aba9dc58 bash

The above command will enter the container with the id c7e1aba9dc58 and open with a bash shell in interactive mode:

Printing the $JENKINS_HOME variable inside the container.

Now that we know what is the path for the Jenkins Home folder, we will exit to the host machine and create a folder to hold our backup:

Creating a backup directory for our backup on the host.

Having the backup folder created, let’s get inside it and perform a copy of the files to the host machine. For this, we will use the docker cp command:

Executing docker cp from the host, and checking that our backup has contents.

Now we can safely spin a new instance of Jenkins, in my case I did it on a completely different host. You can use any method you know to move the backup to the new host. After this, we will take similar steps to the ones made before to get the container ID of the new container.

Execution of docker ps to retrive the Container ID.

As we did previously we will docker exec inside the container and will check again the folder that Jenkins is using to have its folders:

docker exec -it f574070a9a14 bash
Printing the $JENKINS_HOME variable inside the container.

Now we will move to the Jenkins Home folder of the new instance. After this, we will remove all its contents. You will notice that we will receive an error message. This happens since the Jenkins is being executed at the same time and surely is monitoring this folder. Note that, as an improvement, next time I will attempt to this with the Jenkins process stopped.

Remove the contents of the new Jenkins instance.

When I was returned that error I decided to check if the process had deleted all the files inside the folder. As we can see the process was executed successfully.

Checking that the Jenkins Home is empty.

Here we will rename the folder previously created to not enter in conflict with the folder inside the container(since we were not able to delete it). Now we are ready to copy the backup inside the new instance.

Renaming and copying the backup folder next to container’s jenkins home folder.

After we have done this we can check that inside the /var/ directory we can find our folder. Now we can move all the contents inside the backup folder to the Jenkins home folder:

cd /var/jenkins_home_backup/jenkins_home/
mv * /var/jenkins_home/

After performing these steps, you should restart your Jenkins and you will notice all your instance is as the previous one. This is not the easiest way of doing this, so in the next section, I will present the next possible iteration.

Alternatives

  1. Having a job making this backup for you automatically:
    This approach is the next iteration over the approach that I just presented since it consists in simply backing up the $JENKINS_HOME folder so that anytime you require to do this process again you have some snapshots saved. It will neither solve your downtime issue nor the manual steps to move to a new instance.
  2. Sharing the $JENKINS_HOME folder with the host:
    By doing this, you will be able to just have a folder on your host machine with a folder shared. Simply replace the contents and reboot your Jenkins, and you should be up and running.
  3. Plugins that make all the Jenkins configs as code, allowing it to be properly version controlled and used to spin up new instances easily.

Conclusion

Thanks for your time reading this story. I use a simple Jenkins to perform simple tests and compilations for some projects. While this story shows a way of doing a full backup of the system, it is a good practice that your Jenkinsfile holds the most configurations as possible, allowing it to be version controlled, making it easier for any Jenkins instance that needs to run it.
I hope this story helped you with having a view on to solve this eventual situation. As usual, feel free to reach out to me in the comments below for any suggestions you might have.

--

--

Xavier Alves

Software Engineer | Occasional gamer | Exploring new technologies