2018-10-24
GitLab 查看版本
cat /opt/GitLab/embedded/service/GitLab-Rails/VERSION
查看 GitLab 服务状态命令
GitLab-ctl status
启动, 停止服务
- # 启动 GitLab 所有组件
- sudo GitLab-ctl start
- # 停止 GitLab 所有组件
- sudo GitLab-ctl stop
- # 重启 GitLab 所有组件
- sudo GitLab-ctl restart
备份与恢复
备份配置
配置文件再 / etc/GitLab/ 下面, 将所有的配置用 tar 备份即可
- [root@localhost init.d]# cd /etc/GitLab/
- [root@localhost GitLab]# ls
- GitLab.rb GitLab-secrets.JSON trusted-certs
备份数据
GitLab 的备份目录路径设置
- [root@code-server ~]# VIM /etc/GitLab/GitLab.rb
- gitlab_rails['manage_backup_path'] = true
- gitlab_rails['backup_path'] = "/data/gitlab/backups" //GitLab 备份目录
- gitlab_rails['backup_archive_permissions'] = 0644 // 生成的备份文件权限
- gitlab_rails['backup_keep_time'] = 7776000 // 备份保留天数为 3 个月 (即 90 天, 这里是 7776000 秒)
- [root@code-server ~]# mkdir -p /data/GitLab/backups
- [root@code-server ~]# chown -R Git.Git /data/GitLab/backups
- [root@code-server ~]# chmod -R 777 /data/GitLab/backups
如上设置了 GitLab 备份目录路径为 / data/GitLab/backups, 最后使用下面命令重载 GitLab 配置文件, 是上述修改生效!
root@code-server ~]# GitLab-ctl reconfigure
GitLab 备份操作 (使用备份命令 "gitlab-rake gitlab:backup:create")
手动备份 GitLab
- [root@code-server backups]# GitLab-rake GitLab:backup:create
- Dumping database ...
- Dumping PostgreSQL database gitlabhq_production ... [DONE]
- done
- Dumping repositories ...
- * treesign/treesign ... [DONE]
- * gateway/gateway ... [DONE]
- * treesign/treesign-doc ... [SKIPPED]
- * qwsign/qwsign ... [DONE]
- * qwsign/qwsign-doc ... [DONE]
- * test/test ... [DONE]
- 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: 1510471890_2017_11_12_9.4.5_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)
然后查看下备份文件 (文件权限是设定好的 644)
- [root@code-server backups]# ll
- total 244
- -rw-r--r-- 1 Git Git 245760 Nov 12 15:33 1510472027_2017_11_12_9.4.5_gitlab_backup.tar
编写备份脚本, 结合 crontab 实施自动定时备份, 比如每天 0 点, 6 点, 12 点, 18 点各备份一次
- [root@code-server backups]# pwd
- /data/GitLab/backups
- [root@code-server backups]# VIM gitlab_backup.sh
- #!/bin/bash
- /usr/bin/GitLab-rake GitLab:backup:create CRON=1
注意: 环境变量 CRON=1 的作用是如果没有任何错误发生时, 抑制备份脚本的所有进度输出
- [root@code-server backups]# crontab -l
- 0 0,6,12,18 * * */bin/bash -x /data/GitLab/backups/gitlab_backup.sh> /dev/null 2>&1
3)GitLab 恢复操作
GitLab 只能还原到与备份文件相同的 GitLab 版本.
假设在上面 GitLab 备份之前创建了 test 项目, 然后不小心误删了 test 项目, 现在就进行 GitLab 恢复操作:
1) 停止相关数据连接服务
- [root@code-server backups]# GitLab-ctl stop Unicorn
- ok: down: Unicorn: 0s, normally up
- [root@code-server backups]# GitLab-ctl stop sidekiq
- ok: down: sidekiq: 1s, normally up
- [root@code-server backups]# GitLab-ctl status
- run: gitaly: (pid 98087) 1883s; run: log: (pid 194202) 163003s
- run: GitLab-monitor: (pid 98101) 1883s; run: log: (pid 194363) 163002s
- run: GitLab-workhorse: (pid 98104) 1882s; run: log: (pid 194362) 163002s
- run: logrotate: (pid 98117) 1882s; run: log: (pid 5793) 160832s
- run: nginx: (pid 98123) 1881s; run: log: (pid 194359) 163002s
- run: node-exporter: (pid 98167) 1881s; run: log: (pid 194360) 163002s
- run: postgres-exporter: (pid 98173) 1881s; run: log: (pid 194204) 163003s
- run: PostgreSQL: (pid 98179) 1880s; run: log: (pid 194365) 163002s
- run: prometheus: (pid 98187) 1880s; run: log: (pid 194364) 163002s
- run: Redis: (pid 98230) 1879s; run: log: (pid 194358) 163002s
- run: Redis-exporter: (pid 98234) 1879s; run: log: (pid 194208) 163003s
- down: sidekiq: 8s, normally up; run: log: (pid 194437) 163001s
- down: Unicorn: 21s, normally up; run: log: (pid 194443) 163001s
2) 现在通过之前的备份文件进行恢复 (必须要备份文件放到备份路径下, 这里备份路径我自定义的 / data/GitLab/backups, 默认的是 / var/opt/GitLab/backups)
- [root@code-server backups]# pwd
- /data/GitLab/backups
- [root@code-server backups]# ll
- total 244
- -rw-r--r-- 1 Git Git 245760 Nov 12 15:33 1510472027_2017_11_12_9.4.5_gitlab_backup.tar
GitLab 的恢复操作会先将当前所有的数据清空, 然后再根据备份数据进行恢复
- [root@code-server backups]# GitLab-rake GitLab:backup:restore BACKUP=1510472027_2017_11_12_9.4.5
- Unpacking backup ... done
- Before restoring the database we recommend removing 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)?
- ........
- ALTER TABLE
- ALTER TABLE
- ALTER TABLE
- ALTER TABLE
- WARNING: no privileges were granted for "public"
- GRANT
- [DONE]
- done
- Restoring repositories ...
- * treesign/treesign ... [DONE]
- * gateway/gateway ... [DONE]
- * treesign/treesign-doc ... [DONE]
- * qwsign/qwsign ... [DONE]
- * qwsign/qwsign-doc ... [DONE]
- * test/test ... [DONE]
- Put GitLab hooks in repositories dirs [DONE]
- done
- Restoring uploads ...
- done
- Restoring builds ...
- done
- Restoring artifacts ...
- done
- Restoring pages ...
- done
- Restoring lfs objects ...
- done
- This will rebuild an authorized_keys file.
- You will lose any data stored in authorized_keys file.
- Do you want to continue (yes/no)? yes
- Deleting tmp directories ... done
- done
- done
- done
- done
- done
- done
- done
- [root@code-server backups]#
最后再次启动 GitLab
- [root@code-server backups]# GitLab-ctl start
- ok: run: gitaly: (pid 98087) 2138s
- ok: run: GitLab-monitor: (pid 98101) 2138s
- ok: run: GitLab-workhorse: (pid 98104) 2137s
- ok: run: logrotate: (pid 98117) 2137s
- ok: run: nginx: (pid 98123) 2136s
- ok: run: node-exporter: (pid 98167) 2136s
- ok: run: postgres-exporter: (pid 98173) 2136s
- ok: run: PostgreSQL: (pid 98179) 2135s
- ok: run: prometheus: (pid 98187) 2135s
- ok: run: Redis: (pid 98230) 2134s
- ok: run: Redis-exporter: (pid 98234) 2134s
- ok: run: sidekiq: (pid 104494) 0s
- ok: run: Unicorn: (pid 104497) 1s
- [root@code-server backups]# GitLab-ctl status
- run: gitaly: (pid 98087) 2142s; run: log: (pid 194202) 163262s
- run: GitLab-monitor: (pid 98101) 2142s; run: log: (pid 194363) 163261s
- run: GitLab-workhorse: (pid 98104) 2141s; run: log: (pid 194362) 163261s
- run: logrotate: (pid 98117) 2141s; run: log: (pid 5793) 161091s
- run: nginx: (pid 98123) 2140s; run: log: (pid 194359) 163261s
- run: node-exporter: (pid 98167) 2140s; run: log: (pid 194360) 163261s
- run: postgres-exporter: (pid 98173) 2140s; run: log: (pid 194204) 163262s
- run: PostgreSQL: (pid 98179) 2139s; run: log: (pid 194365) 163261s
- run: prometheus: (pid 98187) 2139s; run: log: (pid 194364) 163261s
- run: Redis: (pid 98230) 2138s; run: log: (pid 194358) 163261s
- run: Redis-exporter: (pid 98234) 2138s; run: log: (pid 194208) 163262s
- run: sidekiq: (pid 104494) 4s; run: log: (pid 194437) 163260s
- run: Unicorn: (pid 104497) 4s; run: log: (pid 194443) 163260s
恢复命令完成后, 可以 check 检查一下恢复情况
- [root@code-server backups]# GitLab-rake GitLab:check SANITIZE=true
- Checking GitLab Shell ...
- GitLab Shell version>= 5.3.1 ? ... OK (5.3.1)
- Repo base directory exists?
- default... yes
- Repo storage directories are symlinks?
- default... no
- Repo paths owned by Git:root, or Git:Git?
- default... yes
- Repo paths access is drwxrws---?
- default... yes
- hooks directories in repos are links: ...
- 5/1 ... ok
- 6/2 ... ok
- 5/3 ... repository is empty
- 12/4 ... ok
- 12/5 ... ok
- Running /opt/GitLab/embedded/service/GitLab-shell/bin/check
- Check GitLab API access: OK
- Access to /var/opt/GitLab/.SSH/authorized_keys: OK
- Send ping to Redis server: OK
- GitLab-shell self-check successful
- Checking GitLab Shell ... Finished
- Checking Sidekiq ...
- Running? ... yes
- Number of Sidekiq processes ... 1
- Checking Sidekiq ... Finished
- Checking Reply by email ...
- Reply by email is disabled in config/GitLab.YAML
- Checking Reply by email ... Finished
- Checking LDAP ...
- LDAP is disabled in config/GitLab.YAML
- Checking LDAP ... Finished
- Checking GitLab ...
- Git configured correctly? ... yes
- Database config exists? ... yes
- All migrations up? ... yes
- Database contains orphaned GroupMembers? ... no
- GitLab config exists? ... yes
- GitLab config up to date? ... yes
- Log directory writable? ... yes
- Tmp directory writable? ... yes
- Uploads directory exists? ... yes
- Uploads directory has correct permissions? ... yes
- Uploads directory tmp has correct permissions? ... yes
- Init script exists? ... skipped (omnibus-GitLab has no init script)
- Init script up-to-date? ... skipped (omnibus-GitLab has no init script)
- Projects have namespace: ...
- 5/1 ... yes
- 6/2 ... yes
- 5/3 ... yes
- 12/4 ... yes
- 12/5 ... yes
- Redis version>= 2.8.0? ... yes
- Ruby version>= 2.3.3 ? ... yes (2.3.3)
- Git version>= 2.7.3 ? ... yes (2.13.4)
- Active users: ... 11
- Checking GitLab ... Finished
然后稍等一会 (如果启动 GitLab 后, 访问出现 500, 这是因为 Redis 等程序还没完全启动, 等一会儿访问就 ok 了), 再次登录 GitLab, 就会发现之前误删除的 test 项目已经恢复了!
另外: GitLab 迁移与恢复一样, 但是要求两个 GitLab 版本号一致
GitLab 只能还原到与备份文件相同的 GitLab 版本.
假设在上面 GitLab 备份之前创建了
test
项目, 然后不小心误删了
test
项目, 现在就进行 GitLab 恢复操作:
1) 停止相关数据连接服务
- [root@code-server backups]
- # GitLab-ctl stop Unicorn
- ok: down: Unicorn: 0s, normally up
- [root@code-server backups]
- # GitLab-ctl stop sidekiq
- ok: down: sidekiq: 1s, normally up
- [root@code-server backups]
- # GitLab-ctl status
- run: gitaly: (pid 98087) 1883s; run: log: (pid 194202) 163003s
- run: GitLab-monitor: (pid 98101) 1883s; run: log: (pid 194363) 163002s
- run: GitLab-workhorse: (pid 98104) 1882s; run: log: (pid 194362) 163002s
- run: logrotate: (pid 98117) 1882s; run: log: (pid 5793) 160832s
- run: nginx: (pid 98123) 1881s; run: log: (pid 194359) 163002s
- run: node-exporter: (pid 98167) 1881s; run: log: (pid 194360) 163002s
- run: postgres-exporter: (pid 98173) 1881s; run: log: (pid 194204) 163003s
- run: PostgreSQL: (pid 98179) 1880s; run: log: (pid 194365) 163002s
- run: prometheus: (pid 98187) 1880s; run: log: (pid 194364) 163002s
- run: Redis: (pid 98230) 1879s; run: log: (pid 194358) 163002s
- run: Redis-exporter: (pid 98234) 1879s; run: log: (pid 194208) 163003s
- down: sidekiq: 8s, normally up; run: log: (pid 194437) 163001s
- down: Unicorn: 21s, normally up; run: log: (pid 194443) 163001s
2) 现在通过之前的备份文件进行恢复 (必须要备份文件放到备份路径下, 这里备份路径我自定义的
/data/GitLab/backups
, 默认的是
- /var/opt/GitLab/backups
- )
- [root@code-server backups]
- # pwd
- /data/GitLab/backups
- [root@code-server backups]
- # ll
- total 244
- -rw-r--r-- 1 Git Git 245760 Nov 12 15:33 1510472027_2017_11_12_9.4.5_gitlab_backup.
- tar
GitLab 的恢复操作会先将当前所有的数据清空, 然后再根据备份数据进行恢复
- [root@code-server backups]
- # GitLab-rake GitLab:backup:restore BACKUP=1510472027_2017_11_12_9.4.5
- Unpacking backup ...
- done
- Before restoring the database we recommend removing 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)?
- ........
- ALTER TABLE
- ALTER TABLE
- ALTER TABLE
- ALTER TABLE
- WARNING: no privileges were granted
- for "public"
- GRANT
- [DONE]
- done
- Restoring repositories ...
- * treesign/treesign ... [DONE]
- * gateway/gateway ... [DONE]
- * treesign/treesign-doc ... [DONE]
- * qwsign/qwsign ... [DONE]
- * qwsign/qwsign-doc ... [DONE]
- * test/test ... [DONE]
- Put GitLab hooks in repositories dirs [DONE]
- done
- Restoring uploads ...
- done
- Restoring builds ...
- done
- Restoring artifacts ...
- done
Restoring pages ...
- done
- Restoring lfs objects ...
- done
- This will rebuild an authorized_keys
file.
You will lose any data stored
in authorized_keys file.
- Do you want to continue (yes/no)? yes
- Deleting tmp directories ...
- done
- done
- done
- done
- done
- done
- done
- done
- [root@code-server backups]
- #
最后再次启动 GitLab
- [root@code-server backups]
- # GitLab-ctl start
- ok: run: gitaly: (pid 98087) 2138s
- ok: run: GitLab-monitor: (pid 98101) 2138s
- ok: run: GitLab-workhorse: (pid 98104) 2137s
- ok: run: logrotate: (pid 98117) 2137s
- ok: run: nginx: (pid 98123) 2136s
- ok: run: node-exporter: (pid 98167) 2136s
- ok: run: postgres-exporter: (pid 98173) 2136s
- ok: run: PostgreSQL: (pid 98179) 2135s
- ok: run: prometheus: (pid 98187) 2135s
- ok: run: Redis: (pid 98230) 2134s
- ok: run: Redis-exporter: (pid 98234) 2134s
- ok: run: sidekiq: (pid 104494) 0s
- ok: run: Unicorn: (pid 104497) 1s
- [root@code-server backups]
- # GitLab-ctl status
- run: gitaly: (pid 98087) 2142s; run: log: (pid 194202) 163262s
- run: GitLab-monitor: (pid 98101) 2142s; run: log: (pid 194363) 163261s
- run: GitLab-workhorse: (pid 98104) 2141s; run: log: (pid 194362) 163261s
- run: logrotate: (pid 98117) 2141s; run: log: (pid 5793) 161091s
- run: nginx: (pid 98123) 2140s; run: log: (pid 194359) 163261s
- run: node-exporter: (pid 98167) 2140s; run: log: (pid 194360) 163261s
- run: postgres-exporter: (pid 98173) 2140s; run: log: (pid 194204) 163262s
- run: PostgreSQL: (pid 98179) 2139s; run: log: (pid 194365) 163261s
- run: prometheus: (pid 98187) 2139s; run: log: (pid 194364) 163261s
- run: Redis: (pid 98230) 2138s; run: log: (pid 194358) 163261s
- run: Redis-exporter: (pid 98234) 2138s; run: log: (pid 194208) 163262s
- run: sidekiq: (pid 104494) 4s; run: log: (pid 194437) 163260s
- run: Unicorn: (pid 104497) 4s; run: log: (pid 194443) 163260s
恢复命令完成后, 可以 check 检查一下恢复情况
- [root@code-server backups]
- # GitLab-rake GitLab:check SANITIZE=true
- Checking GitLab Shell ...
- GitLab Shell version>= 5.3.1 ? ... OK (5.3.1)
- Repo base directory exists?
- default... yes
- Repo storage directories are symlinks?
- default... no
- Repo paths owned by Git:root, or Git:Git?
- default... yes
- Repo paths access is drwxrws---?
- default... yes
- hooks directories in
- repos are links: ...
- 5
- /1 ... ok
- 6
- /2 ... ok
- 5
- /3
- ... repository is empty
- 12
- /4 ... ok
- 12
- /5 ... ok
- Running
- /opt/GitLab/embedded/service/GitLab-shell/bin/check
- Check GitLab API access: OK
- Access to /var/opt/GitLab/.SSH/authorized_keys: OK
- Send ping to Redis server: OK
- GitLab-shell self-check successful
- Checking GitLab Shell ... Finished
- Checking Sidekiq ...
- Running? ... yes
- Number of Sidekiq processes ... 1
- Checking Sidekiq ... Finished
- Checking Reply by email ...
- Reply by email is disabled
- in config/GitLab.YAML
- Checking Reply by email ... Finished
Checking LDAP ...
- LDAP is disabled in config/GitLab.YAML
- Checking LDAP ... Finished
Checking GitLab ...
- Git configured correctly? ...
- yes
- Database config exists? ...
- yes
- All migrations up? ...
- yes
- Database contains orphaned GroupMembers? ... no
- GitLab config exists? ...
- yes
- GitLab config up to
- date? ... yes
- Log directory writable? ...
- yes
- Tmp directory writable? ...
- yes
- Uploads directory exists? ...
- yes
- Uploads directory has correct permissions? ...
- yes
- Uploads directory tmp has correct permissions? ...
- yes
- Init script exists? ... skipped (omnibus-GitLab has no init script)
- Init script up-to-date
- ? ... skipped (omnibus-GitLab has no init script)
- Projects have namespace: ...
- 5
- /1 ... yes
- 6
- /2 ... yes
- 5
- /3 ... yes
- 12
- /4 ... yes
- 12
- /5 ... yes
- Redis version>= 2.8.0? ...
- yes
- Ruby version>= 2.3.3 ? ...
- yes (2.3.3)
- Git version>= 2.7.3 ? ...
- yes (2.13.4)
- Active users: ... 11
- Checking GitLab ... Finished
然后稍等一会 (如果启动 GitLab 后, 访问出现 500, 这是因为 Redis 等程序还没完全启动, 等一会儿访问就 ok 了), 再次登录 GitLab, 就会发现之前误删除的
test
项目已经恢复了!
另外: GitLab 迁移与恢复一样, 但是要求两个 GitLab 版本号一致
来源: http://www.bubuko.com/infodetail-2819527.html