bash - mongodb export and remove docs -
i run unix cron every day does:
- export docs on 3 month old document
- remove same docs collections.
for export part use:
mongoexport --db mydb --collection mycollection\ --query "`./test2.sh`" --out ./test2.json
and "./test2.sh
" file contains:
#!/bin/bash d=`date --date="-3 month" -u +"%y-%m-%dt%h:%m:%sz"` echo '{ "timecreated": { "$lte": { "$date": "'$d'" } } }'
for remove part 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 synchronize 2 commends? want run remove commend after export finished. can merge 2 same cron?
yes, can.
the easiest way merge both commands single one-liner:
mongoexport --db mydb --collection mycollection\ --query "`./test2.sh`" --out ./test2.json && mongo mydb /home/dev/removedocs.js
but recommend create shell script archive 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 want append each new chunk of exported data, should replace --out
standard unix stdio
redirection:
mongoexport --db mydb --collection mycollection\ --query "`./test2.sh`" >> ./test2.json
Comments
Post a Comment