> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Elasticsearch Version Upgrade

> Step-by-step guide to upgrade Elasticsearch from 7.9.2 to 7.17.29 on Ubuntu server. Ubuntu 24.04 LTS operating system is recommended.

## Pre-Installation Checks

<Warning>
  **Important**

  To protect against potential system or user errors during the upgrade, snapshots (backups) should be taken on the relevant servers before starting the process.
</Warning>

<Info>
  **Elasticsearch installation file:**

  [https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.29-linux-x86\_64.tar.gz](https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.29-linux-x86_64.tar.gz)

  Note: Access to this address is not mandatory. If the server does not have external internet access, the file can be downloaded from a different environment and manually transferred to the server.
</Info>

## Downloading and Preparing New Elasticsearch Version

```bash theme={null}
# The following steps should be performed on the Elasticsearch server to be updated.

# Switch to elasticsearch user to continue operations
sudo su - elasticsearch
cd /opt/elasticsearch

# Download the new Elasticsearch version
sudo wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.29-linux-x86_64.tar.gz

# Extract the downloaded file
sudo tar -xzf elasticsearch-7.17.29-linux-x86_64.tar.gz

# Set file ownership and permissions
sudo chown -Rf elasticsearch:elasticsearch /opt/elasticsearch
sudo chmod -Rf 775 /opt/elasticsearch
```

## Configuring Elasticsearch Parameters for the Environment

### 1. Copying Configuration File

You need to copy the information from the existing `elasticsearch.yml` file to the new version.

**View the current configuration:**

```bash theme={null}
vi /opt/elasticsearch/elasticsearch-7.9.2/config/elasticsearch.yml
```

**Edit the new version configuration file:**

```bash theme={null}
vi /opt/elasticsearch/elasticsearch-7.17.29/config/elasticsearch.yml
```

Copy the content from the existing configuration file to the new file.

<Info>
  **Certificate Files (Optional)**

  If your existing Elasticsearch installation uses SSL/TLS certificates, you need to copy these certificates to the new version.

  ```bash theme={null}
    # Check the certificate directory
    ls -la /opt/elasticsearch/elasticsearch-7.9.2/config/certs/

    # If certificate files exist, copy them to the new version
    sudo cp -r /opt/elasticsearch/elasticsearch-7.9.2/config/certs /opt/elasticsearch/elasticsearch-7.17.29/config/

    # Set file ownership
    sudo chown -Rf elasticsearch:elasticsearch /opt/elasticsearch/elasticsearch-7.17.29/config/certs
  ```
</Info>

### 2. Configuring JVM Parameters

You should configure the JVM (Java Virtual Machine) values and other JVM parameters that Elasticsearch will use according to the existing elasticsearch settings.

**View the current JVM settings:**

```bash theme={null}
sudo vi /opt/elasticsearch/elasticsearch-7.9.2/config/jvm.options
```

**Edit the new version JVM settings:**

```bash theme={null}
sudo vi /opt/elasticsearch/elasticsearch-7.17.29/config/jvm.options
```

<Warning>
  **Important**

  The value can be set up to half of the operating system's RAM and should not exceed 32GB. It should be entered as follows according to the RAM value used by the existing elasticsearch.
</Warning>

```
-Xms8g
-Xmx8g
```

## Setting Up Elasticsearch as a Linux Service

### 1. Creating New Service Script File

Create the service script file for the new Elasticsearch version:

```bash theme={null}
sudo vi /opt/elasticsearch/elasticsearch-7.17.29/bin/elasticsearch-service.sh
```

Paste the following content into the file:

```bash theme={null}
#!/bin/sh
SERVICE_NAME=elasticsearch
PATH_TO_APP="/opt/elasticsearch/elasticsearch-7.17.29/bin/$SERVICE_NAME"
PID_PATH_NAME="/opt/elasticsearch/elasticsearch-7.17.29/bin/$SERVICE_NAME.pid"
SCRIPTNAME=elasticsearch-service.sh
ES_USER=$SERVICE_NAME
ES_GROUP=$SERVICE_NAME

case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            mkdir $(dirname $PID_PATH_NAME) > /dev/null 2>&1 || true
            chown $ES_USER $(dirname $PID_PATH_NAME)
            $SUDO $PATH_TO_APP -d -p $PID_PATH_NAME
            echo "Return code: $?"
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ..."
            kill -15 $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill -15 $PID;
            sleep 1;
            echo "$SERVICE_NAME stopped ...";
            rm -rf $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            mkdir $(dirname $PID_PATH_NAME) > /dev/null 2>&1 || true
            chown $ES_USER $(dirname $PID_PATH_NAME)
            $SUDO $PATH_TO_APP -d -p $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
        exit 3
    ;;
esac
```

Grant execute permission to the script file:

```bash theme={null}
sudo chmod +x /opt/elasticsearch/elasticsearch-7.17.29/bin/elasticsearch-service.sh
sudo chmod -Rf 775 /opt/elasticsearch/elasticsearch-7.17.29/*
```

### 2. Updating Systemd Service File

First, stop the current Elasticsearch service:

```bash theme={null}
sudo systemctl stop elasticsearch
```

Update the systemd service file for the new version:

```bash theme={null}
sudo vi /etc/systemd/system/elasticsearch.service
```

Write the following content to the file:

```ini theme={null}
[Unit]
Description=ElasticSearch Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
ExecStart=/opt/elasticsearch/elasticsearch-7.17.29/bin/elasticsearch-service.sh start
ExecStop=/opt/elasticsearch/elasticsearch-7.17.29/bin/elasticsearch-service.sh stop
ExecReload=/opt/elasticsearch/elasticsearch-7.17.29/bin/elasticsearch-service.sh restart
LimitNOFILE=65536
LimitMEMLOCK=infinity
User=elasticsearch
```

### 3. Starting New Elasticsearch Service

```bash theme={null}
# Reload the systemd daemon
sudo systemctl daemon-reload

# Start the new Elasticsearch service
sudo systemctl start elasticsearch

# Check service status
sudo systemctl status elasticsearch

# Enable to start automatically on system boot
sudo systemctl enable elasticsearch
```
