Jenkins Server Backup Using Command Line

Jenkins is an open source automation server written in Java and it helps us to automate the software development process, with continuous integration and facilitating technical aspects of continuous delivery.

Introduction

 
Jenkins is an open-source automation server written in Java and it helps us to automate the software development process, with continuous integration and facilitating technical aspects of continuous delivery.
 
As Jenkins is an integral part of any application’s build and release process, thus it becomes extremely important to think of disaster recovery, and backups are the most useful assurance one can have against unfortunate or untimely accidental changes or shut down.
 
Thus, a regular backup becomes important to save us from future mishaps and one can anytime go to the backups and restore the correct settings.
 
 
There are different ways of taking back up, you can do it using the command line, plugins or you can also write a job to take a backup of another job. So, there are a lot of options available.
 
One of the most popular plugins that you can use for back up is Jenkins Thin Backup. It backs up all the data based on your schedule and it handles the backup retention as well.
 
In my previous article, we have seen how we can install and set up the Jenkins server on AWS EC2 instance. So this time we will not discuss those steps again and assume that you have already installed Jenkins as mentioned in one of my previous articles How To Install Jenkins Server On AWS EC2 Instance. So, in this article, we will learn how you can take backup using the command line.
 
Jenkins stores everything under the Jenkins Home directory, so the easiest way to back it up is to simply back up the entire Jenkins Home directory. Even if you have a distributed Jenkins setup, you do not need to back up anything on the slave side. The home directory of Jenkins is /var/lib/Jenkins.
 
Step 1
 
So, let’s see the status of Jenkins with the systemctl command.
 
 
Clearly, it says that Jenkins is active and running.
 
Before taking back up of Jenkins it is recommended that you stop the Jenkins, though there are plugins which allows you to take backup without even stopping the Jenkins server.
 
Step 2
 
Let's first stop the Jenkins server using systemctl command.
 
 
We can see that it has stopped successfully.
 
Step 3
 
Now, we are going to take backup as a tar archive.
 
 
Here f stands for archive filename which is jeknins-backup-25July2019.tar.gz. 
 
After its successful execution, we can see that backup has been generated.
 
 
So, the archive has been created successfully and now we can copy or move it to any system and can extract the archive in /var/lib directory.
 
Step 4
 
We can also check the disk usage for this backup using du command (Used to estimate file space usage).
 
 
Step 5
 
Now, as backup is completed, let’s cd to var/lib/Jenkins and start the Jenkins service again.
 
 
Here we have taken the back up on the Jenkins server only, but this was just to show how the backup can be done with the command line.
 
In the real-world, one should take backups on different servers other than the one whose backup is being done. So, we should move thin backups to either on cloud storage or on any other backup location so that even if Jenkins server crashes we will not lose all the data.
 
If you are on AWS, then we can upload the backups on AWS S3 storage service as well.
 

Summary

 
Most of us keep postponing or never realize the importance of backups until we run into issues that make us think why we didn’t take regular backups sooner.
 
The backup strategy could vary depending on your project and requirements. We all know that setting access rights, selecting and installing the required plugins and jobs configuration is quite a tedious process so in order to save ourselves from doing this again in case of failure we should perform regular backups.