Created
October 27, 2016 12:43
-
-
Save maurorappa/ba131ed11248ac8f8b9a002ece6e104a to your computer and use it in GitHub Desktop.
Jenkins Backup bash script with all latest job status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -xe | |
################################################################################## | |
function usage(){ | |
echo "usage: $(basename $0) /path/to/jenkins_home archive.tar.gz" | |
} | |
################################################################################## | |
readonly JENKINS_HOME=$1 | |
readonly DEST_FILE=$2 | |
readonly CUR_DIR=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd) | |
readonly TMP_DIR="$CUR_DIR/tmp" | |
readonly ARC_NAME="jenkins-backup" | |
readonly ARC_DIR="$TMP_DIR/$ARC_NAME" | |
readonly TMP_TAR_NAME="$TMP_DIR/archive.tar.gz" | |
if [ -z "$JENKINS_HOME" -o -z "$DEST_FILE" ] ; then | |
usage >&2 | |
exit 1 | |
fi | |
rm -rf "$ARC_DIR" "$TMP_TAR_NAME" | |
for i in plugins jobs users secrets nodes;do | |
mkdir -p "$ARC_DIR"/$i | |
done | |
cp "$JENKINS_HOME/"*.xml "$ARC_DIR" | |
cp "$JENKINS_HOME/plugins/"*.[hj]pi "$ARC_DIR/plugins" | |
hpi_pinned_count=$(find $JENKINS_HOME/plugins/ -name *.hpi.pinned | wc -l) | |
jpi_pinned_count=$(find $JENKINS_HOME/plugins/ -name *.jpi.pinned | wc -l) | |
if [ $hpi_pinned_count -ne 0 -o $jpi_pinned_count -ne 0 ]; then | |
cp "$JENKINS_HOME/plugins/"*.[hj]pi.pinned "$ARC_DIR/plugins" | |
fi | |
if [ "$(ls -A $JENKINS_HOME/users/)" ]; then | |
cp -R "$JENKINS_HOME/users/"* "$ARC_DIR/users" | |
fi | |
if [ "$(ls -A $JENKINS_HOME/secrets/)" ] ; then | |
cp -R "$JENKINS_HOME/secrets/"* "$ARC_DIR/secrets" | |
fi | |
if [ "$(ls -A $JENKINS_HOME/nodes/)" ] ; then | |
cp -R "$JENKINS_HOME/nodes/"* "$ARC_DIR/nodes" | |
fi | |
function backup_jobs { | |
local run_in_path=$1 | |
local rel_depth=${run_in_path#$JENKINS_HOME/jobs/} | |
cd "$run_in_path" | |
find . -maxdepth 1 -type d | while read job_name ; do | |
[ "$job_name" = "." ] && continue | |
[ "$job_name" = ".." ] && continue | |
[ -d "$JENKINS_HOME/jobs/$rel_depth/$job_name" ] && mkdir -p "$ARC_DIR/jobs/$rel_depth/$job_name/" | |
[ -f "$JENKINS_HOME/secret.key" ] && cp $JENKINS_HOME/secret.key $ARC_DIR | |
find "$JENKINS_HOME/jobs/$rel_depth/$job_name/" -maxdepth 1 -name "*.xml" -print0 | xargs -0 -I {} cp {} "$ARC_DIR/jobs/$rel_depth/$job_name/" | |
backup_builds $run_in_path "lastSuccessful" | |
backup_builds $run_in_path "lastUnsuccessful" | |
backup_builds $run_in_path "lastStable" | |
backup_builds $run_in_path "lastFailed" | |
if [ -f "$JENKINS_HOME/jobs/$rel_depth/$job_name/config.xml" ] && [ "$(grep -c "com.cloudbees.hudson.plugins.folder.Folder" "$JENKINS_HOME/jobs/$rel_depth/$job_name/config.xml")" -ge 1 ] ; then | |
backup_jobs "$JENKINS_HOME/jobs/$rel_depth/$job_name/jobs" | |
else | |
true | |
fi | |
done | |
cd - | |
} | |
function backup_builds { | |
local run_in_path=$1 | |
local build_type=$2 | |
echo "---++++------+++-- working on $2" | |
local rel_depth=${run_in_path#$JENKINS_HOME/jobs/} | |
cd "$run_in_path" | |
mkdir -p "$ARC_DIR/jobs/$rel_depth/$job_name/builds/" | |
if [ -e "$JENKINS_HOME/jobs/$rel_depth/$job_name/builds/${build_type}Build" ]; then | |
JOB_NUMBER=$(readlink "$JENKINS_HOME/jobs/$rel_depth/$job_name/builds/${build_type}Build") | |
cd "$ARC_DIR/jobs/$rel_depth/$job_name/builds/" | |
ln -s "$JOB_NUMBER" "${build_type}Build" | |
cd - | |
mkdir -p "$ARC_DIR/jobs/$rel_depth/$job_name/builds/$JOB_NUMBER" | |
cp "$JENKINS_HOME/jobs/$rel_depth/$job_name/builds/$JOB_NUMBER/log" "$ARC_DIR/jobs/$rel_depth/$job_name/builds/$JOB_NUMBER/log" | |
find "$JENKINS_HOME/jobs/$rel_depth/$job_name/builds/$JOB_NUMBER/" -maxdepth 1 -name "*.xml" -print0 | xargs -0 -I {} cp {} "$ARC_DIR/jobs/$rel_depth/$job_name/builds/$JOB_NUMBER/" | |
find "$JENKINS_HOME/jobs/$rel_depth/$job_name/builds/$JOB_NUMBER/" -maxdepth 1 -name "*.txt" -print0 | xargs -0 -I {} cp {} "$ARC_DIR/jobs/$rel_depth/$job_name/builds/$JOB_NUMBER/" | |
find "$JENKINS_HOME/jobs/$rel_depth/$job_name/" -maxdepth 1 -name "*.properties" -print0 | xargs -0 -I {} cp {} "$ARC_DIR/jobs/$rel_depth/$job_name/" | |
else | |
echo "no build of this type found" | |
fi | |
if [ -f $JENKINS_HOME/jobs/$rel_depth/$job_name/nextBuildNumber ]; then | |
cp "$JENKINS_HOME/jobs/$rel_depth/$job_name/nextBuildNumber" "$ARC_DIR/jobs/$rel_depth/$job_name/" | |
fi | |
} | |
if [ "$(ls -A $JENKINS_HOME/jobs/)" ] ; then | |
backup_jobs $JENKINS_HOME/jobs/ | |
fi | |
cd "$TMP_DIR" | |
rpl "<useSecurity>true</useSecurity>" "<useSecurity>false</useSecurity>" "$ARC_NAME/config.xml" | |
tar -czvf "$TMP_TAR_NAME" "$ARC_NAME/"* | |
cd - | |
mv -f "$TMP_TAR_NAME" "$DEST_FILE" | |
rm -rf "$ARC_DIR" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment