14. 克隆gitlab仓库到本地客户端
192.168.211.91
在这台服务器上安装git
使用yum默认安装的git版本是1.8,版本较低,我们可以使用源码安装
首先需要安装依赖库:
# yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMake
下载最新的源码包
# cd /usr/local/src/ # wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.19.2.tar.xz # tar xf git-2.19.2.tar.xz # cd git-2.19.2 # make prefix=/usr/local/git all # make prefix=/usr/local/git install # rm -rf /usr/bin/git # ln -s /usr/local/git/bin/git /usr/bin/git # git --version git version 2.19.2 生成ssh key [root@localhost git-2.19.2]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:deiQnPXeqNII+GsnVedBO0255UWtGto4PEbeEYSBTPo root@localhost.localdomain The key's randomart image is: +---[RSA 2048]----+ | o..o+. oo| | oo+.oo o +| | . = o.o= =.| | . . +ooBo+ .| | . . E=.Bo*. | | . ..oO.= | | ..o.oo | | o... | | ..o | +----[SHA256]-----+ [root@localhost git-2.19.2]# ll /root/.ssh/ 总用量 8 -rw------- 1 root root 1675 5月 2 20:44 id_rsa -rw-r--r-- 1 root root 408 5月 2 20:44 id_rsa.pub [root@localhost git-2.19.2]# cat /root/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwUIYoYKPxp23ZH+Q4DmQJtuxv9AWmZUKemTVJ0cBDB03K4AhF64orfODrdGmEr1G0IllPuhMJ7HtbRR7EcsCE8MunxikRyJhb8iOYzTtTkGDy9Q0aZ/BUKx7wAmIj+u NHe+X8YUYtpMfMngtLl5XL0yHRvMoPVaUPT9FlejfRtrj3Qh8+vKiN4q9c36tC8eoyEnKE656yboTNkYE43Djp6DyynPmNcuB4dOVNz+OA+uR7OIidT6fxw16bxkCXUQeQ/Y6doMCOjBuZEUsD0VaSQ5J16ewd47cBbeEv/fVUIoZFXv5VrtowYWU6WlzMu2AQRiQs+ZeYik0L2UqcOXmP root@localhost.localdomain
然后配置192.168.211.91客户端与gitlab上的nqt用户绑定
使用git clone命令克隆仓库到192.168.211.91服务器本地
[root@localhost data]# mkdir /101 [root@localhost data]# cd /101 [root@localhost 101]# git clone git@192.168.137.100:xmlgrg_test/xm.git 正克隆到 'xm'... The authenticity of host '192.168.137.100 (192.168.137.100)' can't be established. ECDSA key fingerprint is SHA256:zUU81Kb1Q5uS1ewufeZ1qLyErPI0uOyIhEmOabssXoE. ECDSA key fingerprint is MD5:1a:bc:a7:24:e7:f6:ce:4e:d3:df:56:ac:4a:e2:a5:3c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.137.100' (ECDSA) to the list of known hosts. remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) 接收对象中: 100% (3/3), 完成. [root@localhost 101]# ll -a 总用量 0 drwxr-xr-x 3 root root 16 5月 2 20:49 . dr-xr-xr-x. 19 root root 247 5月 2 20:49 .. drwxr-xr-x 3 root root 45 5月 2 20:49 xm [root@localhost 101]# cd xm [root@localhost xm]# ll -a 总用量 0 drwxr-xr-x 3 root root 45 5月 2 20:49 . drwxr-xr-x 3 root root 16 5月 2 20:49 .. -rw-r--r-- 1 root root 0 5月 2 20:49 a -rw-r--r-- 1 root root 0 5月 2 20:49 b -rw-r--r-- 1 root root 0 5月 2 20:49 c drwxr-xr-x 8 root root 163 5月 2 20:49 .git [root@localhost xm]# [root@localhost xm]# git remote origin
可以看到已经将gitlab上的xm仓库克隆到192.168.137.101本地,同时为本地仓库添加了一个指向gitlab上xm仓库的远程仓库
在192.168.137.101的xm上创建一个dev的分支,并将dev分支,推送到gitlab上
[root@localhost 101]# cd xm [root@localhost xm]# pwd /101/xm [root@localhost xm]# pwd /101/xm [root@localhost xm]# git checkout -b dev 切换到一个新分支 'dev' [root@localhost xm]# git status 位于分支 dev 无文件要提交,干净的工作区 [root@localhost xm]# git push -u origin dev 总共 0 (差异 0),复用 0 (差异 0) remote: remote: To create a merge request for dev, visit: remote: http://192.168.137.100/xmlgrg_test/xm/merge_requests/new?merge_request%5Bsource_branch%5D=dev remote: To 192.168.137.100:xmlgrg_test/xm.git * [new branch] dev -> dev
分支 ‘dev’ 设置为跟踪来自 ‘origin’ 的远程分支 ‘dev’。
完成后,在gitlab上可以看到推上来的dev分支
15. 设置分支保护
在实际使用过程中,通常会保持master分支的稳定,用于生产环境的版本发布,只有授权的用户才可以向master合并代码。要实现此功能,需要将master设置为保护分支,同时授权什么用户可以向master用户推送代码。
使用root用户登录gitlab,点击xm仓库页面左下角的setting
进入设置页面,选择设置菜单栏下面的Repository选项
找到Protected Branches、点击展开
展开Protected Branches
设置完dev为保护的后,在仓库的分支页面,可看到分支后出现一个绿色的protected
默认的情况下,master分支是不允许developer角色向其推送内容的
16. Gitlab的备份、恢复、升级
对gitlab进行备份将会创建一个包含所有库和附件的归档文件,对备份的恢复只能恢复到与备份时的gitlab相同的版本。将gitlab迁移到另一台服务器上的最佳方法就是通过备份和还原。Gitlab提供了一个简单的命令行来备份整个gitlab,并且能灵活的满足需求。
备份配置
备份文件将保存在配置文件中定义的backup_path中,文件名为TIMESTAMP_GITLAB_BACKUP.TAR,TIMESTAMP为备份的时间戳。TIMESTAMP的格式为:EPOCH_YYYY_MM_DD_Gitlab-version。
默认的备份文件目录为:/var/opt/gitlab/backups,如果自定义备份目录需要赋予目录git权限,具体操作如下:
配置文件中加入:
# vim /etc/gitlab/gitlab.rb ### Backup Settings ###! Docs: https://docs.gitlab.com/omnibus/settings/backups.html #gitlab_rails['manage_backup_path'] = true gitlab_rails['backup_path'] = "/data/gitlab/backups" #备份的目录 ##!Docs:https://docs.gitlab.com/ce/raketasks/backup_restore.html#backup-archive-permissions # gitlab_rails['backup_archive_permissions'] = 0644 # gitlab_rails['backup_pg_schema'] = 'public' ###! The duration in seconds to keep backups before they are allowed to be deleted gitlab_rails['backup_keep_time'] = 604800 #备份保留的时间(以秒为单位,这个是7天默认值) 在命令行执行命令如下 # mkdir -pv /data/gitlab/backups # chown -R git.git /data/gitlab/backups/ # gitlab-ctl reconfigure # gitlab-ctl restart
16.1 手动备份
在命令行执行:
# gitlab-rake gitlab:backup:create 2019-05-18 21:26:25 +0800 -- Dumping database ... Dumping PostgreSQL database gitlabhq_production ... [DONE] 2019-05-18 21:26:25 +0800 -- done 2019-05-18 21:26:25 +0800 -- Dumping repositories ... * xmlgrg_test/xm ... [DONE] [SKIPPED] Wiki 2019-05-18 21:26:26 +0800 -- done 2019-05-18 21:26:26 +0800 -- Dumping uploads ... 2019-05-18 21:26:26 +0800 -- done 2019-05-18 21:26:26 +0800 -- Dumping builds ... 2019-05-18 21:26:26 +0800 -- done 2019-05-18 21:26:26 +0800 -- Dumping artifacts ... 2019-05-18 21:26:26 +0800 -- done 2019-05-18 21:26:26 +0800 -- Dumping pages ... 2019-05-18 21:26:26 +0800 -- done 2019-05-18 21:26:26 +0800 -- Dumping lfs objects ... 2019-05-18 21:26:26 +0800 -- done 2019-05-18 21:26:26 +0800 -- Dumping container registry images ... 2019-05-18 21:26:26 +0800 -- [DISABLED] Creating backup archive: 1558185986_2019_05_18_11.9.1_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 ... done. (0 removed) # ll /data/gitlab/backups/ 在制定的目录下生成对应的备份文件 总用量 112 -rw------- 1 git git 112640 5月 18 21:26 1558185986_2019_05_18_11.9.1_gitlab_backup.tar
16.2 定时备份
通过在定时任务里添加:
每天凌晨2点进行一次自动备份:通过crontab使用备份命令实现,需重启cron服务
#输入命令crontab -e
sudo crontab -e #输入相应的任务 环境变量CRON=1的作用是如果没有任何错误发生时,抑制备份脚本的所有进度输出 0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1 # systemctl restart crond 重启服务
在crontab文件里面,每一行代表一项任务,每行的每个字段代表一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,每个字段之间用空格分割,没用的段用*代替,格式如下:
m h dom mon dow user command
其中:
m: 表示分钟,可以是从0到59之间的任何整数。
h:表示小时,可以是从0到23之间的任何整数。
dom:表示日期,可以是从1到31之间的任何整数。
mon:表示月份,可以是从1到12之间的任何整数。
dow:表示星期几,可以是从0到7之间的任何整数,这里的0或7代表星期日。
user : 表示执行的用户。
command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件(如shell文件)。
16.3 恢复实践
Gitlab的恢复只能还原到与备份文件相同的gitlab版本的系统中,恢复时,停止连接到数据库的进程(也就是停止数据的写入服务),但是保持gitlab是运行的。
# gitlab-ctl stop unicorn ok: down: unicorn: 1s, normally up # gitlab-ctl stop sideki # gitlab-ctl status run: alertmanager: (pid 11282) 1356s; run: log: (pid 1872) 5129s run: gitaly: (pid 11298) 1355s; run: log: (pid 1859) 5129s run: gitlab-monitor: (pid 11338) 1355s; run: log: (pid 1865) 5129s run: gitlab-workhorse: (pid 11376) 1354s; run: log: (pid 1861) 5129s run: logrotate: (pid 11387) 1354s; run: log: (pid 1860) 5129s run: nginx: (pid 11397) 1353s; run: log: (pid 1852) 5129s run: node-exporter: (pid 11407) 1353s; run: log: (pid 1862) 5129s run: postgres-exporter: (pid 11508) 1352s; run: log: (pid 1869) 5129s run: postgresql: (pid 11524) 1351s; run: log: (pid 1847) 5129s run: prometheus: (pid 11540) 1350s; run: log: (pid 1877) 5129s run: redis: (pid 11559) 1349s; run: log: (pid 1875) 5129s run: redis-exporter: (pid 11598) 1349s; run: log: (pid 1870) 5129s run: sidekiq: (pid 11609) 1346s; run: log: (pid 1878) 5129s down: unicorn: 29s, normally up; run: log: (pid 1876) 5129s #
执行恢复操作
# gitlab-rake gitlab:backup:restore
整个恢复执行过程中,可以看到基本是在删除表,创建表
2处需要输入yes Before restoring the database, we will remove all existing tables to avoid future upgrade problems. Be aware that if you have custom tables in the GitLab database these tables and all data will be removed. Do you want to continue (yes/no)? yes This task will now rebuild the authorized_keys file. You will lose any data stored in the authorized_keys file. Do you want to continue (yes/no)? yes








