Skip to content
Snippets Groups Projects

Add rake task for copying 'main' database to 'ci' database

2 files
+ 0
125
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 0
104
#!/usr/bin/env bash
function generate_gitlab_config() {
gitlab_config="gitlab_rails['env'] = { 'GITLAB_ALLOW_SEPARATE_CI_DATABASE' => 'true' }
gitlab_rails['databases']['ci']['enable'] = true
gitlab_rails['databases']['ci']['db_database'] = 'gitlabhq_production_ci'"
if [ $GITLAB_BACKUP_CI_PGUSER ]
then
gitlab_config=$(printf "${gitlab_config}\ngitlab_rails['databases']['ci']['db_username']= '"$GITLAB_BACKUP_CI_PGUSER"'")
fi
if [ $GITLAB_BACKUP_CI_PGHOST ]
then
gitlab_config=$(printf "${gitlab_config}\ngitlab_rails['databases']['ci']['db_host']= '"$GITLAB_BACKUP_CI_PGHOST"'")
fi
if [ $GITLAB_BACKUP_CI_PGPORT ]
then
gitlab_config=$(printf "${gitlab_config}\ngitlab_rails['databases']['ci']['db_port']= '"$GITLAB_BACKUP_CI_PGPORT"'")
fi
if [ $GITLAB_BACKUP_CI_PGPASSWORD ]
then
gitlab_config=$(printf "${gitlab_config}\ngitlab_rails['databases']['ci']['db_password']= '"$GITLAB_BACKUP_CI_PGPASSWORD"'")
fi
if [ $GITLAB_BACKUP_CI_PGSSLMODE ]
then
gitlab_config=$(printf "${gitlab_config}\ngitlab_rails['databases']['ci']['db_sslmode']= '"$GITLAB_BACKUP_CI_PGSSLMODE"'")
fi
if [ $GITLAB_BACKUP_CI_PGSSLKEY ]
then
gitlab_config=$(printf "${gitlab_config}\ngitlab_rails['databases']['ci']['db_sslkey']= '"$GITLAB_BACKUP_CI_PGSSLKEY"'")
fi
if [ $GITLAB_BACKUP_CI_PGSSLCERT ]
then
gitlab_config=$(printf "${gitlab_config}\ngitlab_rails['databases']['ci']['db_sslcert']= '"$GITLAB_BACKUP_CI_PGSSLCERT"'")
fi
if [ $GITLAB_BACKUP_CI_PGSSLROOTCERT ]
then
gitlab_config=$(printf "${gitlab_config}\ngitlab_rails['databases']['ci']['db_sslrootcert']= '"$GITLAB_BACKUP_CI_PGSSLROOTCERT"'")
fi
if [ $GITLAB_BACKUP_CI_PGSSLCRL ]
then
gitlab_config=$(printf "${gitlab_config}\ngitlab_rails['databases']['ci']['db_sslcrl']= '"$GITLAB_BACKUP_CI_PGSSLCRL"'")
fi
if [ $GITLAB_BACKUP_CI_PGSSLCOMPRESSION ]
then
gitlab_config=$(printf "${gitlab_config}\ngitlab_rails['databases']['ci']['db_sslcompression']= '"$GITLAB_BACKUP_CI_PGSSLCOMPRESSION"'")
fi
echo "$gitlab_config"
}
if [ "$(id -u)" -ne 0 ]
then
echo "Please run as root." >&2
exit 1
fi
cat <<- EXPLANATION
This script will switch this GitLab instance from a single-database setup to
decomposed setup. When the script is finished, the database setup will look like this:
- main database: will still use PostgreSQL database 'gitlabhq_production'
- ci database: CI related data will be stored in PostgreSQL database 'gitlabhq_production_ci'
Prerequisites:
- The new ci database should already exist:
sudo gitlab-psql -c "CREATE DATABASE gitlabhq_production_ci WITH OWNER 'gitlab'"
What this script will do:
- Disable background migrations because they should not be active during this migration
See https://docs.gitlab.com/ee/development/database/batched_background_migrations.html#enable-or-disable-background-migrations
- Stop the Gitlab Instance except PostgreSQL
- Copy data in gitlabhq_production to gitlabhq_production_ci (by dumping, then restoring)
Please confirm the upgrade by pressing 'y':
EXPLANATION
read -p "Are you sure? " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
echo "Disabling Background migrations..."
gitlab-rails runner "Feature.disable(:execute_background_migrations) && Feature.disable(:execute_batched_migrations_on_schedule)"
echo "Stopping GitLab..."
gitlab-ctl stop
gitlab-ctl start postgresql
gitlab-rake gitlab:db:decomposition:migrate
if [ ! $? -eq 0 ]
then
echo "Could not copy database, exiting..."
exit 1
fi
cat <<- EOF
First step of migration is now done: data has been copied to new database.
You should now modify the GitLab configuration file in /etc/gitlab/gitlab.rb.
When you are ready, you can start the 'post-migrate' script.
EOF
generate_gitlab_config
Loading