Ritu Gupta
Ritu Gupta

Reputation: 515

How to execute mysql dump file using node

Hello I am using gruntfile to create dump of sql file and want to execute that same file in some another server.

Code to create dump is working fine , but I am unable to execute that dump. Please help me.. I am stuck on it from yesterday.

var grunt=require('grunt');

module.exports = function(grunt) {


console.log("grunt called");
grunt.initConfig({
      // Load database config from external JSON (optional) 
      //db_config: grunt.file.readJSON('config/db.json'),

      db_dump: {
        options: {
          // common options should be defined here 
        },

        // "Local" target 
        "local": {
          "options": {
              "title": "Local DB",

            "database": "test",
            "user": "user",
            "pass": "pass",
            "host": "host1",

            "backup_to": "local.sql"
          }
        }
      },
      mysqlrunfile: {
            options: {
                connection: {
                    "database": "test",
                    "user": "user",
                    "pass": "pass",
                    "host": "host2",
                    multipleStatements: true
                },
                yourtarget1: {
                    src: ['/local.sql']
                }
            }
        }

    });


require('load-grunt-tasks')(grunt);



 };

Upvotes: 1

Views: 4305

Answers (1)

clay
clay

Reputation: 6017

This was a similar script that was run with node rebuild-db. It spawns a child process to run he mysql script and prints out what errors occur. You'll have to fill in with your own variables.

    var exec = require('child_process').exec,
        child,
    var fs = require('fs');

    var rebuild_file = __dirname + '/defaultData.sql';

    var runSqlScript = function(file, callback) {

        rebuild_db = 'mysql -u ' + nconf.get('databaseUser') + ' -h ' + nconf.get('databaseHost') + ' -p' + nconf.get('databasePass') + ' ' + nconf.get('databaseName') + ' < ' + file;

        child = exec(rebuild_db, function(error, stdout, stderr) {
            if (error !== null) {
                console.log('Rebuild Error: ' + error);
                console.log('stdout: ' + stdout);
                console.log('stderr: ' + stderr);
                process.exit(1);
                return;
            }
            console.log('Successfully Rebuild Database using: ');
            console.log('   ' + file);
            callback();
        });

    };


    var rebuild = function() {
        runSqlScript(rebuild_file, function() {
            process.exit(0);
        });
    };

    rebuild();

Upvotes: 3

Related Questions