gitlab备份

GitLab备份

对gitlab进行备份将会创建一个包含所有库和附件的归档文件。对备份的恢复只能恢复到与备份时的gitlab相同的版本。将gitlab迁移到另一台服务器上的最佳方法就是通过备份和还原。
gitlab提供了一个简单的命令行来备份整个gitlab,并且能灵活的满足需求。
备份文件将保存在配置文件中定义的backup_path中,文件名为TIMESTAMP_gitlab_backup.tar,TIMESTAMP为备份时的时间戳。TIMESTAMP的格式为:EPOCH_YYYY_MM_DD_Gitlab-version。
如果自定义备份目录需要赋予git权限
配置文件中加入/etc/gitlab/gitlab.rb
gitlab_rails[‘backup_path’] = ‘/data/backup/gitlab’
gitlab_rails[‘backup_keep_time’] = 604800 备份保留的时间(以秒为单位,这个是七天默认值),
mkdir /data/backup/gitlab
chown -R git.git /data/backup/gitlab
完成后执行gitlab-ctl reconfigure

1、手动备份

执行:gitlab-rake gitlab:backup:create生成一次备份。
[root@node2 ~]# gitlab-rake gitlab:backup:create
Dumping database ... 
Dumping PostgreSQL database gitlabhq_production ... [DONE]
done
Dumping repositories ...
 * web-site/frontend ... [DONE]
 * web-site/frontend.wiki ...  [SKIPPED]
 * web-site/backend ... [SKIPPED]
 * web-site/backend.wiki ...  [SKIPPED]
 * devops/accout ... [DONE]
 * devops/accout.wiki ...  [SKIPPED]
 * devops/user ... [DONE]
 * devops/user.wiki ...  [SKIPPED]
 * web-site/accout ... [DONE]
 * web-site/accout.wiki ...  [SKIPPED]
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: 1512811475_2017_12_09_10.2.2_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
[root@node2 ~]# ll /var/opt/gitlab/backups/
total 272
-rw------- 1 git git 276480 Dec  9 17:24 1512811475_2017_12_09_10.2.2_gitlab_backup.tar

2、定时备份

在定时任务里添加:
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1

环境变量CRON=1的作用是如果没有任何错误发生时, 抑制备份脚本的所有进度输出。

3、恢复

只能还原到与备份文件相同的gitlab版本。
执行恢复操作时,需要gitlab处于运行状态,备份文件位于gitlab_rails[‘backup_path’]。

[root@node2 ~]# ll /var/opt/gitlab/backups/
total 272
-rw------- 1 git git 276480 Dec  9 17:24 1512811475_2017_12_09_10.2.2_gitlab_backup.tar
停止连接到数据库的进程(也就是停止数据写入服务),但是保持GitLab是运行的。
[root@node2 ~]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@node2 ~]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
确认:
[root@node2 ~]# gitlab-ctl status
run: gitaly: (pid 1497) 0s; run: log: (pid 540) 0s
run: gitlab-monitor: (pid 1507) 0s; run: log: (pid 543) 0s
run: gitlab-workhorse: (pid 1517) 0s; run: log: (pid 508) 0s
run: logrotate: (pid 14405) 1564s; run: log: (pid 510) 0s
run: nginx: (pid 1532) 0s; run: log: (pid 507) 0s
run: node-exporter: (pid 1538) 0s; run: log: (pid 525) 0s
run: postgres-exporter: (pid 1543) 0s; run: log: (pid 530) 0s
run: postgresql: (pid 1551) 0s; run: log: (pid 492) 0s
run: prometheus: (pid 1559) 0s; run: log: (pid 535) 0s
run: redis: (pid 1567) 0s; run: log: (pid 491) 0s
run: redis-exporter: (pid 1572) 0s; run: log: (pid 547) 0s
down: sidekiq: 121s, normally up; run: log: (pid 500) 0s
down: unicorn: 133s, normally up; run: log: (pid 502) 0s
[root@node2 ~]# 
接下我们进行恢复,指定时间戳你要从那个备份恢复:
[root@node2 ~]# gitlab-rake gitlab:backup:restore BACKUP=1512811475_2017_12_09_10.2.2
Unpacking backup ... done
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
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)? 
将移除所有的认证Key。回答yes
....
Deleting tmp directories ... done
done
done
done
done
done
done
done
完成后重启GitLab服务
[root@node2 ~]# gitlab-ctl restart
ok: run: gitaly: (pid 18194) 0s
ok: run: gitlab-monitor: (pid 18204) 0s
ok: run: gitlab-workhorse: (pid 18209) 1s
ok: run: logrotate: (pid 18224) 0s
ok: run: nginx: (pid 18231) 1s
ok: run: node-exporter: (pid 18237) 0s
ok: run: postgres-exporter: (pid 18242) 0s
ok: run: postgresql: (pid 18314) 0s
ok: run: prometheus: (pid 18317) 1s
ok: run: redis: (pid 18326) 0s
ok: run: redis-exporter: (pid 18330) 0s
ok: run: sidekiq: (pid 18345) 0s
ok: run: unicorn: (pid 18354) 0s

检查GitLab的服务
[root@node2 ~]# gitlab-rake gitlab:check SANITIZE=true
Checking GitLab Shell ...

GitLab Shell version >= 5.9.4 ? ... OK (5.9.4)
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: ... 
2/3 ... ok
2/4 ... repository is empty
7/5 ... ok
7/7 ... ok
2/8 ... ok
Running /opt/gitlab/embedded/service/gitlab-shell/bin/check
Check GitLab API access: OK
Redis available via internal API: OK

Access to /var/opt/gitlab/.ssh/authorized_keys: OK
gitlab-shell self-check successful

Checking GitLab Shell ... Finished

Checking Sidekiq ...

Running? ... yes
Number of Sidekiq processes ... 1

Checking Sidekiq ... Finished

Reply by email is disabled in config/gitlab.yml
Checking LDAP ...

LDAP is disabled in config/gitlab.yml

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? ... skipped (no tmp uploads folder yet)
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: ... 
2/3 ... yes
2/4 ... yes
7/5 ... yes
7/7 ... yes
2/8 ... yes
Redis version >= 2.8.0? ... yes
Ruby version >= 2.3.5 ? ... yes (2.3.5)
Git version >= 2.7.3 ? ... yes (2.13.6)
Git user has default SSH configuration? ... yes
Active users: ... 5

Checking GitLab ... Finished

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐