-
Notifications
You must be signed in to change notification settings - Fork 14
/
github2synology.sh
62 lines (60 loc) · 2.43 KB
/
github2synology.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
#!/bin/sh
# A script to backup Github repositories to a Synology.
# By Richard Bairwell. http://www.bairwell.com
# MIT Licenced. https://github.com/bairwell/github2synology
# token from https://github.com/settings/tokens
OAUTH_TOKEN="[PUT YOUR TOKEN HERE BETWEEN THE QUOTES]"
# where should the files be saved
BACKUP_PATH="/volume1/serverBackups/github/backup"
# you shouldn't need to change anything below here - unless you have over 100 repos: in which case, see the bottom.
COUNTER=100
TOTALCOUNTER=0
PAGE=1
GIT="c//volume1/@appstore/git/bin/git"
fetch_fromUrl() {
COUNTER=0
API_URL="https://api.github.com/user/repos?type=all&per_page=100&page=${PAGE}"
echo "Fetching from ${API_URL}"
REPOS=`curl -H "Authorization: token ${OAUTH_TOKEN}" -s "${API_URL}" | jq -r 'values[] | "\(.full_name),\(.private),\(.git_url),\(.has_wiki)"'`
for REPO in $REPOS
do
let COUNTER++
let TOTALCOUNTER++
REPONAME=`echo ${REPO} | cut -d ',' -f1`
PRIVATEFLAG=`echo ${REPO} | cut -d ',' -f2`
ORIGINALGITURL=`echo ${REPO} | cut -d ',' -f3`
HASWIKI=`echo ${REPO} | cut -d ',' -f4`
GITURL="${ORIGINALGITURL/git:\/\/github.com\//[email protected]:}"
mkdir "${BACKUP_PATH}/${REPONAME}" -p
REPOPATH="${BACKUP_PATH}/${REPONAME}/code"
if [ -d "$REPOPATH" ]; then
echo "PULLING Repo URL: ${REPONAME} from url ${GITURL} to ${REPOPATH}"
cd ${REPOPATH}
${GIT} pull
else
echo "CLONING Repo URL: ${REPONAME} from url ${GITURL} to ${REPOPATH}"
${GIT} clone ${GITURL} ${REPOPATH}
if [ "true"===${PRIVATEFLAG} ]; then
`touch ${BACKUP_PATH}/${REPONAME}/private`
fi
fi
if [ "true"===${HASWIKI} ]; then
WIKIPATH="${BACKUP_PATH}/${REPONAME}/wiki"
WIKIURL="${ORIGINALGITURL/git:\/\/github.com\//[email protected]:}"
WIKIURL=`echo ${WIKIURL} | sed -e "s/.git$/.wiki.git/"`
if [ -d "$WIKIPATH" ]; then
echo "PULLING Repo Wiki: ${REPONAME} from url ${WIKIURL}: to ${WIKIPATH}"
cd ${WIKIPATH}
${GIT} pull
else
echo "CLONING Repo Wiki: ${REPONAME} from url ${WIKIURL}:to ${WIKIPATH}"
${GIT} clone ${WIKIURL} ${WIKIPATH}
fi
fi
done
}
until [ $COUNTER -lt 100 ]; do
fetch_fromUrl
let PAGE++
done
echo $((TOTALCOUNTER)) repositories updated