bradzacher/mysqldump

how to use with remote db?

antiochtech opened this issue · 1 comments

i connect to my db using forwardOut from ssh2, adding the stream to the connection object thusly. can mysqldump be used similarly?

require("dotenv").config();
const sshClient = require("ssh2").Client;
const ssh = new sshClient();
const getPort = require("get-port");
const mysql = require("mysql2/promise");
const bluebird = require("bluebird");
var connection = {};
const sshConf = {
	host: <host>,
	port: 22,
	username: process.env.SSH_USER,
	password: process.env.SSH_PASS,
	authHandler: [{ type: "password", username: process.env.SSH_USER, password: process.env.SSH_PASS }],
	keepaliveInterval: 1000
};
const sqlConf = {
	host: "localhost",
	user: process.env.DB_USER,
	password: process.env.DB_PASS,
	database: process.env.DB_DATABASE,
	dateStrings: "date",
	Promise: bluebird,
	enableKeepAlive: true,
	typeCast: function castField(field, useDefaultTypeCasting) {
		if ((field.type === "BIT") && (field.length === 1)) {
			var bytes = field.buffer();
			return(bytes[0] === 1);
		}
		return(useDefaultTypeCasting());
	}
};

ssh.connect(sshConf);

ssh.on("ready", async function() {	
	ssh.forwardOut("127.0.0.1", await getPort({port: getPort.makeRange(49152, 65535)}), "127.0.0.1", 3306, async function(err, stream) {
		if (err) throw err;
		sqlConf.stream = stream;
		connection = await mysql.createConnection(sqlConf);
	});
});

yes - read the docs.
https://github.com/bradzacher/mysqldump#usage

you specify a host to connect to.