备份github用户或组织
项目描述
该软件包可以用于备份整个Github组织、仓库或用户账户,包括星标仓库、问题和维基(维基为克隆,问题为json文件)。
要求
GIT 1.9+
Python
安装
通过PyPI使用PIP
pip install github-backup
通过Github使用PIP(可能是最新版本)
pip install git+https://github.com/josegonzalez/python-github-backup.git#egg=github-backup
Python新手的安装说明
Python脚本默认情况下可能不会被包含在您的$PATH中,这意味着您不能直接在终端中通过$ github-backup ... 运行它,您可以将python的安装路径添加到您的环境变量$PATH中,或者直接调用脚本,例如使用$ ~/.local/bin/github-backup.*
基本帮助
显示CLI帮助输出
github-backup -h
CLI帮助输出
github-backup [-h] [-u USERNAME] [-p PASSWORD] [-t TOKEN_CLASSIC] [-f TOKEN_FINE] [--as-app] [-o OUTPUT_DIRECTORY] [-l LOG_LEVEL] [-i] [--starred] [--all-starred] [--watched] [--followers] [--following] [--all] [--issues] [--issue-comments] [--issue-events] [--pulls] [--pull-comments] [--pull-commits] [--pull-details] [--labels] [--hooks] [--milestones] [--repositories] [--bare] [--lfs] [--wikis] [--gists] [--starred-gists] [--skip-archived] [--skip-existing] [-L [LANGUAGES ...]] [-N NAME_REGEX] [-H GITHUB_HOST] [-O] [-R REPOSITORY] [-P] [-F] [--prefer-ssh] [-v] [--keychain-name OSX_KEYCHAIN_ITEM_NAME] [--keychain-account OSX_KEYCHAIN_ITEM_ACCOUNT] [--releases] [--latest-releases NUMBER_OF_LATEST_RELEASES] [--skip-prerelease] [--assets] [--exclude [REPOSITORY [REPOSITORY ...]] [--throttle-limit THROTTLE_LIMIT] [--throttle-pause THROTTLE_PAUSE] USER Backup a github account positional arguments: USER github username optional arguments: -h, --help show this help message and exit -u USERNAME, --username USERNAME username for basic auth -p PASSWORD, --password PASSWORD password for basic auth. If a username is given but not a password, the password will be prompted for. -f TOKEN_FINE, --token-fine TOKEN_FINE fine-grained personal access token or path to token (file://...) -t TOKEN_CLASSIC, --token TOKEN_CLASSIC personal access, OAuth, or JSON Web token, or path to token (file://...) --as-app authenticate as github app instead of as a user. -o OUTPUT_DIRECTORY, --output-directory OUTPUT_DIRECTORY directory at which to backup the repositories -l LOG_LEVEL, --log-level LOG_LEVEL log level to use (default: info, possible levels: debug, info, warning, error, critical) -i, --incremental incremental backup --starred include JSON output of starred repositories in backup --all-starred include starred repositories in backup [*] --watched include JSON output of watched repositories in backup --followers include JSON output of followers in backup --following include JSON output of following users in backup --all include everything in backup (not including [*]) --issues include issues in backup --issue-comments include issue comments in backup --issue-events include issue events in backup --pulls include pull requests in backup --pull-comments include pull request review comments in backup --pull-commits include pull request commits in backup --pull-details include more pull request details in backup [*] --labels include labels in backup --hooks include hooks in backup (works only when authenticated) --milestones include milestones in backup --repositories include repository clone in backup --bare clone bare repositories --lfs clone LFS repositories (requires Git LFS to be installed, https://git-lfs.github.com) [*] --wikis include wiki clone in backup --gists include gists in backup [*] --starred-gists include starred gists in backup [*] --skip-existing skip project if a backup directory exists -L [LANGUAGES [LANGUAGES ...]], --languages [LANGUAGES [LANGUAGES ...]] only allow these languages -N NAME_REGEX, --name-regex NAME_REGEX python regex to match names against -H GITHUB_HOST, --github-host GITHUB_HOST GitHub Enterprise hostname -O, --organization whether or not this is an organization user -R REPOSITORY, --repository REPOSITORY name of repository to limit backup to -P, --private include private repositories [*] -F, --fork include forked repositories [*] --prefer-ssh Clone repositories using SSH instead of HTTPS -v, --version show program's version number and exit --keychain-name OSX_KEYCHAIN_ITEM_NAME OSX ONLY: name field of password item in OSX keychain that holds the personal access or OAuth token --keychain-account OSX_KEYCHAIN_ITEM_ACCOUNT OSX ONLY: account field of password item in OSX keychain that holds the personal access or OAuth token --releases include release information, not including assets or binaries --latest-releases NUMBER_OF_LATEST_RELEASES include certain number of the latest releases; only applies if including releases --skip-prerelease skip prerelease and draft versions; only applies if including releases --assets include assets alongside release information; only applies if including releases --exclude [REPOSITORY [REPOSITORY ...]] names of repositories to exclude from backup. --throttle-limit THROTTLE_LIMIT start throttling of GitHub API requests after this amount of API requests remain --throttle-pause THROTTLE_PAUSE wait this amount of seconds when API request throttling is active (default: 30.0, requires --throttle-limit to be set)
使用详情
身份验证
基于密码的认证如果启用了两步验证将失败,并将在2023年底被弃用。
--username用于基本的密码认证,与位置参数USER分开,该参数指定了您要备份的用户账户。
经典令牌比提供非常粗粒度权限的稍微不安全。
如果您需要对长时间运行的备份(例如cron作业)进行身份验证,建议使用细粒度个人访问令牌 -f TOKEN_FINE。
细粒度令牌
您可以通过选择特定的仓库或所有仓库来选择仓库范围来“生成新令牌”。在Github上,这可以在 设置 -> 开发者设置 -> 个人访问令牌 -> 细粒度令牌 中找到。
根据您的使用情况自定义权限,但为了个人账户的完整备份,您需要启用以下权限
用户权限:读取关注者、星标和关注的权限。
仓库权限:读取代码、提交状态、问题、元数据、页面、拉取请求和仓库钩子的权限。
优先使用SSH
如果启用了 --repositories、--all-starred、--wikis、--gists、--starred-gists 以克隆仓库,并使用 --prefer-ssh 参数,则将使用ssh来克隆git仓库,但所有其他连接仍然使用其自己的协议,例如,问题API请求使用HTTPS。
要使用SSH克隆,您需要设置SSH身份验证,例如使用SSH公钥和私钥,方法与Github上通常的做法相同。
在Mac OS X上使用Keychain
注意:在Mac OS X上,令牌可以安全地存储在用户的Keychain中。为此
从“应用程序 -> 实用工具 -> Keychain Access”打开Keychain
使用“文件 -> 新建密码项”添加一个新的密码项
在“Keychain项名称”框中输入名称。您必须使用该名称通过 –keychain-name 参数在github-backup中使用。
在“帐户名称”框中输入帐户名称,输入上面设置的Github用户名。您必须使用该名称通过 –keychain-account 参数在github-backup中使用。
在“密码”框中输入您的Github个人访问令牌
注意:当您运行github-backup时,您将被询问是否允许“安全”使用您存储在Keychain中的机密信息。您有两个选项
允许:在这种情况下,您需要每次运行 github-backup 时都点击“允许”
始终允许:在这种情况下,当您将来运行 github-backup 时,不会要求您进行权限许可。这不太安全,但如果你想要计划 github-backup 自动运行,则这是必需的
Github速率限制和节流
“github-backup”将根据Github API的反馈自动进行节流。
他们的API通常每小时限制为5000次调用。API会要求github-backup暂停,直到下一个小时开始时限制重置(下一个小时的开始)。这将继续,直到备份完成。
在大型备份过程中,例如 --all-starred,并且在快速连接的情况下,这可能导致(约20分钟)暂停,API调用周期性地达到API限制。如果不合适的话,在现实世界条件下已经观察到,使用 --throttle-limit 5000 --throttle-pause 0.6 --throttle-pause 0.72(3600秒[1小时] / 5000限制)通过 --throttle-limit 5000 --throttle-pause 0.72 节流提供了一小时内平滑的速率,尽管 --throttle-pause 0.72 (3600秒[1小时] / 5000限制)从理论上讲更安全,可以防止大量速率限制暂停。
关于Git LFS
当您使用 --lfs 选项时,您需要确保您已经安装了Git LFS。
有关如何安装的说明可以在 https://git-lfs.github.com 上找到。
在Docker容器中运行
要使用Docker容器运行此工具,请使用以下命令
sudo docker run –rm -v /path/to/backup:/data –name github-backup ghcr.io/josegonzalez/python-github-backup -o /data
USER
常见问题/已知问题
并非所有内容都是必需的
参数--all不包括:克隆私有仓库(-P, --private),克隆分支(-F, --fork),克隆星标仓库(--all-starred),--pull-details),克隆LFS仓库(--lfs),克隆Gist(--starred-gists)或克隆星标Gist仓库(--starred-gists)。更多示例请参考。
克隆所有星标仓库的大小
使用参数--all-starred来克隆所有星标仓库可能会占用大量的存储空间,特别是如果使用了参数--all或其他参数。例如,常见的星标仓库可能有数万个问题、许多大型资产以及仓库本身等。可以考虑仅使用参数--starred将星标仓库的链接存储为JSON格式。
增量备份
使用(-i, --incremental)将只请求自上次运行(成功或失败)以来的新数据。例如,仅请求自上次运行以来API中的问题。
这意味着前次运行中遇到的任何阻塞错误都可能导致备份中缺失大量数据。
已知阻塞错误
某些错误将通过退出脚本阻塞备份运行。例如,从GitHub API收到403禁止错误。
如果使用了增量参数,则下一次备份将仅请求自上次阻塞/失败运行以来的API数据。这可能导致意外的大量数据缺失。
因此,建议只有在积极监控输出/结果或与定期进行完整的非增量运行相结合时才使用增量参数,以避免在常规备份运行中意外缺失数据。
星标公共仓库钩子阻塞
由于参数--all包括--hooks,如果您同时使用--all和--all-starred来克隆用户的星标公共仓库,备份可能会出错并阻止备份继续。
这是因为在公共仓库上需要正确的--hooks权限。
发布阻塞
已知的参数--releases(对于--assets是必需的)错误有时会阻止备份。
如果您正在备份包含发布的许多仓库,例如组织或--all-starred,您可能需要删除--releases(因此删除--assets)以完成备份。在问题209中记录:问题209。
“bare”实际上是“mirror”
使用裸克隆参数(--bare)实际上会调用git的clone --mirror命令。裸克隆和镜像克隆之间有一个细微的差别。
来自git文档:“与–bare相比,–mirror不仅将源分支映射到目标分支,还将所有引用(包括远程跟踪分支、注释等)映射,并设置refspec配置,以便所有这些引用都由目标仓库中的git remote update覆盖。”
星标仓库与星标Gist行为对比
使用星标正常仓库克隆(--all-starred)参数将星标仓库单独存储在用户自己的仓库之外。然而,使用--starred-gists将会将星标Gist存储在用户自己的Gist相同的目录下--gists。此外,所有Gist仓库目录的名称是ID,而不是Gist的名称。
跳过不完整的备份
--skip-existing参数将在目录已存在的情况下跳过备份,即使备份在该目录中失败(可能由于阻塞错误)。这可能导致常规备份中意外丢失数据。
Github备份示例
使用经典令牌备份所有仓库,包括私有仓库
export ACCESS_TOKEN=SOME-GITHUB-TOKEN github-backup WhiteHouse --token $ACCESS_TOKEN --organization --output-directory /tmp/white-house --repositories --private
使用细粒度的访问令牌备份单个组织仓库,包括其他所有内容(wiki、拉取请求、评论、问题等)
export FINE_ACCESS_TOKEN=SOME-GITHUB-TOKEN ORGANIZATION=docker REPO=cli # e.g. git@github.com:docker/cli.git github-backup $ORGANIZATION -P -f $FINE_ACCESS_TOKEN -o . --all -O -R $REPO
安静且增量备份有用的Github用户数据(公共和私有仓库以及SSH),包括所有问题、拉取请求、所有公共星标仓库和Gist(省略“hooks”、“releases”,因此省略“assets”以防止阻塞)。非常适合cron作业。
export FINE_ACCESS_TOKEN=SOME-GITHUB-TOKEN GH_USER=YOUR-GITHUB-USER github-backup -f $FINE_ACCESS_TOKEN --prefer-ssh -o ~/github-backup/ -l error -P -i --all-starred --starred --watched --followers --following --issues --issue-comments --issue-events --pulls --pull-comments --pull-commits --labels --milestones --repositories --wikis --releases --assets --pull-details --gists --starred-gists $GH_USER
在临时目录中调试错误/阻塞或不完整的备份。省略“增量”以填充先前的未完成备份。
export FINE_ACCESS_TOKEN=SOME-GITHUB-TOKEN GH_USER=YOUR-GITHUB-USER github-backup -f $FINE_ACCESS_TOKEN -o /tmp/github-backup/ -l debug -P --all-starred --starred --watched --followers --following --issues --issue-comments --issue-events --pulls --pull-comments --pull-commits --labels --milestones --repositories --wikis --releases --assets --pull-details --gists --starred-gists $GH_USER
开发
该项目的主要维护者 @josegonzalez 认为此项目功能完整。如果您需要错误修复或增强,欢迎提交拉取请求。如果您想赞助工作,请随时联系维护者进行咨询估算。
贡献者
向所有贡献者表示衷心的感谢!
测试
该项目目前不包含单元测试。要运行代码检查
pip install flake8 flake8 --ignore=E501
github_backup-0.46.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 29979240b4cd04b2893b6bedb0579fa4a550c14a1bec78de7c5303ca60b95ae9 |
|
MD5 | 7ad9670d95c7d47b7dad8967f8e96ffb |
|
BLAKE2b-256 | d946758a4660d372b1e9e775d8dd5faa205430a0e1be6fed6672bc69a34da84e |
github_backup-0.46.0-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a6e2a1f330ba7bf0c7a70a353650ac7d1bd91d86375d7789786fa3ff91c5bcff |
|
MD5 | 681083650fe2124b8fbdacf68d03c118 |
|
BLAKE2b-256 | 30f6d85fa886f8b794beddccc1aa948aad14b4aa28f71dfc0ccb27a0ecf345a4 |