Skip to content

Instantly share code, notes, and snippets.

@hiddentao
Last active January 15, 2016 02:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Cron backup script for Mongo db that deletes backups older than 14 days
#!/bin/bash
MONGO_DATABASE="name here"
APP_NAME="name of app"
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="/opt/backup/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
$MONGODUMP_PATH -d $MONGO_DATABASE
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME
find $BACKUPS_DIR -name "*.tgz" -mtime +14 -exec rm {} \; >> $BACKUPS_DIR/purge.log 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment