如何将git从一台服务器备份到另一台服务器
问题描述:
我在我的一台服务器上安装了gitolite,它有很多git存储库。所以我决定采取所有这些的备份,发现this约git bundle
。并在此基础上创建了以下脚本如何将git从一台服务器备份到另一台服务器
#!/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 . [email protected]_address:/home/bean1/git_2nd_backup
在上面的脚本1这是我在存储库的备份在同一台机器,但不同的文件夹中的脚本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
我需要为我的'当地git'我已经'gitolite'配置。 –
您提到想要从服务器A备份到服务器B,而不需要复制本地的拳头,我建议的解决方案是只需将存储库远程拉出,因此避免了本地重复,在任何情况下使用'gitolite'都可能更容易因为你可以解析配置文件,只需拉远程,但一切都是相对的 – nbari