@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
121 lines (120 loc) • 4.67 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.restore = exports.backup = exports.checkConnection = void 0;
const makeDaySlug_1 = require("diginext-utils/dist/string/makeDaySlug");
const log_1 = require("diginext-utils/dist/xconsole/log");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const const_1 = require("../../config/const");
const checkConnection = async (options) => {
const { execa, execaCommand, execaSync } = await Promise.resolve().then(() => __importStar(require("execa")));
try {
const { stdout, stderr } = execaSync(`mysql`, [
"-h",
options.host,
"-P",
(options.port || 3306).toString(),
"-u",
options.user || "root",
`-p${options.pass}`,
"-e",
"SELECT version();",
]);
if (options.isDebugging)
console.log("[MYSQL] Connected :>> ", stdout);
return true;
}
catch (e) {
console.error("[MYSQL]", e);
return false;
}
};
exports.checkConnection = checkConnection;
const backup = async (options) => {
const { execa, execaCommand, execaSync } = await Promise.resolve().then(() => __importStar(require("execa")));
const bkName = `mysql-backup-${(0, makeDaySlug_1.makeDaySlug)()}.sql`;
if (!options.outDir)
options.outDir = path_1.default.resolve(const_1.STORAGE_DIR, `mysql`);
if (!(0, fs_1.existsSync)(options.outDir))
(0, fs_1.mkdirSync)(options.outDir, { recursive: true });
const outPath = path_1.default.resolve(options.outDir, bkName);
const { stdout, stderr } = await execa(`mysqldump`, [
"-h",
options.host,
"-P",
(options.port || 3306).toString(),
"-u",
options.user || "root",
`-p${options.pass}`,
options.dbName ? options.dbName : "--all-databases",
"--no-create-db",
"--lock-tables=false",
// "--column-statistics=false",
"--result-file",
outPath,
]);
if (stderr)
(0, log_1.logError)(stderr);
if (options.isDebugging)
console.log("[MYSQL] Backup successfully :>> ", stdout);
return { backupId: options.backupId, name: bkName, path: outPath };
};
exports.backup = backup;
const restore = async (options) => {
const { execa, execaCommand, execaSync } = await Promise.resolve().then(() => __importStar(require("execa")));
if (!options.path)
throw new Error(`Input "dir" path to ".dump" file is required.`);
if (!options.dbName)
throw new Error(`Database name "dbName" is required.`);
try {
if (!options.path.endsWith(".sql"))
throw new Error(`Invalid backup path, must end with ".sql"`);
const { stdout, stderr } = execaSync(`mysql`, [
"-h",
options.host,
"-P",
(options.port || 3306).toString(),
"-u",
options.user || "root",
`-p${options.pass}`,
`-D${options.dbName}`,
"<",
options.path,
]);
if (options.isDebugging)
console.log(`[MYSQL] Restore "${options.dbName}" database successfully :>> `, stdout);
return true;
}
catch (e) {
return false;
}
};
exports.restore = restore;
const MySQL = { checkConnection: exports.checkConnection, backup: exports.backup, restore: exports.restore };
exports.default = MySQL;