备份/镜像 Github 存储库

发布于 2024-09-15 21:26:09 字数 75 浏览 2 评论 0 原文

我想定期创建 github 存储库的备份。有没有一种快速方法可以在不知道整个列表是什么的情况下提取所有这些内容?

沃尔特

I'd like to periodically create a backup of my github repositories. Is there a quick way to pull all of them without knowing what the entire list is?

Walter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀中猫帐中妖 2024-09-22 21:26:09

现在,接受的答案中使用的 v2 API 不再起作用,是时候使用 Github API v3

您可以获取 JSON 格式的存储库列表,并

curl -i https://api.github.com/users/username/repos

注意分页!默认情况下,结果分页至 30 个项目。如果您的存储库数量超出了单个页面的容纳范围,您将获得一个 Link HTTP 响应标头,其中包含指向其他页面的链接(rel=next/last/first/prev)。您还可以要求更大的页面大小(最多 100):

curl -i https://api.github.com/users/username/repos?per_page=100

完整备份脚本(假设您有 100 个或更少的存储库)将如下所示:

#!/usr/bin/python
import os
import json
import urllib
import subprocess

username = 'username'  # specify your github username
os.chdir(os.expanduser('~/github'))  # location for your backups, must exist

url = 'https://api.github.com/users/%s/repos?per_page=100' % username
for repo in json.load(urllib.urlopen(url)):
    print "+", repo['full_name']
    if os.path.exists(repo['name']):
        subprocess.call(['git', 'pull'], cwd=repo['name'])
    else:
        subprocess.call(['git', 'clone', repo['git_url']])

Now that the v2 API used in the accepted answer no longer works, it's time for an update that uses Github API v3.

You can get the list of repositories in JSON format with

curl -i https://api.github.com/users/username/repos

Watch out for pagination! By default the results are paginated to 30 items. If you have more repositories than fit into a single page, you'll get a Link HTTP response header with links to the other pages (with rel=next/last/first/prev). You can also ask for a larger page size (up to 100):

curl -i https://api.github.com/users/username/repos?per_page=100

A full backup script (assuming you have 100 or fewer repositories) would look something like this:

#!/usr/bin/python
import os
import json
import urllib
import subprocess

username = 'username'  # specify your github username
os.chdir(os.expanduser('~/github'))  # location for your backups, must exist

url = 'https://api.github.com/users/%s/repos?per_page=100' % username
for repo in json.load(urllib.urlopen(url)):
    print "+", repo['full_name']
    if os.path.exists(repo['name']):
        subprocess.call(['git', 'pull'], cwd=repo['name'])
    else:
        subprocess.call(['git', 'clone', repo['git_url']])
梨涡 2024-09-22 21:26:09

您可以通过 GitHub 的 API 获取整个列表:

curl http://github.com/api/v2/yaml/repos/show/walterjwhite

例如,这个简单的 DOS/Unix shell 单行:

ruby -ryaml -ropen-uri -e "puts YAML.load(open('http://github.com/api/v2/yaml/repos/show/walterjwhite'))['repositories'].map {|r| %Q[* **#{r[:name]}** (#{r[:description]}) is at <#{r[:url]}/>] }"

打印(假设您安装了 Ruby):

You can get the entire list via GitHub's API:

curl http://github.com/api/v2/yaml/repos/show/walterjwhite

For example, this simple DOS/Unix shell one-liner:

ruby -ryaml -ropen-uri -e "puts YAML.load(open('http://github.com/api/v2/yaml/repos/show/walterjwhite'))['repositories'].map {|r| %Q[* **#{r[:name]}** (#{r[:description]}) is at <#{r[:url]}/>] }"

prints (assuming you have Ruby installed):

飞烟轻若梦 2024-09-22 21:26:09

我一直在等待的答案。

我决定尝试一下 Ruby,结果还不错。我喜欢它的紧凑性,但它看起来不太漂亮:(。

这有效:

#!/usr/bin/env ruby
require "yaml"
require "open-uri"

time = Time.new
backupDirectory = "/storage/backups/github.com/#{time.year}.#{time.month}.#{time.day}"
username = "walterjwhite"

#repositories =
# .map{|r| %Q[#{r[:name]}] }

#FileUtils.mkdir_p #{backupDirectory}

YAML.load(open("http://github.com/api/v2/yaml/repos/show/#{username}"))['repositories'].map{|repository|

    puts "found repository: #{repository[:name]} ... downloading ..."
    #exec
    system "git clone [email protected]:#{username}/#{repository[:name]}.git #{backupDirectory}/#{repository[:name]}"
}

Walter

The answer I was waiting for.

I decided to give Ruby a try and it is okay. I like how it is compact, but it isn't pretty looking :(.

This works:

#!/usr/bin/env ruby
require "yaml"
require "open-uri"

time = Time.new
backupDirectory = "/storage/backups/github.com/#{time.year}.#{time.month}.#{time.day}"
username = "walterjwhite"

#repositories =
# .map{|r| %Q[#{r[:name]}] }

#FileUtils.mkdir_p #{backupDirectory}

YAML.load(open("http://github.com/api/v2/yaml/repos/show/#{username}"))['repositories'].map{|repository|

    puts "found repository: #{repository[:name]} ... downloading ..."
    #exec
    system "git clone [email protected]:#{username}/#{repository[:name]}.git #{backupDirectory}/#{repository[:name]}"
}

Walter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文