MongoDB
Learn how to created automated backups of your MongoDB databases to be stored on Filebase.
What is MongoDB?
1. Copy the following script onto your server and save it as mongodb_backup.sh.
mongodb_backup.sh.#!/bin/bash
# Your User’s home directory
export HOME=/home/filebase/
# Your server's hostname
HOST=localhost
# Your database name to be backed up
DBNAME=database-name
# S3 bucket name
BUCKET=filebase-sample-bucket
# Linux user account
USER=filebase
# Current time
TIME=`/bin/date +%d-%m-%Y-%T`
# Backup directory
DEST=/home/$USER/tmp
# Tar file of backup directory
TAR=$DEST/../$TIME.tar
# Create backup dir (-p to avoid warning if it already exists)
/bin/mkdir -p $DEST
# Log
echo "Backing up $HOST/$DBNAME to s3://$BUCKET/ on $TIME";
# Dump from mongodb host into backup directory
/usr/bin/mongodump -h $HOST -d $DBNAME -o $DEST
# Create tar of backup directory
/bin/tar cvf $TAR -C $DEST .
# Upload tar to s3
/usr/bin/aws --endpoint https://s3.filebase.com s3 cp $TAR s3://$BUCKET/ --storage-class STANDARD_IA
# Remove tar file locally
/bin/rm -f $TAR
# Remove backup directory
/bin/rm -rf $DEST
# Confirm success
echo "Backup of MongoDB databases to Filebase bucket $BUCKET completed successfully. "2. Run the script to test that your configuration works as expected.


3. Set up automation of this script using a cronjob.
4. Add the following line to your crontab to create a cronjob that runs your mongodb_backup.sh script:

Last updated