# Migrate RevDeBug Server

## Moving RevDeBug instances to a new VM

In the instructions, variable names are provided. Replace them with the ones used by you.

### Backup PostgreSQL

#### Setting Up Environment Variables for PostgreSQL Login

If you are using the default variables to log in to RevDeBug PostgreSQL, you can use the following commands:

Change to the 'revdebug-server-docker-compose' directory:

```bash
cd ./revdebug-server-docker-compose
```

Export the environment variables for PostgreSQL credentials using the following commands:

```bash
export $(grep 'DB_.*:' docker-compose.yml | sed 's/.*{\(REVDEBUG_POSTGRES_.*\):-\(.*\)}/\1=\2/')
export $(eval cat .env | grep -v "^#")
```

These commands will set up the necessary environment variables for PostgreSQL. Make sure you are in the correct directory containing the 'docker-compose.yml' file and the '.env' file before executing the commands.

#### Performing a PostgreSQL database backup

Create a folder for database backups and navigate to it:

```bash
mkdir db && cd db
```

Execute the command to create a backup of the PostgresSQL for the databases revdebug\_db and keycloak:

```bash
sudo docker compose -p revdebug exec -e PGPASSWORD="$REVDEBUG_POSTGRES_PASSWORD" postgres  pg_dump -U $REVDEBUG_POSTGRES_USER revdebug_db > ./pg-backup.sql
```

```bash
sudo docker compose -p revdebug exec -e PGPASSWORD="$REVDEBUG_POSTGRES_PASSWORD" postgres  pg_dump -U $REVDEBUG_POSTGRES_USER keycloak > ./pg-backup-keycloak.sql
```

### Backup OpenSearch

Go to the directory named `revdebug-server-docker-compose`

```bash
cd ./revdebug-server-docker-compose
```

#### Add path.repo configuration to docker-compose.yml

In the `docker-compose.yml` file, add the following configuration to the `environment` section of the `opensearch` container:

<pre class="language-yaml"><code class="lang-yaml"><strong>environment:
</strong>  - path.repo=/usr/share/opensearch/snapshots
</code></pre>

**Recreate the OpenSearch container to apply the changes**

```bash
sudo docker compose -p revdebug up -d
```

#### Enter the OpenSearch container

Run the following command to access the terminal within the `OpenSearch` container:

```bash
sudo docker exec -it revdebug-opensearch-1 bash
```

#### Create a snapshot repository using a request

Issue a PUT request to create a repository for snapshots with the following command:

```bash
curl -X PUT localhost:9200/_snapshot/opensearch-snapshots --header 'Content-Type: application/json' --data '{ "type": "fs", "settings": { "location": "/usr/share/opensearch/snapshots" }}'
```

**Verify if the repository has been created**

You can check if the repository has been successfully created by using the following command:

```bash
curl -X GET localhost:9200/_cat/repositories
```

**List all snapshots taken so far**

To list all the snapshots taken up to this point, run the following command:

```bash
curl -X GET localhost:9200/_snapshot/opensearch-snapshots/_all?pretty
```

#### Take a snapshot of the current OpenSearch database state

Create a snapshot of the current state of the OpenSearch database using the following command:

```bash
curl -X PUT localhost:9200/_snapshot/opensearch-snapshots/snapshot-name
```

{% hint style="info" %}
Note: Replace snapshot-name in previous step with a name of your choice for the snapshot (or use the default).
{% endhint %}

**Verify if the snapshot was successful**

Check if the snapshot was executed successfully using the following command:

```bash
curl -X GET localhost:9200/_snapshot/opensearch-snapshots/_all?pretty
```

Exit the container.

#### Copy the snapshot folder from the OpenSearch container

```bash
sudo docker cp revdebug-opensearch-1:/usr/share/opensearch/snapshots ../db/snapshots
```

### Shut down the RevDeBug server

To shut down the RevDeBug server, you can use the following command:

```bash
sudo docker compose -p revdebug down
```

This command will stop and remove the containers, networks, and volumes associated with the RevDeBug server specified in the docker-compose.yml file. The -p option allows you to specify a project name, and in this case, it's set to revdebug.

### Create a backup of RevDeBug server data, configuration, and databases

Navigate to the parent directory and create a new folder named `backup`:

#### Create a backup of the server data

```bash
cd ..
mkdir backup
cd backup
```

Exclude the directories apm\_database and revdebug\_database and compress the contents of /var/revdebug/ into a tarball named backup.tar.gz:

```bash
sudo tar --exclude='apm_database' --exclude='revdebug_database' -czvf backup.tar.gz -C /var/revdebug/ .
```

#### Create a backup of the server configuration

Compress the contents of the revdebug-server-docker-compose directory into a tarball named configuration.tar.gz:

```bash
sudo tar -czvf configuration.tar.gz -C ../revdebug-server-docker-compose .
```

#### Create a backup of the databases

Compress the contents of the db directory into a tarball named db.tar.gz:

```bash
sudo tar -czvf db.tar.gz -C ../db .
```

After executing these commands, you should have three tarball files (backup.tar.gz, configuration.tar.gz, and db.tar.gz) containing the backups of the RevDeBug server data, configuration, and databases, respectively.

Please ensure that you have the necessary permissions to execute these commands and access the directories you want to back up.

### Transfer backup files to the new server&#x20;

Login to the new virtual machine.

#### If you already have a fresh instance of RevDeBug Server running, follow these steps to remove it data

Stop the RevDeBug server:

```bash
sudo docker compose -p revdebug down
```

Remove all RevDeBug data:

```bash
sudo rm -rf /var/revdebug
```

Remove the RevDeBug server configuration files (optional, but steps below restore those from the old server):

```bash
sudo rm -rf ./revdebug-server-docker-compose
```

#### Transfer backup files from the old server to the new server

Assuming you have access to both servers, use `scp` to securely copy the `BACKUP_FOLDER` from the old server to the `BACKUP` folder on the new server. Replace `YOUR_OLD_SERVER_ADDRESS`, `HOME`, and `USER` with appropriate values:

```bash
scp -r USER@YOUR_OLD_SERVER_ADDRESS:/home/USER/backup /home/USER/backup
```

### Unpack the backup files

#### Unpack the configuration backup (optional if you have a fresh RevDeBug Server configured already)

Create a new directory named revdebug-server-docker-compose and extract the contents of the configuration.tar.gz backup file inside it:

```bash
mkdir revdebug-server-docker-compose && sudo tar --same-owner -xzvf /home/USER/backup/configuration.tar.gz -C ./revdebug-server-docker-compose
```

#### Unpack the server data backup

Create a new directory at /var/revdebug and extract the contents of the backup.tar.gz backup file into it:

```bash
sudo mkdir /var/revdebug && sudo tar --same-owner -xzvf /home/USER/backup/backup.tar.gz -C /var/revdebug
```

#### Unpack the database backup

Extract the contents of the db.tar.gz backup file into the ./db directory:

```bash
sudo tar -xzvf /home/USER/backup/db.tar.gz -C ./db
```

Please ensure you replace YOUR\_OLD\_SERVER\_ADDRESS, HOME, and USER in step 5 with the appropriate values specific to your old server. Similarly, in step 6, ensure that you use the correct paths for unpacking the backups on your new server.

Always be cautious when performing operations that may modify or overwrite data on your server. Make sure to have backups and verify the paths before executing the commands.

### Set the given variables in the .env configuration file

Go to ./revdebug-server-docker-compose

Open the `.env` file using a text editor and set the `REVDEBUG_SERVER_NAME` variable to your new server address. For example:

```bash
REVDEBUG_SERVER_NAME=YOUR_SERVER_ADDRESS
```

Save the changes to the .env file.

#### Add path.repo configuration (optional if configuration is also moved from the old server)

In the `docker-compose.yml` file, add the following configuration to the `environment` section of the `opensearch` container:

<pre class="language-yaml"><code class="lang-yaml"><strong>environment:
</strong>  - path.repo=/usr/share/opensearch/snapshots
</code></pre>

### Start the containers with the databases

To start the containers for the databases (PostgreSQL and OpenSearch) in the background, use the following command:

```bash
sudo docker compose -p revdebug up -d postgres opensearch
```

This command will start the PostgreSQL and OpenSearch containers as specified in the docker-compose.yml file with the project name revdebug.

Please make sure you have the required permissions to execute Docker commands.

### Restore PostgreSQL database

#### Setting Up Environment Variables for PostgreSQL Login

If you are using the default variables to log in to RevDeBug PostgreSQL, you can use the following commands:

Change to the 'revdebug-server-docker-compose' directory:

```bash
cd ./revdebug-server-docker-compose
```

Export the environment variables for PostgreSQL login using the following commands:

```bash
export $(grep 'DB_.*:' docker-compose.yml | sed 's/.*{\(REVDEBUG_POSTGRES_.*\):-\(.*\)}/\1=\2/')
export $(eval cat .env | grep -v "^#")
```

These commands will set up the necessary environment variables for logging into PostgreSQL. Make sure you are in the correct directory containing the 'docker-compose.yml' file and the '.env' file before executing the commands.

#### Navigate to the uncompressed 'db' folder

Change to the directory where the database backup files are located:

```bash
cd ../db
```

#### Restore the Keycloak database using the following command:

```bash
sudo docker exec -i revdebug-postgres-1 sh -c 'PGPASSWORD="$REVDEBUG_POSTGRES_PASSWORD" psql -U $REVDEBUG_POSTGRES_USER keycloak' < /home/USER/db/pg-backup-keycloak.sql
```

#### Restore the revdebug\_db database using the following command:

```bash
sudo docker exec -i revdebug-postgres-1 sh -c 'PGPASSWORD="$REVDEBUG_POSTGRES_PASSWORD" psql -U $REVDEBUG_POSTGRES_USER revdebug_db' < /home/USER/db/pg-backup.sql
```

{% hint style="info" %}
Please make sure to replace /home/USER/db/pg-backup-keycloak.sql and /home/USER/db/pg-backup.sql with the correct paths to your PostgreSQL backup files.
{% endhint %}

These commands will restore the databases 'keycloak' and 'revdebug\_db' in the PostgreSQL container named 'revdebug-postgres-1'.

### Restore OpenSearch database

#### Copy the snapshot folder to the OpenSearch container

Copy the `snapshots` folder to the `OpenSearch` container using the following command:

```bash
sudo docker cp ./snapshots revdebug-opensearch-1:/usr/share/opensearch/
```

#### Enter the OpenSearch container as root

Access the OpenSearch container as the root user with the following command:

```bash
sudo docker exec -it --user root revdebug-opensearch-1 bash
```

#### Set the user 'opensearch' as the owner of the 'snapshots' folder

Change the owner of the snapshots folder to the 'opensearch' user using the following command inside the container:

```bash
chown -R opensearch:opensearch snapshots
```

#### Execute the request to create the snapshot repository

Issue the request to create the snapshot repository with the following command:

```bash
curl -X PUT localhost:9200/_snapshot/opensearch-snapshots --header 'Content-Type: application/json' --data '{ "type": "fs", "settings": { "location": "/usr/share/opensearch/snapshots" }}'
```

**Verify if the repository has been created**

Check if the repository has been successfully created using the following command:

```bash
curl -X GET localhost:9200/_cat/repositories
```

You can check the current structure of the database using the following command:

```bash
curl -X GET localhost:9200/_cat/indices
```

#### (Optional) Delete data from OpenSearch&#x20;

If needed, you can delete all data from OpenSearch using the following command:

```bash
curl -X DELETE localhost:9200/_all
```

#### Perform the restore using the request

Issue the request to perform the restore with the following command:

```bash
curl -X POST localhost:9200/_snapshot/opensearch-snapshots/snapshot-name/_restore --header 'Content-Type: application/json' --data '{"indices": "-.opendistro*"}'
```

{% hint style="info" %}
Note: Replace snapshot-name in the previous step with a name assigned when doing the snapshot during backup or leave it as is if you have used the default.
{% endhint %}

**Verify the database structure after the restore**

You can check the structure of the database after the restore using the following command:

```bash
curl -X GET localhost:9200/_cat/indices
```

### Start all RevDeBug Server containers

#### Navigate to the 'revdebug-server-docker-compose' directory

Change to the 'revdebug-server-docker-compose' directory using the following command:

```bash
cd ../revdebug-server-docker-compose
```

#### Start all containers in the project

To start all the containers defined in the docker-compose.yml file, run the following command:

```bash
sudo docker compose -p revdebug up -d
```

The -p option specifies the project name, and in this case, it's set to revdebug. The -d option runs the containers in the background (detached mode).

After running this command migrated RevDeBug Server should get running.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://revdebug.gitbook.io/revdebug/installing-revdebug-server/migrate-revdebug-server.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
