I've seen instructions for backing up GitHub using Ruby (such as this example) or a shell script (like this one), but I wanted to use Python, which was already installed on my computer.
My environment:
- Windows 7
- Python 3.3.0
import json
import os
import sys
import urllib.request
import zipfile
def main():
# Change this to your own GitHub username.
user = 'xxxxx'
# The repos will be backed up to the directory you
# specify as a command-line argument.Example: gitback.py c:\gitbkup
target = sys.argv[1]
# Delete the target directory if it exists. Then create it.
if (target != None and target != '' and os.path.exists(target)):
os.system('rd /S /Q ' + target)
os.makedirs(target)
# Get up to 100 repos. If your account has more than 100 repos,
# increase the per_page value.
req = urllib.request.Request('https://api.github.com/users/' + user + '/repos?per_page=100')
resp = urllib.request.urlopen(req)
# The result is in byte format, so we need to specify
# the encoding and decode it.
content = resp.read().decode('utf-8')
# The result is in JSON format. Parse the JSON.
data = json.loads(content)
# Loop over all the repos, cloning each one.
for repo in data:
name = repo['name']
os.system('git clone git@github.com:' + user + '/' + name + ' ' + target + "\\" + name)
if __name__ == '__main__':
main()
No comments:
Post a Comment