forked from amitsaha/gitbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (156 loc) · 6.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"context"
"flag"
"fmt"
"log"
"net/url"
"sync"
"time"
"github.com/google/go-github/v34/github"
)
// MaxConcurrentClones is the upper limit of the maximum number of
// concurrent git clones
const MaxConcurrentClones = 20
const defaultMaxUserMigrationRetry = 5
var gitHostToken string
var useHTTPSClone *bool
var ignorePrivate *bool
var gitHostUsername string
func main() {
// Git host
var gitHost string
// Used for waiting for all the goroutines to finish before exiting
var wg sync.WaitGroup
defer wg.Wait()
// The services we know of and their default public host names
knownServices := map[string]string{
"github": "github.com",
"gitlab": "gitlab.com",
"bitbucket": "bitbucker.org",
}
// Generic flags
service := flag.String("service", "", "Git Hosted Service Name (github/gitlab/bitbucket)")
githostURL := flag.String("githost.url", "", "DNS of the custom Git host")
backupDir := flag.String("backupdir", "", "Backup directory")
ignorePrivate = flag.Bool("ignore-private", false, "Ignore private repositories/projects")
ignoreFork := flag.Bool("ignore-fork", false, "Ignore repositories which are forks")
useHTTPSClone = flag.Bool("use-https-clone", false, "Use HTTPS for cloning instead of SSH")
bare := flag.Bool("bare", false, "Clone bare repositories")
// GitHub specific flags
githubRepoType := flag.String("github.repoType", "all", "Repo types to backup (all, owner, member, starred)")
githubCreateUserMigration := flag.Bool("github.createUserMigration", false, "Download user data")
githubCreateUserMigrationRetry := flag.Bool("github.createUserMigrationRetry", true, "Retry creating the GitHub user migration if we get an error")
githubCreateUserMigrationRetryMax := flag.Int("github.createUserMigrationRetryMax", defaultMaxUserMigrationRetry, "Number of retries to attempt for creating GitHub user migration")
githubListUserMigrations := flag.Bool("github.listUserMigrations", false, "List available user migrations")
githubWaitForMigrationComplete := flag.Bool("github.waitForUserMigration", true, "Wait for migration to complete")
// Gitlab specific flags
gitlabProjectVisibility := flag.String("gitlab.projectVisibility", "internal", "Visibility level of Projects to clone (internal, public, private)")
gitlabProjectMembershipType := flag.String("gitlab.projectMembershipType", "all", "Project type to clone (all, owner, member, starred)")
flag.Parse()
if _, ok := knownServices[*service]; !ok {
log.Fatal("Please specify the git service type: github, gitlab, bitbucket")
}
if !validGitlabProjectMembership(*gitlabProjectMembershipType) {
log.Fatal("Please specify a valid gitlab project membership - all/owner/member")
}
if len(*githostURL) != 0 {
u, err := url.Parse(*githostURL)
if err != nil {
panic(err)
}
gitHost = u.Host
} else {
gitHost = knownServices[*service]
}
*backupDir = setupBackupDir(*backupDir, gitHost)
client := newClient(*service, *githostURL)
if *githubListUserMigrations {
mList, err := getGithubUserMigrations(client)
if err != nil {
log.Fatal(err)
}
for _, m := range mList {
mData, err := GetGithubUserMigration(client, m.ID)
if err != nil {
fmt.Printf("Error getting migration data: %v", *m.ID)
// FIXME
continue
}
var archiveURL string
_, err = client.(*github.Client).Migrations.UserMigrationArchiveURL(context.Background(), *m.ID)
if err != nil {
archiveURL = "No Longer Available"
} else {
archiveURL = "Available for Download"
}
fmt.Printf("%v - %v - %v - %v\n", *mData.ID, *mData.CreatedAt, *mData.State, archiveURL)
}
} else if *githubCreateUserMigration {
repos, err := getRepositories(client, *service, *githubRepoType, *gitlabProjectVisibility, *gitlabProjectMembershipType, *ignoreFork)
if err != nil {
log.Fatalf("Error getting list of repositories: %v", err)
}
log.Printf("Creating a user migration for %d repos", len(repos))
m, err := createGithubUserMigration(context.Background(), client, repos, *githubCreateUserMigrationRetry, *githubCreateUserMigrationRetryMax)
if err != nil {
log.Fatalf("Error creating migration: %v", err)
}
if *githubWaitForMigrationComplete {
migrationStatePollingDuration := 60 * time.Second
err = downloadGithubUserMigrationData(context.Background(), client, *backupDir, m.ID, migrationStatePollingDuration)
if err != nil {
log.Fatalf("Error querying/downloading migration: %v", err)
}
}
orgs, err := getGithubUserOwnedOrgs(context.Background(), client)
if err != nil {
log.Fatal("Error getting user organizations", err)
}
for _, o := range orgs {
orgRepos, err := getGithubOrgRepositories(context.Background(), client, o)
if err != nil {
log.Fatal("Error getting org repos", err)
}
if len(orgRepos) == 0 {
log.Printf("No repos found in %s", *o.Login)
continue
}
log.Printf("Creating a org migration (%s) for %d repos", *o.Login, len(orgRepos))
oMigration, err := createGithubOrgMigration(context.Background(), client, *o.Login, orgRepos)
if err != nil {
log.Fatalf("Error creating migration: %v", err)
}
if *githubWaitForMigrationComplete {
migrationStatePollingDuration := 60 * time.Second
downloadGithubOrgMigrationData(context.Background(), client, *o.Login, *backupDir, oMigration.ID, migrationStatePollingDuration)
}
}
} else {
tokens := make(chan bool, MaxConcurrentClones)
gitHostUsername = getUsername(client, *service)
if len(gitHostUsername) == 0 && !*ignorePrivate && *useHTTPSClone {
log.Fatal("Your Git host's username is needed for backing up private repositories via HTTPS")
}
repos, err := getRepositories(
client, *service, *githubRepoType,
*gitlabProjectVisibility, *gitlabProjectMembershipType, *ignoreFork,
)
if err != nil {
log.Fatal(err)
} else {
log.Printf("Backing up %v repositories now..\n", len(repos))
for _, repo := range repos {
tokens <- true
wg.Add(1)
go func(repo *Repository) {
stdoutStderr, err := backUp(*backupDir, repo, *bare, &wg)
if err != nil {
log.Printf("Error backing up %s: %s\n", repo.Name, stdoutStderr)
}
<-tokens
}(repo)
}
}
}
}