> For the complete documentation index, see [llms.txt](https://docs.enginsight.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.enginsight.com/docs/knowledge-base/english/database/how-do-i-create-a-backup-of-one-or-more-organizations-in-enginsight.md).

# How do I create a backup of one or more organizations in Enginsight?

This article explains how to create a backup of one or more organizations in Enginsight. To do so, follow these steps:

{% stepper %}
{% step %}

### Log in to the Database Server

Log in to the **Enginsight Database Server**.&#x20;
{% endstep %}

{% step %}

### Create a directory for the backup script and the backup base folder

1. Use the following command to create a directory where you want to store your backup script, for example `/opt/enginsight/scripts`:

```
sudo mkdir -p /opt/enginsight/<ScriptFolderName>
cd /opt/enginsight/<ScriptFolderName>
```

{% hint style="info" %}
Remember to replace `<ScriptFolderName>` accordingly.
{% endhint %}

2. Use the following command to create a dedicated backup destination path with a backup base folder, for example `/var/backups/enginsight/orga_backup`:

```
sudo mkdir -p /var/backups/enginsight/<BackupBaseFolder>
```

{% hint style="info" %}
Remember to replace `<BackupBaseFolder>` accordingly.
{% endhint %}
{% endstep %}

{% step %}

### Create and save the script file

1. Use the following command to create a backup script file:

```
sudo nano /opt/enginsight/<ScriptFolderName>/<ScriptFileName>.sh
```

{% hint style="info" %}
Remember to replace `<ScriptFolderName>` and `<ScriptFileName>` accordingly.
{% endhint %}

2. Insert the following script template and replace the placeholders in `<>` accordingly:

<pre><code>#!/usr/bin/env bash

set -euo pipefail

# 1) Organization(s): Enter one or more organization IDs (without spaces or separators within the IDs), e.g. ("OrganizationID1" "OrganizationID2")
<strong>ORG_IDS=( "&#x3C;OrganizationID1>" )
</strong>
# 2) Backup base folder
<strong>BACKUP_BASE="/var/backups/enginsight/&#x3C;BackupBaseFolder>"
</strong>
# Optional: Timestamp for each run
TS="$(date +%F_%H%M%S)"
RUN_DIR="${BACKUP_BASE}/${TS}"

# Optional: If your Mongo CLI/connection requires parameters, you can add them here.
# Default (local on the Database Server, no authentication, no TLS encryption):
MONGO_EXPORT_BASE=(mongoexport --db "enginsight")
MONGO_SHELL_BASE=(mongo "enginsight" --quiet)

# Examples (ONLY if required) – add username/password/TLS as appropriate:
<strong># MONGO_EXPORT_BASE=(mongoexport --host "rs0/&#x3C;DatabaseServerIPAddress>:27017" --username "mongoDefaultUser" --password "$(&#x3C;/etc/enginsight/mongoDefaultUserPassword)" --authenticationDatabase "enginsight" --ssl --sslCAFile /etc/enginsight/ssl/mongodbCA.crt --sslPEMKeyFile /etc/enginsight/ssl/mongodb.pem --db "enginsight")
</strong># MONGO_SHELL_BASE=(mongo "enginsight" --quiet)  # add host/auth/TLS here as well if required

# === Checks ===
command -v mongoexport >/dev/null 2>&#x26;1 || { echo "mongoexport nicht gefunden"; exit 1; }
command -v mongo >/dev/null 2>&#x26;1 || { echo "mongo (Shell) nicht gefunden"; exit 1; }

mkdir -p "$RUN_DIR"

for ORG_ID in "${ORG_IDS[@]}"; do
  OUT_DIR="${RUN_DIR}/${ORG_ID}"
  mkdir -p "$OUT_DIR"
  cd "$OUT_DIR"

  # Determine all collections and export each collection using the 'organisation' filter
  echo "show collections" | "${MONGO_SHELL_BASE[@]}" \
    | xargs -I {} "${MONGO_EXPORT_BASE[@]}" --collection {} \
        --query '{"organisation":{"$eq":{"$oid":"'"$ORG_ID"'"}}}' \
        --out {}.json
  # In addition, back up the organization itself from the 'organisations' collection
  "${MONGO_EXPORT_BASE[@]}" --collection organisations \
    --query '{"_id":{"$eq":{"$oid":"'"$ORG_ID"'"}}}' \
    --out organisations.json

  cd - >/dev/null
done
</code></pre>

{% hint style="info" %}
Remember to replace the following placeholders in `<>` accordingly:

* `<OrganizationIDx>`
* `<BackupBaseFolder>`
* `<DatabaseServerIPAddress>`
  {% endhint %}

3. Save the file (**Ctrl** + **o**) and confirm the save process. Close the file (**Ctrl** + **x**).
4. Enter the following command to make the file executable:

```
sudo chmod +x /opt/enginsight/<ScriptFolderName>/<ScriptFileName>.sh
```

{% hint style="info" %}
Remember to replace `<ScriptFolderName>` and `<ScriptFileName>` accordingly.
{% endhint %}
{% endstep %}

{% step %}

### Check the backup script

1. Use the following command to check whether you have entered the correct organization IDs and paths:

```
sudo head -n 60 /opt/enginsight/<ScriptFolderName>/<ScriptFileName>.sh
```

{% hint style="info" %}
Remember to replace `<ScriptFolderName>` and `<ScriptFileName>` accordingly.
{% endhint %}

2. **Optional**: Run ShellCheck with the following command if you have this tool installed:

```
shellcheck /opt/enginsight/<ScriptFolderName>/<ScriptFileName>.sh
```

{% hint style="info" %}
Remember to replace `<ScriptFolderName>` and `<ScriptFileName>` accordingly.
{% endhint %}
{% endstep %}

{% step %}

### Run the backup script and check the result

Now run the backup script with the following command:

```
sudo /opt/enginsight/<ScriptFolderName>/<ScriptFileName>.sh
```

{% hint style="info" %}
Remember to replace `<ScriptFolderName>` and `<ScriptFileName>` accordingly.
{% endhint %}

You can check the result, for example, as follows:

```
ls -la /var/backups/enginsight/<BackupBaseFolder>
find /var/backups/enginsight/<BackupBaseFolder> -maxdepth 3 -type f | head
```

{% hint style="info" %}
Remember to replace `<BackupBaseFolder>` accordingly.
{% endhint %}
{% endstep %}
{% endstepper %}

***
