mirror of
https://github.com/clockfort/GitHub-Backup.git
synced 2025-03-16 00:00:06 +01:00
37 lines
1.2 KiB
Perl
Executable file
37 lines
1.2 KiB
Perl
Executable file
#!/usr/bin/perl
|
|
use strict;
|
|
use warnings;
|
|
use LWP::Simple;
|
|
|
|
die "Please supply a github username and backups directory\n" unless (@ARGV == 2);
|
|
|
|
my $username = $ARGV[0];
|
|
my $backupdir = $ARGV[1];
|
|
my $page = get("http://github.com/api/v2/yaml/repos/show/$username");
|
|
die "Could not complete github API query for $username\n" unless defined $page;
|
|
|
|
my @list = split(/\n/, $page);
|
|
my @urls = grep { /url/ } @list;
|
|
my @giturls = grep s/ :url: http/git/, @urls;
|
|
@urls = @giturls;
|
|
my @reponames = grep s/.*$username\///, @giturls;
|
|
|
|
foreach my $name (@reponames){
|
|
print "NAME: $name\n";
|
|
}
|
|
|
|
for(my $i = 0; $i < @urls; ++$i){
|
|
my $url = $urls[$i];
|
|
my $name = $reponames[$i];
|
|
unless(-e $backupdir){
|
|
system("mkdir $backupdir") and die "Couldn't make $backupdir.\n";
|
|
}
|
|
unless(-e "$backupdir/$name"){ #We haven't backed this up before, let's clone it
|
|
print "CLONING $url\n";
|
|
system("cd backups && git clone $url") and die "Encountered an error while git-cloning repository $name\n";
|
|
}
|
|
else{ #We've backed it up before, just fetch the most recent copy
|
|
print "EXISTED, FETCHING $name\n";
|
|
system("cd $backupdir/$name && git fetch -q") and die "Encountered an error while git-fetching repository $name\n";
|
|
}
|
|
}
|