Linux系统备份Gitee等云git所有仓库与所有分支的数字资产

发布于:2024-10-11 ⋅ 阅读:(142) ⋅ 点赞:(0)

思路:

1. ssh 配置

2. reps.txt 列出所有仓库名

3. exp的自动化备份脚本

--

环境安装:

exp需要依赖安装的文件,所以先执行下(以ubuntu为例):

   sudo apt-get install expect

操作步骤:

ssh 配置

1. 添加公钥至 Gitee(Linux服务器)

 

ssh-keygen -t rsa -C "your_email@example.com" -f ~/.ssh/gitee
cat ~/.ssh/gitee.pub

 2. 编写 config 文件

 在 ~/.ssh/ 目录中创建或编辑 config 文件,添加以下内容:

Host Gitee
    User git
    Hostname gitee.com
    IdentityFile ~/.ssh/gitee

注意事项

  • 确保 ~/.ssh/ 目录和 config 文件的权限设置正确。通常使用以下命令设置权限:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/gitee
chmod 600 ~/.ssh/config

整理需要clone的仓库名称:reps.txt

rep001

rep002

rep003

自动化备份脚本 

vim clone_repos.exp

#!/usr/bin/expect

set original_dir [pwd]  ;# 记录当前的原始目录
set rep [open "reps.txt"]
while { [gets $rep line] != -1 } {
    set repo_path Gitee:chihj/$line.git"
    set mirror_path "$line.git"  ;# 假设镜像仓库存储在当前目录下,名称为 $line.git

    # 检查镜像仓库是否存在
    if {![file exists $mirror_path]} {
        # 如果不存在,克隆镜像
        spawn git clone --mirror $repo_path
    } else {
        # 如果存在,进入镜像目录并执行更新
        cd $mirror_path
        spawn git fetch --all --prune
    }

    expect {
        "Are you sure you want to continue connecting" {
            send "yes\r"
            expect eof
        }
        eof
    }

    # 返回到原始目录
    cd $original_dir
}

close $rep

运行: 

chmod +x clone_repos.exp
./clone_repos.exp

推荐方案

对于数字资产的长期备份,推荐使用 git clone --mirrorgit fetch --all 的结合方式.

再做个定时任务,就OKAY了。


网站公告

今日签到

点亮在社区的每一天
去签到