로컬 Git 저장소를 백업하는 방법?
저는 비교적 작은 프로젝트에서 git을 사용하고 있는데 .git 디렉터리의 내용을 압축하는 것이 프로젝트를 백업하는 좋은 방법이 될 수 있다고 생각합니다.근데 이건 좀 이상한데요 왜냐면 제가 복구할 때 제일 먼저 해야할 일은git reset --hard.
깃 레포를 이런 식으로 백업하는 데 문제가 있습니까?또한 더 나은 방법(예: 휴대용 깃 포맷 등)이 있습니까?
다른 공식적인 방법은 깃 번들(git bundle)을 사용하는 것입니다.
그러면 다음을 지원하는 파일이 생성됩니다.git fetch그리고.git pull두번째 레포를 업데이트할 수 있습니다.
증분 백업 및 복원에 유용합니다.
하지만 모든 것을 백업해야 하는 경우(일부 오래된 컨텐츠가 포함된 두 번째 레포가 이미 없기 때문에), Kent Fredric의 다음과 같이 제 다른 답변에서 언급했듯이 백업을 좀 더 정교하게 수행해야 합니다.
$ git bundle create /tmp/foo master
$ git bundle create /tmp/foo-all --all
$ git bundle list-heads /tmp/foo
$ git bundle list-heads /tmp/foo-all
(이것은 원자력 작업이며, 그것은 보관소를 만드는 것과는 반대입니다..gitfolder(folder, fantabolous에서 언급한 대로)
경고:저는 레포를 복제하는 팻 노츠의 해결책을 추천하고 싶지 않습니다.
많은 파일을 백업하는 것은 백업이나 업데이트하는 것보다 항상 까다롭습니다.단 하나
OP Yaranswer의 편집 내역을 보면 Yar가 처음에 사용한 것을 알 수 있습니다.clone --mirror, ... 편집과 함께:
이것을 Dropbox와 함께 사용하는 것은 완전히 엉망입니다.
동기화 오류가 발생하며, 디렉터리를 드롭박스에 롤백할 수 없습니다.
사용하다git bundle당신의 드롭박스에 백업하고 싶다면요.
야르의 현재 솔루션은git bundle.
내가 말한 대로잖아.
이 방법은 원격(베어) 저장소(별도의 드라이브, USB 키, 백업 서버 또는 심지어 깃허브에도 있음)를 만든 후 사용하는 것입니다.push --mirror원격 저장소를 내 로컬 저장소와 똑같이 보이게 하는 것입니다(원격 저장소가 베어 저장소라는 점은 제외).
이렇게 하면 빠른 전달이 아닌 업데이트를 포함한 모든 참조(브랜치 및 태그)가 푸시됩니다.로컬 저장소의 백업을 만드는 데 사용합니다.
맨 페이지는 다음과 같이 설명합니다.
푸시할 각 참조의 이름을 지정하는 대신, 모든 참조를 아래에 지정합니다.
$GIT_DIR/refs/(이에 포함되지만 이에 제한되는 것은 아닙니다.refs/heads/,refs/remotes/,그리고.refs/tags/원격 저장소에 미러링됩니다.새로 생성된 로컬 참조는 원격 끝으로 푸시되고 로컬 업데이트된 참조는 원격 끝에서 강제 업데이트되며 삭제된 참조는 원격 끝에서 제거됩니다.구성 옵션의 경우 기본값입니다.remote.<remote>.mirror설정되어 있습니다.
밀기 위해서 가명을 만들었어요
git config --add alias.bak "push --mirror github"
그리고는 그냥 달려갑니다.git bak백업을 하고 싶을 때마다 말입니다.
[이것은 제가 참고하기 위해 여기에 남겨둔 것입니다.]
나의 번들 스크립트는 다음과 같습니다.git-backup 생겼네요 ㅠㅠ
#!/usr/bin/env ruby
if __FILE__ == $0
bundle_name = ARGV[0] if (ARGV[0])
bundle_name = `pwd`.split('/').last.chomp if bundle_name.nil?
bundle_name += ".git.bundle"
puts "Backing up to bundle #{bundle_name}"
`git bundle create /data/Dropbox/backup/git-repos/#{bundle_name} --all`
end
은 합니다를 사용합니다.git backup그리고 가끔은 제가 사용하기도 합니다.git backup different-name제게 필요한 대부분의 가능성을 제공합니다
Yar의 스크립트를 조금 해킹하기 시작했고 그 결과는 man page와 install script를 포함하여 github에 있습니다.
https://github.com/najamelan/git-backup
설치:
git clone "https://github.com/najamelan/git-backup.git"
cd git-backup
sudo ./install.sh
깃허브에 대한 모든 제안과 요청을 환영합니다.
#!/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
이 질문에 대한 두 가지 답변은 모두 맞지만, 여전히 Github 저장소를 로컬 파일로 백업하기 위한 완전하고 짧은 솔루션을 놓치고 있었습니다.요점은 여기서 확인할 수 있습니다. 자유롭게 당신의 요구에 맞추거나 조정하세요.
backup.sh :
#!/bin/bash
# Backup the repositories indicated in the command line
# Example:
# bin/backup user1/repo1 user1/repo2
set -e
for i in $@; do
FILENAME=$(echo $i | sed 's/\//-/g')
echo "== Backing up $i to $FILENAME.bak"
git clone git@github.com:$i $FILENAME.git --mirror
cd "$FILENAME.git"
git bundle create ../$FILENAME.bak --all
cd ..
rm -rf $i.git
echo "== Repository saved as $FILENAME.bak"
done
복원.sh:
#!/bin/bash
# Restore the repository indicated in the command line
# Example:
# bin/restore filename.bak
set -e
FOLDER_NAME=$(echo $1 | sed 's/.bak//')
git clone --bare $1 $FOLDER_NAME.git
위에 있는 텍스트 벽을 헤치고 지나가면 아무 것도 없다고 생각할 수 있는 간단한 공식적인 방법을 찾았습니다.
다음을 사용하여 전체 번들 생성:
$ git bundle create <filename> --all
복원 방법:
$ git clone <filename> <folder>
이 작업은 원자 AFAIK입니다.자세한 사항은 공식 문서를 확인해 보세요.
zip과 관련하여: git 번들은 .git 폴더 크기에 비해 압축되고 놀라울 정도로 작습니다.
git-copy로 git repo를 백업할 수 있습니다. git-copy 저장된 새 프로젝트는 최소 스토리지 비용을 의미합니다.
git copy /path/to/project /backup/project.backup
그러면 프로젝트를 복원할 수 있습니다.git clone
git clone /backup/project.backup project
구글을 통해 이 질문에 도달했습니다.
여기 제가 가장 간단한 방법으로 한 일이 있습니다.
git checkout branch_to_clone
그런 다음 이 분기에서 새 깃 분기를 만듭니다.
git checkout -b new_cloned_branch
Switched to branch 'new_cloned_branch'
원래 지점으로 돌아와 다음 작업을 계속합니다.
git checkout branch_to_clone
백업 브랜치에서 무언가를 복구해야 하는 상황을 가정하면 다음과 같습니다.
git checkout new_cloned_branch -- <filepath> #notice the space before and after "--"
문제가 생기면 소스 브랜치를 삭제하고 백업 브랜치로 다시 이동하면 됩니다!!
언급URL : https://stackoverflow.com/questions/2129214/how-to-backup-a-local-git-repository
'programing' 카테고리의 다른 글
| apt-add-repository: 명령을 찾을 수 없음 오류가 도커 파일에 있습니다. (0) | 2023.09.10 |
|---|---|
| Powershell을 사용하여 파일의 속성을 변경하려면 어떻게 해야 합니까? (0) | 2023.09.10 |
| 파이썬의 파이프 문자 (0) | 2023.09.10 |
| Swift에서 파일/URL을 한 줄씩 읽기 (0) | 2023.09.10 |
| Spring MVC + Spring Data Rest를 혼합하면 홀수 MVC 응답이 발생합니다. (0) | 2023.09.05 |