1

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);



 };
5
  • how do you transfer the file from the source server (where you make the dump) to the target server (where you want to import the dump)? Commented Jul 15, 2015 at 9:10
  • I have created the dump of host1 server (not local) on server where node is running and want to connect to host 2 to execute the file from node server not form host1 server Commented Jul 15, 2015 at 9:14
  • so there are 3 servers? host1 (orig db), host2 (target db) and host3 (with node server)? Commented Jul 15, 2015 at 9:44
  • yes..you can say that Commented Jul 15, 2015 at 9:54
  • you need the dump file somehow on host3 and you need to execute the mysql command on the shell level on that host3. Commented Jul 15, 2015 at 10:00

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

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.