I would like to run a unix cron every day that does:
For the export part I use:
mongoexport --db mydb --collection mycollection\ --query "`./test2.sh`" --out ./test2.json
and the "./test2.sh
" file contains:
#!/bin/bash
d=`date --date="-3 month" -u +"%Y-%m-%dT%H:%M:%SZ"`
echo '{ "timeCreated": { "$lte": { "$date": "'$d'" } } }'
For the remove part I can do:
mongo mydb /home/dev/removeDocs.js
removeDocs.js:
var d = new Date();
d.setMonth(d.getMonth()-3);
db.GameHistory.remove({ timeCreated: { $lte: d} });
How can I synchronize the 2 commands? I want to run the remove commend after the export finished. can I merge the 2 to the same cron?
Yes, you can.
The easiest way is to merge both commands into single one-liner:
mongoexport --db mydb --collection mycollection\ --query "`./test2.sh`" --out ./test2.json && mongo mydb /home/dev/removeDocs.js
But I would recommend you to create shell script to archive your database:
#!/bin/bash
set -e # stop on first exception
mongoexport --db mydb --collection mycollection\ --query "`./test2.sh`" --out ./test2.json
mongo mydb /home/dev/removeDocs.js
If you want to append each new chunk of exported data, you should replace --out
with standard unix stdio
redirection:
mongoexport --db mydb --collection mycollection\ --query "`./test2.sh`" >> ./test2.json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With