GITLAB使用了一段时间之后,你会想到需要备份与恢复。

GITLAB的备份与恢复所仰仗的命令是【gitlab-rake】。
关于该命令,它的信息如下:

[root@ansible-server ~]# whereis gitlab-rake
gitlab-rake: /usr/bin/gitlab-rake /opt/gitlab/bin/gitlab-rake
[root@ansible-server ~]# 
[root@ansible-server ~]# gitlab-rake -V
rake, version 12.3.1
[root@ansible-server ~]# 
[root@ansible-server ~]# gitlab-rake -h
rake [-f rakefile] {options} targets...

Options are ...
        --backtrace=[OUT]            Enable full backtrace.  OUT can be stderr (default) or stdout.
        --comments                   Show commented tasks only
        --job-stats [LEVEL]          Display job statistics. LEVEL=history displays a complete job list
        --rules                      Trace the rules resolution.
        --suppress-backtrace PATTERN Suppress backtrace lines matching regexp PATTERN. Ignored if --trace is on.
    -A, --all                        Show all tasks, even uncommented ones (in combination with -T or -D)
    -B, --build-all                  Build all prerequisites, including those which are up-to-date.
    -D, --describe [PATTERN]         Describe the tasks (matching optional PATTERN), then exit.
    -e, --execute CODE               Execute some Ruby code and exit.
    -E, --execute-continue CODE      Execute some Ruby code, then continue with normal task processing.
    -f, --rakefile [FILENAME]        Use FILENAME as the rakefile to search for.
    -G, --no-system, --nosystem      Use standard project Rakefile search paths, ignore system wide rakefiles.
    -g, --system                     Using system wide (global) rakefiles (usually '~/.rake/*.rake').
    -I, --libdir LIBDIR              Include LIBDIR in the search path for required modules.
    -j, --jobs [NUMBER]              Specifies the maximum number of tasks to execute in parallel. (default is number of CPU cores + 4)
    -m, --multitask                  Treat all tasks as multitasks.
    -n, --dry-run                    Do a dry run without executing actions.
    -N, --no-search, --nosearch      Do not search parent directories for the Rakefile.
    -P, --prereqs                    Display the tasks and dependencies, then exit.
    -p, --execute-print CODE         Execute some Ruby code, print the result, then exit.
    -q, --quiet                      Do not log messages to standard output.
    -r, --require MODULE             Require MODULE before executing rakefile.
    -R, --rakelibdir RAKELIBDIR,     Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')
        --rakelib
    -s, --silent                     Like --quiet, but also suppresses the 'in directory' announcement.
    -t, --trace=[OUT]                Turn on invoke/execute tracing, enable full backtrace. OUT can be stderr (default) or stdout.
    -T, --tasks [PATTERN]            Display the tasks (matching optional PATTERN) with descriptions, then exit. -AT combination displays all of tasks contained no description.
    -v, --verbose                    Log message to standard output.
    -V, --version                    Display the program version.
    -W, --where [PATTERN]            Describe the tasks (matching optional PATTERN), then exit.
    -X, --no-deprecation-warnings    Disable the deprecation warnings.
    -h, -H, --help                   Display this help message.
[root@ansible-server ~]# 

可以看到,我当前的rake的版本是:【12.3.1】

英文中,rake的意思是:【靶子】

rake
英 [reɪk]  美 [rek] 
n.耙子; 放荡的男人,舞台等的倾斜度,;
vt.& vi.搜索; 以耙子耙平(泥土等); 用耙子把…耙在一起; (翻阅旧记录、文件等以)搜寻事实等
变形
复数: rakes 过去式: raked 过去分词: raked 现在分词: raking 第三人称单数: rakes

一、备份(omnibus)
command:
gitlab-rake gitlab:backup:create

[root@ansible-server gitlab_data]# pwd
/backup/gitlab_data
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# ls -ltr
total 0
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# ls -ltr /var/opt/gitlab/backups/
total 0
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# gitlab-rake gitlab:backup:create
Dumping database ... 
Dumping PostgreSQL database gitlabhq_production ... [DONE]
done
Dumping repositories ...
 * root/ansible_maintain ... [DONE]
[SKIPPED] Wiki
 * root/linux_maintain ... [DONE]
[SKIPPED] Wiki
 * root/database_oracle_maintain ... [DONE]
[SKIPPED] Wiki
 * root/database_mysql_maintain ... [DONE]
[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: 1547025122_2019_01_09_11.6.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@ansible-server gitlab_data]# 

如上,就备份完成了。

看看备份文件:

[root@ansible-server gitlab_data]# ls -ltr
total 0
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# ls -ltr /var/opt/gitlab/backups/
total 190172
-rw------- 1 git git 194734080 Jan  9 17:12 1547025122_2019_01_09_11.6.2_gitlab_backup.tar
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# du -sh /var/opt/gitlab/backups/*
186M    /var/opt/gitlab/backups/1547025122_2019_01_09_11.6.2_gitlab_backup.tar
[root@ansible-server gitlab_data]# 

默认,备份文件会存到【/var/opt/gitlab/backups】

可以在GITLAB的配置中看到默认的备份文件的设置:

[root@ansible-server gitlab_data]# cat /etc/gitlab/gitlab.rb | grep "gitlab_rails" | grep backup
# gitlab_rails['manage_backup_path'] = true
# gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
# gitlab_rails['backup_archive_permissions'] = 0644
# gitlab_rails['backup_pg_schema'] = 'public'
# gitlab_rails['backup_keep_time'] = 604800
# gitlab_rails['backup_upload_connection'] = {
# gitlab_rails['backup_upload_remote_directory'] = 'my.s3.bucket'
# gitlab_rails['backup_multipart_chunk_size'] = 104857600
# gitlab_rails['backup_encryption'] = 'AES256'
# gitlab_rails['backup_storage_class'] = 'STANDARD'
[root@ansible-server gitlab_data]# 

如上修改其中【gitlab_rails[‘backup_path’]】,就可以修改默认的备份位置。
注意:
对gitlab.rb修改后,需要reconfigure,才会真正生效:

gitlab-ctl reconfigure

如果需要放入计划任务(CRONTAB),需要加参数【CRON=1】

0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1

其中,【CRON=1】的目的是:
如果备份的过程中没有错误发生,则减少备份脚本运行时的进度输出(避免过多的输出)

二、恢复:

在恢复前,先看看我们GITHUB当前的状态。(为了演示,我会创建一个新的REPO,在后面恢复后,按照预期,这个新建的REPO会消失)
当前状态:

创建测试REPO:

可以开始准备恢复了。

首先,停掉GITLAB中数据连接相关的服务:

[root@ansible-server gitlab_data]# gitlab-ctl status
run: alertmanager: (pid 2375) 429850s; run: log: (pid 8140) 441706s
run: gitaly: (pid 2391) 429850s; run: log: (pid 7902) 441712s
run: gitlab-monitor: (pid 2409) 429849s; run: log: (pid 7959) 441710s
run: gitlab-workhorse: (pid 2438) 429849s; run: log: (pid 7843) 441714s
run: logrotate: (pid 4523) 1426s; run: log: (pid 7868) 441713s
run: nginx: (pid 2501) 429848s; run: log: (pid 7852) 441713s
run: node-exporter: (pid 2522) 429848s; run: log: (pid 7941) 441711s
run: postgres-exporter: (pid 2530) 429847s; run: log: (pid 8163) 441705s
run: postgresql: (pid 2540) 429847s; run: log: (pid 7798) 441715s
run: prometheus: (pid 2637) 429846s; run: log: (pid 8109) 441707s
run: redis: (pid 2680) 429846s; run: log: (pid 7790) 441716s
run: redis-exporter: (pid 2686) 429846s; run: log: (pid 7988) 441709s
run: sidekiq: (pid 3465) 1726s; run: log: (pid 7814) 441715s
run: unicorn: (pid 2714) 429842s; run: log: (pid 7805) 441715s
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# gitlab-ctl status
run: alertmanager: (pid 2375) 429885s; run: log: (pid 8140) 441741s
run: gitaly: (pid 2391) 429885s; run: log: (pid 7902) 441747s
run: gitlab-monitor: (pid 2409) 429884s; run: log: (pid 7959) 441745s
run: gitlab-workhorse: (pid 2438) 429884s; run: log: (pid 7843) 441749s
run: logrotate: (pid 4523) 1461s; run: log: (pid 7868) 441748s
run: nginx: (pid 2501) 429883s; run: log: (pid 7852) 441748s
run: node-exporter: (pid 2522) 429883s; run: log: (pid 7941) 441746s
run: postgres-exporter: (pid 2530) 429882s; run: log: (pid 8163) 441740s
run: postgresql: (pid 2540) 429882s; run: log: (pid 7798) 441750s
run: prometheus: (pid 2637) 429881s; run: log: (pid 8109) 441742s
run: redis: (pid 2680) 429881s; run: log: (pid 7790) 441751s
run: redis-exporter: (pid 2686) 429881s; run: log: (pid 7988) 441744s
down: sidekiq: 5s, normally up; run: log: (pid 7814) 441750s
down: unicorn: 20s, normally up; run: log: (pid 7805) 441750s
[root@ansible-server gitlab_data]# 

执行恢复:

[root@ansible-server gitlab_data]# du -sh /var/opt/gitlab/backups/*
186M    /var/opt/gitlab/backups/1547025122_2019_01_09_11.6.2_gitlab_backup.tar
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# gitlab-rake gitlab:backup:restore BACKUP=1547025122_2019_01_09_11.6.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
Removing all tables. Press `Ctrl-C` within 5 seconds to abort

(过多的输出。)

ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
WARNING:  no privileges were granted for "public"
GRANT
[DONE]
done
Restoring repositories ...
 * root/ansible_maintain ... [DONE]
 * root/linux_maintain ... [DONE]
 * root/database_oracle_maintain ... [DONE]
 * root/database_mysql_maintain ... [DONE]
done
Restoring uploads ... 
done
Restoring builds ... 
done
Restoring artifacts ... 
done
Restoring pages ... 
done
Restoring lfs objects ... 
done
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


Deleting tmp directories ... done
done
done
done
done
done
done
done
[root@ansible-server gitlab_data]# 

可以看到,上面的过程中:
1. 清空当前的数据
2. 恢复备份文件的数据。

然后重新恢复前面停掉的服务:

[root@ansible-server gitlab_data]# gitlab-ctl status
run: alertmanager: (pid 2375) 430345s; run: log: (pid 8140) 442201s
run: gitaly: (pid 2391) 430345s; run: log: (pid 7902) 442207s
run: gitlab-monitor: (pid 2409) 430344s; run: log: (pid 7959) 442205s
run: gitlab-workhorse: (pid 2438) 430344s; run: log: (pid 7843) 442209s
run: logrotate: (pid 4523) 1921s; run: log: (pid 7868) 442208s
run: nginx: (pid 2501) 430343s; run: log: (pid 7852) 442208s
run: node-exporter: (pid 2522) 430343s; run: log: (pid 7941) 442206s
run: postgres-exporter: (pid 2530) 430342s; run: log: (pid 8163) 442200s
run: postgresql: (pid 2540) 430342s; run: log: (pid 7798) 442210s
run: prometheus: (pid 2637) 430341s; run: log: (pid 8109) 442202s
run: redis: (pid 2680) 430341s; run: log: (pid 7790) 442211s
run: redis-exporter: (pid 2686) 430341s; run: log: (pid 7988) 442204s
down: sidekiq: 465s, normally up; run: log: (pid 7814) 442210s
down: unicorn: 480s, normally up; run: log: (pid 7805) 442210s
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# gitlab-ctl start
ok: run: alertmanager: (pid 2375) 430352s
ok: run: gitaly: (pid 2391) 430352s
ok: run: gitlab-monitor: (pid 2409) 430351s
ok: run: gitlab-workhorse: (pid 2438) 430351s
ok: run: logrotate: (pid 4523) 1928s
ok: run: nginx: (pid 2501) 430350s
ok: run: node-exporter: (pid 2522) 430350s
ok: run: postgres-exporter: (pid 2530) 430349s
ok: run: postgresql: (pid 2540) 430349s
ok: run: prometheus: (pid 2637) 430348s
ok: run: redis: (pid 2680) 430348s
ok: run: redis-exporter: (pid 2686) 430348s
ok: run: sidekiq: (pid 10326) 0s
ok: run: unicorn: (pid 10333) 0s
[root@ansible-server gitlab_data]# 
[root@ansible-server gitlab_data]# gitlab-ctl status
run: alertmanager: (pid 2375) 430356s; run: log: (pid 8140) 442212s
run: gitaly: (pid 2391) 430356s; run: log: (pid 7902) 442218s
run: gitlab-monitor: (pid 2409) 430355s; run: log: (pid 7959) 442216s
run: gitlab-workhorse: (pid 2438) 430355s; run: log: (pid 7843) 442220s
run: logrotate: (pid 4523) 1932s; run: log: (pid 7868) 442219s
run: nginx: (pid 2501) 430354s; run: log: (pid 7852) 442219s
run: node-exporter: (pid 2522) 430354s; run: log: (pid 7941) 442217s
run: postgres-exporter: (pid 2530) 430353s; run: log: (pid 8163) 442211s
run: postgresql: (pid 2540) 430353s; run: log: (pid 7798) 442221s
run: prometheus: (pid 2637) 430352s; run: log: (pid 8109) 442213s
run: redis: (pid 2680) 430352s; run: log: (pid 7790) 442222s
run: redis-exporter: (pid 2686) 430352s; run: log: (pid 7988) 442215s
run: sidekiq: (pid 10326) 4s; run: log: (pid 7814) 442221s
run: unicorn: (pid 10333) 4s; run: log: (pid 7805) 442221s
[root@ansible-server gitlab_data]# 

恢复完成后,可以对GITLAB做一个CHECK检查:

[root@ansible-server gitlab_data]# gitlab-rake gitlab:check SANITIZE=true
Checking GitLab subtasks ...

Checking GitLab Shell ...

GitLab Shell: ... GitLab Shell version >= 8.4.3 ? ... OK (8.4.3)
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 Gitaly ...

Gitaly: ... default ... OK

Checking Gitaly ... Finished

Checking Sidekiq ...

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

Checking Sidekiq ... Finished

Checking Incoming Email ...

Incoming Email: ... Reply by email is disabled in config/gitlab.yml

Checking Incoming Email ... Finished

Checking LDAP ...

LDAP: ... LDAP is disabled in config/gitlab.yml

Checking LDAP ... Finished

Checking GitLab App ...

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: ... 
1/1 ... yes
1/2 ... yes
1/3 ... yes
1/4 ... yes
Redis version >= 2.8.0? ... yes
Ruby version >= 2.3.5 ? ... yes (2.5.3)
Git version >= 2.18.0 ? ... yes (2.18.1)
Git user has default SSH configuration? ... yes
Active users: ... 1

Checking GitLab App ... Finished


Checking GitLab subtasks ... Finished

[root@ansible-server gitlab_data]# 

然后,再看看GITLAB的页面:

如上,可以看到恢复前后的对比:
1. 左边,恢复前的状态
2. 右边,恢复后的状态

可以很清楚的看到,恢复前创建的【After_Recovery_I_Will_Gone 】已经被清掉了。

——————————————

至此,简单的备份与恢复已经操作完成。

——————————————
Done。

1
说点什么

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Adamhuan Recent comment authors

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒
Adamhuan
游客
Adamhuan

注意:备份的文件只能还原到与备份文件同版本的GITLAB中。
——————————
版本限制得很死,这个感觉不是很好。