I am trying to use mongodump in order to create a bson file from a specified point in the oplog. I am working with mongoDB version 4.2 on linux.
When I perform the gollowing command, all is good:
# mongodump --authenticationDatabase=admin -d local -c oplog.rs --query "{\"ts\":{\"\$gt\":{\"\$timestamp\":{\"t\":1629206557,\"i\":1}}}}" -u myuser-p mypassword --port 27020 -o - > /data/db/backup/a.bson
2021-08-19T05:38:52.550+0000 writing local.oplog.rs to stdout
2021-08-19T05:38:53.935+0000 dumped 20871 documents
However, if I try to put the query string into a varialbe, the dump fails, even though the string looks the same:
# query_string="\"{\\\"ts\\\":{\\\"\\\$gt\\\":{\\\"\\\$timestamp\\\":{\\\"t\\\":${TIMESTAMP_LAST_OPLOG_ENTRY},\\\"i\\\":${INC_NUMBER_LAST_OPLOG_ENTRY}}}}}\""
# echo $query_string
"{\"ts\":{\"\$gt\":{\"\$timestamp\":{\"t\":1629206557,\"i\":1}}}}"
mongodump --authenticationDatabase=admin -d local -c oplog.rs --query '$query_string' -u octopusdba -p oct-DBA+2020 --port 27020 -o - > /data/db/backup/a.bson
2021-08-19T05:45:20.109+0000 Failed: error parsing query as Extended JSON: invalid JSON input
Trying to put the query_string in double quotes did not work either:
root@10:/# mongodump --authenticationDatabase=admin -d local -c oplog.rs --query "$query_string" -u myuser-p mypassword --port 27020 -o - > /data/db/backup/a.bson
2021-08-19T05:45:30.257+0000 error parsing command line options: invalid argument for flag `-q, --query' (expected string): invalid syntax
2021-08-19T05:45:30.257+0000 try 'mongodump --help' for more information