Backup: Create temporary folder to storage files/folders #345

Merged
Sirherobrine23 merged 3 commits from 344-todo-create-tmp-folder-to-backups into main 2022-04-17 20:02:22 +00:00
4 changed files with 61 additions and 19 deletions
Showing only changes of commit ce4416dc96 - Show all commits

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ node_modules/
dist/
src/**/*.d.ts
src/**/*.d.js
backup_*.zip

View File

@@ -1 +1,2 @@
!dist/
!dist/
backup_*.zip

View File

@@ -16,20 +16,24 @@ async function createTempFolder() {
await fsPromise.mkdir(tempFolderPath, { recursive: true });
/**
* Add file to temp Folder
*
* @param filePath - Original file path
* @param onStorage - on Storage temp file path, example: serverName/fileName
* @returns
*/
const addFile = async (filePath: string, onStorage: string = path.basename(filePath)) => {
const addFile = async (filePath: string, onStorage?: string) => {
if (cleaned) throw new Error("Cannot add file after cleaning");
const baseName = path.join(tempFolderPath, path.basename(onStorage));
if (!(fs.existsSync(baseName))) await fsPromise.mkdir(baseName, { recursive: true });
await fsPromise.copyFile(filePath, path.join(tempFolderPath, onStorage));
if (onStorage === undefined) onStorage = path.parse(filePath).name;
const onTempStorage = path.join(tempFolderPath, onStorage);
const basenameFolder = path.parse(onTempStorage).dir;
await fsPromise.mkdir(basenameFolder, { recursive: true }).catch(() => undefined);
await fsPromise.copyFile(filePath, onTempStorage);
return;
}
/**
* Add folder to temp Folder (include subfolders)
*
* @param folderPath - Original folder path
* @param onStorage - on Storage temp folder path, example: serverName/folderName
@@ -42,6 +46,11 @@ async function createTempFolder() {
return;
}
/**
* Get only files from temp folder recursively
*
* @returns list files
*/
const listFiles = async () => {
if (cleaned) throw new Error("Cannot list files after cleaning");
const listFolder = async (folderPath: string) => {
@@ -58,6 +67,11 @@ async function createTempFolder() {
return FilesMaped;
}
/**
* Remove temp folder and lock to add new files and folders
*
* @returns
*/
const cleanFolder = async () => {
if (cleaned) throw new Error("Cannot clean folder after cleaning");
await fse.rm(tempFolderPath, {recursive: true, force: true});
@@ -73,9 +87,7 @@ async function createTempFolder() {
};
}
export default CreateBackup;
export async function CreateBackup(WriteFile: {path: string}|true|false = false) {
if (!(fs.existsSync(backupFolderPath))) await fsPromise.mkdir(backupFolderPath, {recursive: true});
async function genericAddFiles() {
// Create empty zip Buffer
const TempFolder = await createTempFolder()
@@ -97,10 +109,19 @@ export async function CreateBackup(WriteFile: {path: string}|true|false = false)
}
}
}
return TempFolder;
}
// Get Zip Buffer
export default CreateBackup;
export async function CreateBackup(WriteFile: {path: string}|true|false = false) {
if (!(fs.existsSync(backupFolderPath))) await fsPromise.mkdir(backupFolderPath, {recursive: true});
// Add Folders and files
const TempFolder = await genericAddFiles()
// Create empty zip Buffer
const zip = new AdmZip();
for (const file of await TempFolder.listFiles()) zip.addLocalFile(path.join(TempFolder.tempFolderPath, file), file);
for (const file of await TempFolder.listFiles()) zip.addLocalFile(path.join(TempFolder.tempFolderPath, file), (path.sep+path.parse(file).dir));
await TempFolder.cleanFolder();
// Get Zip Buffer
const zipBuffer = zip.toBuffer();
if (typeof WriteFile === "object") {
let BackupFile = path.resolve(backupFolderPath, `${new Date().toString().replace(/[-\(\)\:\s+]/gi, "_")}.zip`);

View File

@@ -5,20 +5,22 @@ import { isValidCron } from "cron-validator";
import * as BdsCore from "../index";
import * as bdsTypes from "../globalType";
import cli_color from "cli-color";
import path from "path";
import { promises as fsPromise } from "fs";
const Yargs = yargs(process.argv.slice(2)).option("platform", {
alias: "p",
describe: "Bds Core Platform",
demandOption: true,
type: "string",
choices: ["bedrock", "java", "pocketmine", "spigot", "dragonfly"],
default: "bedrock"
}).command("download", "Download and Install server", yargs => {
const Yargs = yargs(process.argv.slice(2)).command("download", "Download and Install server", yargs => {
const options = yargs.option("version", {
alias: "v",
describe: "Server Version",
demandOption: true,
type: "string"
}).option("platform", {
alias: "p",
describe: "Bds Core Platform",
demandOption: true,
type: "string",
choices: ["bedrock", "java", "pocketmine", "spigot", "dragonfly"],
default: "bedrock"
}).parseSync();
const Platform = options.platform as bdsTypes.Platform;
console.log("Starting Download...");
@@ -26,8 +28,25 @@ const Yargs = yargs(process.argv.slice(2)).option("platform", {
console.log("Sucess to download server");
console.info("Release date: %s", `${res.Date.getDate()}/${res.Date.getMonth()+1}/${res.Date.getFullYear()}`);
});
}).command("backup", "Create Backups", async yargs => {
const {storage} = yargs.option("storage", {
alias: "s",
describe: "Storage Path",
demandOption: false,
type: "string",
default: path.join(process.cwd(), "backup_"+new Date().toString().replace(/[-\(\)\:\s+]/gi, "_"))+".zip"
}).parseSync();
const zipBuffer = await BdsCore.Backup.CreateBackup(false);
await fsPromise.writeFile(storage, zipBuffer);
}).command("start", "Start Server", async yargs => {
const options = await yargs.option("cronBackup", {
const options = await yargs.option("platform", {
alias: "p",
describe: "Bds Core Platform",
demandOption: true,
type: "string",
choices: ["bedrock", "java", "pocketmine", "spigot", "dragonfly"],
default: "bedrock"
}).option("cronBackup", {
alias: "b",
describe: "cron job to backup server maps",
type: "string"