Skip to content

Instantly share code, notes, and snippets.

@hiddentao
Last active January 15, 2016 02:32
Show Gist options
  • Save hiddentao/d642a16c7b04b79e584a to your computer and use it in GitHub Desktop.
Save hiddentao/d642a16c7b04b79e584a to your computer and use it in GitHub Desktop.
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