# Reproduce the isolated MISP 3.0 database rehearsal

Run this only with the repository's fictional lab. The source is the Compose stack using MISP 2.5.53. The target is a standalone PHP CLI container with the MISP `3.0_wip` source fixed at commit `76dc5858bc2a0bb9fb9729de1b9ef6b16e14257d`. The rollback is another Compose project using 2.5.53. They use different database volumes. The third project uses another loopback port.

The [official Git installation migration](https://www.circl.lu/doc/misp/upgrade-2-5-to-3-0/index.html) covers the full procedure for Ubuntu with Apache and Supervisor. This narrower Docker exercise uses the same application CLI to test a database migration without upgrading a running application.

## Back up the source

Set `SOURCE_PROJECT` to the fictional Compose project actually running on your Docker context. The default for the repository is `sectools-misp-lab`. Complete `verify.py` first. Pause any fixture writers before taking the snapshot.

```sh
umask 077
LAB="$PWD/lab"
SOURCE_PROJECT=sectools-misp-lab
REHEARSAL="$(mktemp -d)"
REV=76dc5858bc2a0bb9fb9729de1b9ef6b16e14257d

cd "$LAB"
docker compose -p "$SOURCE_PROJECT" exec -T db sh -c \
  'exec mariadb-dump -uroot -p"$MYSQL_ROOT_PASSWORD" --single-transaction --add-drop-table --routines --triggers --databases misp' \
  > "$REHEARSAL/misp25.sql"

SOURCE_CORE="$(docker compose -p "$SOURCE_PROJECT" ps -q misp-core)"
mkdir "$REHEARSAL/config"
docker cp "$SOURCE_CORE:/var/www/MISP/app/Config/database.php" "$REHEARSAL/config/database.php"
docker cp "$SOURCE_CORE:/var/www/MISP/app/Config/config.php" "$REHEARSAL/config/config.php"
```

The SQL, configuration, and environment files can contain secrets. Keep them in the private rehearsal directory. Do not upload them with verification evidence.

## Restore into another project and stage the 3.0 CLI

Use new project and container names. Do not reuse a project that contains anything you want to preserve.

```sh
TARGET=sectools-migration-rehearsal
CLI=sectools-migration-rehearsal-cli

cd "$LAB"
docker compose -p "$TARGET" up -d --wait db redis
docker compose -p "$TARGET" exec -T db sh -c \
  'exec mariadb -uroot -p"$MYSQL_ROOT_PASSWORD"' < "$REHEARSAL/misp25.sql"

git clone --depth 1 --single-branch --branch 3.0_wip https://github.com/MISP/MISP.git "$REHEARSAL/source"
test "$(git -C "$REHEARSAL/source" rev-parse HEAD)" = "$REV"
git -C "$REHEARSAL/source" submodule update --init --depth 1 app/Lib/cakephp
```

If the branch has advanced, fetch the pinned commit in the scratch checkout and check it out before proceeding. Make sure the commit check passes. The CLI container uses the lab's existing PHP 8.4 runtime, with the target code and Composer dependencies in a separate path.

```sh
docker run -d --name "$CLI" --network "${TARGET}_default" --entrypoint sleep \
  ghcr.io/misp/misp-docker/misp-core:v2.5.53 infinity
docker exec "$CLI" mkdir -p /srv/misp3/app/tmp/cache/models /srv/misp3/app/tmp/cache/persistent /srv/misp3/app/tmp/logs
tar -C "$REHEARSAL/source" --exclude=.git -cf "$REHEARSAL/source.tar" .
docker cp - "$CLI:/srv/misp3/" < "$REHEARSAL/source.tar"

docker cp "$REHEARSAL/config/database.php" "$CLI:/srv/misp3/app/Config/database.php"
docker cp "$REHEARSAL/config/config.php" "$CLI:/srv/misp3/app/Config/config.php"
docker exec "$CLI" sh -c 'chown -R www-data:www-data /srv/misp3 && chmod 600 /srv/misp3/app/Config/config.php /srv/misp3/app/Config/database.php'
docker exec -u www-data -w /srv/misp3/app "$CLI" sh -c \
  'cp Config/bootstrap.default.php Config/bootstrap.php && cp Config/core.default.php Config/core.php'
docker exec -u www-data "$CLI" php -r 'require "/srv/misp3/app/Config/config.php"; $config["MISP"]["background_jobs"] = false; file_put_contents("/srv/misp3/app/Config/config.php", "<?php\n\$config = " . var_export($config, true) . ";\n");'

docker run --rm --entrypoint cat \
  composer@sha256:5b2137f112f939a92788ceb5e55f54859d9dbf467a44037cf7760bdda949f365 \
  /usr/bin/composer > "$REHEARSAL/composer.phar"
docker cp "$REHEARSAL/composer.phar" "$CLI:/tmp/composer.phar"
docker exec "$CLI" chmod 644 /tmp/composer.phar
docker exec -u www-data -e COMPOSER_HOME=/tmp/composer-workshop "$CLI" \
  php /tmp/composer.phar --working-dir=/srv/misp3/app install --no-dev --no-interaction --prefer-dist
```

The target's copied database configuration should point at `db` on the isolated target network. Its Redis instance is also isolated. No 2.5 web application or worker uses this target database. Disabling workers is specific to this CLI-only rehearsal.

## Run and verify the migration

```sh
docker exec -u www-data -w /srv/misp3/app "$CLI" Console/cake Admin schemaDiagnostics
docker exec -u www-data -w /srv/misp3/app "$CLI" Console/cake Admin runUpgrade
docker exec -u www-data -w /srv/misp3/app "$CLI" Console/cake Admin schemaDiagnostics
docker exec -u www-data -w /srv/misp3/app "$CLI" Console/cake AttributeValue verify
```

Require zero failed and pending migrations. The value verifier can no longer compare the old text columns after the conversion drops them. For the separate content check, compare the source or rollback Attributes' `value1` and `value2` with joins from the migrated `value1_id` and `value2_id` to `attribute_values.value`. Keep the same Event UUID, Attribute type and detection flags, and Object UUID across the comparison.

## Restore the same backup to 2.5.53

Restore into a third project using another free, loopback-only port. The 3.0 database stays in its own project.

```sh
mkdir "$REHEARSAL/rollback"
cp "$LAB/compose.yaml" "$LAB/http-check.py" "$LAB/misplab.py" "$REHEARSAL/rollback/"
sed 's/^MISP_HTTP_PORT=.*/MISP_HTTP_PORT=18092/' "$LAB/.env" > "$REHEARSAL/rollback/.env"
cd "$REHEARSAL/rollback"
ROLLBACK=sectools-migration-rollback
docker compose -p "$ROLLBACK" up -d --wait db
docker compose -p "$ROLLBACK" exec -T db sh -c \
  'exec mariadb -uroot -p"$MYSQL_ROOT_PASSWORD"' < "$REHEARSAL/misp25.sql"
docker compose -p "$ROLLBACK" up -d --wait
python3 http-check.py
python3 -c "from misplab import find_event; e=find_event('99f6f54e-d6ce-5b2a-bea3-bdbf6fda1b87'); assert e and e['published'] and len(e['Attribute']) == 4 and len(e['Object']) == 1"
```

This validates rollback to the old application and restored database for the sample. Plan separate restores and checks for production attachments, GPG, configuration, background jobs, and integrations. Stop the named exercise projects when finished and handle the private backups under your own retention rules.
