-
Notifications
You must be signed in to change notification settings - Fork 430
/
jenkins-backup.sh
executable file
·108 lines (81 loc) · 3.03 KB
/
jenkins-backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash -xe
#
# jenkins backup scripts
# https://github.com/sue445/jenkins-backup-script
#
# Usage: ./jenkins-backup.sh /path/to/jenkins_home /path/to/destination/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"
function usage() {
echo "usage: $(basename $0) /path/to/jenkins_home archive.tar.gz"
}
function backup_jobs() {
local run_in_path="$1"
local rel_depth=${run_in_path#${JENKINS_HOME}/jobs/}
if [ -d "${run_in_path}" ]; then
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}/"
find "${JENKINS_HOME}/jobs/${rel_depth}/${job_name}/" -maxdepth 1 \( -name "*.xml" -o -name "nextBuildNumber" \) -print0 | xargs -0 -I {} cp {} "${ARC_DIR}/jobs/${rel_depth}/${job_name}/"
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
#echo "Folder! $JENKINS_HOME/jobs/$rel_depth/$job_name/jobs"
backup_jobs "${JENKINS_HOME}/jobs/${rel_depth}/${job_name}/jobs"
else
true
#echo "Job! $JENKINS_HOME/jobs/$rel_depth/$job_name"
fi
done
#echo "Done in $(pwd)"
cd -
fi
}
function cleanup() {
rm -rf "${ARC_DIR}"
}
function main() {
if [ -z "${JENKINS_HOME}" -o -z "${DEST_FILE}" ] ; then
usage >&2
exit 1
fi
rm -rf "${ARC_DIR}" "{$TMP_TAR_NAME}"
for plugin in plugins jobs users secrets nodes; do
mkdir -p "${ARC_DIR}/${plugin}"
done
cp "${JENKINS_HOME}/"*.xml "${ARC_DIR}"
jks_count=$(find ${JENKINS_HOME} -maxdepth 1 -type f -name *.jks | wc -l)
if [ ${jks_count} -ne 0 ]; then
cp "${JENKINS_HOME}/"*.jks "${ARC_DIR}/"
fi
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
if [ "$(ls -A ${JENKINS_HOME}/jobs/)" ] ; then
backup_jobs ${JENKINS_HOME}/jobs/
fi
cd "${TMP_DIR}"
tar -czvf "${TMP_TAR_NAME}" "${ARC_NAME}/"*
cd -
mv -f "${TMP_TAR_NAME}" "${DEST_FILE}"
cleanup
exit 0
}
main