k8s gitlab 恢复手册
老规矩断电,k8s挂了,恢复gitlab,记录一下思路
- 先搞一套gitlab用于还原旧的数据,把旧的挂载数据放进去(可以打开看,但是不能提交,会报错)
- 再搞一套gitlab用于同步旧数据的方式,把历史记录等信息,还原出来。
k8s环境:
-
postgresql数据库
挂载了data文件夹,复制到新的挂载下,直接使用。 -
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();
}
}