This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgithub-backup.py
executable file
·232 lines (181 loc) · 7.17 KB
/
github-backup.py
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
#!/usr/bin/env python
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import os
import sys
import git
import json
import glob
import time
import shutil
import syslog
import tarfile
import optparse
from configparser import ConfigParser
from github3 import login
from tempfile import mkdtemp
from os.path import join, exists
TODAY=time.strftime("%Y-%m-%d")
REPO_URL = "https://%(username)s:%(password)s@github.com/%(organization)s/%(repository)s.git"
WIKI_URL = "https://%(username)s:%(password)s@github.com/%(organization)s/%(repository)s.wiki.git"
def remove_older_than(pattern, x):
files = glob.glob(pattern)
files.sort(key=lambda f: os.path.getmtime(f))
for file in files[:-x]:
os.remove(file)
syslog.syslog("remove %s" % file)
def json_dump(fd, values):
json.dump(values, fd, indent=4, sort_keys=True)
def dump_members(gh, org, destdir, retention, dirname='members'):
ddir = join(destdir, dirname)
archname = join(ddir, "%s-%s.tar.bz2" % ( dirname, TODAY ))
if exists(archname):
syslog.syslog("%s already exists" % archname)
return
if not os.path.isdir(ddir):
os.mkdir(ddir)
temp = mkdtemp(dir=destdir)
for member in org.members():
fd = open(join(temp, "%s.json" % member.login), 'w')
json_dump(fd, gh.user(member.login).as_dict())
fd.close()
tar = tarfile.open(archname, "w:bz2")
tar.add(temp, arcname=dirname)
tar.close()
shutil.rmtree(temp)
syslog.syslog("%s are backed up in %s" % ( dirname, archname ))
remove_older_than(join(ddir, "%s-*.tar.bz2" % dirname), retention)
def dump_teams(org, destdir, retention, dirname='teams'):
ddir = join(destdir, dirname)
archname = join(ddir, "%s-%s.tar.bz2" % ( dirname, TODAY))
if exists(archname):
syslog.syslog("%s already exists" % archname)
return
if not os.path.isdir(ddir):
os.mkdir(ddir)
temp = mkdtemp(dir=destdir)
for team in org.teams():
fd = open(join(temp, "%s.json" % team.name), 'w')
members = []
for member in team.members():
members.append(member.as_dict())
json_dump(fd, members)
fd.close()
tar = tarfile.open(archname, "w:bz2")
tar.add(temp, arcname=dirname)
tar.close()
shutil.rmtree(temp)
syslog.syslog("%s are backed up in %s" % ( dirname, archname ))
remove_older_than(join(ddir, "%s-*.tar.bz2" % dirname), retention)
def dump_repo_details(repo, destdir):
fd = open(join(destdir, "%s.json" % repo.name), 'w')
json_dump(fd, repo.as_dict())
fd.close()
def dump_collaborators(repo, destdir):
fd = open(join(destdir, "%s-collaborators.json" % repo.name), 'w')
members = []
for team in repo.teams():
members.append(team.to_json())
json_dump(fd, members)
fd.close()
def dump_repo_issues(repo, destdir):
fd = open(join(destdir, "%s-issues.json" % repo.name), 'w')
fdc = open(join(destdir, "%s-issues-comments.json" % repo.name), 'w')
issues = []
comments = []
for issue in repo.issues():
issues.append(issue.as_dict())
for comment in issue.iter_comments():
comments.append(comment.as_dict())
json_dump(fd, issues)
json_dump(fdc, comments)
fd.close()
fdc.close()
def dump_repo_pulls(repo, destdir):
fd = open(join(destdir, "%s-pulls.json" % repo.name), 'w')
fdc = open(join(destdir, "%s-pulls-comments.json" % repo.name), 'w')
pulls = []
comments = []
for pull in repo.pull_requests():
pulls.append(pull.as_dict())
for comment in pull.comments():
comments.append(comment.as_dict())
json_dump(fd, pulls)
json_dump(fdc, comments)
fd.close()
fdc.close()
def dump_repo(org, username, password, type, destdir, retention):
count = 0
for repo in org.repositories(type=type):
while True:
try:
repodir = "%s-repos" % type
ddir = join(destdir, repodir)
reponame = "%s-%s" % ( repo.name, repo.id )
archname = join(ddir, "%s-%s.tar.bz2" % ( reponame, TODAY ))
if exists(archname):
syslog.syslog("%s already exists" % archname)
continue
if not os.path.isdir(ddir):
os.mkdir(ddir)
temp = mkdtemp(dir=destdir)
repo_data = {
'username' : username,
'password' : password,
'organization': org.login,
'repository' : repo.name
}
git.Repo.clone_from(
REPO_URL % repo_data, join(temp, repo.name), mirror=True)
if repo.has_wiki:
try:
git.Repo.clone_from(
WIKI_URL % repo_data, join(temp, "%s.wiki" % repo.name), mirror=True)
except git.exc.GitCommandError:
pass # the wiki is enabled but empty!"
dump_repo_details(repo, temp)
dump_collaborators(repo, temp)
dump_repo_pulls(repo, temp)
if repo.has_issues:
dump_repo_issues(repo, temp)
tar = tarfile.open(archname, "w:bz2")
tar.add(temp, arcname=reponame)
tar.close()
shutil.rmtree(temp)
syslog.syslog("repo %s is backed up in %s" % ( repo.name, archname ))
count += 1
remove_older_than(join(ddir, "%s-*.tar.bz2" % reponame), retention)
break
except:
syslog.syslog("Unexpected error: %s" % sys.exc_info()[0])
syslog.syslog("sleep 5 seconds and try again ...")
shutil.rmtree(temp)
time.sleep(5)
return count
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option("-c", "--config", dest="config", metavar="CONFIG",
type="string", help="github-backup configuration file")
(options, args) = parser.parse_args()
if options.config is None:
parser.error('configuration file not given')
config = ConfigParser()
config.read(options.config)
token = config.get('github-backup', 'token', fallback=None)
username = config.get('github-backup', 'username', fallback=None)
password = config.get('github-backup', 'password', fallback=None)
organization = config.get('github-backup', 'organization')
destdir = config.get('github-backup', 'destdir')
retention = int(config.get('github-backup', 'retention'))
syslog.openlog('github-backup')
syslog.syslog("starting a new session of backup")
if token:
gh = login(token=token)
else:
gh = login(username, password)
org = gh.organization(organization)
dump_members(gh, org, destdir, retention)
dump_teams(org, destdir, retention)
public_count = dump_repo(org, username, password, 'public', destdir, retention)
private_count = dump_repo(org, username, password, 'private', destdir, retention)
syslog.syslog("session is now completed, %s public and %s private repositories backed up" % ( public_count, private_count ))
syslog.closelog()