Skip to content

Instantly share code, notes, and snippets.

@jasonthename
Forked from njh/backup-github-repos.pl
Created November 11, 2018 04:36
Show Gist options
  • Save jasonthename/09f20d47994493312d7083f477f366f3 to your computer and use it in GitHub Desktop.
Save jasonthename/09f20d47994493312d7083f477f366f3 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment