22.13-22.17 搭建git服務器,安裝gitlab,使用gitlab,gitlab備份與恢復

22.13 搭建git服務器

22.14 22.15 安裝gitlab

22.16 使用gitlab

22.17 gitlab備份與恢復


22.13 搭建git服務器

github畢竟是公開的,而私有倉庫又得花錢買。所以我們可以想辦法搭建一個私有的,只自己公司使用的。Gitlab是個不錯的選擇。在介紹它之前,先講述一下命令行的git服務器

搭建準備工作

1 找一臺服務器,首先要安裝git,

yum install -y git

2 添加git用戶,並且設置shell爲/usr/bin/git-shell,目的是爲了不讓git用戶遠程登陸

useradd -s /usr/bin/git-shell git 
cd /home/git

3 創建authorized_keys文件,並更改屬主、屬組和權限,用來存客戶端機器上的公鑰

mkdir .ssh
touch .ssh/authorized_keys
chown -R git.git .ssh
chmod 600 .ssh/authorized_keys

4 把客戶端的公鑰,放到服務器的/root/.ssh/authorized_keys上,失效免密登錄

[root@9F-VM1 ~]# cat .ssh/id_rsa.pub
Nlet2N+TXAQJDOnrRtN42qFoRLAYXO+D8xsYDmwPMLdJymcYBbQJL5jU65/+loL7kuHcTtRSwk46xQXdLFPjN/c2c0I6OofxZAVKgPHGtjd8Y8dkOzYep6S6n7C/jug5PwbQedFoNy16+0JU1lBQa6IxqrWsXdLuM3Gc8Oko4sAvKQgZQRfJh root@9F-VM1
[root@DD-Server-9F ~]# vim !$
vim .ssh/authorized_keys
#9F-VM1
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7TRMjzg1SiLIZg3X3HBVaVzvfCMouDV/d0owu/SLddyHAitEvii3Nq1ElOhuB2hDbVq9fhEH61ZuBVqI+PkcI0KE04p8/KCSzF4dEvUdIYrQAeafxnLd7hl4QofmJbEnP07FQOurFFXItnniGz12mN95cbvijoLPNlet2N+TXA

5 登錄驗證測試

[root@9F-VM1 ~]# ssh [email protected]
Last login: Tue Sep  4 16:16:31 2018 from 192.88.29.250
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.88.24.200 closed.

不能直接遠程通過登錄的,但是能驗證到客戶端29.250的公鑰,稍後會再做說明。


在服務器創建git倉庫

6定好存儲git倉庫的目錄,比如 /data/git

 mkdir /data/git
 cd /data/git

7 初始化倉庫

[root@DD-Server-9F git]#  git init --bare sample.git


# 會創建一個裸倉庫,裸倉庫沒有工作區,因爲服務器上的Git倉庫純粹是爲了共享,所以不讓用戶直接登錄到服務器上去改工作區,並且服務器上的Git倉庫通常都以.git結尾

chown -R git.git sample.git

以上操作是在git服務器上做的,平時git服務器是不需要開發人員登錄修改代碼的,它僅僅是充當着一個服務器的角色,就像github一樣,平時操作都是在我們自己的pc上做的

 

8 客戶端使用倉庫

 首先要把客戶端上的公鑰放到git服務器上/home/git/.ssh/authorized_keys文件裏

 在客戶端上(自己pc)克隆遠程倉庫

 

執行命令:

 git clone git@ip:/data/git/sample.git
[root@9F-VM1 git]# cd /data/git
[root@9F-VM1 git]# git clone [email protected]:/data/git/sample.git
正克隆到 'sample'...
warning: 您似乎克隆了一個空版本庫。
[root@9F-VM1 git]# cd sample/
[root@9F-VM1 sample]# ls -la
總用量 0
drwxr-xr-x 3 root root  18 9月   4 16:41 .
drwxr-xr-x 9 root root 131 9月   4 16:41 ..
drwxr-xr-x 7 root root 119 9月   4 16:41 .git

此時就可以在當前目錄下生成一個sample的目錄,這個就是我們克隆的遠程倉庫了。進入到這裏面,可以開發一些代碼,然後push到遠程。


9 測試

添加文件實現遠程倉庫功能

[root@9F-VM1 sample]# touch 1.txt && echo -e "111\n222\n333" >> 1.txt
[root@9F-VM1 sample]# cat 1.txt
111
222
333
[root@9F-VM1 sample]# git add 1.txt
[root@9F-VM1 sample]# git commit -m "add the first file"
[master(根提交) e46c02a] add the first file
 1 file changed, 3 insertions(+)
 create mode 100644 1.txt
[root@9F-VM1 sample]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 219 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/data/git/sample.git
 * [new branch]      master -> master

9.1 在別的目錄克隆遠程倉庫,查看協同效果是否實現

[root@9F-VM1 git]# cd /data/git
[root@9F-VM1 git]# mkdir sample-t
[root@9F-VM1 git]# cd sample-t/
[root@9F-VM1 sample-t]# git clone [email protected]:/data/git/sample.git
正克隆到 'sample'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收對象中: 100% (3/3), done.
[root@9F-VM1 sample-t]# ls
sample
[root@9F-VM1 sample-t]# cd sample/
[root@9F-VM1 sample]# ls
1.txt
[root@9F-VM1 sample]# vim 2.txt
[root@9F-VM1 sample]# echo "222" > 2.txt
[root@9F-VM1 sample]# ls
1.txt  2.txt
[root@9F-VM1 sample]# git add 2.txt
[root@9F-VM1 sample]# git commit -m "add 2.txt"
[master 0f7b313] add 2.txt
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt
[root@9F-VM1 sample]# git push
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/data/git/sample.git
   e46c02a..0f7b313  master -> master

切換原來開始的倉庫目錄

[root@9F-VM1 sample]# cd -
/data/git/sample
[root@9F-VM1 sample]# ls
1.txt
[root@9F-VM1 sample]# git pull
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
來自 192.88.24.200:/data/git/sample
   e46c02a..0f7b313  master     -> origin/master
更新 e46c02a..0f7b313
Fast-forward
 2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt
[root@9F-VM1 sample]# ls
1.txt  2.txt

協同實現,遠程倉庫項目搭建完成



22.14 安裝gitlab(上)


安裝方法一:

 gitlab官網 https://about.gitlab.com/gitlab-com/

 官方安裝文檔 https://about.gitlab.com/installation/?version=ce#centos-7 (ce/ee)

 要求服務器內存不少於2g

方法二:本地yum源安裝

vim /etc/yum.repos.d/gitlab.repo //加入如下內容
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
yum install -y gitlab-ce
gitlab-ctl reconfigure


22.15 安裝gitlab(下)

執行命令自動加載配置

 gitlab-ctl reconfigure


如果系統自帶nginx,先把它停掉,因爲gitlab也自帶了一個nginx,以免發生衝突

/etc/init.d/nginx stop
chkconfig nginx off
netstat -lnpt  //查看監聽端口
gitlab-ctl 
#後面帶的執行命令(停止,開始,重啓,狀態)
gitlab-ctl stop/restart/start/status

訪問gitlab服務器ip:192.88.24.200

第一次登錄需要創建新的密碼

改完密碼後需要登錄

默認賬戶:root

密碼:新的密碼

blob.png


22.16 使用gitlab


gitlab常用命令  https://www.cnyunwei.cc/archives/1204 

1 使用gitlab自帶的nginx配置代理

路徑:

/var/opt/gitlab/nginx/conf/nginx.conf

#主配置文件

/var/opt/gitlab/nginx/conf/gitlab-http.conf

#gitlab相關的web配置文件,修改相關gitlab web可以在這裏修改


2 創建新的組group

blob.png

blob.png

3 創建新的項目project

blob.png

4 在gitlab添加SSH Key

User Settings-->SSH key-->把公鑰複製進去

blob.png

5 添加組

管理員區域 添加用戶,組,項目

blob.png


6 添加用戶

密碼獲取方法一:新的用戶密碼會使用鏈接方式將密碼發送通知給郵件地址來獲取

blob.png

密碼獲取方法二:選中用戶-->Edit下直接編輯定義密碼

blob.png


7 新用戶登錄

創建項目project


22.17 gitlab備份與恢復


1 gitlab備份  

gitlab-rake gitlab:backup:create
[root@DD-Server-9F ~]# gitlab-rake gitlab:backup:create
Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
done
Dumping repositories ...
 * g1/p1 ... [SKIPPED]
[SKIPPED] Wiki
 * kevin/kp01 ... [SKIPPED]
[SKIPPED] Wiki
done
Dumping uploads ...
done
Dumping builds ...
done
Dumping artifacts ...
done
Dumping pages ...
done
Dumping lfs objects ...
done
Dumping container registry images ...
[DISABLED]
Creating backup archive: 1536129054_2018_09_05_11.2.3_gitlab_backup.tar ... done
Uploading backup archive to remote storage  ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... skipping

備份目錄在/var/opt/gitlab/backups

[root@DD-Server-9F ~]# ls /var/opt/gitlab/backups/
1536129054_2018_09_05_11.2.3_gitlab_backup.tar

2 gitlab 恢復

gitlab 恢復  先停服務 gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq

[root@DD-Server-9F ~]# gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq
ok: down: unicorn: 1s, normally up
ok: down: sidekiq: 0s, normally up

停掉服務後,繼續恢復

gitlab-rake gitlab:backup:restore BACKUP=xxxxx (這裏是一個編號,即備份文件的前綴,時間戳_日期_版本號)

[root@DD-Server-9F ~]# gitlab-rake gitlab:backup:restore BACKUP=1536129054_2018_09_05_11.2.3


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章