﻿# Backup and restore

## Backup

The contacts are saved in Mongo database. The data files of the database are stored in `/data/mongo` directory on the host computer. This means that data are not lost if you remove mongo container. However, you should not back up these files by simply copying them while mongo is running unless your system provides instantaneous snapshots. To make regular backups of the database, we recommend you run the following command regularly. It will create a timestamped directory with database content inside of `/data/mongo/dump` which you can then backup normally.

```bash
docker exec contacts_mongo_1 mongodump -o /data/db/dump/$(date -I)
```

`contacts_mongo_1` is the name of contacts container that was created when running docker-compose.

Besides these data you should also backup files located in `/etc/acrobits`.

## Restore

Place your directory with backup (we will use name `mybackup` in this example) into `/data/mongo/dump`. There should be a directory `/data/mongo/dump/mybackup/contact_server/` containing several `.bson` files (and possibly others).

Stop container `contacts_contact_server_1`.

```bash
docker stop contacts_contact_server_1
```

And run the following commands:

```bash
docker exec -i contacts_mongo_1 mongorestore --drop /data/db/dump/mybackup
```

!!! note

    If restore fails with error `Failed: contact_server.contactBlock: error creating indexes for contact_server.contactBlock: createIndex error: WiredTigerIndex::insert: key too large to index` run the following command and try restoring again.

```bash
echo "db.getSiblingDB('admin').runCommand( { setParameter: 1, failIndexKeyTooLong: false } )" | docker exec -i contacts_mongo_1 mongo
```

