如何备份本地Git存储库? [英] How to backup a local Git repository?

查看:159
本文介绍了如何备份本地Git存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个相对较小的项目上使用git,我发现压缩.git目录的内容可能是备份项目的好方法。但这有点奇怪,因为当我恢复时,我需要做的第一件事是 git reset --hard



以这种方式备份git回购存在任何问题吗?另外,有没有更好的方法来做到这一点(例如,一个可移植的git格式或类似的东西?)? 对Yar的脚本进行了一些攻击,结果在github上,包括man页面和安装脚本: / git-backuprel =noreferrer> https://github.com/najamelan/git-backup



安装

  git clonehttps://github.com/najamelan/git-backup.git
cd git-backup
sudo ./install.sh

欢迎所有关于github的建议和请求。


$ b

 #!/ usr / bin / env ruby​​ 

#对于文档,请海员git-backup(1)

#TODO:
# - 使其成为类而不是函数
# - 检查标准格式的git警告符合
# - 更好地检查git repo而不是调用git status
# - 如果在配置文件中找到多个条目,指定哪个文件
# - 使其与子模块一起工作
# - 建议在备份目录不存在时
# - 深入特征配置(例如。只保留3个备份以进行回购 - 例如旋转...)
# - 测试



#允许从其他脚本调用
def git_backup


#常量:
git_dir_name ='.git'#只是为了避免魔术字符串
filename_suffix =.git.bundle#将被添加到创建的备份文件名


#测试我们是否在git仓库中
`git status 2>& 1`

if $? .exitstatus!= 0

puts'fatal:不是git存储库:.git或者至少不能从git status获得零退出状态
exit 2


else #git status success
$ b $直到File :: directory?(Dir.pwd +'/'+ git_dir_name)\
或File :: directory?(Dir .pwd)=='/'


Dir.chdir('..')
结束


除非文件::目录?(Dir.pwd +'/.git')

raise('fatal:Directory still not a git repo:'+ Dir.pwd)

结束

结束


#git-config - 版本1.7.10做:

#如果密钥不存在git config用1
退出如果密钥在同一个文件中存在两次
#如果密钥恰好存在一次与0

#如果密钥不存在,则将一个空字符串发送到stdin
#如果密钥存在多次,则最后一个值发送到stdin
#如果一次只能找到一个密钥,它的值就会发送到stdin



#获取备份目录的设置
#------ ----------------------------------

directory =`git config --get backup.directory`


#git config添加一个换行符,所以删除它
directory.chomp!


#检查git config的退出状态
case $ ?. exitstatus

当1:directory = Dir.pwd [/(.+) \ / [^ \ /] + /,1]

puts'警告:在您的git配置文件中找不到backup.directory。请设置它。有关git配置文件的更多详细信息,请参阅man git config。默认为同一个directroy你的git repo在:+目录

当2:puts'警告:在你的git配置文件中找到多个backup.directory条目。将使用最后一个:'+目录

else除非$ ?. exitstatus == 0然后引发('致命:来自git-config的未知退出状态:'+ $ ?. exitstatus)end

end


#verify目录存在
除非File :: directory?(目录)

raise('fatal:backup目录不存在:'+目录)

结束


#日期和时间前缀
#--------- ---------------

prefix =''
prefix_date = Time.now.strftime('%F')+' - '#% F = YYYY-MM-DD
prefix_time = Time.now.strftime('%H:%M:%S')+' - '
add_date_default = true
add_time_default = false

前缀+ = prefix_date如果git_config_bool('backup.prefix-date',add_date_default)
前缀+ = prefix_time if git_config_bool('backup.prefix-time',add_time_default)



#默认捆绑包名称是回购的名称
bundle_name = Dir.pwd.split(' /').last

#如果给定
bundle_name = ARGV [0] if(ARGV [0])
$,则将文件的名称设置为第一个命令行参数b
$ b bundle_name = File :: join(目录,前缀+ bundle_name +文件名_suffix)


备份到包#{bundle_name.inspect}


#git bundle会打印它自己的错误信息,如果失败
`git bundle create#{bundle_name.inspect} --all --remotes`


end#def git_backup



#帮助函数调用git config检索布尔值
def git_config_bool(option,default_value)

#从git config获得前缀时间的设置
config_value =`git config --get#{option.inspect}`

#检查git的退出状态config
case $ ?. exitstatus

#未设置时取默认值
当返回值为default_value

时0:返回true,除非config_value =〜 /(false | no | 0)/ i

当2:放'警告:在你的git配置文件中找到#{option.inspect}的多个条目。将使用最后一个:'+ config_value
返回true,除非config_value =〜/(false | no | 0)/ i

else raise('致命:来自git-config的未知退出状态:'+ $ ?. exitstatus)

end
end

#如果我们没有包含在另一个脚本中,需要调用
git_backup if __FILE__ == $ 0


I am using git on a relatively small project and I find that zipping the .git directory's contents might be a fine way to back up the project. But this is kind of weird because, when I restore, the first thing I need to do is git reset --hard.

Are there any problems with backing up a git repo this way? Also, is there any better way to do it (e.g., a portable git format or something similar?)?

解决方案

I started hacking away a bit on Yar's script and the result is on github, including man pages and install script:

https://github.com/najamelan/git-backup

Installation:

git clone "https://github.com/najamelan/git-backup.git"
cd git-backup
sudo ./install.sh

Welcoming all suggestions and pull request on github.

#!/usr/bin/env ruby
#
# For documentation please sea man git-backup(1)
#
# TODO:
# - make it a class rather than a function
# - check the standard format of git warnings to be conform
# - do better checking for git repo than calling git status
# - if multiple entries found in config file, specify which file
# - make it work with submodules
# - propose to make backup directory if it does not exists
# - depth feature in git config (eg. only keep 3 backups for a repo - like rotate...)
# - TESTING



# allow calling from other scripts
def git_backup


# constants:
git_dir_name    = '.git'          # just to avoid magic "strings"
filename_suffix = ".git.bundle"   # will be added to the filename of the created backup


# Test if we are inside a git repo
`git status 2>&1`

if $?.exitstatus != 0

   puts 'fatal: Not a git repository: .git or at least cannot get zero exit status from "git status"'
   exit 2


else # git status success

   until        File::directory?( Dir.pwd + '/' + git_dir_name )             \
            or  File::directory?( Dir.pwd                      ) == '/'


         Dir.chdir( '..' )
   end


   unless File::directory?( Dir.pwd + '/.git' )

      raise( 'fatal: Directory still not a git repo: ' + Dir.pwd )

   end

end


# git-config --get of version 1.7.10 does:
#
# if the key does not exist git config exits with 1
# if the key exists twice in the same file   with 2
# if the key exists exactly once             with 0
#
# if the key does not exist       , an empty string is send to stdin
# if the key exists multiple times, the last value  is send to stdin
# if exaclty one key is found once, it's value      is send to stdin
#


# get the setting for the backup directory
# ----------------------------------------

directory = `git config --get backup.directory`


# git config adds a newline, so remove it
directory.chomp!


# check exit status of git config
case $?.exitstatus

   when 1 : directory = Dir.pwd[ /(.+)\/[^\/]+/, 1]

            puts 'Warning: Could not find backup.directory in your git config file. Please set it. See "man git config" for more details on git configuration files. Defaulting to the same directroy your git repo is in: ' + directory

   when 2 : puts 'Warning: Multiple entries of backup.directory found in your git config file. Will use the last one: ' + directory

   else     unless $?.exitstatus == 0 then raise( 'fatal: unknown exit status from git-config: ' + $?.exitstatus ) end

end


# verify directory exists
unless File::directory?( directory )

   raise( 'fatal: backup directory does not exists: ' + directory )

end


# The date and time prefix
# ------------------------

prefix           = ''
prefix_date      = Time.now.strftime( '%F'       ) + ' - ' # %F = YYYY-MM-DD
prefix_time      = Time.now.strftime( '%H:%M:%S' ) + ' - '
add_date_default = true
add_time_default = false

prefix += prefix_date if git_config_bool( 'backup.prefix-date', add_date_default )
prefix += prefix_time if git_config_bool( 'backup.prefix-time', add_time_default )



# default bundle name is the name of the repo
bundle_name = Dir.pwd.split('/').last

# set the name of the file to the first command line argument if given
bundle_name = ARGV[0] if( ARGV[0] )


bundle_name = File::join( directory, prefix + bundle_name + filename_suffix )


puts "Backing up to bundle #{bundle_name.inspect}"


# git bundle will print it's own error messages if it fails
`git bundle create #{bundle_name.inspect} --all --remotes`


end # def git_backup



# helper function to call git config to retrieve a boolean setting
def git_config_bool( option, default_value )

   # get the setting for the prefix-time from git config
   config_value = `git config --get #{option.inspect}`

   # check exit status of git config
   case $?.exitstatus

      # when not set take default
      when 1 : return default_value

      when 0 : return true unless config_value =~ /(false|no|0)/i

      when 2 : puts 'Warning: Multiple entries of #{option.inspect} found in your git config file. Will use the last one: ' + config_value
               return true unless config_value =~ /(false|no|0)/i

      else     raise( 'fatal: unknown exit status from git-config: ' + $?.exitstatus )

   end
end

# function needs to be called if we are not included in another script
git_backup if __FILE__ == $0

这篇关于如何备份本地Git存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆