# Migrate from server to cloud

Move a Platforma installation from Single Server mode to a Kubernetes cluster on AWS, with all projects and results.

Other ways: [Migrate between sign-in providers](/maintain-migrating-sign-in-providers/).

When you move to Kubernetes (A system that runs programs on a group of machines (a cluster). Platforma can run in a Kubernetes cluster.), users keep all their projects and results, and their jobs (One run of a tool on data. The Backend starts jobs on the server where it runs, or in a Kubernetes cluster.) then run in the cluster, so many jobs can run at the same time. You move three things: the database as one backup file, the primary storage (The bucket (Amazon S3 or Google Cloud Storage) or folder where Platforma keeps all results and uploaded files.) as a copy in the bucket of the cluster, and the master secret. The server keeps running without changes until you know that the cluster works.

The full procedure, with all commands and the AWS permissions, is in the [migration guide](https://github.com/milaboratory/platforma-helm/blob/release/5.0/infrastructure/aws/migration.md). This page tells what happens and what you must know before you start.

## Before you start

- **The same version on both sides.** The Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) on the cluster must have the same version (major and minor) as the Backend on the server. Upgrade after the migration: you can go directly to the most recent release (see [Supported versions](/reference-versions/#supported-versions)).
- **A cluster that is ready.** Install the cluster and its parts as for the Helm chart, but do not install Platforma in this step: see [Install in custom infrastructure](/install-custom-infrastructure/).
- **An empty bucket** for the primary storage of the cluster.
- **Your license key.** The restore and the cache cleanup run the Backend program in the cluster, and it needs the license.
- **A time without users.** From the backup of the database to the end of the migration, nobody may write to the server. Work that starts after the backup is lost.

## Where the results are now

The database refers to the files in the primary storage by their path and by an ID of the storage, not by the name of the bucket or the folder. The files never change after the Backend writes them. Because of this, you can copy them to the new bucket with the same paths while users work, and stop the work only for a short time at the end.

| Primary storage of the server                                                            | How to copy it                                                                                                                                                                             |
| ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **A folder** (the default: `<main-root>/primary`, for example `/data/platforma/primary`) | Copy the content of the folder to the root of the new bucket, or to its prefix, with the same paths: `aws s3 sync /data/platforma/primary/ s3://<bucket>/<prefix>/`. Run it on the server. |
| **An S3 bucket**                                                                         | Copy the bucket to the new bucket. For several TiB, use S3 Batch Replication. For less data, use a sync tool such as `aws s3 sync` or `s5cmd`.                                             |

Each new run of `aws s3 sync` copies only the files that are new, so the last run is short.

## How the migration works

1. **Copy the primary storage while users work.** See [Where the results are now](#where-the-results-are-now). The first copy can take hours or days.

2. **Stop all work on the server.** Tell users to stop, and stop the programs that send work to Platforma.

3. **Back up the server two times, and cancel the running Blocks (A small application for one step of an analysis, for example the import of reads or clustering. It can combine several tools.) between them.**

   1. Make a safety backup of the database. It is your way back for the server.
   2. Stop the Backend and run it one time with `--cancel-running-blocks`. Blocks that run now cannot continue on the cluster. Without this step, they stay “running” there forever. On Kubernetes, see [Cancel running Blocks on Kubernetes](#cancel-running-blocks-on-kubernetes).
   3. Start the Backend again and make the migration backup. You restore this file in the cluster.

   Make each backup on the server:

   The Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) gives a copy of its database through its debug API. In Single Server mode (The mode in which the Backend and all its jobs run on one Linux machine, the server, with no Kubernetes cluster.), the debug API is off by default. To turn it on, add a line to the environment file of the service and restart the Backend. You do this one time:

   ```sh
   echo 'PL_DEBUG_ENABLED=true' | sudo tee -a /etc/platforma/platforma.env > /dev/null
   sudo systemctl restart platforma
   ```

   The option `--debug-enabled` in the start command does the same. The debug API then listens on `127.0.0.1:9091` (the options `--debug-ip` and `--debug-port`), so run the next command on the server. The Backend continues to work during the copy.

   ```sh
   curl -fsS http://127.0.0.1:9091/db/state_raw | gzip > backup-$(date +%F).tsv.gz
   ```

   When the Backend uses TLS (`--listen-tls` or `--self-signed-tls`), the debug API accepts only HTTPS. Then use `curl -fsSk https://127.0.0.1:9091/db/state_raw` in the command: the certificate names the domain of the server, not `127.0.0.1`, and the connection does not go out of the server.

   > **The debug API does not ask for a sign-in:**
   >
   > Each person or program that can connect to the debug API can read the full database. Keep it on a local address: do not set `--debug-ip` to an address that other hosts can reach.

4. **Copy the last changes of the primary storage.** Run the copy again, in the same stop as the migration backup: then the bucket has every file that the backup refers to.

5. **Give the cluster the master secret of the server.** The database keeps credentials and signatures that only this secret opens. Put it in the Secret `platforma-master-secret` (key `master-secret`) before you install Platforma.

   - If the server starts with `--master-secret` or `--master-secret-file`, use that value.

   - If the server has only a login token, the Backend makes the secret from the token. The token is in `PL_AUTH_TOKEN`, or after the colon in `<main-root>/token.htpasswd`. Make the same secret:

     ```sh
     printf '%s' '<token>' | openssl dgst -sha256 -binary | base64
     ```

6. **Restore the database in the cluster.** Make the database volume `platforma-database` before you install Platforma, with the Helm labels and annotations, so that the chart can use it:

   ```yaml
   metadata:
     name: platforma-database
     namespace: platforma
     labels:
       app.kubernetes.io/managed-by: Helm
     annotations:
       meta.helm.sh/release-name: platforma
       meta.helm.sh/release-namespace: platforma
   ```

   Put the backup file on the volume, and restore it with `platforma --restore-db=<file> --db-dir=/data/database --force`. The pods must run as user and group `1010`, the user of the Helm chart: if not, the Backend cannot read the restored files.

7. **Install Platforma with the Helm chart**, with the new bucket as primary storage. Use the same prefix as in the copy. The chart uses the volume that you made.

   *values.yaml (part)*

   ```yaml
   primaryStorage:
     s3:
       bucket: <bucket>
       region: <region>
       prefix: <prefix>
   ```

8. **Clean the caches.** On the server, the tools ran directly on its disk. In the cluster, they run in containers. Stop the Backend and run it one time with `--invalidate-caches`. The Backend then runs the tools again in the cluster when a project needs their results. Then start the Backend.

If you also change the sign-in provider (A configured way for users to sign in to the Backend: single sign-on (SSO), LDAP or a static list of users. One Backend can have more than one.), for example from a static list of users (A sign-in provider that is a file with the logins and the password hashes of the users, in the htpasswd format. The Backend reads it again when it changes.) to single sign-on (Sign-in through the identity service of the company, with the same account as for other company systems.), users can lose access to their projects: see [Migrate between sign-in providers](/maintain-migrating-sign-in-providers/).

## Cancel running Blocks on Kubernetes

When the installation that you move from runs on Kubernetes, run the Backend one time with `--cancel-running-blocks` in a separate pod. The pod copies the full settings of the Backend, so it reads the same database, license and storage. The Backend must be stopped: only one program at a time can open the database.

1. **Stop the Backend.**

   ```sh
   kubectl scale deployment platforma -n platforma --replicas=0
   ```

   Check that the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) pod is gone. The filter leaves out the pods of the cleanup job (One run of a tool on data. The Backend starts jobs on the server where it runs, or in a Kubernetes cluster.), which have the same release label:

   ```sh
   kubectl get pods -n platforma -l 'app.kubernetes.io/instance=platforma,!app.kubernetes.io/component'
   ```

   Result: `No resources found in platforma namespace.` If the list shows a pod, also in the state `Terminating`, wait and run the command again. Do not continue while the pod exists: it can still write to the database.

2. **Start the pod.** It runs the Backend with the extra option, without the health checks, and stops when the Blocks are cancelled. The command makes the pod with [`jq`](https://jqlang.org/download/): install it on your computer first.

   ```sh
   kubectl get deployment platforma -n platforma -o json | jq '
     .spec.template.spec as $s
     | {
         apiVersion: "v1", kind: "Pod",
         metadata: {name: "platforma-cancel-blocks", namespace: .metadata.namespace},
         spec: ($s
           | .restartPolicy = "Never"
           | .containers = [.containers[]
               | select(.name == "platforma")
               | .args += ["--cancel-running-blocks"]
               | del(.startupProbe, .livenessProbe, .readinessProbe)])
       }' | kubectl apply -f -
   ```

3. **Wait until the pod stops, and read the result.**

   ```sh
   kubectl get pod platforma-cancel-blocks -n platforma -w
   ```

   When the status is `Completed` or `Error`, stop the command with Ctrl+C, and read the log:

   ```sh
   kubectl logs platforma-cancel-blocks -n platforma -c platforma | grep 'running blocks'
   ```

   Result: `All running blocks were successfully cancelled`. If the log shows `Failed to cancel running blocks`, keep the pod and send its log to MiLaboratories support.

4. **Delete the pod, and start the Backend.**

   ```sh
   kubectl delete pod platforma-cancel-blocks -n platforma
   kubectl scale deployment platforma -n platforma --replicas=1
   ```

## Check the result

- The database volume is `Bound` and not empty, and the Backend pod is `Ready`.
- The number of objects in the new bucket is the same as in the source.
- In the Desktop App (The program that each user installs on their own computer to work with Platforma. It is a client of the Backend.), connected to the cluster, your existing projects open with their results.

## Go back

The server stays as it was, except for the cancelled Blocks. If the cluster does not work:

- To try again in the cluster: uninstall the Helm release (the database volume stays), delete the database volume, and restore the migration backup again.

- To keep working on the server: start it again. To also get back the Blocks that you cancelled, restore the safety backup on the server while the Backend is stopped:

  ```sh
  sudo -u platforma /opt/platforma/binaries/platforma \
    --restore-db=<safety-backup> --db-dir=/data/platforma/rocksdb --force
  ```

## Next

- [Verify the installation](/install-verify/)
- [Connect clients](/install-connect-clients/)
- [Backup and restore](/maintain-backup-and-restore/)

Source: https://docs.platforma.bio/maintain-migrating-to-cloud/
