gitlab 无备份,通过文件恢复

k8s gitlab 恢复手册

老规矩断电,k8s挂了,恢复gitlab,记录一下思路

  1. 先搞一套gitlab用于还原旧的数据,把旧的挂载数据放进去(可以打开看,但是不能提交,会报错)
  2. 再搞一套gitlab用于同步旧数据的方式,把历史记录等信息,还原出来。

k8s环境:

  1. postgresql数据库
    挂载了data文件夹,复制到新的挂载下,直接使用。

  2. gitlab-ce

挂载3个路径

/var/opt/gitlab
data
data下的数据有用

/etc/gitlab
config
没用了

/var/log/gitlab
logs
没用了

具体操作:
先复制postgresql数据库下的data到,2套新的容器中(用户信息等内容全在里面)。
数据库启动成功后,启动2个gitlab-ce服务,查看项目和用户信息等内容。
在2个新的gitlab-ce中,能查看到项目的情况下,进入项目创建上传的空间。
把老的ce下的data里的数据复制到,用于备份的ce的data的目录下。(例如:data/git-data/repositories/@hashed/xxxx)
如果下载不了,可能需要重启下容器。
然后用代码进行恢复

package com.example;

//import org.springframework.boot.SpringApplication;
//import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.*;

//@SpringBootApplication
public class Git2gilabApplication {
    static String[] git = {
            "http://ip:port/group/xxxxxx.git",
    };
    static String tmpPath = "D:\\download\\code";

    public static void main(String[] args) {
        Process process = null;
        for (int i = 0; i < git.length; i++) {
            try {
                ProcessBuilder builder = new ProcessBuilder("cmd", "/c", "dir");
                builder.directory(new File(tmpPath));
                process = builder.start();
                String line = getLine(process);
                System.out.println(line);

                builder.command("git", "clone", git[i]);
                process = builder.start();
                line = getLine(process);
                System.out.println(line);

                String folder = git[i].substring(git[i].lastIndexOf("/") + 1, git[i].lastIndexOf("."));
                builder.directory(new File(tmpPath + "\\" + folder));

                builder.command("git", "branch", "-a");
                process = builder.start();
                line = getLine(process);
                System.out.println(line);

                String[] branchList = line.split("\\n");
                for (int j = 0; j < branchList.length; j++) {
                    if (branchList[j].contains("remotes/origin/")) {
                        String branch = branchList[j].substring(branchList[j].lastIndexOf("/") + 1);
                        builder.command("git", "checkout", branch);
                        process = builder.start();
                        line = getLine(process);
                        System.out.println(line);
                    }
                }

                builder.command("git", "remote", "remove", "origin");
                process = builder.start();
                line = getLine(process);
                System.out.println(line);

                builder.command("git", "remote", "add", "origin", "http://ip:port/group/" + folder + ".git");
                process = builder.start();
                line = getLine(process);
                System.out.println(line);

                builder.command("git", "push", "origin", "--all");
                process = builder.start();
                line = getLine(process);
                System.out.println(line);

                builder.command("git", "push", "origin", "--tags");
                process = builder.start();
                line = getLine(process);
                System.out.println(line);

                // 等待命令执行完成
                process.waitFor();

                System.out.println("done " + folder);
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            } finally {
                if (process != null) {
                    process.destroy();
                }
            }

        }
        System.out.println("done all");
    }


    public static String getLine(Process process) throws IOException {
        // 获取命令输出结果
        InputStream inputStream = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK
        StringBuffer sb = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        return sb.toString();
    }
}

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值