Skip to content

Instantly share code, notes, and snippets.

@erans
Last active October 7, 2019 16:59
Show Gist options
  • Save erans/ce21c919921608d064cd to your computer and use it in GitHub Desktop.
Save erans/ce21c919921608d064cd to your computer and use it in GitHub Desktop.

Revisions

  1. erans revised this gist Oct 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongobackup.sh
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ fi
    CURRENT_DATE=`date +"%Y%m%d-%H%M"`

    # Backup filename
    BACKUP_FILENAME="$DB_NAME_$CURRENT_DATE.tar.gz"
    BACKUP_FILENAME="${DB_NAME}_${CURRENT_DATE}.tar.gz"

    # Create the backup
    mongodump --db $DB_NAME -o $BACKUP_PATH
  2. erans revised this gist Jan 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongobackup.sh
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ then
    exit 2
    fi

    CURRENT_DATE=`date +"%Y%m%d-%H%m"`
    CURRENT_DATE=`date +"%Y%m%d-%H%M"`

    # Backup filename
    BACKUP_FILENAME="$DB_NAME_$CURRENT_DATE.tar.gz"
  3. erans created this gist Sep 5, 2014.
    37 changes: 37 additions & 0 deletions mongobackup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # Path to boto config file, needed by gsutils
    BOTO_CONFIG="/etc/boto.cfg"

    # Path in which to create the backup (will get cleaned later)
    BACKUP_PATH="/mnt/data/dump/"

    # DB name
    DB_NAME="mydatabase"

    # Google Cloud Storage Bucket Name
    BUCKET_NAME="mybackupbucket"

    # Don't run on the master of a replica set, only on replicas
    IS_MASTER=`mongo --quiet --eval "d=db.isMaster(); print( d['ismaster'] );"`
    if [ "$IS_MASTER" == "true" ]
    then
    exit 2
    fi

    CURRENT_DATE=`date +"%Y%m%d-%H%m"`

    # Backup filename
    BACKUP_FILENAME="$DB_NAME_$CURRENT_DATE.tar.gz"

    # Create the backup
    mongodump --db $DB_NAME -o $BACKUP_PATH
    cd $BACKUP_PATH

    # Archive and compress
    tar -cvzf $BACKUP_PATH$BACKUP_FILENAME *

    # Copy to Google Cloud Storage
    echo "Copying $BACKUP_PATH$BACKUP_FILENAME to gs://$BUCKET_NAME/"
    /usr/local/bin/gsutil cp $BACKUP_PATH$BACKUP_FILENAME gs://$BUCKET_NAME/ 2>&1
    echo "Copying finished"
    echo "Removing backup data"
    rm -rf $BACKUP_PATH*