Skip to content

Instantly share code, notes, and snippets.

@gwbischof
Last active February 22, 2021 20:43
Show Gist options
  • Save gwbischof/a09520cd519c3d0131dd5af5f4742ad8 to your computer and use it in GitHub Desktop.
Save gwbischof/a09520cd519c3d0131dd5af5f4742ad8 to your computer and use it in GitHub Desktop.
mongo_backup_cron
#!/bin/bash
# ------------------------------------------------------------------
# Simple Backup Script for MongoDB
# ------------------------------------------------------------------
# Definitions
MONGO_HOST='localhost'
MONGO_PORT="27017"
DUMP_PATH='/backup/mongo_backup'
DAYS_OF_BACKUPS=2
# Timestamp for filename
TIMESTAMP=`date +%F--%H-%M-%S`
# Ensure the dump dir exists or exit with error
if [ -z $DUMP_PATH ]; then
echo "ERROR!!! mongo DUMP_PATH can not be an empty string! exiting mongo_backup!"
exit -1
fi
if [ ! -d $DUMP_PATH ]; then
echo "WARNING!!! mongo dump dir ($DUMP_PATH) doesn't exist! attempting to create..."
if ! mkdir -p $DUMP_PATH; then
echo "ERROR!!! mongo dump dir ($DUMP_PATH) doesn't exist and couldn't be created! exiting mongo_backup!"
exit -1
fi
fi
echo "Running Mongo Dump Command"
# Dump Command
mongodump --host $MONGO_HOST:$MONGO_PORT --out ${DUMP_PATH}/$TIMESTAMP --gzip
# Cleanup backups older than DAYS_OF_BACKUPS.
find ${DUMP_PATH} -mtime +${DAYS_OF_BACKUPS} -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment