Instantly share code, notes, and snippets.

@hamxiaoz /crontab
Last active Mar 8, 2016

Embed
What would you like to do?
Use git to backup MongoDB for Meteor
# run every day at 12am
00 00 * * * /home/x/run_backup.sh
#!/bin/bash
MONGO_DATABASE="X"
APP_NAME="X"
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="/home/X/backups/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
GIT_PATH="/usr/bin/git"
# mongo admin --eval "printjson(db.fsyncLock())"
# $MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
$MONGODUMP_PATH -d $MONGO_DATABASE
# mongo admin --eval "printjson(db.fsyncUnlock())"
# dump the database and zip it (remove the dump folder afterwards)
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME
# commit the new backup
cd $BACKUPS_DIR
$GIT_PATH add .
$GIT_PATH commit -m "backup from `date +'%Y-%m-%d'`"
$GIT_PATH push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment