Search code examples
mongodbmongodump

Stop mongodump from overwriting existing files (rename instead)


From mongodocs:

Overwrite Files

"Mongodump overwrites output files if they exist in the backup data folder. Before running the mongodump command multiple times, either ensure that you no longer need the files in the output folder (the default is the dump/ folder) or rename the folders or files."

Hey guys, I want to do a daily backup and sometimes even two backups a day. The Dump-filename gets named by the actual date. If I backup twice a day, the first backup gets overwritten due to same names. Is there any way to tell mongodump to rename (in e.g. 5.9.2016(1)) the file if it already exists?


Solution

  • You can use the --out option of mongodump to specify the path where to dummp the data.

    Create a script that run mongodump and give different name for your path, i.e. using a date:

    mongodump --out /data/dump/090516/
    

    Shell script example:

    #!/bin/sh
    DIR=`date +%m%d%y`
    DEST=$DIR
    mkdir $DEST
    mongodump --out=/data/dump/$DEST