# Database Backup and Recovery

From time to time, data is backed up to a secondary database to avoid total corruption or loss of data. For now, data can be backed up on a local database in a development environment. This is done with the use of two MongoDB database containers, docker-compose.yml, the mongodump and mongorestore commands, and also by installing the MongoDB server and its database tools in the Dockerfile.

Sub-processes were used to run these commands in bash scripts with the help of a cron job (for backup) and an endpoint (for restoration). MongoDB Compass can be used to view and manage the databases graphically.

```docker
# backend/Dockerfile

FROM node:17-alpine As development

# setting the image working directory which the backend folder of the root directory
WORKDIR /usr/src/app/backend

# copy package.json and package-lock.json to the image working directory
COPY package*.json ./

# install tools for mongodump and mongorestore commands
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/main' >> /etc/apk/repositories
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/community' >> /etc/apk/repositories
RUN apk update
RUN apk add mongodb
RUN apk add mongodb-tools


# install development dependencies
RUN npm install --only=development --force


# copy the application code to the working directory of image
COPY . .

# EXPOSE 3000

#  build the application
RUN npm run build
```

```yaml
# docker-compose.yml

version: '3.7'

services:

# Backend service for Elixir.io
  backend:
    container_name: elixir_backend
    build: 
      context: ./backend
      target: development
    ports:
      - 9229:9229
      - 3000:3000
    command: npm run start:dev
    environment:
      JWT_CONSTANT_SECRET: ${JWT_CONSTANT_SECRET}
      S3_ADMINISTRATOR_ACCESS_KEY_ID: ${S3_ADMINISTRATOR_ACCESS_KEY_ID}
      S3_ADMINISTRATOR_SECRET_ACCESS_KEY: ${S3_ADMINISTRATOR_SECRET_ACCESS_KEY}
      S3_BUCKET: ${S3_BUCKET}
      NODE_ENV: development
    volumes:
      - .:/usr/src/app/ # mounting the directory inside the container
      - /usr/src/app/backend/node_modules # mounting node_modules inside the backend container
    env_file:
      - .env
    depends_on:
      - mongo

# MongoDB service for Elixir.io
  mongo:
    container_name: elixir_mongodb
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_DATABASE: ${MONGO_DATABASE}
      MONGO_INITDB_ROOT_USERNAME: ${MONGOROOTUSERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGOROOTPASSWD}
    
    volumes:
      - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo-js:ro
      - ./mongo-volume:/data/db
    ports: 
      - '27017:27017'


  mongo-backup:
    container_name: elixir_mongodb_backup
    image: mongo
    restart: always

    environment:
      MONGO_INITDB_DATABASE: ${MONGO_DATABASE}
      MONGO_INITDB_ROOT_USERNAME: ${MONGOROOTUSERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGOROOTPASSWD}

    volumes:
      - ./mongo-backup-volume:/data/db
    ports: 
      - '27021:27017'
```


---

# 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://the-bubble.gitbook.io/elixir.io-documentation/components-and-features/database-backup-and-recovery.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.
