forked from sue445/jenkins-backup-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins-backup.sh
executable file
·83 lines (66 loc) · 1.93 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
#!/bin/sh
readonly CUR_DIR=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd)
JENKINS_HOME=/var/jenkins_home
DEST_FILE=$CUR_DIR/jenkins-backup-$(date "+%Y.%m.%d-%H.%M.%S").tar.gz
usage() {
cat <<-END
usage: $(basename $0) [-j JENKINS_HOME] [-b BACKUP_FILE]
optional arguments:
-h, --help show this help message and exit
-j DIR, --jenkins-home DIR
Jenkins home directory.
Default is $JENKINS_HOME
-b FILE, --backup-file FILE
output backup archive file.
Default is $DEST_FILE
END
}
GETOPT_OUTPUT=$(getopt -o "hj:b:" --long "help,jenkins-home:,backup-file:" -- "$@")
if [ $? -gt 0 ] ; then
usage
exit 1
fi
eval set -- "$GETOPT_OUTPUT"
while true ; do
case "$1" in
-h|--help)
usage
exit 0
;;
-j|--jenkins-home)
JENKINS_HOME=$2
shift 2
;;
-b|--backup-file)
DEST_FILE$2
shift 2
;;
--) shift ; break ;;
*)
echo "Command line parsing internal error!"
usage
exit 1
;;
esac
done
readonly SCRATCH=$(mktemp -t tmp.XXXXXXXXXX)
on_exit() {
rm -rf "$SCRATCH"
}
trap on_exit INT TERM HUP EXIT
if [ ! -d "$JENKINS_HOME" ]; then
(>&2 echo "$JENKINS_HOME does not exist!")
exit 1000
fi
set -e
find "$JENKINS_HOME" -maxdepth 1 -type f -print >> $SCRATCH
find "$JENKINS_HOME" -name ".ssh" -maxdepth 1 -type d -print >> $SCRATCH
find "$JENKINS_HOME/plugins/" -name "*.[hj]pi" -maxdepth 1 -type f -print >> $SCRATCH
find "$JENKINS_HOME/plugins/" -name "*.[hj]pi.pinned" -maxdepth 1 -type f -print >> $SCRATCH
find "$JENKINS_HOME" -name "users" -maxdepth 1 -type d -print >> $SCRATCH
find "$JENKINS_HOME" -name "secrets" -maxdepth 1 -type d -print >> $SCRATCH
find "$JENKINS_HOME" -name "nodes" -maxdepth 1 -type d -print >> $SCRATCH
if [ "$(ls -A $JENKINS_HOME/jobs/)" ] ; then
find "$JENKINS_HOME/jobs" -type f -not -path "*/builds/*" -print >> $SCRATCH
fi
tar -czvf $DEST_FILE -T $SCRATCH