Backup: Create temporary folder to storage files/folders #345
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ node_modules/
|
|||||||
dist/
|
dist/
|
||||||
src/**/*.d.ts
|
src/**/*.d.ts
|
||||||
src/**/*.d.js
|
src/**/*.d.js
|
||||||
|
backup_*.zip
|
@@ -1 +1,2 @@
|
|||||||
!dist/
|
!dist/
|
||||||
|
backup_*.zip
|
@@ -16,20 +16,24 @@ async function createTempFolder() {
|
|||||||
await fsPromise.mkdir(tempFolderPath, { recursive: true });
|
await fsPromise.mkdir(tempFolderPath, { recursive: true });
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Add file to temp Folder
|
||||||
*
|
*
|
||||||
* @param filePath - Original file path
|
* @param filePath - Original file path
|
||||||
* @param onStorage - on Storage temp file path, example: serverName/fileName
|
* @param onStorage - on Storage temp file path, example: serverName/fileName
|
||||||
* @returns
|
* @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");
|
if (cleaned) throw new Error("Cannot add file after cleaning");
|
||||||
const baseName = path.join(tempFolderPath, path.basename(onStorage));
|
if (onStorage === undefined) onStorage = path.parse(filePath).name;
|
||||||
if (!(fs.existsSync(baseName))) await fsPromise.mkdir(baseName, { recursive: true });
|
const onTempStorage = path.join(tempFolderPath, onStorage);
|
||||||
await fsPromise.copyFile(filePath, path.join(tempFolderPath, onStorage));
|
const basenameFolder = path.parse(onTempStorage).dir;
|
||||||
|
await fsPromise.mkdir(basenameFolder, { recursive: true }).catch(() => undefined);
|
||||||
|
await fsPromise.copyFile(filePath, onTempStorage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Add folder to temp Folder (include subfolders)
|
||||||
*
|
*
|
||||||
* @param folderPath - Original folder path
|
* @param folderPath - Original folder path
|
||||||
* @param onStorage - on Storage temp folder path, example: serverName/folderName
|
* @param onStorage - on Storage temp folder path, example: serverName/folderName
|
||||||
@@ -42,6 +46,11 @@ async function createTempFolder() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get only files from temp folder recursively
|
||||||
|
*
|
||||||
|
* @returns list files
|
||||||
|
*/
|
||||||
const listFiles = async () => {
|
const listFiles = async () => {
|
||||||
if (cleaned) throw new Error("Cannot list files after cleaning");
|
if (cleaned) throw new Error("Cannot list files after cleaning");
|
||||||
const listFolder = async (folderPath: string) => {
|
const listFolder = async (folderPath: string) => {
|
||||||
@@ -58,6 +67,11 @@ async function createTempFolder() {
|
|||||||
return FilesMaped;
|
return FilesMaped;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove temp folder and lock to add new files and folders
|
||||||
|
*
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
const cleanFolder = async () => {
|
const cleanFolder = async () => {
|
||||||
if (cleaned) throw new Error("Cannot clean folder after cleaning");
|
if (cleaned) throw new Error("Cannot clean folder after cleaning");
|
||||||
await fse.rm(tempFolderPath, {recursive: true, force: true});
|
await fse.rm(tempFolderPath, {recursive: true, force: true});
|
||||||
@@ -73,9 +87,7 @@ async function createTempFolder() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default CreateBackup;
|
async function genericAddFiles() {
|
||||||
export async function CreateBackup(WriteFile: {path: string}|true|false = false) {
|
|
||||||
if (!(fs.existsSync(backupFolderPath))) await fsPromise.mkdir(backupFolderPath, {recursive: true});
|
|
||||||
// Create empty zip Buffer
|
// Create empty zip Buffer
|
||||||
const TempFolder = await createTempFolder()
|
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();
|
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();
|
const zipBuffer = zip.toBuffer();
|
||||||
if (typeof WriteFile === "object") {
|
if (typeof WriteFile === "object") {
|
||||||
let BackupFile = path.resolve(backupFolderPath, `${new Date().toString().replace(/[-\(\)\:\s+]/gi, "_")}.zip`);
|
let BackupFile = path.resolve(backupFolderPath, `${new Date().toString().replace(/[-\(\)\:\s+]/gi, "_")}.zip`);
|
||||||
|
@@ -5,20 +5,22 @@ import { isValidCron } from "cron-validator";
|
|||||||
import * as BdsCore from "../index";
|
import * as BdsCore from "../index";
|
||||||
import * as bdsTypes from "../globalType";
|
import * as bdsTypes from "../globalType";
|
||||||
import cli_color from "cli-color";
|
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", {
|
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",
|
alias: "p",
|
||||||
describe: "Bds Core Platform",
|
describe: "Bds Core Platform",
|
||||||
demandOption: true,
|
demandOption: true,
|
||||||
type: "string",
|
type: "string",
|
||||||
choices: ["bedrock", "java", "pocketmine", "spigot", "dragonfly"],
|
choices: ["bedrock", "java", "pocketmine", "spigot", "dragonfly"],
|
||||||
default: "bedrock"
|
default: "bedrock"
|
||||||
}).command("download", "Download and Install server", yargs => {
|
|
||||||
const options = yargs.option("version", {
|
|
||||||
alias: "v",
|
|
||||||
describe: "Server Version",
|
|
||||||
demandOption: true,
|
|
||||||
type: "string"
|
|
||||||
}).parseSync();
|
}).parseSync();
|
||||||
const Platform = options.platform as bdsTypes.Platform;
|
const Platform = options.platform as bdsTypes.Platform;
|
||||||
console.log("Starting Download...");
|
console.log("Starting Download...");
|
||||||
@@ -26,8 +28,25 @@ const Yargs = yargs(process.argv.slice(2)).option("platform", {
|
|||||||
console.log("Sucess to download server");
|
console.log("Sucess to download server");
|
||||||
console.info("Release date: %s", `${res.Date.getDate()}/${res.Date.getMonth()+1}/${res.Date.getFullYear()}`);
|
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 => {
|
}).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",
|
alias: "b",
|
||||||
describe: "cron job to backup server maps",
|
describe: "cron job to backup server maps",
|
||||||
type: "string"
|
type: "string"
|
||||||
|
Reference in New Issue
Block a user