Filebase
Search…
MongoDB
Learn how to created automated backups of your MongoDB databases to be stored on Filebase.

What is MongoDB?

MongoDB is a cross platform database program that uses JSON-like documents. MongoDB is open source and free to download and self-host on any server.
Read below to learn how to set up automated backups of your MongoDB databases to be stored in a Filebase bucket.
This guide was written using Ubuntu 20.04. Commands and workflow may vary depending on your operating system.

Prerequisites:

1. Copy the following script onto your server and save it as mongodb_backup.sh.

Edit the values to reflect your local configuration.
1
#!/bin/bash
2
3
# Your User’s home directory
4
export HOME=/home/filebase/
5
6
# Your server's hostname
7
HOST=localhost
8
9
# Your database name to be backed up
10
DBNAME=database-name
11
12
# S3 bucket name
13
BUCKET=filebase-sample-bucket
14
15
# Linux user account
16
USER=filebase
17
18
# Current time
19
TIME=`/bin/date +%d-%m-%Y-%T`
20
21
# Backup directory
22
DEST=/home/$USER/tmp
23
24
# Tar file of backup directory
25
TAR=$DEST/../$TIME.tar
26
27
# Create backup dir (-p to avoid warning if it already exists)
28
/bin/mkdir -p $DEST
29
30
# Log
31
echo "Backing up $HOST/$DBNAME to s3://$BUCKET/ on $TIME";
32
33
# Dump from mongodb host into backup directory
34
/usr/bin/mongodump -h $HOST -d $DBNAME -o $DEST
35
36
# Create tar of backup directory
37
/bin/tar cvf $TAR -C $DEST .
38
39
# Upload tar to s3
40
/usr/bin/aws --endpoint <https://s3.filebase.com> s3 cp $TAR s3://$BUCKET/ --storage-class STANDARD_IA
41
42
# Remove tar file locally
43
/bin/rm -f $TAR
44
45
# Remove backup directory
46
/bin/rm -rf $DEST
47
48
# Confirm success
49
echo "Backup of MongoDB databases to Filebase bucket $BUCKET completed successfully. "
Copied!
This script uses the mongodbdump function to create a backup dump of all MongoDB databases and stores it to a tar file that is then uploaded to Filebase using an AWS CLI command.

2. Run the script to test that your configuration works as expected.

It should return the following output:
In your Filebase bucket, you should see the tar file uploaded:

3. Set up automation of this script using a cronjob.

Open your crontab with the following command:
crontab -e

4. Add the following line to your crontab to create a cronjob that runs your mongodb_backup.sh script:

0 8 * * * /path/to/mongodb_backup.sh
Replace the file path with the correct path for your configuration.
This cronjob is scheduled to run every day at 8:00AM on your server. Configure the cronjob values to reflect your desired configuration.
You’ve now configured automated backups for your MongoDB databases!
If you have any questions, please send us an email at [email protected].
Last modified 11d ago