module.exports = function(configLocalDb) {

  var childProcess = require('child_process');

  function isoDate() {
    return (new Date()).toISOString().slice(0, 16).replace(/T|:/g, '-');
  }

  function dumpLocalDB() {
    var localDumpDir = 'syncdb/sql';
    var localDumpName = 'local-db-' + isoDate() + '.sql';
    var localDumpFile = localDumpDir + '/' + localDumpName;
    var localDump = 'mysqldump -u ' + configLocalDb.user
      + ' -p' + configLocalDb.password + ' ' + configLocalDb.name
      + ' > ' + localDumpFile;

    if (childProcess.spawnSync(localDump).status !== 0) {
      console.log('dump failed');
      process.exit(1);
    }

    return localDumpFile;
  }

  function populateLocalDB(dumpFile) {
    var localPopulate = 'mysql -u ' + configLocalDb.user
      + ' -p' + configLocalDb.password + ' ' + configLocalDb.name
      + ' < ' + dumpFile;

    if (childProcess.spawnSync(localPopulate).status !== 0) {
      console.log('populating failed');
      process.exit(1);
    }
  }

  return {
    dump: dumpLocalDB,
    populate: populateLocalDB
  };
}

module.exports = function(configLocalDb) {

  var childProcess = require('child_process');
  var fs           = require('fs');

  function isoDate() {

    return (new Date()).toISOString().slice(0, 16).replace(/T|:/g, '-');
  }

  function dumpLocalDB(callback) {

    var localDumpDir = 'syncdb/sql';
    var localDumpName = 'local-db-' + isoDate() + '.sql';
    var localDumpFile = localDumpDir + '/' + localDumpName;

    var mysqldump = childProcess.spawn('mysqldump', [
        '-u', configLocalDb.user,
        '-p' + configLocalDb.password, // sic, password is attached w/o space!
        configLocalDb.name
    ]);

    mysqldump.on('close', function(code) {
      if (code !== 0) {
        callback(new Error('Local mysqldump failed!'));
      } else {
        callback();
      }
    });

    mysqldump.stdout.pipe(fs.createWriteStream(localDumpFile));
  }

  function populateLocalDB(dumpFile, callback) {

    var mysql = childProcess.spawn('mysql', [
        '-u', configLocalDb.user,
        '-p' + configLocalDb.password,
        configLocalDb.name
    ]);

    mysql.on('close', function(code) {
      if (code !== 0) {
        callback(new Error('Local database population failed!'));
      } else {
        callback();
      }
    });

    fs.createReadStream(dumpFile).pipe(mysql.stdin);
  }

  return {
    dump: dumpLocalDB,
    populate: populateLocalDB
  };
}

Use asynchronous child_process and fs apis
Use asynchronous child_process and fs apis

so that we don’t have to exit the process from this module;
instead, take callbacks and call them with errors like good
javascript hackers do.
