Skip to content

Instantly share code, notes, and snippets.

@barthr
Created June 5, 2017 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save barthr/c01019efcc3eac773775a278d1ffd361 to your computer and use it in GitHub Desktop.
Save barthr/c01019efcc3eac773775a278d1ffd361 to your computer and use it in GitHub Desktop.
Backup all your github repositories
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"golang.org/x/oauth2"
"github.com/google/go-github/github"
)
func main() {
token := flag.String("token", "", "acces-token to access github")
flag.Parse()
var tc *http.Client
if *token != "" {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: *token},
)
tc = oauth2.NewClient(ctx, ts)
}
client := github.NewClient(tc)
repositories, _, err := client.Repositories.List("", &github.RepositoryListOptions{
Visibility: "all",
Affiliation: "owner",
})
if err != nil {
log.Fatalln(err)
}
for _, repo := range repositories {
downloadURL := fmt.Sprintf("%s/archive/master.zip", *repo.HTMLURL)
resp, err := http.Get(downloadURL)
if err != nil {
log.Fatalf("Error when downloading %s => %v", downloadURL, err)
continue
}
f, err := os.Create(fmt.Sprintf("%s.zip", *repo.Name))
if err != nil {
panic(err)
}
io.Copy(f, resp.Body)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment