-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_backup_script.sh
More file actions
146 lines (124 loc) · 3.36 KB
/
github_backup_script.sh
File metadata and controls
146 lines (124 loc) · 3.36 KB
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
#!/usr/bin/bash
set -e
# Source the environment variables from the .env file
source /etc/environment
RESET_COLOR="\\033[0m"
RED_COLOR="\\033[0;31m"
YELLOW_COLOR="\\033[0;33m"
GREEN_COLOR="\\033[0;32m"
function reset_color() {
echo -e "${RESET_COLOR}\\c"
}
function red_color() {
echo -e "${RED_COLOR}\\c"
}
function green_color() {
echo -e "${GREEN_COLOR}\\c"
}
function yellow_color() {
echo -e "${YELLOW_COLOR}\\c"
}
green_color
now=$(date)
echo "Starting GitHub Backup [${now}]"
echo
reset_color
### -------------- ###
### Check for curl ###
### -------------- ###
if ! [ "$(command -v curl)" ]; then
red_color
echo "You don't have installed curl"
exit 1
else
green_color
echo "curl is present on your machine, continue..."
fi
reset_color
### ------------ ###
### Check for jq ###
### ------------ ###
if ! [ "$(command -v jq)" ]; then
red_color
echo "You don't have installed jq"
exit 1
else
green_color
echo "jq is present on your machine, continue..."
fi
reset_color
### ------------------ ###
### Add GitHub to known hosts ###
### ------------------ ###
green_color
echo
echo "Adding GitHub to known hosts"
ssh-keyscan github.com >> ~/.ssh/known_hosts
reset_color
### ------------------ ###
### Update PATH ###
### ------------------ ###
green_color
echo
echo "Changing path to ${OUTPUT_PATH}"
cd "${OUTPUT_PATH}"
reset_color
### ------------------ ###
### Clone Repositories ###
### ------------------ ###
green_color
echo
page=0
while :; do
page=$((page+1))
repositories=$(curl -sf -u "${GITHUB_USERNAME}:${GITHUB_TOKEN}" "https://api.github.com/user/repos?per_page=100&page=${page}&visibility=all&affiliation=owner,organization_member" | jq -c --raw-output ".[] | {name, ssh_url}")
[ -z "$repositories" ] && break
for repository in ${repositories}; do
# Name of Repository (Used to check if we have to pull or clone)
name=$(jq -r ".name" <<< $repository)
# SSH URL of repository (Required SSH key setup in GitHub, this can also be replaced by html_url so that ssh key is not required)
url=$(jq -r ".ssh_url" <<< $repository)
# URL of repository locally (if it would exist)
local_url="${OUTPUT_PATH}/${name}"
if [[ -d "$local_url" ]]
then
echo "Pulling ${url}..."
cd "${local_url}"
reset_color
if ! git pull --rebase --quiet; then
yellow_color
echo "Detected dubious ownership in repository at '${local_url}'"
git config --global --add safe.directory "${local_url}"
green_color
echo "Added '${local_url}' to safe.directory"
git pull --rebase --quiet
fi
cd "${OUTPUT_PATH}"
else
echo "Cloning ${url}..."
reset_color
if ! git clone --quiet "${url}"; then
yellow_color
echo "Detected dubious ownership in repository at '${local_url}'"
git config --global --add safe.directory "${local_url}"
green_color
echo "Added '${local_url}' to safe.directory"
git clone --quiet "${url}"
fi
fi
green_color
echo "Repository ${name} backed up successfully"
done
done
green_color
echo
echo "All your repositories are successfully cloned in ${OUTPUT_PATH}"
echo
reset_color
### ------ ###
### Footer ###
### ------ ###
green_color
now=$(date)
echo "Local GitHub Backup is up-to-date [${now}]"
reset_color