完全备份git repo?

Dan*_*ton 116 git backup

有没有一种简单的方法来备份整个git仓库,包括所有分支和标签?

Von*_*onC 172

git bundle
Run Code Online (Sandbox Code Playgroud)

我喜欢这种方法,因为它只产生一个文件,更容易复制.
看到ProGit:一点欢乐.
另请参阅" 如何通过电子邮件向某人发送git存储库? "命令

git bundle create /tmp/foo-all --all
Run Code Online (Sandbox Code Playgroud)

详细说明:

git bundle将只打包由git show-ref显示的引用:这包括头,标签和远程头.
目的地使用的基础非常重要.
可以谨慎行事,导致捆绑文件包含目标中已有的对象,因为在目的地解包时会忽略这些对象.


要使用该捆绑包,您可以克隆它,指定一个不存在的文件夹(在任何git仓库之外):

git clone /tmp/foo-all newFolder
Run Code Online (Sandbox Code Playgroud)

  • 添加--all以完成备份 (7认同)
  • 这个,`git bundle` 是我认为的正确答案,而不是公认的答案。我觉得他对clone命令很了解,能问出这样的问题,对他来说显然是不够的(因为是clone,而不是dump)。转储与简单副本不同,例如:1)它们不需要是正常工作的最佳(或什至有能力)2)但它们需要对数据损坏具有良好的抵抗力和可修复性 3)它通常很有用如果它们对于增量备份很容易区分,而这不是副本的目标。 (2认同)
  • 请注意,`git bundle`或`git clone`都不能获得_everything_,例如钩子脚本. (2认同)
  • @Zitrax是的,它是设计的.挂钩可能很危险或包含敏感信息. (2认同)

Kin*_*nch 55

什么只是克隆它?

git clone --mirror other/repo.git
Run Code Online (Sandbox Code Playgroud)

每个存储库都是其远程的备份.

  • @Daniel:如果克隆存储库,则会获取每个分支,但仅检查默认分支.试试`git branch -a`.也许这种方式更明显:克隆存储库后,你不会获取每个分支,你获取每个提交.分支仅引用现有提交. (6认同)

fan*_*ous 21

扩展其他一些答案,这就是我所做的:

设置回购: git clone --mirror user@server:/url-to-repo.git

然后,当您要刷新备份时:git remote update从克隆位置.

这会备份所有分支和标记,包括稍后添加的新分支和标记,但值得注意的是,删除的分支不会从克隆中删除(备份可能是一件好事).

这是原子的,因此没有简单副本会出现的问题.

http://www.garron.me/en/bits/backup-git-bare-repo.html


Kim*_*kas 12

扩展KingCrunchVonC的绝佳答案

我将它们结合在一起:

git clone --mirror git@some.origin/reponame reponame.git
cd reponame.git
git bundle create reponame.bundle --all
Run Code Online (Sandbox Code Playgroud)

之后,您将拥有一个reponame.bundle可以轻松复制的文件。然后,您可以使用从其中创建一个新的普通git存储库git clone reponame.bundle reponame

请注意,git bundle仅副本提交会在存储库中导致某些引用(分支或标记)。因此,纠结提交不会存储到分发包中。

  • 我想你的意思是`git bundle create reponame.bundle --all`? (2认同)

Mad*_*des 7

该线程对于了解如何完成 git repos 的备份非常有帮助。我认为它仍然缺乏一些提示,信息或结论来为自己找到“正确的方法”(tm)。因此,在这里分享我的想法以帮助其他人,并将它们提交讨论以增强它们。谢谢。

所以从拿起原来的问题开始:

  • 目标是尽可能接近 git 存储库的“完整”备份。

然后用典型的愿望丰富它并指定一些预设:

  • 首选通过“热复制”进行备份以避免服务停机。
  • git 的缺点将通过额外的命令来解决。
  • 脚本应该执行备份以组合单个备份的多个步骤并避免人为错误(错别字等)。
  • 此外,脚本应该进行恢复以使转储适应目标机器,例如,甚至自备份以来原始机器的配置可能已经改变。
  • 环境是 Linux 机器上的 git 服务器,具有支持硬链接的文件系统。

1. 什么是“完整”git repo 备份?

关于什么是“100%”备份的观点不同。这里有两个典型的。

#1 开发者的观点

  • 内容
  • 参考

git 是一个开发者工具,通过git clone --mirror和支持这个观点git bundle --all

#2 管理员的观点

  • 内容文件
    • 特殊情况“packfile”:git 在垃圾收集期间将对象合并并压缩到 packfile 中(请参阅 参考资料git gc
  • git配置
  • 可选:操作系统配置(文件系统权限等)

git 是一个开发人员工具,它留给管理员。git 配置和操作系统配置的备份应该与内容的备份分开。

2. 技巧

  • “冷拷贝”
    • 停止服务以独占访问其文件。停机时间!
  • “热拷贝”
    • 服务为备份目的提供固定状态。正在进行的更改不会影响该状态。

3. 其他需要考虑的话题

它们中的大多数是通用的备份。

  • 是否有足够的空间来保存完整备份?将存储多少代?
  • 是否需要增量方法?将存储多少代以及何时再次创建完整备份?
  • 如何验证备份在创建后或随着时间的推移没有损坏?
  • 文件系统是否支持硬链接?
  • 将备份放入单个存档文件或使用目录结构?

4. git 提供什么来备份内容

  • git gc --auto

    • 文档:man git-gc
    • 清理并压缩存储库。
  • git bundle --all

    • 文档:man git-bundle,man git-rev-list
    • 原子 =“热拷贝”
    • 捆绑包是转储文件,可以直接与 git 一起使用(验证、克隆等)。
    • 支持增量提取。
    • 可通过git bundle verify.
  • git clone --mirror

    • 文档:man git-clone,man git-fsck,git clone --mirror 和 git clone --bare 有什么区别
    • 原子 =“热拷贝”
    • 镜像是真正的 git 存储库。
    • Primary intention of this command is to build a full active mirror, that periodically fetches updates from the original repository.
    • Supports hardlinks for mirrors on same file system to avoid wasting space.
    • Verifiable via git fsck.
    • Mirrors can be used as a basis for a full file backup script.

5. Cold-Copy

A cold-copy backup can always do a full file backup: deny all accesses to the git repos, do backup and allow accesses again.

  • Possible Issues
    • May not be easy - or even possible - to deny all accesses, e.g. shared access via file system.
    • Even if the repo is on a client-only machine with a single user, then the user still may commit something during an automated backup run :(
    • Downtime may not be acceptable on server and doing a backup of multiple huge repos can take a long time.
  • Ideas for Mitigation:
    • Prevent direct repo access via file system in general, even if clients are on the same machine.
    • For SSH/HTTP access use git authorization managers (e.g. gitolite) to dynamically manage access or modify authentication files in a scripted way.
    • Backup repos one-by-one to reduce downtime for each repo. Deny one repo, do backup and allow access again, then continue with the next repo.
    • Have planned maintenance schedule to avoid upset of developers.
    • Only backup when repository has changed. Maybe very hard to implement, e.g. list of objects plus having packfiles in mind, checksums of config and hooks, etc.

6. Hot-Copy

File backups cannot be done with active repos due to risk of corrupted data by on-going commits. A hot-copy provides a fixed state of an active repository for backup purposes. On-going commits do not affect that copy. As listed above git's clone and bundle functionalities support this, but for a "100% admin" backup several things have to be done via additional commands.

"100% admin" hot-copy backup

  • Option 1: use git bundle --all to create full/incremental dump files of content and copy/backup configuration files separately.
  • Option 2: use git clone --mirror, handle and copy configuration separately, then do full file backup of mirror.
    • Notes:
    • A mirror is a new repository, that is populated with the current git template on creation.
    • Clean up configuration files and directories, then copy configuration files from original source repository.
    • Backup script may also apply OS configuration like file permissions on the mirror.
    • Use a filesystem that supports hardlinks and create the mirror on the same filesystem as the source repository to gain speed and reduce space consumption during backup.

7. Restore

  • Check and adopt git configuration to target machine and latest "way of doing" philosophy.
  • Check and adopt OS configuration to target machine and latest "way of doing" philosophy.


Sun*_*ani 5

使用 git bundle 或 clone

复制 git 目录不是一个好的解决方案,因为它不是原子的。如果您有一个需要很长时间复制的大型存储库,并且有人推送到您的存储库,则会影响您的备份。克隆或制作捆绑包不会有这个问题。


Joh*_*ohn 5

IMO 的正确答案是git clone --mirror。这将完全备份您的回购。

Git clone mirror 会克隆整个仓库、notes、heads、refs 等,通常用于将整个仓库复制到新的 git 服务器。这将拉下所有分支和所有内容,整个存储库。

git clone --mirror git@example.com/your-repo.git
Run Code Online (Sandbox Code Playgroud)
  • 通常克隆一个 repo 不包括所有分支,只包括 Master。

  • 复制 repo 文件夹只会“复制”已拉入的分支……因此默认情况下,只有主分支或您之前签出的其他分支。

  • Git bundle 命令也不是您想要的:“bundle 命令会将通常使用 git push 命令通过网络推送的所有内容打包成一个二进制文件,您可以通过电子邮件将其发送给某人或放在闪存驱动器上,然后解绑到另一个存储库。” (来自git clone --mirror 和 git clone --bare 之间的区别是什么