I'm working on a document backup solution that needs to be bulletproof. Im running my backup each week, and Im able to do a dryrun before the actual dump to determine how many documents I am going to dump. So, I have a target number.
Issue is, when I actually RUN the mongodump, it writes all output to the CONSOLE, not to stdout. So I cant capture the output to a variable, find the number of documents it dumped, and compare to my target number.
Ive also looked and dont see any way to use mongorestore to get a record count in the file after the fact.
And no, wc -l doesnt work.
So, this is what I see written to console during the mongodump:
2022-03-03T22:25:15.069 0000 writing mydb.records to archive '/opt/weekly/records_2022-02-13'
2022-03-03T22:25:17.529 0000 mydb.records 101
2022-03-03T22:25:22.510 0000 mydb.records 1675
2022-03-03T22:25:22.513 0000 done dumping mydb.records (1675 documents)
The above is going to console, and Im not able to capture it from within my script (i.e. varname=$(mongodumpcommandhere) ), nor am I able to redirect the output to a .log file and then scrub the contents.
So, any mongo experts out there know how I can do what I want? Sure, Im already checking the exit status of the mongodump command, just trying to go the extra mile and line up the document count as well.
I could install a blank DB and load the dump and count the documents, but that seems like a little far to go just to get the number of documents in a dump. Looking for something a little less brute force.
Oh, and this is a bash script handling all of the logic.
CodePudding user response:
mongodumpcommandhere
may be writing to standard error instead of standard output. If that is the case
varname=$(mongodumpcommandhere 2>&1)
will work. If it doesn't work it probably means that the command is writing directly to the terminal. That's a bad thing to do, but there are ways to capture it.