gpt4 book ai didi

linux - 如何将 git 从一台服务器备份到另一台服务器

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:07 24 4
gpt4 key购买 nike

我在我的一台服务器上安装了 gitolite,它有很多 git 存储库。所以我决定备份所有这些并找到this关于 git bundle。并基于它创建了以下脚本

#!/bin/bash
projects_dir=/home/bean/git/
backup_base_dir=/home/bean/gitbackup/

test -d ${backup_base_dir} || mkdir -p ${backup_base_dir}
pushd $projects_dir

for repo in `find $projects_dir -type d -name .git`; do
cd $repo/..
this_repo=`basename $PWD`
git bundle create ${backup_base_dir}/${this_repo}.bundle --all
cd -
done
popd

cd $backup_base_dir
rsync -r . username@ip_address:/home/bean1/git_2nd_backup

在上面的脚本中,我首先在同一台机器上备份存储库,但在脚本 backup_base_dir=/home/bean/gitbackup/ 中提到的不同文件夹中,然后使用 rsync 在另一台机器/服务器上进行备份。所以我的问题是,有什么办法可以避免在同一台机器上备份,我可以直接备份到另一台服务器/机器上。只是想消除在同一台机器上的备份,并想直接备份到服务器机器上。

两台机器/服务器都是Ubuntu 16

最佳答案

对于 GitHub,这有效:

for repo in $(/usr/local/bin/curl -s -u <username>:<api-key> "https://api.github.com/user/repos?per_page=100&type=owner" | 
/usr/local/bin/jq -r '.[] | .ssh_url')
do
/usr/local/bin/git clone --mirror ${repo}
done

要遵循相同的方法,您需要将所有存储库的列表作为服务公开,在这里我在 go 中创建了一些小东西希望可以帮助你作为一个起点:

https://gist.github.com/nbari/c98225144dcdd8c3c1466f6af733c73a

package main

import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"

"github.com/nbari/violetear"
)

type Repos struct {
Remotes []string
}

func findRemotes(w http.ResponseWriter, r *http.Request) {
files, _ := filepath.Glob("*")
repos := &Repos{}
for _, f := range files {
fi, err := os.Stat(f)
if err != nil {
fmt.Println(err)
continue
}
if fi.Mode().IsDir() {
out, err := exec.Command("git",
fmt.Sprintf("--git-dir=%s/.git", f),
"remote",
"get-url",
"--push",
"--all",
"origin").Output()
if err != nil {
log.Println(err)
continue
}
if len(out) > 0 {
repos.Remotes = append(repos.Remotes, strings.TrimSpace(fmt.Sprintf("%s", out)))
}
}
}
if err := json.NewEncoder(w).Encode(repos); err != nil {
log.Println(err)
}
}

func main() {
router := violetear.New()
router.HandleFunc("*", findRemotes)
log.Fatal(http.ListenAndServe(":8080", router))
}

基本上,将扫描您在服务器上运行代码的所有目录,并尝试查找 git remotes 以 JSON 格式返回找到的所有远程列表。

稍后您可以从您的备份服务器使用与 Github 和备份相同的方法,而无需重复。

在这种情况下,您可以这样使用它:

for repo in $(curl -s your-server:8080 | jq -r '.[][]')
do
/usr/local/bin/git clone --mirror ${repo}
done

关于linux - 如何将 git 从一台服务器备份到另一台服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46249819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com