> ## 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.

# MongoDB Backup and Restore

> Apinizer platform stores all configurations in MongoDB database and the database needs to be backed up regularly. Platform can perform backup and restore operations with mongodump and mongorestore commands and works with Replica Set MongoDB database.

## Backup

<Warning>
  Apinizer works with Replica Set MongoDB database. This situation should be taken into account when taking backup from MongoDB server.
</Warning>

```bash mongodump theme={null}
mongodump \
  --host=<PRIMARY_MONGODB_IP> \
  --port=<PRIMARY_MONGODB_PORT> \
  --authenticationDatabase admin \
  --username apinizer \
  --password <MONGO_PASSWORD> \
  --db <DATABASE_NAME> \
  --gzip \
  --archive=/home/apinizer/apinizer-backup--v<CURRENT_VERSION>--<BACKUP_DATE>--01.archive
```

<Info>
  It is recommended to use the **date** when backup was taken and **Apinizer version information** in backup file naming. This way:

  * It is easy to understand which backup belongs to which system version
  * Version incompatibilities are prevented in restore operations
  * Management of archived backups is facilitated
</Info>

<Warning>
  **If application logs or audit logs that may take up high space are not desired to be included, they can be separated with this command:**

  `--excludeCollection apinizer_log --excludeCollection audit_event`

  However, this operation will lead to data not being backed up, so it is not recommended.
</Warning>

You can review the [MongoDB documentation](https://docs.mongodb.com/manual/reference/program/mongodump/) for detailed information.

***

## Restore

To restore Apinizer configuration data from backup, the backup files you created are used. This operation is performed with `mongorestore` command on Linux shell.

### Basic Restore Command

The previously taken backup file can be restored using the following command:

```bash mongorestore theme={null}
mongorestore \
  --drop \
  --host=<PRIMARY_MONGODB_IP> \
  --port=<PRIMARY_MONGODB_PORT> \
  --username=apinizer \
  --authenticationDatabase=admin \
  --gzip \
  --archive=/home/apinizer/apinizer-backup-v<CURRENT_VERSION>--<BACKUP_DATE>--01.archive
```

<Warning>
  When `--drop` parameter is used, existing collections are deleted and recreated. This operation cannot be undone, so it should be used carefully.
</Warning>

### Restore to Replica Set

Example command that restores to entire replica set without targeting Primary MongoDB server:

```bash mongorestore theme={null}
mongorestore \
  --drop \
  --host apinizer-replicaset/10.0.0.1:25080,10.0.0.2:25080,10.0.0.3:25080 \
  --username apinizer \
  --password "123456" \
  --gzip \
  --archive=/home/apinizer/apinizer-backup-v2024092--20241231--01.archive
```

### Single Collection Restore

If only a single collection is backed up, not the entire database, to restore it:

```bash mongorestore theme={null}
mongorestore \
  --drop \
  --host 10.0.0.1 \
  --port 25080 \
  --authenticationDatabase "admin" \
  -d apinizerdb \
  -u apinizer \
  -p "123456" \
  --collection=api_proxy \
  /home/apinizer/apinizerdb/api_proxy.bson
```

## Detailed Information

You can review the [MongoDB documentation](https://docs.mongodb.com/manual/reference/program/mongodump/) for detailed information.
