美文网首页
gitlab备份

gitlab备份

作者: fanzhouyuanhang | 来源:发表于2018-11-07 18:24 被阅读0次

    使用gitlab自带的backup命令进行备份。在gitlab的help文档里面有相关的说明,URL地址是: http://your-gitlab-server/help/raketasks/backup_restore.md

    利用gitlab的backup机制会生成一个名为[TIMESTAMP]_gitlab_backup.tar的tar文件,这个tar文件会包含所有的数据库数据、所有的repo数据,以及所有的附件。TIMESTAMP是以秒为单位的时间戳,用来区分不同的备份文件,如:1541527310_2018_11_07_9.5.1_gitlab_backup.tar。需要注意的是,利用backup机制进行备份的话,对gitlab的版本是要求严格一致的。例如用8.6版的gitlab生成的备份文件,拿到8.7版的gitlab上进行恢复,是会报错的。

    (1) backup操作的命令

    #如果使用包安装的gitlab的话,使用以下命令

    sudo gitlab-rake gitlab:backup:create

    #如果使用源码安装的gitlab话,使用以下命令

    sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production

    也可以使用SKIP变量来选择要备份的内容,SKIP变量的选项有:db, uploads (attachments), repositories, builds(CI build output logs), artifacts (CI build artifacts), lfs (LFS objects)。多个项之间用逗号隔开:

    sudo gitlab-rake gitlab:backup:create SKIP=db,uploads

    backup命令执行之后,终端上会出现导出数据库及repo数据等的的操作日志。

    (2) backup的相关配置

    backup文件的保存位置在config/gitlab.yml配置文件里面设置。打开这个配置文件,可以找到关于backup的这么一段配置:

      ## Backup settings

      backup:

        path: "/var/opt/gitlab/backups"  # Relative paths are relative to Rails.root (default: tmp/backups/)

        archive_permissions:  # Permissions for the resulting backup.tar file (default: 0600)

        keep_time:    # default: 0 (forever) (in seconds)

        pg_schema:    # default: nil, it means that all schemas will be backed up

        upload:

          # Fog storage connection settings, see http://fog.io/storage/ .

          connection:

          # The remote 'directory' to store your backups. For S3, this would be the bucket name.

          remote_directory:

          multipart_chunk_size:

          encryption:

    默认情况下,备份文件是放在/var/opt/gitlab/backups目录下的,另外archive_permissions属性用来指定生成tar文件的权限属性,默认为0600。还可以通过upload设置,将备份上传到远程服务器,详细配置这里不做介绍。

    注意:后来自己在实际配置中发现,在gitlab.yml里面配置backups之后,执行gitlab-ctl reconfigure命令,发现配置的东西被冲掉了,需要在gitlab.rb里面配置才行:

    gitlab_rails['manage_backup_path'] = true

    gitlab_rails['backup_path'] = "/var/opt/gitlab/backups" #备份目录

    gitlab_rails['backup_archive_permissions'] = 0644 #生成的文件权限

    gitlab_rails['backup_keep_time'] = 864000  #保留10天

    (3) 利用backup文件恢复

    首先,用来恢复的服务器上要有一个跟生成backup文件同版本的gitlab运行环境。我使用的是同一个gitlab的ubuntu镜像,所以版本的一致性是可以确保的。

    接下来,需要确保backup的tar文件在配置文件中指定的/var/opt/gitlab/backups目录下面。

    sudo cp 1541527310_2018_11_07_9.5.1_gitlab_backup.tar /var/opt/gitlab/backups/

    然后,就可以同过命令来执行恢复操作了:

    # 先关闭连接数据库的进程

    sudo gitlab-ctl stop unicorn

    sudo gitlab-ctl stop sidekiq

    # 通过指定时间戳来执行restore操作,这个操作会复写gitlab的数据库

    sudo gitlab-rake gitlab:backup:restore BACKUP=1541527310

    # 再次启动gitlab

    sudo gitlab-ctl start

    # 通过下面命令检查gitlab

    sudo gitlab-rake gitlab:check SANITIZE=true

    需要注意的是:backup生成的tar文件的备份是不会对gitlab的配置文件进行备份的,gitlab.rb, gitlab.yml, /etc/gitlab/gitlab-secrets.json(存放着数据库中为two-factor authentication加密信息的key)等这些配置文件需要另行备份。

    (4) 配置备份的定时任务 

    利用cron来进行定时备份操作

    0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create SKIP=db,uploads

    设置脚本定期清理备份文件gitlab.sh,配置以下参数:

    find /var/opt/gitlab/backups/ -mtime +7 -type f |xargs rm -rf

    如果想指定备份文件的保存时间的话,也可以在/etc/gitlab/gitlab.rb中进行配置:

    # limit backup lifetime to 7 days - 604800 seconds

    gitlab_rails['backup_keep_time'] = 604800

    相关文章

      网友评论

          本文标题:gitlab备份

          本文链接:https://www.haomeiwen.com/subject/gqtbxqtx.html