1

I need to run mongodump command with below arguments

    var dbhost = mongoose.connection.host || "127.0.0.1",
                    dbport = mongoose.connection.port,
                    dbname = mongoose.connection.name,
                    dbuser = mongoose.connection.options.user,
                    dbpass = mongoose.connection.options.pass,
                    backupPath = path,
                    date = new Date(),
                    currentDate =  date.toLocaleString(),
                    backupFileName ='DBBACKUP-'+currentDate;

how to pass above variables to child process

I've tried with below code

var backupDB = spawn('mongodump --host '+dbhost+' --port '+dbport+' --username '+dbuser+' --password '+dbpass+' --db '+dbname+' --archive=backupFileName.gz --gzip');
backupDB.stdout.on('data',function(data){ console.log('stdout: ' + data);

it throwed this error

error: uncaughtException: spawn mongodump --host 127.0.0.1 --port 27017 --username --password --db mydb --archive=backupFileName.gz --gzip ENOENT 
2
  • Can you not just interpolate the values into the string? Commented May 30, 2017 at 4:31
  • But really, just read here Template Literals Commented May 30, 2017 at 4:37

2 Answers 2

2

According to the fine manual, spawn() takes the name of a command, and an array of arguments to pass to that command:

var backupDB = spawn('mongodump', [
  '--host',     dbhost,
  '--port',     dbport,
  '--username', dbuser,
  '--password', dbpass,
  '--db',       dbname,
  '--archive=backupFileName.gz',
  '--gzip'
]);
Sign up to request clarification or add additional context in comments.

Comments

1

I've tried this and it works perfectly

var backupDB = exec('mongodump --host='+dbhost+' --port='+dbport+' --username='+dbuser+' --password='+dbpass+' --db='+dbname+' --archive='+backupPathDir+'/'+backupFileName+'.gz  --gzip');
            backupDB.stdout.on('data',function(data){
                console.log('stdout: ' + data);// process output will be displayed here
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.