我在我的一台服务器上安装了gitolite,它有很多git存储库。因此,我决定备份所有这些,并找到了这关于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
在上面的脚本1中,我在同一台机器中对存储库进行备份,但在脚本backup_base_dir=/home/bean/gitbackup/
中提到的不同文件夹中进行备份,然后使用rsync
在另一台机器/服务器中进行备份。所以我的问题是,有没有办法避免在同一台机器上备份,我可以直接备份到另一台服务器/机器。只是想消除同一台机器中的备份,并希望直接备份到服务器机器。
两台机器/服务器都是Ubuntu 16
发布于 2017-09-16 03:45:51
对于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
为了遵循同样的方法,您需要以服务的形式公开所有存储库的列表,这里我在去中创建了一些小东西,希望它可以帮助您作为一个起点:
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))
}
基本上,将扫描您在服务器上运行代码的所有目录,并尝试以JSON格式找到返回的git remotes
--找到所有远程程序的列表。
从您的备份服务器以后,您可以只使用相同的方法,用于Github和备份,而不重复。
在这种情况下,您可以这样使用它:
for repo in $(curl -s your-server:8080 | jq -r '.[][]')
do
/usr/local/bin/git clone --mirror ${repo}
done
https://stackoverflow.com/questions/46249819
复制相似问题