-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup-gitlab
executable file
·91 lines (79 loc) · 2.23 KB
/
backup-gitlab
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
#!/bin/sh
SCRIPTPATH="$(readlink -f 2>/dev/null "$0" || perl -MCwd -e 'print Cwd::abs_path shift' "$0")"
SCRIPTDIR="$(dirname "${SCRIPTPATH}")"
GITLAB_HOST="$1"
GITLAB_TOKEN="$2"
PREFIX="$3"
EXCLUDE_REPOS="$4"
GITLAB_HTTP_PROTO="https"
FETCH_PROTO="ssh"
GITLAB_USER="git"
CURL_OPTS=""
if test "x${GITLAB_HOST}" = "x" || test "x{GITLAB_TOKEN}" = "x"
then
echo "Usage: $0 GITLAB_HOST GITLAB_TOKEN LOCAL_PATH_PREFIX [EXCLUDE_REPOS] [GITLAB_HTTP_PROTO] [FETCH_PROTO] [FETCH_USER] [CURL_OPTS]"
exit 2
fi
if test "x$5" != "x"
then
GITLAB_HTTP_PROTO="$5"
fi
if test "x$6" != "x"
then
FETCH_PROTO="$6"
fi
if test "x$7" != "x"
then
GITLAB_USER="$7"
fi
if test "x$8" != "x"
then
CURL_OPTS="$8"
fi
GITLAB_HTTP="${GITLAB_HTTP_PROTO}://${GITLAB_HOST}/"
GITLAB_URL="${GITLAB_USER}@${GITLAB_HOST}:"
if test "x${FETCH_PROTO}" = "xhttp" || test "x${FETCH_PROTO}" = "xhttps"
then
GITLAB_URL="${FETCH_PROTO}://${GITLAB_USER}:${GITLAB_TOKEN}@${GITLAB_HOST}/"
fi
getRepos() {
REPOS="$(curl ${CURL_OPTS} -s -f -H "Private-token: $GITLAB_TOKEN" "${GITLAB_HTTP}api/v4/projects?per_page=100&simple=1&membership=1&archived=0" | tr -d '\r\n' | jq '.[].path_with_namespace')"
echo "$REPOS"
i=1
#echo "LENGTH: $(echo "${REPOS}" | jq -s length)" >&2
while test "$(echo "${REPOS}" | jq -s length)" = "100"
do
i=$((i + 1))
REPOS="$(curl ${CURL_OPTS} -s -f -H "Private-token: $GITLAB_TOKEN" "${GITLAB_HTTP}api/v4/projects?per_page=100&simple=1&membership=1&archived=0&page=${i}" | tr -d '\r\n' | jq '.[].path_with_namespace')"
echo "$REPOS"
#echo "LENGTH: $(echo "${REPOS}" | jq -s length)" >&2
done
}
REPOS="$( getRepos )"
echo "Repositories: $(echo "${REPOS}" | jq -s length)"
echo "${REPOS}" | jq -s . >"${PREFIX}/repos.json"
for rr in ${REPOS}
do
r="$(echo "${rr}" | jq -r .)"
ignore=0
if test "x${EXCLUDE_REPOS}" != "x"; then
echo "${r}" | grep -E "${EXCLUDE_REPOS}" >/dev/null
if test "x$?" = "x0"; then
echo "# ignore repo: ${r}"
ignore=1
fi
fi
if test "x${ignore}" != "x1"; then
"${SCRIPTDIR}/backup-git-repo" "${GITLAB_URL}${r}.git" "${PREFIX}${r}"
fi
done
#get deleted repos:
echo
echo "Deleted repositories:"
for r in $(cd "${PREFIX}"; ls -d */*)
do
if ! echo "${REPOS}" | jq -s -e --arg r "${r}" 'index($r)' >/dev/null
then
echo ${r}
fi
done