in System Administration

Migrate gitlab mysql to omnibus postgres

Migrating gitlab from mysql to postgres using omnibus is rather straight forward, thanks to the magnificent documentation. However, having the benefit of a brain dump helps most of the times. That’s what this post is for. If you went into troubles migrating gitlab, read on.

First things first: You tried to insert the backup without converting it to postgres first? Big mistake, fortunately nothing that we are not able to fix. Following steps will “factory reset” gitlab (yepp, everything’s gone afterwards).

# This code will drop your existing database
gitlab-ctl cleanse
gitlab-ctl reconfigure
gitlab-ctl start
echo "Job done."

# cleanse will create a directory with all config data in your current directory

Let’s start from the beginning:

Download and follow the install recipe here.

On your old (mysql) server do the following:

# Original here: https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/update/mysql_to_postgresql.md#converting-a-gitlab-backup-file-from-mysql-to-postgres

# Stop GitLab
sudo service gitlab stop

# Create the backup
cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production

# Note the filename of the backup that was created. We will call it
# TIMESTAMP_gitlab_backup.tar below.

# Move the backup file we will convert to its own directory
sudo -u git -H mkdir -p tmp/backups/postgresql
sudo -u git -H mv tmp/backups/TIMESTAMP_gitlab_backup.tar tmp/backups/postgresql/

# Create a separate database dump with PostgreSQL compatibility
# check your database.yml for passphrase and database name:
sudo cat /home/git/gitlab/config/database.yml | grep -A 8 "production:" | grep username && sudo cat /home/git/gitlab/config/database.yml | grep -A 8 "production:" | grep password
cd tmp/backups/postgresql
sudo -u git -H mysqldump --compatible=postgresql --default-character-set=utf8 -r gitlabhq_production.mysql -u root gitlabhq_production -p

# Clone the database converter
sudo -u git -H git clone https://github.com/gitlabhq/mysql-postgresql-converter.git -b gitlab

# Convert gitlabhq_production.mysql
sudo -u git -H mkdir db
sudo -u git -H python mysql-postgresql-converter/db_converter.py gitlabhq_production.mysql db/database.sql

# Replace the MySQL dump in TIMESTAMP_gitlab_backup.tar.

# Warning: if you forget to replace TIMESTAMP below, tar will create a new file
# 'TIMESTAMP_gitlab_backup.tar' without giving an error.

sudo -u git -H tar rf TIMESTAMP_gitlab_backup.tar db/database.sql

# Done! TIMESTAMP_gitlab_backup.tar can now be restored into a Postgres GitLab
# installation. Remember to recreate the indexes after the import.

Only thing missing is reindexing:

# This guide is for Gitlab Version 6.9 and newer

# Clone the database converter on your Postgres-backed GitLab server
cd /tmp
/opt/gitlab/embedded/bin/git clone https://github.com/gitlabhq/mysql-postgresql-converter.git -b gitlab
cd /tmp/mysql-postgresql-converter

# Generate add_index.rb
/opt/gitlab/embedded/bin/ruby add_index_statements.rb /opt/gitlab/embedded/service/gitlab-rails/db/schema.rb.bundled > add_index.rb

# Create the indexes
/opt/gitlab/bin/gitlab-rails runner 'eval $stdin.read' < add_index.rb

Done.