折騰 GitLab 系列 - 2025 最新版安裝指南
本文將完整記錄如何在自家伺服器環境中安裝最新版 GitLab,並實作自動化備份機制。這篇文章面向對 Linux 與 GitLab 完全不熟悉的朋友,即便你是第一次接觸 GitLab,也能成功操作。
一、環境準備
這次安裝選擇的架構如下:
實體主機安裝 Proxmox VE (PVE)
在 PVE 中建立一台 Ubuntu Server 24.04 VM
於該 VM 中安裝 GitLab Omnibus
備份目標為 Google Drive,使用 restic + rclone
建立 PVE VM
PVE Web UI 建立 VM 時需注意幾個重點:
-
客體作業系統版本 選擇 6.x - Linux Kernel。
-
磁碟大小 視需求建議 50GB 起跳。
-
CPU Flags 建議勾選:md-clear, pcid, spec-ctrl,這些會對性能及安全性有幫助。
(PVE Web UI 中建立 VM 選擇 OS 版本的畫面)
(磁碟與 CPU Flags 設定畫面)
- 網路設定:若你想讓 VM 擁有固定 IP,可選擇 virtio 網卡,並於 Ubuntu 安裝時手動設定靜態 IP。
(建議插圖:Ubuntu Server 安裝過程中設定固定 IP 的畫面)
二、安裝 GitLab Omnibus
- 更新套件並安裝必要工具:
sudo apt update && sudo apt upgrade -y
sudo apt install curl openssh-server ca-certificates tzdata perl -y
- 安裝 GitLab 套件倉庫:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
- 安裝 GitLab CE 並指定網址:
sudo EXTERNAL_URL="https://your.gitlab.domain" apt install gitlab-ce
(請確保你已設定好反向代理(如 Nginx)及 HTTPS 憑證,或使用 Cloudflare 反代至內部。)
三、初始化 GitLab
安裝完成後,訪問你的 GitLab 網址,會看到初始登入畫面。
預設帳號為 root
首次登入會要求你設定密碼
(GitLab 首次登入畫面)
可視需求修改 root 的帳號名稱,但非必要。
四、設定備份機制(restic + rclone)
- 安裝 restic 與 rclone
sudo apt install restic rclone -y
- 設定 rclone
使用 rclone config 建立連線,例如掛載 Google Drive:
rclone config
(rclone config 過程中的操作畫面)
完成後可透過下列方式將 Google Drive 掛載到目錄:
mkdir /mnt/gdrive-restic
rclone mount myremote:path/to/folder /mnt/gdrive-restic --daemon --vfs-cache-mode writes
若 –daemon 無效,建議使用 tmux 啟動 mount 指令並 detach。
- 建立備份腳本
建立一個 gitlab_backup_to_gdrive.sh 檔案,內容如下:
#!/bin/bash
export RESTIC_PASSWORD="yourpassword"
export RESTIC_REPOSITORY="/mnt/gdrive-restic"
/opt/gitlab/bin/gitlab-backup create STRATEGY=copy
cp -r /etc/gitlab /var/opt/gitlab/backups/etc-gitlab
cp -r /var/log/gitlab /var/opt/gitlab/backups/logs-gitlab
restic backup /var/opt/gitlab/backups
restic forget --keep-daily 7 --keep-monthly 12 --keep-yearly 2 --prune
cd /var/opt/gitlab/backups
ls -1t *.tar | tail -n +2 | xargs -r rm --
建議在腳本執行時加上 2>&1 | tee /var/log/gitlab_restic_backup.log 讓 log 可查詢
- 設定排程任務(Crontab)
sudo crontab -e
加入:
5 * * * * /home/youruser/gitlab_backup_to_gdrive.sh >> /var/log/gitlab_restic_backup.log 2>&1
表示每小時第 5 分鐘自動執行備份任務
(crontab 編輯畫面)
- 驗證備份結果
tail -f /var/log/gitlab_restic_backup.log
或透過 rclone 掛載目錄 /mnt/gdrive-restic 檢查是否已建立 snapshot 資料夾。
小結
至此,我們已成功完成:
在 Proxmox VE 中建立 GitLab 專屬 VM
安裝並啟用 GitLab CE
設定外部反代與固定網址
實作 restic + rclone 備份 GitLab
下一篇預告
《折騰 GitLab 系列(二)- GitLab Pages 正確部署與自動化》