Skip to content

Instantly share code, notes, and snippets.

@danielcarr
Last active April 6, 2016 12:57
Show Gist options
  • Save danielcarr/bcd2da83cf9954f426aa48a2852eea1e to your computer and use it in GitHub Desktop.
Save danielcarr/bcd2da83cf9954f426aa48a2852eea1e to your computer and use it in GitHub Desktop.
Jenkins build server administration scripts
#!/usr/bin/env bash
archiveflag="--keep-archive"
deleteflag="--delete"
nullflag="-"
archivedirectory="${HOME}/Library/JenkinsArchives"
configdir="${archivedirectory}/configs"
archivename=jenkins.tar.gz
jenkinshome=.jenkins
usage() {
echo "Usage: $0 [--help|-h|-?] [${nullflag}] [<secondary location>] [${archiveflag}] [${deleteflag}]"
echo "Without arguments, only the configuration files for each job in ${jenkinshome} will be copied into ${configdir}."
echo "To back up the whole server configuration in place, without copying it anywhere, pass \"${nullflag}\" as the only argument."
echo "If a directory is given, the archive will be copied to that directory."
echo "The directory can be on a remote server, in the form <user>@<server>:<path>."
echo "If more than one directory is given, the archive will be copied into all of them."
echo "If ${archiveflag} is set, the archive will be copied, as a timestamped archive,"
echo " into ${archivedirectory}."
echo "If ${deleteflag} is set, the local archive is deleted after being copied."
echo "Help flags print this message and exit (but only after copying the archive,"
echo " if they come after valid directory paths or flags)."
exit ${1:-0}
}
if [[ "$1" = "--help" || "$1" = "-h" || "$1" = "-?" ]]; then
usage
fi
cd
if test ! -d "${configdir}"; then
mkdir "${configdir}"
fi
for job in ${jenkinshome}/jobs/*; do
cp "${job}/config.xml" "${configdir}/${job##*/}.xml"
done
if test -z $*; then
exit 0
fi
tar czfv $archivename --exclude='workspace/*'\
--exclude="*.xcarchive"\
--exclude="*.ipa"\
--exclude="*.app"\
--exclude="developer-profiles"\
--exclude=".git/"\
$jenkinshome
if test $? -ne 0; then
usage $?
fi
while test -n "$1"; do
if [[ "$1" = "${nullflag}" ]]; then
: # do nothing
fi
if [[ "$1" = "--help" || "$1" = "-h" || "$1" = "-?" ]]; then
usage
elif [[ "$1" = "${archiveflag}" ]]; then
if test ! -d "${archivedirectory}"; then
mkdir "${archivedirectory}"
fi
cp "${archivename}" "${archivedirectory}"/$(date "+%Y-%m-%d_%H%M").tar.gz
elif [[ "$1" = "${deleteflag}" ]]; then
shoulddelete=1
else
echo "Copying to $1"
scp $archivename "$1"
fi
if test $? -ne 0; then
usage $?
fi
shift
done
if test ${shoulddelete:-0} -gt 0; then
rm "${archivename}"
fi
#!/usr/bin/env bash
jenkinsjobs="${HOME}/.jenkins/jobs"
configarchive="${HOME}/Library/JenkinsArchives/configs"
for job in "${jenkinsjobs}"/*; do
jobname="${job##*/}"
echo -e "\033[1m${jobname}\033[0m"
if test ! -f "${configarchive}/${jobname}.xml"; then
echo "Has not been backed up yet"
echo
continue
elif diff "${job}/config.xml" "${configarchive}/${jobname}.xml"; then
# If the saved config and the current config are the same,
echo -en "\033[1A\033[2K\r" # erase job name and go back to beginning of previous line
else
echo # leave the diff printed out and continue with next file
fi
done
#!/usr/bin/env bash
jenkinsdir="${HOME}/.jenkins"
configdir="${HOME}/Library/JenkinsArchives/configs"
helpflag="--help"
listflag="--list"
usage() {
echo "Usage:"
echo " $0 [<configuration name> [<configuration name>]...]"
echo " Restores given configuration(s), or all configurations if none are provided"
echo " $0 ${listflag}"
echo " Lists backuped jobs/configurations and exits"
echo " $0 [${helpflag}]"
echo " Prints this message and exits"
echo "Remember to restart jenkins or reload configuration from disk after restoring"
exit ${1:-0}
}
configwithname() {
echo "${configdir}/${1}.xml"
}
jobwithname() {
echo "${jenkinsdir}/jobs/${1}"
}
isbackedupconf() {
test -f "$(configwithname "${1}")"
}
jobexists() {
test -d "$(jobwithname "${1}")"
}
restoreconfig() {
if jobexists "${1}"; then
cp "$(configwithname "${1}")" "$(jobwithname "${1}")/config.xml"
else
echo "No job found with name ${1}"
return 1
fi
}
if test "${1}" = "${helpflag}"; then
usage
fi
if test "${1}" = "${listflag}"; then
echo "Backed up configurations:"
for config in "${configdir}/*.xml"; do
config="${config##*/}"
echo " * ${config%.xml}"
done
exit
fi
if test $# -eq 0; then
for config in "${configdir}"/*.xml; do
config="${config##*/}"
restoreconfig "${config%.xml}"
done
else
while test $# -gt 0; do
if isbackedupconf "${1}"; then
restoreconfig "${1}"
else
echo "No backup for job named ${1}"
fi
shift
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment