Mongodump with timestamp

2,409 views
Skip to first unread message

Robert Bell - Limeworks

unread,
Sep 26, 2011, 9:32:36 PM9/26/11
to mongodb-user
Hi Everyone,

I want to setup a simple cron to backup my database every day, into a
timestamped directory name.

I have tried something like : mongodump -u USERNAME -p PASSWORD -o /
backupdir/_%Y-%m-%d/

But it just explicitely names the directory.
Is there any way to have this conversion take place?

Sorry if this is a command line question and not a mongodump question.

Thanks,
Rob

Anthony Ortenzi

unread,
Sep 26, 2011, 11:40:53 PM9/26/11
to mongodb-user
Replace the %Y-%m-%d with `date "+%Y-%m-%d"`

(those are backticks surrounding the date command with the format
specifier you gave)

-Anthony

On Sep 26, 9:32 pm, Robert Bell - Limeworks <r...@limeworks.com.au>
wrote:

Robert Bell - Limeworks

unread,
Sep 27, 2011, 5:17:27 AM9/27/11
to mongodb-user
Thanks Anthony - i knew it was something simple i was doing wrong.
That worked a treat and a setup a cron to give me regular backups :D

Rob

Tim Hawkins

unread,
Oct 5, 2011, 8:37:27 PM10/5/11
to mongod...@googlegroups.com
#!/bin/sh

######################################a
# Edit These to define source and destinations

MONGO_DBS=""
BACKUP_TMP=/tmp
BACKUP_DEST=~/backup
######################################

BACKUPFILE_DATE=`date +%Y%m%d-%H%M`

# _do_store_archive <Database> <Dump_dir> <Dest_Dir> <Dest_file> 

function _do_store_archive {
mkdir -p $3
        cd $2
        tar -cvzf $3/$4 dump
}

# _do_backup <Database name>

function _do_backup {
UNIQ_DIR="$BACKUP_TMP/$1"`date "+%s"`
        mkdir -p $UNIQ_DIR/dump
        echo "dumping Mongo Database $1"
if [ "all" = "$1" ]; then
mongodump -o $UNIQ_DIR/dump
else
mongodump -d $1 -o $UNIQ_DIR/dump
fi
        KEY="database-$BACKUPFILE_DATE.tgz"
        echo "Archiving Mongo database to $BACKUP_DEST/$1/$KEY"
DEST_DIR=$BACKUP_DEST/$1

        _do_store_archive $1 $UNIQ_DIR $DEST_DIR $KEY
        
rm -rf $UNIQ_DIR
}

# check to see if individual databases have been specified, otherwise backup the whole server
#  to "all" 

if [ "" = "$MONGO_DBS"  ]; then
MONGO_DB="all"
        _do_backup $MONGO_DB
else 
for MONGO_DB in $MONGO_DBS; do
_do_backup $MONGO_DB
done
fi

-- 
Tim Hawkins
Sent with Sparrow
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Reply all
Reply to author
Forward
0 new messages