1

I want to create a script to backup a collection in MongoDB on a daily basis. I want it to be run at 00:30 every day to backup data that has been inserted to this collection during the day before. When I do this manually I run it like this:

mongodump -d History  -c history -q '{ czas:  { $gte: new Date(1443571201000), $lte: new Date(1443657540000)} }' --out /home/test/backup

now I want those unix timestamp to be generated automatically. In a bash this command makes the trick:

TZ=UTC date --date="`date +"%Y-%m-%d 00:00:00" -d '1 day ago'`" +"%s"

But I dont know how to combine it in a mongodb command. This command fails:

mongodump -d History  -c history -q '{ czas:  { $gte: new Date(`date --date="`date +"%Y-%m-%d 00:00:00" -d '2 days ago'`" +"%s"`), $lte: new Date(`date --date="`date +"%Y-%m-%d 00:00:00" -d '1 day ago'`" +"%s"`)} }' --out /home/test/backup

with a message:

2015-11-26T16:45:03.051+0100    positional arguments not allowed: [days ago`" +"%s"`), $lte: new Date(`TZ=UTC date --date="`date +"%Y-%m-%d 00:00:00" -d 1 day ago`" +"%s"`)} }]

I'm not unix guru, please help me how to get this working.

Regards, Przemek

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Przemek Piechota
  • 1,513
  • 2
  • 12
  • 19
  • Related: [executing shell (bash) commands from within the mongo client](http://dba.stackexchange.com/q/112113/27119). In general, it is a bad idea. – fedorqui Nov 26 '15 at 16:48
  • Mongodump will be run from shell and not from mongo client. I want to create a bash script that will be run from crontab. I just need to know how to put that calculated unixtimestamp value inside mongodump command – Przemek Piechota Nov 26 '15 at 19:11
  • Probably with [`eval`](http://stackoverflow.com/a/6000360/1983854)? – fedorqui Nov 27 '15 at 10:07

1 Answers1

0

The shell won't be able to provide values inside of single quotes (it assumes 'literal values' vs 'get_these values') so, try something like:

START_DATE="some value"  
END_DATE="some value"  

"{ czas:  { $gte: new Date(${START_DATE}), $lte: new Date(${END_DATE})} }"

I would hope that the manual for the tool you are using would provide a solution; I am guessing that the syntax requires a 'Epoch Date' (seconds since the 'Epoch'.)

:)
Dale

Dale_Reagan
  • 1,953
  • 14
  • 11