1

I am trying to automate some mongobackups and restores. For which I am using rust for learning purposes as well.

fn dump(name: &str, user: &str, pass: &str, root: &settings::Root) {
    let cmd = format!("mongodump  -u {} -p{} --authenticationDatabase=admin --db={} --gzip --archive={}{}.archive", root.user, root.pass, name, name, get_time() );

    println!("{}", cmd);
    let output = Command::new(cmd).output();

    match output {
        Ok(res) => {
            println!("{:?}", res);
        },
        Err(err) => {
            println!("{}",err);
            panic!(err);
        }
    }
}

This is the function for running the mongodump command. I can see the print of cmd which is mongodump -u manish -pterminator --authenticationDatabase=admin --db=todos --gzip --archive=todos16-Jan-2021-23:15:02.archive

which works fine if run directly in terminal, but fails in the program with error

No such file or directory (os error 2)

I have done multiple hit and trials but can't get it working.

Manish Singh
  • 360
  • 1
  • 6
  • 18
  • 1
    Does this answer your question? [Unable to use std::process::Command to SSH - No such file or directory](https://stackoverflow.com/questions/40836973/unable-to-use-stdprocesscommand-to-ssh-no-such-file-or-directory). You aren't creating the `Command` correctly. It should be `Command::new("mongodump").args(&["-u", root.user, ...])`. – kmdreko Jan 17 '21 at 02:19
  • Also, if `mongodump` is not an actually CLI application, but a bash function or alias, for example, you need to run the command via `bash -c ` to get the same result as running that from a shell. – Renato Jan 17 '21 at 11:31
  • @kmdreko I tried with the given procedure, `let output = Command::new("mongodump") .args(&[ "-u", root.user.as_str(), "-pterminator", "--authenticationDatabase", "admin", "--db", name, "--gzip", "--archive", "todos.archive", ]) .output();` Its still giving me an error **stderr: "terror parsing command line options error parsing positional arguments: provideonlyone MongoDB connection string. ** – Manish Singh Jan 18 '21 at 10:11
  • 1
    Passing `"--db"` and `name` as separate arguments will send them to `mongodump` as if they were space separated. To pass `"--db=name"`, you'll need to use `&format!("--db={}", name)` as a single argument. – kmdreko Jan 18 '21 at 17:54

0 Answers0