# MongoDB Backup/Restore

Script automates the process of creating a backup of a MongoDB database in a Docker container, copying the backup to the host machine, and cleaning up temporary files, with an optional notification at the end

### Backup Script

1. #### Creating a Backup Directory

```
mkdir /path/to/backups
```

#### How to Perform a MongoDB Backup from a Docker Container

This guide provides step-by-step instructions on how to backup a MongoDB database that is running within a Docker container. The script automates this process, including exporting the backup to a host machine and clearing temporary files.

**Prerequisites**

* Docker daemon running
* MongoDB running within a Docker container
* Access to a terminal or command prompt

**Step-by-Step Guide**

1. **Create a Backup Directory on Host** Ensure there is a directory on the host machine where the backup will be stored. If not, create it using:

   ```sh
   mkdir /path/to/backups
   ```

   Replace `/path/to/backups` with your desired directory path.
2. **Set Parameters** Configure the script with your MongoDB Docker container name, backup directory, MongoDB connection details (if authentication is needed), and a timestamp for the backup filename.
3. **Execute Backup** The script uses `mongodump` to create a backup of the MongoDB database within the Docker container. Replace the placeholders with your MongoDB's specific details.
4. **Copy Backup to Host** The created backup is then transferred from the Docker container to the specified backup directory on the host machine.
5. **Cleanup** Temporary files created during the backup process within the Docker container are removed to free up space.
6. **Optional: Send Notification** A notification can be sent to a specified endpoint to signify the completion of the backup process, providing details like the backup location.

**Automation and Considerations**

* **Automation with Cron**: To automate this backup process, a cron job can be created. It's important to remove the `-it` option from `docker exec` commands for compatibility with cron, since cron jobs do not have an interactive shell.
* **Security**: Ensure proper security measures are in place when dealing with backups, especially if they contain sensitive information. Secure the backup directory and use secured connections when transferring the backup.
* **Restoration**: Know the process for restoring from the backup if needed, which typically involves using `mongorestore` and pointing it to the backup file location.

#### Backup Script

<pre class="language-sh"><code class="lang-sh"># MongoDB Docker Container Name
DOCKER_CONTAINER_NAME="mongodb"

# Backup Destination Directory
BACKUP_DIR="/home/nyka/backup"

<strong># MongoDB Connection Details
</strong>MONGO_HOST="localhost"  # Replace with your MongoDB host if not running on localhost
MONGO_PORT="27017"
MONGO_USERNAME=""
MONGO_PASSWORD=""

# Timestamp for Backup
TIMESTAMP=$(date +%Y%m%d)

# Backup Command
docker exec -it ${DOCKER_CONTAINER_NAME} mongodump \
  --host=${MONGO_HOST} \
  --port=${MONGO_PORT} \
  --username=${MONGO_USERNAME} \
  --password=${MONGO_PASSWORD} \
  --out=/tmp/mongodump-${TIMESTAMP}

# Copy Backup from Container to Host
docker cp ${DOCKER_CONTAINER_NAME}:/tmp/mongodump-${TIMESTAMP} ${BACKUP_DIR}

# Clean up temporary backup files inside the container
docker exec -it ${DOCKER_CONTAINER_NAME} rm -rf /tmp/mongodump-${TIMESTAMP}

echo "Backup completed successfully. The backup is stored in ${BACKUP_DIR}/mongodump-${TIMESTAMP}"

# Send Notification to Ntfy
curl -H "Tags: package, MongoDB, Daily Backup" -H "X-Title: MongoDB Daily Backup" -d "Backup completed successfully. The backup is stored in >

</code></pre>

**MongoDB Backup Script Summary**

This script automates the backup process for a MongoDB database running in a Docker container. Here's how it works:

1. **Setting Variables**: It defines necessary variables including the Docker container name, backup directory, MongoDB connection details, and a timestamp.
2. **Executing Backup**: Performs a `mongodump` within the specified Docker container targeting the MongoDB host and port, using optional authentication if provided.
3. **Copying Backup to Host**: Transfers the backup from the Docker container to the host machine's specified backup directory.
4. **Cleanup**: Removes the temporary backup files from within the container after copying.
5. **Notification**: Sends a notification with the backup completion status and location using `curl` to a specified endpoint.

**Note**: For automating this script with cron jobs, it's recommended to remove the `-it` option from `docker exec` commands to avoid issues, as cron jobs do not have an interactive shell.

####
