From e6ea3d45d39ee8a0e1666b614c616479b97f6dec Mon Sep 17 00:00:00 2001 From: Musselman Date: Tue, 5 Nov 2024 16:23:23 -0600 Subject: [PATCH 1/2] Add Multi-Folder Backups --- example-macros/auto-backup/index.ts | 33 +++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/example-macros/auto-backup/index.ts b/example-macros/auto-backup/index.ts index 3d3e4c9f..acd75de4 100644 --- a/example-macros/auto-backup/index.ts +++ b/example-macros/auto-backup/index.ts @@ -4,6 +4,7 @@ import { sleep } from "https://deno.land/x/sleep@v1.2.1/mod.ts"; import { EventStream } from "https://raw.githubusercontent.com/Lodestone-Team/lodestone-macro-lib/main/events.ts"; import { lodestoneVersion } from "https://raw.githubusercontent.com/Lodestone-Team/lodestone-macro-lib/main/prelude.ts"; import { MinecraftJavaInstance } from "https://raw.githubusercontent.com/Lodestone-Team/lodestone-macro-lib/main/instance.ts"; +import { compress } from "https://deno.land/x/zip@v1.2.5/mod.ts"; const currentInstance = await MinecraftJavaInstance.current(); @@ -18,6 +19,10 @@ class LodestoneConfig { backupFolderRelative: string = "backups"; // How long to wait between backups in seconds delaySec: number = 3600; + // Comma-separated list of folders to back up + foldersToBackup: string = "world, world_nether, world_the_end"; + // Compress the backup? + compressBackup: boolean = true; } // not technically necessary, but it's a good practice to appease the linter @@ -38,11 +43,31 @@ while (true) { const now = new Date(); const now_str = format(now, "yy-MM-dd_HH"); - try { - await copy(`${instancePath}/world`, `${backupFolder}/backup_${now_str}`); - } catch (e) { - console.log(e); + const combinedBackupFolder = `${backupFolder}/backup_${now_str}`; + // Split the string by commas to get an array of folders and iterate over them + for (const folder of config.foldersToBackup.split(',')) { + const trimmedFolder = folder.trim(); + try { + const sourceFolder = `${instancePath}/${trimmedFolder}`; + const destinationFolder = `${combinedBackupFolder}/${trimmedFolder}`; + await eventStream.emitConsoleOut(`[Backup Macro] Backing up ${trimmedFolder}...`); + await copy(sourceFolder, destinationFolder); + await eventStream.emitConsoleOut(`[Backup Macro] Backup of ${trimmedFolder} completed.`); + } catch (e) { + console.log(`[Backup Macro] Error backing up ${trimmedFolder}:`, e); + } + } + + if (config.compressBackup) { + try { + await eventStream.emitConsoleOut(`[Backup Macro] Compressing backup...`); + await compress(`${combinedBackupFolder}`, `${instancePath}/${config.backupFolderRelative}/${combinedBackupFolder}.zip`); + await eventStream.emitConsoleOut(`[Backup Macro] Compression completed.`); + } catch (e) { + console.log(`[Backup Macro] Error compressing backup folder:`, e); + } } await sleep(config.delaySec); } + From a81ebf3eb455222cc0c65ef1f51b0138a5df7ac3 Mon Sep 17 00:00:00 2001 From: Musselman Date: Wed, 6 Nov 2024 13:12:32 -0600 Subject: [PATCH 2/2] Fix backup path for zip --- example-macros/auto-backup/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example-macros/auto-backup/index.ts b/example-macros/auto-backup/index.ts index acd75de4..ea3f023c 100644 --- a/example-macros/auto-backup/index.ts +++ b/example-macros/auto-backup/index.ts @@ -60,8 +60,8 @@ while (true) { if (config.compressBackup) { try { - await eventStream.emitConsoleOut(`[Backup Macro] Compressing backup...`); - await compress(`${combinedBackupFolder}`, `${instancePath}/${config.backupFolderRelative}/${combinedBackupFolder}.zip`); + await eventStream.emitConsoleOut(`[Backup Macro] Compressing backup folder: ${combinedBackupFolder}...`); + await compress(`${combinedBackupFolder}/`, `${combinedBackupFolder}.zip`); await eventStream.emitConsoleOut(`[Backup Macro] Compression completed.`); } catch (e) { console.log(`[Backup Macro] Error compressing backup folder:`, e);