Skip to content

Instantly share code, notes, and snippets.

@njh
Created August 23, 2017 21:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save njh/0f55985070927b106ff234899f364a09 to your computer and use it in GitHub Desktop.
Save njh/0f55985070927b106ff234899f364a09 to your computer and use it in GitHub Desktop.
Script to download/clone all of your Github repos
#!/usr/bin/perl
#
# Script to download/clone all of your Github repos
#
# Copyright Nicholas Humfrey, 22nd August 2017
#
# License: http://unlicense.org
#
use JSON;
use strict;
my $username = 'xxx'; # Your GitHub username
my $git_dir = '/tmp/git-backups';
unless (-d $git_dir) {
die "Git directory does not exist: $git_dir";
}
for(my $page=1; $page < 50; $page++) {
print "Fetching respository list page $page\n";
my $json = `curl --fail -s https://api.github.com/users/$username/repos?page=$page`;
if ($json eq '') {
die "Failed to get list of repos";
}
my $repos = decode_json($json);
if (scalar(@$repos) == 0) {
print "Done!\n";
exit;
}
foreach my $repo (@$repos) {
my $name = $repo->{'name'};
if ($repo->{'fork'}) {
print "Ignoring Fork: $name\n";
} else {
print "\nFetching: $name\n";
my $git_url = $repo->{'git_url'};
my $backup_path = $git_dir.'/'.$repo->{'name'}.'.git';
print "$git_url => $backup_path\n";
if (-e $backup_path) {
chdir($backup_path) or die "Failed to change to $backup_path: $!";
if (system('git', 'fetch', '-q', '--all') != 0) {
die "Failed to fetch repository: $backup_path";
}
} else {
if (system('git', 'clone', '--mirror', $git_url, $backup_path) != 0) {
die "Failed to clone repository: $git_url";
}
}
open(DESC, ">$backup_path/description") or
die "Failed to open description file: $!";
print DESC $repo->{'description'};
close(DESC);
open(CLONEURL, ">$backup_path/cloneurl") or
die "Failed to open cloneurl file: $!";
print CLONEURL $git_url;
close(CLONEURL);
open(GITHUBJSON, ">$backup_path/github.json") or
die "Failed to open cloneurl file: $!";
print GITHUBJSON to_json($repo, {pretty => 1});
close(GITHUBJSON);
print "\n";
}
}
# Avoid hitting the GitHub API rate limiter
sleep(1);
}
@samholst
Copy link

Thanks man! Works like a charm!

@EdReynolds3
Copy link

Brilliant! Thanks. Only had to edit "xxxx" to my username and it worked as advertised.

@prolightHub
Copy link

Awesome I wanted to do this so I would have these offline! You should check my Khan Academy project downloader extension!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment