Updates
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import { APIResponseObject } from "../../../../../types";
|
||||
type Params = {
|
||||
targetUserId?: string | number;
|
||||
};
|
||||
export default function suAddBackup({ targetUserId, }: Params): Promise<APIResponseObject>;
|
||||
export {};
|
||||
@@ -0,0 +1,71 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = suAddBackup;
|
||||
const write_backup_files_1 = __importDefault(require("./write-backup-files"));
|
||||
const grab_dir_names_1 = __importDefault(require("../../../../../utils/backend/names/grab-dir-names"));
|
||||
const addDbEntry_1 = __importDefault(require("../../../db/addDbEntry"));
|
||||
const numberfy_1 = __importDefault(require("../../../../../utils/numberfy"));
|
||||
const grab_user_resource_1 = __importDefault(require("../../../../web-app/db/grab-user-resource"));
|
||||
function suAddBackup(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ targetUserId, }) {
|
||||
var _b, _c;
|
||||
try {
|
||||
const { mainBackupDir, userBackupDir } = (0, grab_dir_names_1.default)({
|
||||
userId: targetUserId,
|
||||
});
|
||||
if (targetUserId && !userBackupDir) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Error grabbing user backup directory`,
|
||||
};
|
||||
}
|
||||
const newBackup = {
|
||||
user_id: targetUserId ? (0, numberfy_1.default)(targetUserId) : undefined,
|
||||
};
|
||||
const newBackupEntry = yield (0, addDbEntry_1.default)({
|
||||
tableName: "backups",
|
||||
data: newBackup,
|
||||
});
|
||||
if (!((_b = newBackupEntry === null || newBackupEntry === void 0 ? void 0 : newBackupEntry.payload) === null || _b === void 0 ? void 0 : _b.insertId)) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Couldn't create new backup entry`,
|
||||
};
|
||||
}
|
||||
const { single: newlyAddedBackup } = yield (0, grab_user_resource_1.default)({
|
||||
tableName: "backups",
|
||||
targetID: (_c = newBackupEntry.payload) === null || _c === void 0 ? void 0 : _c.insertId,
|
||||
isSuperUser: true,
|
||||
});
|
||||
if (!(newlyAddedBackup === null || newlyAddedBackup === void 0 ? void 0 : newlyAddedBackup.id)) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Couldn't fetch newly added backup`,
|
||||
};
|
||||
}
|
||||
const writeBackup = yield (0, write_backup_files_1.default)({
|
||||
backup: newlyAddedBackup,
|
||||
});
|
||||
return writeBackup;
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Backup add failed`,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import { APIResponseObject, UserType } from "../../../../../types";
|
||||
import { DSQL_DATASQUIREL_BACKUPS } from "../../../../../types/dsql";
|
||||
type Params = {
|
||||
user: UserType;
|
||||
backup: DSQL_DATASQUIREL_BACKUPS;
|
||||
};
|
||||
export default function suRestoreBackup({ user, backup, }: Params): Promise<APIResponseObject>;
|
||||
export {};
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = suRestoreBackup;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const grab_dir_names_1 = __importDefault(require("../../../../../utils/backend/names/grab-dir-names"));
|
||||
const import_mariadb_database_1 = __importDefault(require("../../../../../utils/backend/import-mariadb-database"));
|
||||
function suRestoreBackup(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ user, backup, }) {
|
||||
try {
|
||||
const { mainBackupDir, userBackupDir, sqlBackupDirName, schemasBackupDirName, targetUserPrivateDir, oldSchemasDir, } = (0, grab_dir_names_1.default)({
|
||||
userId: backup.user_id,
|
||||
});
|
||||
if (backup.user_id && !userBackupDir) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Error grabbing user backup directory`,
|
||||
};
|
||||
}
|
||||
if (!backup.uuid) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No UUID found for backup`,
|
||||
};
|
||||
}
|
||||
const existingBackupDir = path_1.default.join(backup.user_id && userBackupDir ? userBackupDir : mainBackupDir, backup.uuid);
|
||||
const userDatabases = fs_1.default.readdirSync(path_1.default.join(existingBackupDir, sqlBackupDirName));
|
||||
const databasesToRestore = (userDatabases === null || userDatabases === void 0 ? void 0 : userDatabases.map((db) => db.split(".")[0])) || [process.env.DSQL_DB_NAME || "datasquirel"];
|
||||
for (let i = 0; i < databasesToRestore.length; i++) {
|
||||
const dbToBackup = databasesToRestore[i];
|
||||
if (!dbToBackup)
|
||||
continue;
|
||||
const dbFileName = `${dbToBackup}.sql`;
|
||||
const dbFilePath = path_1.default.join(existingBackupDir, sqlBackupDirName, dbFileName);
|
||||
(0, import_mariadb_database_1.default)({
|
||||
dbFullName: dbToBackup,
|
||||
targetFilePath: dbFilePath,
|
||||
});
|
||||
}
|
||||
const userSchemaDirFiles = targetUserPrivateDir
|
||||
? fs_1.default
|
||||
.readdirSync(targetUserPrivateDir)
|
||||
.filter((dirName) => dirName.match(/^\d+\.json$/))
|
||||
: undefined;
|
||||
const appSchemaDirFiles = fs_1.default
|
||||
.readdirSync(oldSchemasDir)
|
||||
.filter((dirName) => dirName.match(/^\d+\.json$/));
|
||||
const schemaFilesToWrite = backup.user_id && userSchemaDirFiles
|
||||
? userSchemaDirFiles
|
||||
: appSchemaDirFiles;
|
||||
for (let i = 0; i < schemaFilesToWrite.length; i++) {
|
||||
const schemaFileName = schemaFilesToWrite[i];
|
||||
const originSchemaFilePath = path_1.default.join(existingBackupDir, schemasBackupDirName, schemaFileName);
|
||||
const destinationSchemaFilePath = path_1.default.join(backup.user_id && targetUserPrivateDir
|
||||
? targetUserPrivateDir
|
||||
: oldSchemasDir, schemaFileName);
|
||||
fs_1.default.copyFileSync(originSchemaFilePath, destinationSchemaFilePath);
|
||||
}
|
||||
return { success: true };
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Failed to write backup files`,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import { DSQL_DATASQUIREL_BACKUPS } from "../../../../../types/dsql";
|
||||
import { APIResponseObject } from "../../../../../types";
|
||||
type Params = {
|
||||
backup: DSQL_DATASQUIREL_BACKUPS;
|
||||
};
|
||||
export default function writeBackupFiles({ backup, }: Params): Promise<APIResponseObject>;
|
||||
export {};
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = writeBackupFiles;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const grab_dir_names_1 = __importDefault(require("../../../../../utils/backend/names/grab-dir-names"));
|
||||
const app_names_1 = require("../../../../../dict/app-names");
|
||||
const dbHandler_1 = __importDefault(require("../../../dbHandler"));
|
||||
const export_mariadb_database_1 = __importDefault(require("../../../../../utils/backend/export-mariadb-database"));
|
||||
function writeBackupFiles(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ backup, }) {
|
||||
try {
|
||||
const { mainBackupDir, userBackupDir, sqlBackupDirName, schemasBackupDirName, targetUserPrivateDir, oldSchemasDir, } = (0, grab_dir_names_1.default)({
|
||||
userId: backup.user_id,
|
||||
});
|
||||
if (backup.user_id && !userBackupDir) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Error grabbing user backup directory`,
|
||||
};
|
||||
}
|
||||
if (!backup.uuid) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No UUID found for backup`,
|
||||
};
|
||||
}
|
||||
const newBackupDir = path_1.default.join(backup.user_id && userBackupDir ? userBackupDir : mainBackupDir, backup.uuid);
|
||||
fs_1.default.mkdirSync(newBackupDir, { recursive: true });
|
||||
fs_1.default.mkdirSync(path_1.default.join(newBackupDir, sqlBackupDirName), {
|
||||
recursive: true,
|
||||
});
|
||||
fs_1.default.mkdirSync(path_1.default.join(newBackupDir, schemasBackupDirName), {
|
||||
recursive: true,
|
||||
});
|
||||
const userDatabases = backup.user_id
|
||||
? (yield (0, dbHandler_1.default)({
|
||||
query: `SHOW DATABASES LIKE '${app_names_1.AppNames["DsqlDbPrefix"]}${backup.user_id}_%'`,
|
||||
}))
|
||||
: undefined;
|
||||
const databasesToBackup = (userDatabases === null || userDatabases === void 0 ? void 0 : userDatabases.map((db) => Object.values(db)[0])) || [process.env.DSQL_DB_NAME || "datasquirel"];
|
||||
for (let i = 0; i < databasesToBackup.length; i++) {
|
||||
const dbToBackup = databasesToBackup[i];
|
||||
if (!dbToBackup)
|
||||
continue;
|
||||
const dbFileName = `${dbToBackup}.sql`;
|
||||
const dbFilePath = path_1.default.join(newBackupDir, sqlBackupDirName, dbFileName);
|
||||
(0, export_mariadb_database_1.default)({
|
||||
dbFullName: dbToBackup,
|
||||
targetFilePath: dbFilePath,
|
||||
});
|
||||
}
|
||||
const userSchemaDirFiles = targetUserPrivateDir
|
||||
? fs_1.default
|
||||
.readdirSync(targetUserPrivateDir)
|
||||
.filter((dirName) => dirName.match(/^\d+\.json$/))
|
||||
: undefined;
|
||||
const appSchemaDirFiles = fs_1.default
|
||||
.readdirSync(oldSchemasDir)
|
||||
.filter((dirName) => dirName.match(/^\d+\.json$/));
|
||||
const schemaFilesToWrite = backup.user_id && userSchemaDirFiles
|
||||
? userSchemaDirFiles
|
||||
: appSchemaDirFiles;
|
||||
for (let i = 0; i < schemaFilesToWrite.length; i++) {
|
||||
const schemaFileName = schemaFilesToWrite[i];
|
||||
const originSchemaFilePath = path_1.default.join(backup.user_id && targetUserPrivateDir
|
||||
? targetUserPrivateDir
|
||||
: oldSchemasDir, schemaFileName);
|
||||
const destinationSchemaFilePath = path_1.default.join(newBackupDir, schemasBackupDirName, schemaFileName);
|
||||
fs_1.default.copyFileSync(originSchemaFilePath, destinationSchemaFilePath);
|
||||
}
|
||||
return { success: true };
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Failed to write backup files`,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { DSQL_DATASQUIREL_BACKUPS } from "../../../../types/dsql";
|
||||
import { APIResponseObject } from "../../../../types";
|
||||
type Params = {
|
||||
backup: DSQL_DATASQUIREL_BACKUPS;
|
||||
};
|
||||
export default function deleteBackup({ backup, }: Params): Promise<APIResponseObject>;
|
||||
export {};
|
||||
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = deleteBackup;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const grab_dir_names_1 = __importDefault(require("../../../../utils/backend/names/grab-dir-names"));
|
||||
const deleteDbEntry_1 = __importDefault(require("../../db/deleteDbEntry"));
|
||||
const numberfy_1 = __importDefault(require("../../../../utils/numberfy"));
|
||||
function deleteBackup(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ backup, }) {
|
||||
try {
|
||||
const { mainBackupDir, userBackupDir } = (0, grab_dir_names_1.default)({
|
||||
userId: backup.user_id,
|
||||
});
|
||||
if (backup.user_id && !userBackupDir) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Error grabbing user backup directory`,
|
||||
};
|
||||
}
|
||||
if (!backup.uuid) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No UUID found for backup`,
|
||||
};
|
||||
}
|
||||
const newBackupDir = path_1.default.join(backup.user_id && userBackupDir ? userBackupDir : mainBackupDir, backup.uuid);
|
||||
fs_1.default.rmSync(newBackupDir, { recursive: true, force: true });
|
||||
yield (0, deleteDbEntry_1.default)({
|
||||
identifierColumnName: "id",
|
||||
identifierValue: (0, numberfy_1.default)(backup.id),
|
||||
tableName: "backups",
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Failed to write backup files`,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
+4
-4
@@ -13,10 +13,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = handleBackup;
|
||||
const grab_user_resource_1 = __importDefault(require("@/src/functions/db/grab-user-resource"));
|
||||
const add_backup_1 = __importDefault(require("@/src/functions/api/su/add-backup"));
|
||||
const grab_config_1 = __importDefault(require("@/package-shared/utils/backend/config/grab-config"));
|
||||
const delete_backup_1 = __importDefault(require("@/src/functions/api/su/add-backup/delete-backup"));
|
||||
const grab_config_1 = __importDefault(require("../../utils/backend/config/grab-config"));
|
||||
const grab_user_resource_1 = __importDefault(require("../web-app/db/grab-user-resource"));
|
||||
const add_backup_1 = __importDefault(require("./backups/su/add-backup"));
|
||||
const delete_backup_1 = __importDefault(require("./backups/su/delete-backup"));
|
||||
function handleBackup(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ appBackup, userId, }) {
|
||||
var _b;
|
||||
|
||||
+2
-1
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = dockerTestDbConnection;
|
||||
const debug_log_1 = __importDefault(require("./logging/debug-log"));
|
||||
const mariadb_local_query_1 = __importDefault(require("./mariadb-local-query"));
|
||||
const sleep_1 = __importDefault(require("./sleep"));
|
||||
let checkDbRetries = 0;
|
||||
const MAX_CHECK_DB_RETRIES = 10;
|
||||
const DEFAULT_SLEEP_TIME = 3000;
|
||||
@@ -39,7 +40,7 @@ function dockerTestDbConnection(params) {
|
||||
}
|
||||
else {
|
||||
checkDbRetries++;
|
||||
yield Bun.sleep(sleepTime);
|
||||
yield (0, sleep_1.default)(sleepTime);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
+2
-2
@@ -5,9 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = mariaDBlocalQuery;
|
||||
exports.removeQueryDoubleQuotes = removeQueryDoubleQuotes;
|
||||
const grab_docker_stack_services_names_1 = __importDefault(require("@/package-shared/utils/backend/names/grab-docker-stack-services-names"));
|
||||
const normalize_text_1 = __importDefault(require("@/package-shared/utils/normalize-text"));
|
||||
const grab_docker_stack_services_names_1 = __importDefault(require("./backend/names/grab-docker-stack-services-names"));
|
||||
const execute_1 = __importDefault(require("./execute"));
|
||||
const normalize_text_1 = __importDefault(require("./normalize-text"));
|
||||
function mariaDBlocalQuery(query) {
|
||||
const { dbServiceName, maxScaleServiceName } = (0, grab_docker_stack_services_names_1.default)();
|
||||
const MARIADB_CMD_PREFIX = `docker exec ${dbServiceName} mariadb -u root -p"${process.env.DSQL_MARIADB_ROOT_PASSWORD}"`;
|
||||
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
export default function sleep(time: number): Promise<unknown>;
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = sleep;
|
||||
function sleep(time) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
return new Promise((resolve) => setTimeout(resolve, time));
|
||||
});
|
||||
}
|
||||
+3
-2
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = testDbConnection;
|
||||
const dbHandler_1 = __importDefault(require("../functions/backend/dbHandler"));
|
||||
const sleep_1 = __importDefault(require("./sleep"));
|
||||
let testDbConnRetries = 0;
|
||||
const MAX_TEST_DB_CONN_RETRIES = 10;
|
||||
const SLEEP_TIME = 2000;
|
||||
@@ -28,7 +29,7 @@ function testDbConnection(params) {
|
||||
console.log("Database Connection Complete!");
|
||||
break;
|
||||
}
|
||||
yield Bun.sleep((params === null || params === void 0 ? void 0 : params.sleepTime) || SLEEP_TIME);
|
||||
yield (0, sleep_1.default)((params === null || params === void 0 ? void 0 : params.sleepTime) || SLEEP_TIME);
|
||||
if (testDbConnRetries >
|
||||
((params === null || params === void 0 ? void 0 : params.maxRetries) || MAX_TEST_DB_CONN_RETRIES)) {
|
||||
console.log("Database Connection Failed!");
|
||||
@@ -36,7 +37,7 @@ function testDbConnection(params) {
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
yield Bun.sleep((params === null || params === void 0 ? void 0 : params.sleepTime) || SLEEP_TIME);
|
||||
yield (0, sleep_1.default)((params === null || params === void 0 ? void 0 : params.sleepTime) || SLEEP_TIME);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user