I am wirting a mongodb auto backup code but i am stuck in some error: 'mongodump' is not recognized as an internal or external command.
can anyone help me out?
or is there another way to get auto backup with mongodb
exports.dbAutoBackUp = () => {
let cmd =
'mongodump --host ' +
dbOptions.host +
' --port ' +
dbOptions.port +
' --db ' +
dbOptions.database +
' --username ' +
dbOptions.user +
' --password ' +
dbOptions.pass +
' --out ' +
newBackupPath;
exec(cmd, (error, stdout, stderr) => {
console.log("Error : "+error)
console.log("Error 1: "+stdout)
console.log("Error 2: "+stderr)
if (this.empty(error)) {
// check for remove old backup after keeping # of days given in configuration.
if (dbOptions.removeOldBackup == true) {
if (fs.existsSync(oldBackupPath)) {
exec('rm -rf ' + oldBackupPath, err => {
console.log(err);
});
}
}
}
});
}
};
The error is probably because you are not in the directory where you have mongodb executable.
There are two ways to do it.
change your directory to mongodb's installation path
Add the mongodb executable to your environment variables
Path should be something like
{installation_directory}:\Program Files\MongoDB\Server\{version}\bin
For example
C:\Program Files\MongoDB\Server\4.2\bin
Related
How to run SELECT ##SERVERNAME SQL statement in NodeJS?
Please beware that the said SQL statement will return an empty header with a single record (hostname).
How to process and take that record and display it on express site ( or put it into some variable and call it in .ejs page
I manage to use os.hostname, but that will only give the NodeJS machine name, not the machine name where SSQL Server is running.
This is for demonstration of container/Kubernetes, so, If the web page returns the info of where the NodeJS and Server SQL is running, it will helpful.
Thank You again.
Update: What I've done so far:
const mssql = require('mssql');
const dbconfig = require('./src/config/dbconfig.json');
const pool = new mssql.ConnectionPool(dbconfig);
mssql.globalConnectionPool = pool;
//open database connection
pool.connect((err)=>{
if (err){
console.log('connection failed to server:' + dbconfig.server + ' database:' + dbconfig.database);
console.log(err);
}
else {
console.log ('Connected to ' + dbconfig.server
+ '/' + dbconfig.database
+ ' (' + dbconfig.user + ')');
const request = new mssql.Request(pool);
request.query('select ##SERVERNAME',(err,rec)=>{
console.log('SQL RUNNING AT ' +JSON.stringify(rec.recordset, null, 0)
.replace('[{"":"', '')
.replace('"}]','')
);
});
}
});
The above code returns the ##SERVERNAME value, but only prints out at console.log. How do I put this into a variable? and send to ejs?
When executing your query in SQL, such as from SSMS, you would give your result a column name, e.g.:
select [MyServerName] = ##SERVERNAME
MyServerName
--------------------
FOO\MSSQLSERVER
Or:
select ##SERVERNAME as MyServerName
MyServerName
--------------------
FOO\MSSQLSERVER
Do the same when invoking from NodeJS:
request.query('select ##SERVERNAME as MyServerName',(err, result)=>{
const myServerName = result.recordset[0].MyServerName;
console.log('SQL RUNNING AT ' + myServerName)
);
});
I am trying to connect to a DB2 server but I am getting bellow given error.
I'm following the given documentation: npm db2 Doc
I have done npm i ibm_db2
Code:
const ibmdb = require('ibm_db');
const connectQuery =
'DATABASE=' +
DATABASE +
';HOSTNAME=' +
HOSTNAME +
';UID=' +
UID +
';PWD=' +
PWD +
';PORT=' +
PORT +
';PROTOCOL=TCPIP';
ibmdb.open(connectQuery, function(err, conn) {
if (err) return console.log(err);
conn.query('select 1 from sysibm.sysdummy1', function(err, data) {
if (err) console.log('err');
else console.log('data');
conn.close(function() {
console.log('done');
});
});
});
Error:
Error: Could not locate the bindings file. Tried:
→ ...\node_modules\ibm_db\build\odbc_bindings.node
→ ...\node_modules\ibm_db\build\Debug\odbc_bindings.node
→ ...\node_modules\ibm_db\build\Release\odbc_bindings.node
→ ...\node_modules\ibm_db\out\Debug\odbc_bindings.node
→ ...\node_modules\ibm_db\Debug\odbc_bindings.node
Is there any other node package to establish connection ?
I have the same issue on windows 10. Because your ibm_db module is not installed successfully.
Download directly clidriver generated by IBM. After setting IBM_DB_HOME environment variable to point the directory, and reinstall ibm_db module to skip downloading clidriver.
I have this PowerShell that I usually call like this :
powershell -ExecutionPolicy Bypass -File "D:\tmp\getmember2.ps1" -groupnames "ABC"
Now, I need to call this from my nodejs. So, this is what I create :
var spawn = Meteor.npmRequire("child_process").spawn;
child = spawn("powershell.exe",["-ExecutionPolicy ByPass -File d:\\tmp\\getmember2.ps1 -groupnames \"ABC\""]);
child.stdout.on("data",function(data){
console.log("Powershell Data: " + data);
});
child.stderr.on("data",function(data){
console.log("Powershell Errors: " + data);
});
child.on("exit",function(){
console.log("Powershell Script finished");
});
child.stdin.end();
However I am getting this error :
I20170222-16:58:25.257(8)? API started
I20170222-16:58:26.175(8)? Powershell Errors: At line:1 char:2
I20170222-16:58:26.174(8)?
I20170222-16:58:26.175(8)? + CategoryInfo : ParserError: (-:String)
[], ParentContainsErrorR
I20170222-16:58:26.175(8)? + - <<<< ExecutionPolicy ByPass -File d:\tmp\getmember2.ps1
I20170222-16:58:26.176(8)? Powershell Errors: ecordException
I20170222-16:58:26.176(8)? Powershell Errors: + FullyQualifiedErrorId : MissingExpressionAfterOperator
I20170222-16:58:26.176(8)? Powershell Errors:
I20170222-16:58:26.177(8)?
I20170222-16:58:26.174(8)? Powershell Errors: Missing expression after unary operator '-'.
I20170222-16:58:26.259(8)? Powershell Script finished
Any idea on why it is not working?
Change my code to use child_process.exec() instead of spawn().
As described in this link :
Spawn is best used to when you want the child process to return a large amount of data to Node - image processing, reading binary data etc. Use exec to run programs that return result statuses, instead of data.
Code changed to below and run my powershell neatly.
var child = exec('Powershell.exe -executionpolicy ByPass -File d:\\tmp\\getmember.ps1 -processdir d:\\temp -groupnames "ABC"',
function(err, stdout, stderr){
//callback(err)
if(err){
console.error(err);
}else {
console.log("should be no issue");
}
});
child.stdout.on('data', function(data) {
// Print out what being printed in the powershell
console.log(data);
});
child.stderr.on('data', function(data) {
//print error being printed in powershell
console.log('stderr: ' + data);
});
child.on('close',function(code){
// To check the result of powershell exit code
console.log("exit code is "+code);
if(code==0)
{
console.log("Powershell run successfully, exit code = 0 ")
}else {
console.log("ERROR, powershell exited with exit code = "+ code);
}
});
child.stdin.end();
Why does this not work? How to do it right?
var exec = require("child_process").exec;
exec("ls data/tile_0_{0..63}.jpg", function(error, stdout, stderr){
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr)}
);
// stdout:
// stderr: ls: cannot access data/tile_0_{0..63}.jpg: No such file or directory
In bash terminal this lists all files like tile_0_0.jpg, tile_0_1.jpg, etc.
This executed from node works as expected but does not do the thing I want.
ls data/tile_61_[0-9].jpg
Please help me ...
I'm using Linux Mint 14.
Why not do
var _ = require('underscore'),
fs = require('fs'),
files = fs.readdirSync('data');
var filtered = _.filter(files, function(filename){
return filename.indexOf('tile_0_') == 0;
});
I have a complicated MakeFile file I want to call as part of my node.js app, the make file is a couple of directories deep from root. I know I need to spawn a 'make' chile process but moving node into the sub directory to call the make im not so sure about.
Сould you move the make to the nested folder? I would try something like
var util = require('util'),
exec = require('child_process').exec,
child;
child = exec('cd samples/nestedmake && make',
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
(copied with minimal changes from Node Child Processes documentation).