-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoBackupGitHub.sh
executable file
·260 lines (229 loc) · 6.78 KB
/
autoBackupGitHub.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!bin/bash
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
NC='\033[0m'
PURPLE='\033[35m'
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
UNDERLINE='\033[4m'
computeruser=$(whoami)
_help() {
cols=$(tput cols)
title="autoBackupGithub help manual"
printf "${BOLD}%*s${NORMAL}" $(((${#title}+$cols)/2)) "$title"
echo -e "\n${BOLD}NAME${NORMAL}"
echo -e "\tautoBackupGithub - backup all your public and private repository on github"
echo -e "\n${BOLD}SYNOPSIS${NORMAL}"
echo -e "\t./autoBackup -u [${UNDERLINE}username${NC}]... -l [${UNDERLINE}location${NC}]..."
echo -e "\n${BOLD}DESCRIPTION${NORMAL}"
echo -e "\tBackup all your github repository via github api."
echo -e "\tIf you wish to backup your private repository ,"
echo -e "\tyou need to go to github to generate Personal Access Token( PAT )."
echo -e "\n\t${BOLD}-u, --user${NORMAL}"
echo -e "\t\tgithub account's user name"
echo -e "\n\t${BOLD}-t, --token( optional )${NORMAL}"
echo -e "\t\tPersonal Access Token( PAT ) file location"
echo -e "\n\t${BOLD}-l, --location${NORMAL}"
echo -e "\t\tbackup storage device( e.g. /dev/sda2 )"
echo -e "\n\t${BOLD}--help${NORMAL}"
echo -e "\t\tbring up help manual"
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-u|--user)
username="$2"
shift
shift
;;
-t|--token)
token="$2"
shift
shift
;;
-l|--location)
directoryLocation="$2"
shift
shift
;;
--help)
_help
exit 0
shift
;;
*)
shift
;;
esac
done
if [[ -z ${token} ]]; then
token="-1"
else
token=$(cat "$token" 2>&1)
fi
if [[ -z ${username} ]] || [[ -z ${token} ]] || [[ -z ${directoryLocation} ]]; then
echo -e "${RED}argument can not be empty${NC} , use ./autoGithubBackup --help to bring up help manual"
exit 0
fi
directoryLocation=${directoryLocation%/}
time=$(date +"%Y/%m/%d-%H:%M:%S")
echo -e "\nexecute start time: ${time}\n"
checkCurl=$(apt-cache policy curl | grep Installed | cut -c14-)
echo -e "check curl"
if [[ ! -z ${checkCurl} ]]; then
echo -e "\t${GREEN}curl checked${NC}"
else
echo -e "\t${RED}curl not installed , please install it and re-run the program${NC}"
fi
# checkMount=$(cat /proc/mounts | grep "$directoryLocation")
# echo -e "check hard disk status"
# if [[ ! -z ${checkMount} ]]; then
# echo -e "\t${GREEN}${directoryLocation} mounted${NC}"
# else
# echo -e "\t${RED}${directoryLocation} unmount , mounting...${NC}"
# check=$(sudo mount -t ntfs ${directoryLocation} /media/${computeruser} 2>&1)
# checkMount=$(cat /proc/mounts | grep "$directoryLocation")
# if [[ ! -z ${checkMount} ]]; then
# echo -e "\t${GREEN}${directoryLocation} mounted${NC}"
# else
# echo -e "\t${RED}error occurred!!${NC}"
# exit 0
# fi
# fi
checkInternet=$(ping -c 1 8.8.8.8 | grep 64\ bytes\ from)
echo -e "check internet connection"
if [ "$checkInternet" == "" ]; then
echo -e "\t${RED}connection error${NC}"
exit 1
else
echo -e "\t${GREEN}connection okay${NC}"
fi
echo -e "check ssh"
checkSSH=$(ssh -T [email protected] 2>&1)
checkSSH=$(echo "$checkSSH" | grep -we "authenticated" | grep -we ""$username"")
if [[ ! -z ${checkSSH} ]]; then
echo -e "\t${GREEN}ssh check done${NC}"
else
echo -e "\t${RED}ssh check failed${NC}"
echo -e "\t${YELLOW}use https instead${NC}"
fi
echo -e "check token"
if [[ ${token} == "-1" ]]; then
echo -e "\t${YELLOW}token not specified , using username ${username} to get repository${NC}"
repo=$(curl -s https://api.github.com/users/"$username"/repos 2>&1)
temp=$(echo "$repo" | grep "\"message\"\: \"Not Found\"")
if [[ ! -z ${temp} ]]; then
echo -e "\t${RED}username not found , please check again${NC}"
exit 0
else
echo -e "\t${GREEN}username checked${NC}"
repo=$(echo "$repo" | grep -wE "\"full_name\"|\"private\"|\"clone_url\"")
fi
else
echo -e "\t${GREEN}token specified${NC}"
repo=$(curl -sH "Authorization: token "$token"" https://api.github.com/user/repos 2>&1)
temp=$(echo "$repo" | grep "\"message\"\: \"Bad credentials\"")
if [[ ! -z ${temp} ]]; then
echo -e "\t${RED}token not found , please check again${NC}"
exit 0
else
echo -e "\t${GREEN}token checked${NC}"
repo=$(echo "$repo" | grep -wE "\"full_name\"|\"private\"|\"ssh_url\"")
fi
fi
echo -e "using github api querying user's repo"
privaeRepoCount=$(echo "$repo" | grep "\"private\": true" | wc -l)
repoCount=$(echo "$repo" | wc -l)
repoCount=$((repoCount/3))
echo -e "\t${username}'s total repo count = ${repoCount}"
echo -e "\t${username}'s ${YELLOW}private${NC} repo count = ${privaeRepoCount}"
echo -e "creating backup directory"
backupDir="${directoryLocation}/github_repo_backup"
tempDir="${directoryLocation}/temp_dir"
readonly backupDir
readonly tempDir
mkdir -p ${backupDir}
mkdir -p ${tempDir}
if [[ -d ${backupDir} ]]; then
echo -e "\t${GREEN}${backupDir} directory create successfully${NC}"
temp=$(rm -rf ${backupDir}/* && rm -rf ${backupDir}/.* 2>&1)
else
echo -e "\t${RED}${backupDir} directory create failed${NC}"
exit 0
fi
if [[ -d ${tempDir} ]]; then
echo -e "\t${GREEN}${tempDir} directory create successfully${NC}"
temp=$(rm -rf ${backupDir}/* && rm -rf ${backupDir}/.* 2>&1)
else
echo -e "\t${RED}${tempDir} directory create failed${NC}"
exit 0
fi
echo -e "backing up..."
# setting case-insensetive
shopt -s nocasematch
repo=$(echo "$repo" | sed 's/\"//g')
repo=$(echo "$repo" | sed 's/\,//g')
name=""
echo "$repo" | while IFS= read -r line; do
IFS=' '
count=0
flag=0
line=$(echo "$line" | sed 's/\://')
for word in $line; do
# check if full name
if [[ ${word} = "full_name" ]]; then
flag=1
fi
# the first word of each line does not need to output , just need to save it in to variable "name"
if [[ count -eq 0 ]]; then
((count++))
continue
else
if [[ ${word} = ${username}/* ]] || [[ flag -eq 1 ]]; then
# get repo name
name=$(echo ${word} | cut -d '/' -f2-)
flag=0
continue
fi
if [[ ${word} == "false" ]] || [[ ${word} == "true" ]]; then
echo -en "\tprivate: "
if [[ ${word} == "true" ]]; then
printf "${YELLOW}${word}${NC}"
else
printf "${word}"
fi
else
# output url
echo -e "\t${word}"
# get full repository path
if [ "$token" != "-1" ]; then
repoPathT="${username}:${token}@"
else
repoPathT=""
fi
repoPath1="https://"
repoPath2="github.com/${username}/${name}.git"
fullRepoPath="${repoPath1}${repoPathT}${repoPath2}"
mkdir -p ${tempDir}/${name}
# clone repo
temp=$(git clone ${fullRepoPath} ${tempDir}/${name})
if [[ -d "${tempDir}/${name}" ]]; then
mkdir -p ${backupDir}/${name}
shopt -s dotglob
mv ${tempDir}/* ${backupDir}/.
shopt -u dotglob
temp=$(rm -rf ${tempDir}/* ${tempDir}/.* 2>&1)
echo -e "\t${GREEN}${name} clone done${NC}"
else
echo -e "\t${RED}${name} clone failed${NC}"
fi
fi
fi
done
done
rm -rf ${tempDir}
echo -e "done"
# set back case sensetive
shopt -u nocasematch