improved migration script (no local mysql required anymore ...)

... support for custom or sql output format; migrate to latest version, not just 10
pull/9145/head
Markus Kahl 4 years ago
parent af65092348
commit f4a859ef96
  1. 81
      script/migration/migrate-from-pre-8.sh

@ -12,17 +12,20 @@
if [[ -z "$1" ]] || [[ -z "$2" ]]; then if [[ -z "$1" ]] || [[ -z "$2" ]]; then
echo echo
echo " usage: bash migrate-from-pre-8.sh <docker host IP> <MySQL dump>" echo " usage: bash migrate-from-pre-8.sh <docker host IP> <MySQL dump> [dump format = sql|custom (default)]"
echo echo
echo " example: bash migrate-from-pre-8.sh 192.168.1.42 /var/db/openproject/backups/dump.sql" echo " example: bash migrate-from-pre-8.sh 192.168.1.42 /var/db/openproject/backups/dump.sql"
echo echo
exit 1 exit 1
fi fi
CURRENT_OP_MAJOR_VERSION=11
SECONDS=0 SECONDS=0
DOCKER_HOST_IP=$1 DOCKER_HOST_IP=$1
MYSQL_DUMP_FILE=$2 MYSQL_DUMP_FILE=$2
DUMP_FORMAT=${3:-custom}
MYSQL_CONTAINER=opmysql MYSQL_CONTAINER=opmysql
POSTGRES_CONTAINER=oppostgres POSTGRES_CONTAINER=oppostgres
@ -35,7 +38,7 @@ REMOVE_CONTAINERS=true
POSTGRES_PORT=5439 POSTGRES_PORT=5439
MYSQL_PORT=3305 # has to be free on localhost MYSQL_PORT=3305 # has to be free on localhost
MYSQL_USER=root MYSQL_USER=root
export MYSQL_PWD=root # export to be used by mysql client MYSQL_PWD=root
DATABASE=openproject DATABASE=openproject
SKIP_STEP_1=false SKIP_STEP_1=false
@ -79,7 +82,7 @@ if [[ ! "$SKIP_STEP_1" = "true" ]]; then
echo echo
echo "1.4) Creating MySQL database for migration from OP 7 to 8" echo "1.4) Creating MySQL database for migration from OP 7 to 8"
echo "drop database if exists $DATABASE; create database $DATABASE;" | mysql -uroot -h 127.0.0.1 -P $MYSQL_PORT echo "drop database if exists $DATABASE; create database $DATABASE;" | docker exec -i $MYSQL_CONTAINER mysql -p$MYSQL_PWD
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
echo " Could not create database" echo " Could not create database"
@ -91,7 +94,7 @@ if [[ ! "$SKIP_STEP_1" = "true" ]]; then
echo echo
echo "1.5) Importing MySQL dump ($MYSQL_DUMP_FILE)" echo "1.5) Importing MySQL dump ($MYSQL_DUMP_FILE)"
cat $MYSQL_DUMP_FILE | mysql -uroot -h 127.0.0.1 -P $MYSQL_PORT $DATABASE cat $MYSQL_DUMP_FILE | docker exec -i $MYSQL_CONTAINER mysql -p$MYSQL_PWD $DATABASE
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
echo " Could not import database" echo " Could not import database"
@ -147,7 +150,7 @@ if [[ ! "$SKIP_STEP_1" = "true" ]]; then
echo echo
echo "1.8) Dumping intermediate database in version 8 ..." echo "1.8) Dumping intermediate database in version 8 ..."
mysqldump -uroot -h 127.0.0.1 -P $MYSQL_PORT $DATABASE > $DATABASE-dump-8.sql docker exec -it $MYSQL_CONTAINER mysqldump -p$MYSQL_PWD $DATABASE > $DATABASE-mysql-dump-8.sql
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
echo " Could not dump database" echo " Could not dump database"
@ -228,18 +231,62 @@ else
echo " Moved tables from $DATABASE to public" echo " Moved tables from $DATABASE to public"
fi fi
docker stop migrate8to10 > /dev/null # don't need this anymore
echo echo
echo "2.4) Dumping migrated database to $DATABASE-migrated.dump" echo "2.4) Migrating from 10 to current ($CURRENT_OP_MAJOR_VERSION)"
# using the running docker image to dump the database to ensure we use the same docker pull openproject/community:$CURRENT_OP_MAJOR_VERSION
# postgres client version and also so that a postgres client is not necessary to run this script
docker exec -e PGPASSWORD=postgres -it migrate8to10 pg_dump \ docker run \
-h $DOCKER_HOST_IP \ --rm \
-p $POSTGRES_PORT \ -v $PWD:/data \
-U postgres \ --name migrate10tocurrent \
-d $DATABASE \ -e DATABASE_URL="postgresql://postgres:postgres@$DOCKER_HOST_IP:$POSTGRES_PORT/$DATABASE" \
-F custom \ -it openproject/community:$CURRENT_OP_MAJOR_VERSION \
-f /data/$DATABASE-migrated.dump bundle exec rake db:migrate > migration.log
MIGRATION_STATUS=$?
if [[ $MIGRATION_STATUS -gt 0 ]]; then
echo " migration unsuccessful. Please check migration.log"
exit 1
else
echo " migration SUCCESSFUL!"
fi
EXT="dump"
if [[ "$DUMP_FORMAT" = "sql" ]]; then
EXT="sql"
fi
echo
echo "2.5) Dumping migrated database to $DATABASE-migrated.$EXT"
OUTPUT_PARAMS="-F custom -f /data/$DATABASE-migrated.dump"
OUTPUT_FILE="/dev/stdout"
if [[ "$DUMP_FORMAT" = "sql" ]]; then
OUTPUT_PARAMS=""
OUTPUT_FILE="./$DATABASE-migrated.sql"
fi
# Using the running docker image to dump the database to ensure we use the same
# postgres client version and also so that a postgres client is not necessary to run this script.
# We are not using the latest container since the Postgres version might have changed in it.
docker run \
--rm \
-e PGPASSWORD=postgres \
-v $PWD:/data \
-it openproject/community:11 pg_dump \
-h $DOCKER_HOST_IP \
-p $POSTGRES_PORT \
-U postgres \
-d $DATABASE \
-n public \
-x -O \
$OUTPUT_PARAMS \
> $OUTPUT_FILE
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
echo " Could not dump database" echo " Could not dump database"
@ -252,10 +299,6 @@ if [[ ! "$REMOVE_CONTAINERS" = "false" ]]; then
echo echo
echo "Cleaning up used docker containers..." echo "Cleaning up used docker containers..."
# ... and then kill it (there is no one-off command for the migration in the docker container yet
# and running it via docker run doesn't work since the user has to be root and not the app user)
docker stop migrate8to10
docker stop $MYSQL_CONTAINER && docker rm $MYSQL_CONTAINER docker stop $MYSQL_CONTAINER && docker rm $MYSQL_CONTAINER
docker stop $OP7_CONTAINER && docker rm $OP7_CONTAINER docker stop $OP7_CONTAINER && docker rm $OP7_CONTAINER
docker stop $OP8_CONTAINER && docker rm $OP8_CONTAINER docker stop $OP8_CONTAINER && docker rm $OP8_CONTAINER

Loading…
Cancel
Save