Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backup Jenkins home periodicallly with git.
#!/bin/bash
# Setup
#
# - Create a new Jenkins Job
# - Mark "None" for Source Control Management
# - Select the "Build Periodically" build trigger
# - configure to run as frequently as you like
# - Add a new "Execute Shell" build step
# - Paste the contents of this file as the command
# - Save
#
# NOTE: before this job will work, you'll need to manually navigate to the $JENKINS_HOME directory
# and do the initial set up of the git repository.
# Make sure the appropriate remote is added and the default remote/branch set up.
#
# Jenkins Configuraitons Directory
cd $JENKINS_HOME
# Add general configurations, job configurations, and user content
git add -- *.xml jobs/*/*.xml userContent/*
# only add user configurations if they exist
if [ -d users ]; then
user_configs=`ls users/*/config.xml`
if [ -n "$user_configs" ]; then
git add $user_configs
fi
fi
# mark as deleted anything that's been, well, deleted
to_remove=`git status | grep "deleted" | awk '{print $3}'`
if [ -n "$to_remove" ]; then
git rm --ignore-unmatch $to_remove
fi
git commit -m "Automated Jenkins commit"
git push -q -u origin master
@rodriguezsergio

This comment has been minimized.

rodriguezsergio commented Feb 24, 2016

Saved me the time of writing my own backup script. Thanks 👍

@kuldazbraslav

This comment has been minimized.

kuldazbraslav commented Mar 21, 2016

Thanks, it saved me a lot of time too.

@AlexanderDobrodey

This comment has been minimized.

AlexanderDobrodey commented Mar 29, 2016

Thank you!

@squrppi

This comment has been minimized.

squrppi commented Feb 22, 2017

I wonder if the line 34 awk should actually read '{print $2}' instead of '{print $3}'?
The output of "git status" is like

   deleted:    test.py
@Alokdavita

This comment has been minimized.

Alokdavita commented Jul 17, 2017

Thanks a lot !!

@gardner

This comment has been minimized.

gardner commented Aug 10, 2017

Thank you! I want to post a "You Da Real MVP" meme but I don't think that's allowed on GH.

@syquus

This comment has been minimized.

syquus commented Sep 13, 2017

Just a note:
'{print $3}'
is returning empty string for me. Therefore there are elements not being git rm-ed....

I replaced it for
'{print $2}'
and works correctly.

@franc3000

This comment has been minimized.

franc3000 commented Mar 8, 2018

this is great, thank you!

what do you add from the $JENKINS_HOME directory when doing the initial set up of the git repository?

git add * ?

I added all the secret files, and now I'm getting this:

ubuntu@:~/jenkins$ git status
jenkins-data/secrets/filepath-filters.d/30-default.conf: Permission denied
jenkins-data/secrets/hudson.util.Secret: Permission denied
jenkins-data/secrets/initialAdminPassword: Permission denied
jenkins-data/secrets/jenkins.model.Jenkins.crumbSalt: Permission denied
jenkins-data/secrets/jenkins.security.ApiTokenProperty.seed: Permission denied
jenkins-data/secrets/master.key: Permission denied
jenkins-data/secrets/org.jenkinsci.main.modules.instance_identity.InstanceIdentity.KEY: Permission denied
jenkins-data/secrets/slave-to-master-security-kill-switch: Permission denied
jenkins-data/secrets/whitelisted-callables.d/default.conf: Permission denied
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
@maximba

This comment has been minimized.

maximba commented Mar 9, 2018

Modified "to_remove" expression to be more robust:
to_remove=$(git status | grep "deleted" | cut -d ":" -f2 | sed -r 's/^[ \t]*//p')

@greilly

This comment has been minimized.

greilly commented Mar 27, 2018

Adding user configurations gave me some errors, and can be simplified:

# only add user configurations if they exist
if [ -d users ]; then
    git add users/*/config.xml
fi
@msvticket

This comment has been minimized.

msvticket commented Apr 18, 2018

A simpler way of doing deletion resilient to whitespace in file names:
git ls-files -dz | xargs -0r git rm

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