SoFunction
Updated on 2025-04-14

GitLab project code recovery method when server downtime

GitLab project code recovery method when server downtime

Updated: April 1, 2025 08:46:38 Author: laugh12321
When the GitLab server crashes unexpectedly and there is no backup, the recovery of project code becomes particularly critical. This article mainly introduces the project code recovery method when the GitLab server crashes. Friends who need it can refer to it.

Important premise: The GitLab data mount disk must be able to be read normally, and/var/opt/gitlab/git-data/repositoriesThe data in the directory can be completely copied.

Recovering project code becomes especially critical when the GitLab server unexpectedly goes down and there is no backup. The following is an optimized recovery process, which is simpler and more efficient than the traditional method.

1. Data copying and preparation

  • Mount the data diskMount the data disk of the downtime server to another running host or server. make sure/var/opt/gitlab/git-dataAll contents in the directory can be completely copied to a new host or server.

    sudo mount /dev/sdX /mnt/data  # Sample mount command,Need to be adjusted according to actual conditions
  • Copy dataWill/var/opt/gitlab/git-dataAll contents in the directory are completely copied to the specified directory of the new host, for example/mnt/recovery

    sudo cp -r /mnt/data/var/opt/gitlab/git-data /mnt/recovery/

2. Identify project data

GitLab's project data is stored in/var/opt/gitlab/git-data/repositories/@hashedIn the directory, the folder name hashed and the project information cannot be directly identified. But each project folder (such as)configThe file stores some information related to the project, and the warehouse owner and warehouse name can be extracted.

NoticeandFolders can usually be ignored because they do not contain important code data, and theirconfigThe file does not contain the warehouse owner and warehouse name.

3. Simplify recovery methods

Traditional recovery methods usually require building a new GitLab server and mirroring data, but this method has the following problems:

  • You need to ensure that the GitLab versions of the new and old servers are exactly the same, otherwise the data may not be mirrored correctly.
  • The operation steps are cumbersome, time-consuming and error-prone.

In fact, we can use a simpler method to directly recover code without building a new server.

Taking project folder73/47/As an example, the following are the specific steps:

  • Setting up a security directorySince GitLab's project directory may be recognized as an unsafe directory, it needs to be marked as a safe directory through the following command:

    git config --global --add  /mnt/recovery/repositories/@hashed/73/47/
  • Clone projectAs mentioned above,configThe complete repository owner and repository name are stored in the file (e.g.author/project_name). We can restore the project to the local directory through cloning operations. Assume that the target project path isyour_clone_dir/author/project_name, then you can execute the following command to complete the cloning:

    git clone /mnt/recovery/repositories/@hashed/73/47/ your_clone_dir/author/project_name

4. Automatic recovery script

To further simplify operations, the following is a Python script that can quickly perform the above operations, simply providing the source directory of the hashed repository and the target directory of the cloned repository.

#!/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
#
#     /licenses/gpl-3.  
#
# 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@
# Date    :   2025/03/31 14:51:38
# Desc    :   None
# ==============================================================================
from pathlib import Path
import configparser
import subprocess
import argparse
from typing import Optional
from  import track
import sys
def extract_repo_name_from_config(config_path: Path) -> str:
    """
     Extract the full path of the repository from the Git configuration file
     :param config_path: Git configuration file path
     :return: The complete path of the warehouse
     :raises ValueError: If the configuration is missing the gitlab segment or fullpath key
     :raises FileNotFoundError: If the configuration file does not exist
     """
    if not config_path.is_file():
        raise FileNotFoundError(f"Git config file not found: {config_path}")
    config = ()
    (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 ('gitlab', 'fullpath')
def add_safe_directory(git_dir: Path) -> None:
    """
     Add Git directory to the secure directory list
     :param git_dir: Git repository path
     """
    (
        ["git", "config", "--global", "--add", "", str(git_dir)],
        check=True,
        stdout=,  # Redirect standard output to /dev/null        stderr=   # Redirect standard error to /dev/null    )
def clone_repository(source_dir: Path, target_dir: Path, repo_name: str) -> None:
    """
     Clone the repository to the target directory
     :param source_dir: source Git repository path
     :param target_dir: target directory path
     :param repo_name: repository name
     """
    target_path = target_dir / repo_name
    (
        ["git", "clone", str(source_dir), str(target_path)],
        check=True,
        stdout=,  # Redirect standard output to /dev/null        stderr=   # Redirect standard error to /dev/null    )
def process_git_repositories(hashed_repos_dir: Path, output_dir: Path) -> None:
    """
     Process all hashedged Git repositories and clone them to the output directory
     :param hashed_repos_dir: directory containing hashed repository
     :param output_dir: output directory
     """
    # Pre-filter .git directory, exclude wiki and design repositories    git_folders = [
        folder for folder in hashed_repos_dir.rglob("*.git")
        if not ((".", "."))
    ]
    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}")
            ()  # Terminate the programdef validate_directory(path: Optional[str]) -> Path:
    """
     Verify and convert path string to Path object
     :param path: path string
     :return: Path object
     :raises ValueError: if the path does not exist or is not a directory
     """
    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():
    """
     Resolve command line parameters
     :return: namespace containing parameters
     """
    parser = (
        description="Clone the GitLab hashing repository to the target directory",
        formatter_class=
    )
    parser.add_argument(
        "--source",
        type=str,
        required=True,
        help="The source directory containing the hashed repository (must)"
    )
    parser.add_argument(
        "--output",
        type=str,
        required=True,
        help="Clone the target directory of the repository (must)"
    )
    return parser.parse_args()
def main():
    args = parse_arguments()
    try:
        source_dir = validate_directory()
        output_dir = Path()
        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())

How to use

Run the following command to start the script:

python hashed_repo_cloner.py --source gitlab_hashed_dir --output project_out_dir

5. Follow-up operations

  • Verify recovery resultsGo to the cloned project directory, check the code integrity, and make sure all branches and commit records are correctly restored.

    cd project_out_dir/author/project_name
    git log  # View submission historygit branch -a  # View all branches
  • Rehost to GitLab or other platformIf you need to rehost your recovered code to GitLab or other code hosting platforms, you can follow these steps:

    • Create a new repository on the target platform.
    • Push locally cloned projects to a new repository:
      git remote add origin <New warehouseURL>
      git push -u origin --all
      git push -u origin --tags

Through the above method, we do not need to build a new server or worry about version compatibility issues, and we can quickly and efficiently restore GitLab project code.

This is the article about the project code recovery method when GitLab server downtime. For more related content on GitLab server downtime, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!

  • GitLab
  • server
  • Downtime

Related Articles

  • WampServer sets apache pseudo-static appearance 404 not found and You don't have permissions

    This article mainly introduces the solution to WampServer setting apache pseudo-static appearance 404 not found and You don't have permission to access / on this server. It analyzes several common situations in a more detailed manner and is very practical. Friends who need it can refer to it.
    2015-10-10
  • The solution steps to not log in to the server's rabbitmq guest account

    This article mainly introduces the solution steps for the server's rabbitmq guest account to not log in. This article introduces you very detailedly. Interested friends, let's take a look.
    2024-07-07
  • Steps to implement multi-server file sharing in NFS

    NFS allows computers in the network to share resources, and clients can transparently read and write files on remote NFS servers. This article will introduce the methods and steps of NFS to realize multi-server file sharing. Those who are interested can learn about it.
    2025-01-01
  • 800401F3 error solution

    Today, ASP called Web Service and reported an error, with the error code 800401F3, and the error message was: Failed.
    2009-08-08
  • MongoDB Study Notes (VI) MongoDB Index Usage and Efficiency Analysis

    The indexes in MongoDB are actually similar to relational databases. They are all designed to improve the efficiency of query and sorting, and the implementation principles are basically the same.
    2013-07-07
  • How to deploy a local server in Webpack

    webpack-dev-server is a web server for development environment. It integrates Webpack and provides functions such as real-time reloading and hot replacement. The following is a simple webpack-dev-server configuration and usage example. Interested friends follow the editor to take a look.
    2024-03-03
  • Dell dell poweredge r730 server system installation and configuration tutorial

    This article mainly introduces the detailed tutorial on the installation and configuration of Dell dell poweredge r730 server system. Friends who need it can refer to it.
    2018-05-05
  • DELL DOSA Server Boot CD Dell Systems Build and Update Utility I

    Dell System Build and Update Utility is a bootable utility for single-server operating system installation, preloaded OS firmware updates, and preloaded OS system configurations
    2016-04-04
  • Illustration of the tutorial for setting up a VSCode+CMake+Clang+GCC environment under win10

    This article mainly introduces the construction of VSCode+CMake+Clang+GCC environment under win10. This article introduces you very detailed in pictures and texts, and has certain reference value. Friends who need it can refer to it.
    2019-10-10
  • Tutorial on the installation and use of Vestacp free VPS host control panel

    In addition to providing us with a simple and easy-to-use management panel for website building, Vestacp also provides us with free post office functions and VPS performance monitoring, helping us better manage VPS servers and providing a visual website management panel, which is very suitable for multiple users.
    2017-07-07

Latest Comments