Back up and restore large reference architectures

This document describes how to:

This document is intended for environments using:

Configure daily backups

Configure backup of PostgreSQL and object storage data

The backup command uses pg_dump, which is not appropriate for databases over 100 GB. You must choose a PostgreSQL solution which has native, robust backup capabilities.

Object storage, (not NFS) is recommended for storing GitLab data, including blobs and Container registry.

  1. Configure AWS Backup to back up both RDS and S3 data. For maximum protection, configure continuous backups as well as snapshot backups.
  2. Configure AWS Backup to copy backups to a separate region. When AWS takes a backup, the backup can only be restored in the region the backup is stored.
  3. After AWS Backup has run at least one scheduled backup, then you can create an on-demand backup as needed.

Configure backup of Git repositories

note
There is a feature proposal to add the ability to back up repositories directly from Gitaly to object storage. See epic 10077.

To back up the Git repositories:

  1. Ensure that the GitLab Rails node has enough attached storage to store Git repositories and an archive of the Git repositories. Additionally, the contents of forked repositories are duplicated into their forks during backup. For example, if you have 5 GB worth of Git repositories and two forks of a 1 GB repository, then you require at least 14 GB of attached storage to account for:
    • 7 GB of Git data.
    • A 7 GB archive file of all Git data.
  2. SSH into the GitLab Rails node.
  3. Configure uploading backups to remote cloud storage.
  4. Configure AWS Backup for this bucket, or use a bucket in the same account and region as your production data object storage buckets, and ensure this bucket is included in your preexisting AWS Backup.
  5. Run the backup command, skipping PostgreSQL data:

    sudo gitlab-backup create SKIP=db
    

    The resulting tar file will include only the Git repositories and some metadata. Blobs such as uploads, artifacts, and LFS do not need to be explicitly skipped, because the command does not back up object storage by default. The tar file will be created in the /var/opt/gitlab/backups directory and the filename will end in _gitlab_backup.tar.

    Since we configured uploading backups to remote cloud storage, the tar file will be uploaded to the remote region and deleted from disk.

  6. Note the timestamp of the backup file for the next step. For example, if the backup name is 1493107454_2018_04_25_10.6.4-ce_gitlab_backup.tar, the timestamp is 1493107454_2018_04_25_10.6.4-ce.
  7. Run the backup command again, this time specifying incremental backup of Git repositories, and the timestamp of the source backup file. Using the example timestamp from the previous step, the command is:

    sudo gitlab-backup create SKIP=db INCREMENTAL=yes PREVIOUS_BACKUP=1493107454_2018_04_25_10.6.4-ce
    
  8. Check that the incremental backup succeeded and uploaded to object storage.
  9. Configure cron to make daily backups. Edit the crontab for the root user:

    sudo su -
    crontab -e
    
  10. There, add the following line to schedule the backup for everyday at 2 AM:

    0 2 * * * /opt/gitlab/bin/gitlab-backup create SKIP=db INCREMENTAL=yes PREVIOUS_BACKUP=1493107454_2018_04_25_10.6.4-ce CRON=1
    

Configure backup of configuration files

If your configuration and secrets are defined outside of your deployment and then deployed into it, then the implementation of the backup strategy depends on your specific setup and requirements. As an example, you can store secrets in AWS Secret Manager with replication to multiple regions and configure a script to back up secrets automatically.

If your configuration and secrets are only defined inside your deployment:

  1. Storing configuration files describes how to extract configuration and secrets files.
  2. These files should be uploaded to a separate, more restrictive, object storage account.

Restore a backup

Restore a backup of a GitLab instance.

Prerequisites

Before restoring a backup:

  1. Choose a working destination GitLab instance.
  2. Ensure the destination GitLab instance is in a region where your AWS backups are stored.
  3. Check that the destination GitLab instance uses exactly the same version and type (CE or EE) of GitLab on which the backup data was created. For example, CE 15.1.4.
  4. Restore backed up secrets to the destination GitLab instance.
  5. Ensure that the destination GitLab instance has the same repository storages configured. Additional storages are fine.
  6. If the backed up GitLab instance had any blobs stored in object storage, ensure that object storage is configured for those kinds of blobs.
  7. If the backed up GitLab instance had any blobs stored on the file system, ensure that NFS is configured.
  8. To use new secrets or configuration, and to avoid unexpected configuration changes during restore:

    • Linux package installations on all nodes:
      1. Reconfigure the destination GitLab instance.
      2. Restart the destination GitLab instance.
    • Helm chart (Kubernetes) installations:

      1. On all GitLab Linux package nodes, run:

        sudo gitlab-ctl reconfigure
        sudo gitlab-ctl start
        
      2. Make sure you have a running GitLab instance by deploying the charts. Ensure the Toolbox pod is enabled and running by executing the following command:

        kubectl get pods -lrelease=RELEASE_NAME,app=toolbox
        
      3. The Webservice, Sidekiq and Toolbox pods must be restarted. The safest way to restart those pods is to run:

        kubectl delete pods -lapp=sidekiq,release=<helm release name>
        kubectl delete pods -lapp=webservice,release=<helm release name>
        kubectl delete pods -lapp=toolbox,release=<helm release name>
        
  9. Confirm the destination GitLab instance still works. For example:

  10. Stop GitLab services which connect to the PostgreSQL database.

    • Linux package installations on all nodes running Puma or Sidekiq, run:

      sudo gitlab-ctl stop
      
    • Helm chart (Kubernetes) installations:

      1. Note the current number of replicas for database clients for subsequent restart:

        kubectl get deploy -n <namespace> -lapp=sidekiq,release=<helm release name> -o jsonpath='{.items[].spec.replicas}{"\n"}'
        kubectl get deploy -n <namespace> -lapp=webservice,release=<helm release name> -o jsonpath='{.items[].spec.replicas}{"\n"}'
        kubectl get deploy -n <namespace> -lapp=prometheus,release=<helm release name> -o jsonpath='{.items[].spec.replicas}{"\n"}'
        
      2. Stop the clients of the database to prevent locks interfering with the restore process:

        kubectl scale deploy -lapp=sidekiq,release=<helm release name> -n <namespace> --replicas=0
        kubectl scale deploy -lapp=webservice,release=<helm release name> -n <namespace> --replicas=0
        kubectl scale deploy -lapp=prometheus,release=<helm release name> -n <namespace> --replicas=0
        

Restore object storage data

Each bucket exists as a separate backup within AWS and each backup can be restored to an existing or new bucket.

  1. To restore buckets, an IAM role with the correct permissions is required:

    • AWSBackupServiceRolePolicyForBackup
    • AWSBackupServiceRolePolicyForRestores
    • AWSBackupServiceRolePolicyForS3Restore
    • AWSBackupServiceRolePolicyForS3Backup
  2. If existing buckets are being used, they must have Access Control Lists enabled.
  3. Restore the S3 buckets using built-in tooling.
  4. You can move on to Restore PostgreSQL data while the restore job is running.

Restore PostgreSQL data

  1. Restore the AWS RDS database using built-in tooling, which creates a new RDS instance.
  2. Because the new RDS instance has a different endpoint, you must reconfigure the destination GitLab instance to point to the new database:

  3. Before moving on, wait until the new RDS instance is created and ready to use.

Restore Git repositories

Select or create a node to restore:

To restore Git repositories:

  1. Ensure the node has enough attached storage to store both the .tar file of Git repositories, and its extracted data.
  2. SSH into the GitLab Rails node.
  3. As part of Restore object storage data, you should have restored a bucket containing the GitLab backup .tar file of Git repositories.
  4. Download the backup .tar file from its bucket into the backup directory described in the gitlab.rb configuration gitlab_rails['backup_path']. The default is /var/opt/gitlab/backups. The backup file must be owned by the git user.

    sudo cp 11493107454_2018_04_25_10.6.4-ce_gitlab_backup.tar /var/opt/gitlab/backups/
    sudo chown git:git /var/opt/gitlab/backups/11493107454_2018_04_25_10.6.4-ce_gitlab_backup.tar
    
  5. Restore the backup, specifying the timestamp of the backup you wish to restore:

    caution
    The restore command requires additional parameters when your installation is using PgBouncer, for either performance reasons or when using it with a Patroni cluster.
    # This command will overwrite the contents of your GitLab database!
    # NOTE: "_gitlab_backup.tar" is omitted from the name
    sudo gitlab-backup restore BACKUP=11493107454_2018_04_25_10.6.4-ce
    

    If there’s a GitLab version mismatch between your backup tar file and the installed version of GitLab, the restore command aborts with an error message. Install the correct GitLab version, and then try again.

  6. Restart and check GitLab:

    • Linux package installations:

      1. In all Puma or Sidekiq nodes, run:

        sudo gitlab-ctl restart
        
      2. In one Puma or Sidekiq node, run:

        sudo gitlab-rake gitlab:check SANITIZE=true
        
    • Helm chart (Kubernetes) installations:

      1. Start the stopped deployments, using the number of replicas noted in Prerequisites:

        kubectl scale deploy -lapp=sidekiq,release=<helm release name> -n <namespace> --replicas=<original value>
        kubectl scale deploy -lapp=webservice,release=<helm release name> -n <namespace> --replicas=<original value>
        kubectl scale deploy -lapp=prometheus,release=<helm release name> -n <namespace> --replicas=<original value>
        
      2. In the Toolbox pod, run:

        sudo gitlab-rake gitlab:check SANITIZE=true
        
  7. Check that database values can be decrypted especially if /etc/gitlab/gitlab-secrets.json was restored, or if a different server is the target for the restore:

    • For Linux package installations, in a Puma or Sidekiq node, run:

      sudo gitlab-rake gitlab:doctor:secrets
      
    • For Helm chart (Kubernetes) installations, in the Toolbox pod, run:

      sudo gitlab-rake gitlab:doctor:secrets
      
  8. For added assurance, you can perform an integrity check on the uploaded files:

    • For Linux package installations, in a Puma or Sidekiq node, run:

      sudo gitlab-rake gitlab:artifacts:check
      sudo gitlab-rake gitlab:lfs:check
      sudo gitlab-rake gitlab:uploads:check
      
    • For Helm chart (Kubernetes) installations, because these commands can take a long time because they iterate over all rows, run the following commands the GitLab Rails node, rather than a Toolbox pod:

      sudo gitlab-rake gitlab:artifacts:check
      sudo gitlab-rake gitlab:lfs:check
      sudo gitlab-rake gitlab:uploads:check
      

    If missing or corrupted files are found, it does not always mean the back up and restore process failed. For example, the files might be missing or corrupted on the source GitLab instance. You might need to cross-reference prior backups. If you are migrating GitLab to a new environment, you can run the same checks on the source GitLab instance to determine whether the integrity check result is preexisting or related to the backup and restore process.

The restoration should be complete.