GitLab?服務(wù)器宕機(jī)時(shí)的項(xiàng)目代碼恢復(fù)方法
重要前提:GitLab 數(shù)據(jù)掛載盤必須能夠正常讀取,且
/var/opt/gitlab/git-data/repositories
目錄下的數(shù)據(jù)可以完整拷貝。
當(dāng) GitLab 服務(wù)器意外宕機(jī)且沒(méi)有備份時(shí),項(xiàng)目代碼的恢復(fù)變得尤為關(guān)鍵。以下是經(jīng)過(guò)優(yōu)化的恢復(fù)流程,相比傳統(tǒng)方法更為簡(jiǎn)潔高效。
一、數(shù)據(jù)拷貝與準(zhǔn)備
掛載數(shù)據(jù)盤將宕機(jī)服務(wù)器的數(shù)據(jù)盤掛載到其他正常運(yùn)行的主機(jī)或服務(wù)器上。確保
/var/opt/gitlab/git-data
目錄下的所有內(nèi)容能夠完整拷貝到新的主機(jī)或服務(wù)器中。sudo mount /dev/sdX /mnt/data # 示例掛載命令,需根據(jù)實(shí)際情況調(diào)整
拷貝數(shù)據(jù)將
/var/opt/gitlab/git-data
目錄下的所有內(nèi)容完整拷貝到新主機(jī)的指定目錄,例如/mnt/recovery
。sudo cp -r /mnt/data/var/opt/gitlab/git-data /mnt/recovery/
二、識(shí)別項(xiàng)目數(shù)據(jù)
GitLab 的項(xiàng)目數(shù)據(jù)存儲(chǔ)在 /var/opt/gitlab/git-data/repositories/@hashed
目錄下,文件夾名稱經(jīng)過(guò)哈希處理,無(wú)法直接識(shí)別項(xiàng)目信息。但每個(gè)項(xiàng)目文件夾(如 xxxxx.git
)下的 config
文件中存儲(chǔ)了項(xiàng)目相關(guān)的部分信息,可以提取倉(cāng)庫(kù)所有者及倉(cāng)庫(kù)名稱。
注意:
xxx.wiki.git
和xxx.design.git
文件夾通??梢院雎?,因?yàn)樗鼈儾话匾a數(shù)據(jù),且其config
文件中也不包含倉(cāng)庫(kù)所有者及倉(cāng)庫(kù)名稱。
三、簡(jiǎn)化恢復(fù)方法
傳統(tǒng)的恢復(fù)方法通常需要搭建新的 GitLab 服務(wù)器并進(jìn)行數(shù)據(jù)鏡像,但這種方法存在以下問(wèn)題:
- 需要確保新舊服務(wù)器的 GitLab 版本完全一致,否則可能導(dǎo)致數(shù)據(jù)無(wú)法正確鏡像。
- 操作步驟繁瑣,耗時(shí)且容易出錯(cuò)。
事實(shí)上,我們可以采用更簡(jiǎn)單的方法直接恢復(fù)代碼,無(wú)需搭建新服務(wù)器。
以項(xiàng)目文件夾 73/47/73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049.git
為例,以下是具體步驟:
設(shè)置安全目錄由于 GitLab 的項(xiàng)目目錄可能被識(shí)別為不安全目錄,需要通過(guò)以下命令將其標(biāo)記為安全目錄:
git config --global --add safe.directory /mnt/recovery/repositories/@hashed/73/47/73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049.git
克隆項(xiàng)目在上文中提到,
config
文件中存儲(chǔ)了完整的倉(cāng)庫(kù)所有者和倉(cāng)庫(kù)名稱(例如author/project_name
)。我們可以通過(guò)克隆操作將項(xiàng)目恢復(fù)到本地目錄。假設(shè)目標(biāo)項(xiàng)目路徑是your_clone_dir/author/project_name
,那么可以執(zhí)行以下命令來(lái)完成克?。?/p>git clone /mnt/recovery/repositories/@hashed/73/47/73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049.git your_clone_dir/author/project_name
四、自動(dòng)化恢復(fù)腳本
為了進(jìn)一步簡(jiǎn)化操作,以下是一個(gè) Python 腳本,可以快速執(zhí)行上述操作,只需提供哈?;瘋}(cāng)庫(kù)的源目錄和克隆倉(cāng)庫(kù)的目標(biāo)目錄。
#!/usr/bin/env python # -*-coding:utf-8 -*- # ============================================================================== # Copyright (c) 2025 laugh12321 Authors. All Rights Reserved. # # Licensed under the GNU General Public License v3.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.gnu.org/licenses/gpl-3.0.html # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== # File : hashed_repo_cloner.py # Version : 1.0 # Author : laugh12321 # Contact : laugh12321@vip.qq.com # Date : 2025/03/31 14:51:38 # Desc : None # ============================================================================== from pathlib import Path import configparser import subprocess import argparse from typing import Optional from rich.progress import track import sys def extract_repo_name_from_config(config_path: Path) -> str: """ 從Git配置文件中提取倉(cāng)庫(kù)完整路徑 :param config_path: Git配置文件路徑 :return: 倉(cāng)庫(kù)完整路徑 :raises ValueError: 如果配置缺少gitlab段或fullpath鍵 :raises FileNotFoundError: 如果配置文件不存在 """ if not config_path.is_file(): raise FileNotFoundError(f"Git config file not found: {config_path}") config = configparser.ConfigParser() config.read(config_path) if 'gitlab' not in config or 'fullpath' not in config['gitlab']: raise ValueError(f"Config file missing required gitlab section or fullpath key: {config_path}") return config.get('gitlab', 'fullpath') def add_safe_directory(git_dir: Path) -> None: """ 將Git目錄添加到安全目錄列表 :param git_dir: Git倉(cāng)庫(kù)路徑 """ subprocess.run( ["git", "config", "--global", "--add", "safe.directory", str(git_dir)], check=True, stdout=subprocess.DEVNULL, # 將標(biāo)準(zhǔn)輸出重定向到 /dev/null stderr=subprocess.DEVNULL # 將標(biāo)準(zhǔn)錯(cuò)誤重定向到 /dev/null ) def clone_repository(source_dir: Path, target_dir: Path, repo_name: str) -> None: """ 克隆倉(cāng)庫(kù)到目標(biāo)目錄 :param source_dir: 源Git倉(cāng)庫(kù)路徑 :param target_dir: 目標(biāo)目錄路徑 :param repo_name: 倉(cāng)庫(kù)名稱 """ target_path = target_dir / repo_name subprocess.run( ["git", "clone", str(source_dir), str(target_path)], check=True, stdout=subprocess.DEVNULL, # 將標(biāo)準(zhǔn)輸出重定向到 /dev/null stderr=subprocess.DEVNULL # 將標(biāo)準(zhǔn)錯(cuò)誤重定向到 /dev/null ) def process_git_repositories(hashed_repos_dir: Path, output_dir: Path) -> None: """ 處理所有哈?;腉it倉(cāng)庫(kù)并將其克隆到輸出目錄 :param hashed_repos_dir: 包含哈希化倉(cāng)庫(kù)的目錄 :param output_dir: 輸出目錄 """ # 預(yù)過(guò)濾.git目錄,排除wiki和design倉(cāng)庫(kù) git_folders = [ folder for folder in hashed_repos_dir.rglob("*.git") if not folder.name.endswith((".wiki.git", ".design.git")) ] if not git_folders: print("No valid Git repositories found to process.") return for git_folder in track(git_folders, description="Processing repositories"): config_path = git_folder / "config" try: repo_name = extract_repo_name_from_config(config_path) add_safe_directory(git_folder) clone_repository(git_folder, output_dir, repo_name) except Exception as e: print(f"Error processing {git_folder.name}: {e}") sys.exit() # 終止程序 def validate_directory(path: Optional[str]) -> Path: """ 驗(yàn)證并將路徑字符串轉(zhuǎn)換為Path對(duì)象 :param path: 路徑字符串 :return: Path對(duì)象 :raises ValueError: 如果路徑不存在或不是目錄 """ if path is None: raise ValueError("Path cannot be None") path_obj = Path(path) if not path_obj.exists(): raise ValueError(f"Path does not exist: {path}") if not path_obj.is_dir(): raise ValueError(f"Path is not a directory: {path}") return path_obj def parse_arguments(): """ 解析命令行參數(shù) :return: 包含參數(shù)的命名空間 """ parser = argparse.ArgumentParser( description="將GitLab哈希化倉(cāng)庫(kù)克隆到目標(biāo)目錄", formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument( "--source", type=str, required=True, help="包含哈?;瘋}(cāng)庫(kù)的源目錄(必須)" ) parser.add_argument( "--output", type=str, required=True, help="克隆倉(cāng)庫(kù)的目標(biāo)目錄(必須)" ) return parser.parse_args() def main(): args = parse_arguments() try: source_dir = validate_directory(args.source) output_dir = Path(args.output) process_git_repositories(source_dir, output_dir) except ValueError as e: print(f"Argument error: {e}") return 1 return 0 if __name__ == "__main__": exit(main())
使用方法
運(yùn)行以下命令即可啟動(dòng)腳本:
python hashed_repo_cloner.py --source gitlab_hashed_dir --output project_out_dir
五、后續(xù)操作
驗(yàn)證恢復(fù)結(jié)果進(jìn)入克隆后的項(xiàng)目目錄,檢查代碼完整性,確保所有分支和提交記錄都已正確恢復(fù)。
cd project_out_dir/author/project_name git log # 查看提交記錄 git branch -a # 查看所有分支
重新托管到 GitLab 或其他平臺(tái)如果需要將恢復(fù)的代碼重新托管到 GitLab 或其他代碼托管平臺(tái),可以按照以下步驟操作:
- 在目標(biāo)平臺(tái)創(chuàng)建新的倉(cāng)庫(kù)。
- 將本地克隆的項(xiàng)目推送到新倉(cāng)庫(kù):
git remote add origin <新倉(cāng)庫(kù)的URL> git push -u origin --all git push -u origin --tags
通過(guò)上述方法,我們無(wú)需搭建新服務(wù)器,也無(wú)需擔(dān)心版本兼容問(wèn)題,能夠快速高效地恢復(fù) GitLab 項(xiàng)目代碼。
到此這篇關(guān)于GitLab 服務(wù)器宕機(jī)時(shí)的項(xiàng)目代碼恢復(fù)方法的文章就介紹到這了,更多相關(guān)GitLab 服務(wù)器宕機(jī)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
服務(wù)器 jupyter 文件名亂碼問(wèn)題及解決方法
對(duì)于本臺(tái)電腦,autodl服務(wù)器,上傳中文文件時(shí),從壓縮包名到壓縮包里的文件名先后會(huì)出現(xiàn)中文亂碼的問(wèn)題,本文給大家分享服務(wù)器 jupyter 文件名亂碼問(wèn)題及解決方法,感興趣的朋友一起看看吧2023-11-11云服務(wù)器Centos中安裝Docker的方法小結(jié)
Docker是一個(gè)開(kāi)源的應(yīng)用容器引擎,利用操作系統(tǒng)本身已有的機(jī)制和特性,可以實(shí)現(xiàn)遠(yuǎn)超傳統(tǒng)虛擬機(jī)的輕量級(jí)虛擬化,CentOS是Linux發(fā)行版之一,本文給大家介紹云服務(wù)器Centos中安裝Docker的方法,感興趣的朋友一起看看吧2023-12-12服務(wù)器配置禁止IP直接訪問(wèn)只允許域名訪問(wèn)的實(shí)現(xiàn)步驟
聯(lián)網(wǎng)信息系統(tǒng)需設(shè)置只允許通過(guò)域名訪問(wèn),禁止使用IP地址直接訪問(wèn),建議同時(shí)采用云防護(hù)技術(shù)隱藏系統(tǒng)真實(shí)IP地址且只允許云防護(hù)節(jié)點(diǎn)IP訪問(wèn)服務(wù)器,提升網(wǎng)絡(luò)安全防護(hù)能力,這篇文章主要介紹了服務(wù)器配置禁止IP直接訪問(wèn)只允許域名訪問(wèn),需要的朋友可以參考下2024-03-03銀河麒麟V10服務(wù)器版安裝達(dá)夢(mèng)DM8數(shù)據(jù)庫(kù)的詳細(xì)過(guò)程
這篇文章主要介紹了銀河麒麟V10服務(wù)器版安裝達(dá)夢(mèng)DM8數(shù)據(jù)庫(kù)的詳細(xì)過(guò)程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-03-03