#!/bin/bash
echo Backup config.xml files in ${JENKINS_HOME}
if [ ! -f ${JENKINS_HOME}/config.xml ]; then
echo "ERROR: JENKINS_HOME doesn't contain a config.xml file: ${JENKINS_HOME}"
exit -1
fi
if [ "svnConfig$SVN_HOME" = "svnConfig" ] ; then
echo "ERROR: Please set property SVN_HOME=${SVN_HOME:-NOT SET}"
exit -1
else
if [ ! -d ${SVN_HOME}/bin ]; then
echo "ERROR: SVN directory missing: ${SVN_HOME}/bin"
exit 1
fi
fi
SVN_CMD=${SVN_HOME}/bin/svn
echo "Current directory should be the SVN working dir"
SVN_BACKUP_PATH=${WORKSPACE}/svn_backup
cd ${SVN_BACKUP_PATH}
if [[ $? -ne 0 ]] ; then
echo "ERROR: failed to cd into ${SVN_BACKUP_PATH}"
exit 1
fi
#########################################
# This doesn't quite work.
# SVN keeps adding them back in
# Do this manually for now
##########################################
echo "Deleting unused directories"
for folderToDelete in $(find . -type d ! -path "*\.svn*"); do
if [ "$(ls "$folderToDelete"| wc -l)" -eq 0 ]; then
if [ "$folderToDelete" = "." ]; then
echo "Ignoring: current working dir"
else
echo "Removing: $folderToDelete"
${SVN_CMD} delete $folderToDelete
fi
fi
done
echo "Clean the SVN checkout"
find . -type f ! -path "*\.svn*" -delete
echo "Copy Jenkins config files into the cleaned SVN dir"
cd ${JENKINS_HOME}
######################################################################
# Copy the jenkins jobs config
# Restrict depth to ignore other config.xml files e.g. from this backup script
# Separate out jobs and users; users may not be available
######################################################################
if [ -d jobs ]; then
find jobs -maxdepth 2 -type f -name config.xml | cpio -updm ${SVN_BACKUP_PATH}
fi
# Copy the jenkins users config
if [ -d users ]; then
find users -maxdepth 2 -type f -name config.xml | cpio -updm ${SVN_BACKUP_PATH}
fi
# Copy the jenkins server config
cp config.xml ${SVN_BACKUP_PATH}/config.xml
echo "Process the files to see if they need to be deleted/added/updated in repository"
cd ${SVN_BACKUP_PATH}
export SVN_MESSAGE="Backup taken on `(date +%Y%m%d@%T)` for ${JENKINS_URL} from ${JENKINS_HOME}"
SVN_USERNAME=username
SVN_PASSWORD=password
stat=`${SVN_CMD} status`
if [[ $stat != '' ]]; then
#There are some changes.. Lets process them
# Do we have any files to add
#############################
# Find lines that start with a question mark (unknown files)
raw_add_files=$(${SVN_CMD} status | grep -e "^\?")
if [[ $raw_add_files != '' ]]; then
# Echo removes the EOL characters and sed removes the question marks
add_files=$(echo ${raw_add_files} | sed 's/?//g')
if [[ $add_files != '' ]]; then
${SVN_CMD} add --username=${SVN_USERNAME} --password=${SVN_PASSWORD} $add_files
fi
fi
# Do we have files to delete
############################
# Find lines that start with an exclamation mark (missing files)
delete_files_raw=$(${SVN_CMD} status | grep -e '^\!')
if [[ $delete_files_raw != '' ]]; then
# Echo removes the EOL characters and sed removes the exclamation marks
delete_files=$(echo ${delete_files_raw} | sed 's/!//g')
if [[ $delete_files != '' ]]; then
${SVN_CMD} delete --username=${SVN_USERNAME} --password=${SVN_PASSWORD} $delete_files
fi
fi
# Are there changes to commit
############################
# Make sure everything is synced up before commiting
${SVN_CMD} update --username=${SVN_USERNAME} --password=${SVN_PASSWORD}
${SVN_CMD} commit --username=${SVN_USERNAME} --password=${SVN_PASSWORD} --non-interactive -m "$SVN_MESSAGE"
# fail so that Jenkins will send an email
echo "SOME CHANGES WERE COMMITTED! MAKE SURE WE EMAIL SOMEONE!"
else
echo "NO CHANGES DETECTED! NOTHING TO COMMIT!"
fi