From cdd8427feaf67f1fea1f07918e121def57f98700 Mon Sep 17 00:00:00 2001 From: leaftail1880 <110915645+leaftail1880@users.noreply.github.com> Date: Sat, 16 Aug 2025 21:42:23 +0300 Subject: [PATCH 1/4] feat: add backupinterval --- assets/config.ini | 13 +++++---- src/OldBackupHelper/Backup.cpp | 2 ++ src/OldBackupHelper/Entry.cpp | 14 ++++++--- src/OldBackupHelper/Interval.cpp | 49 ++++++++++++++++++++++++++++++++ src/OldBackupHelper/Interval.h | 5 ++++ 5 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 src/OldBackupHelper/Interval.cpp create mode 100644 src/OldBackupHelper/Interval.h diff --git a/assets/config.ini b/assets/config.ini index 7981d8b..93addae 100644 --- a/assets/config.ini +++ b/assets/config.ini @@ -1,13 +1,16 @@ [Main] -; 备份存档保存的最长时间,单位:天 +; 备份存档保存的最长时间,单位:天 | Maximum backup storage time in days MaxStorageTime=7 -; 备份文件夹位置 +; 备份文件夹位置,可以是相对路径或绝对路径 | Backup folder location (relative or absolute path) BackupPath=.\backup -; 备份文件压缩等级,可选等级有0,1,3,5,7,9 -; 默认为0,即仅打包 +; 备份文件压缩等级,可选等级有0,1,3,5,7,9 | Backup compression level (0=store only, 1=fastest, 9=best) +; 默认为0,即仅打包 | Default is 0 (no compression, just archive) Compress=0 -; 等待压缩的最长时间,单位:秒,如果为0则无限等待 +; 等待压缩的最长时间,单位:秒,如果为0则无限等待 | Maximum wait time for compression in seconds (0=unlimited) MaxWaitForZip=1800 + +; 定期自动备份间隔时间(单位:小时),设为0则禁用自动备份 | Automatic backup interval in hours (0=disabled) +BackupInterval=24 \ No newline at end of file diff --git a/src/OldBackupHelper/Backup.cpp b/src/OldBackupHelper/Backup.cpp index bde9ce8..c2c2626 100644 --- a/src/OldBackupHelper/Backup.cpp +++ b/src/OldBackupHelper/Backup.cpp @@ -1,5 +1,6 @@ #include "Backup.h" #include "Entry.h" +#include "Interval.h" #include "Tools.h" #include "ll/api/chrono/GameChrono.h" #include "ll/api/coro/CoroTask.h" @@ -48,6 +49,7 @@ void ResumeBackup(); void SuccessEnd() { SendFeedback(playerUuid, "Backup ended successfully"_tr()); playerUuid = mce::UUID::EMPTY(); + IntervalOnBackupFinished(); // The isWorking assignment here has been moved to line 321 } diff --git a/src/OldBackupHelper/Entry.cpp b/src/OldBackupHelper/Entry.cpp index 5c14fc7..58b3487 100644 --- a/src/OldBackupHelper/Entry.cpp +++ b/src/OldBackupHelper/Entry.cpp @@ -2,15 +2,17 @@ #include "Backup.h" #include "BackupCommand.h" +#include "Interval.h" #include "ll/api/Expected.h" #include "ll/api/i18n/I18n.h" #include "ll/api/memory/Hook.h" #include "ll/api/mod/RegisterHelper.h" #include "ll/api/service/Bedrock.h" -#include "mc/server/commands/CommandOrigin.h" +#include "ll/api/utils/ErrorUtils.h" #include "mc/server/PropertiesSettings.h" +#include "mc/server/commands/CommandOrigin.h" #include "mc/server/commands/StopCommand.h" -#include "ll/api/utils/ErrorUtils.h" + #include #include @@ -55,7 +57,7 @@ BackupHelper& BackupHelper::getInstance() { bool BackupHelper::load() { Raw_IniOpen(getConfigPath().string(), ""); auto& instance = ll::i18n::getInstance(); - auto result = instance.load(getSelf().getLangDir()); + auto result = instance.load(getSelf().getLangDir()); if (!result) { ll::error_utils::printCurrentException(getSelf().getLogger()); return false; @@ -66,10 +68,14 @@ bool BackupHelper::load() { bool BackupHelper::enable() { RegisterCommand(); + StartInterval(); return true; } -bool BackupHelper::disable() { return true; } +bool BackupHelper::disable() { + StopInterval(); + return true; +} // 存档开始加载前替换存档文件 LL_AUTO_TYPE_INSTANCE_HOOK( diff --git a/src/OldBackupHelper/Interval.cpp b/src/OldBackupHelper/Interval.cpp new file mode 100644 index 0000000..b0e2525 --- /dev/null +++ b/src/OldBackupHelper/Interval.cpp @@ -0,0 +1,49 @@ +#include "Interval.h" +#include "Backup.h" +#include "Entry.h" +#include "ll/api/chrono/GameChrono.h" +#include "ll/api/coro/CoroTask.h" +#include "ll/api/thread/ServerThreadExecutor.h" +#include + +bool isRunning = false; + +std::chrono::seconds GetNow() { + return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); +} + +void IntervalOnBackupFinished() { + backup_helper::getConfig().SetLongValue("BackFile", "lastTime", GetNow().count()); + backup_helper::getConfig().SaveFile(backup_helper::getConfigPath().c_str()); +} + +void StartInterval() { + long intervalHours = backup_helper::getConfig().GetLongValue("Main", "BackupInterval", 0); + if (intervalHours == 0) { + // Interval not set + return; + }; + + isRunning = true; + + ll::coro::keepThis([intervalHours]() -> ll::coro::CoroTask<> { + while (isRunning) { + auto lastBackupTime = backup_helper::getConfig().GetLongValue("BackFile", "lastTime", GetNow().count()); + auto lastBackupSeconds = std::chrono::seconds(lastBackupTime); + + // Seconds + ll::chrono::game::ticks waitTime = (lastBackupSeconds + std::chrono::hours(intervalHours)) - GetNow(); + co_await (waitTime); + + if (!isRunning) co_return; + + if (!GetIsWorking()) { + StartBackup(); + } + + co_await ll::chrono::game::ticks(20); + } + }).launch(ll::thread::ServerThreadExecutor::getDefault()); +} + +void StopInterval() { isRunning = false; }; diff --git a/src/OldBackupHelper/Interval.h b/src/OldBackupHelper/Interval.h new file mode 100644 index 0000000..d974083 --- /dev/null +++ b/src/OldBackupHelper/Interval.h @@ -0,0 +1,5 @@ +#pragma once + +void StartInterval(); +void StopInterval(); +void IntervalOnBackupFinished(); \ No newline at end of file From 64f28d97d7b77b4200b93168d63fafc5369d0bcf Mon Sep 17 00:00:00 2001 From: leaftail1880 <110915645+leaftail1880@users.noreply.github.com> Date: Sat, 16 Aug 2025 21:47:27 +0300 Subject: [PATCH 2/4] doc: update doc --- README.md | 16 +++++++++------- assets/config.ini | 3 ++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e4695e6..1662c2e 100644 --- a/README.md +++ b/README.md @@ -21,24 +21,26 @@ lip install github.com/ShrBox/BackupHelper These commands can be executed at BDS console, or by OPs in game. - - ## Config file At `plugins/BackupHelper/config.ini`, with comments in file ```ini [Main] -; 备份存档保存的最长时间,单位:天 +; 备份存档保存的最长时间,单位:天 | Maximum backup storage time in days MaxStorageTime=7 -; 备份文件夹位置 +; 备份文件夹位置(相对于服务器根目录) | Backup folder location (relative to server root) BackupPath=.\backup -; 备份文件压缩等级,可选等级有0,1,3,5,7,9 -; 默认为0,即仅打包 +; 备份文件压缩等级,可选等级有0,1,3,5,7,9 | Backup compression level (0=store only, 1=fastest, 9=best) +; 默认为0,即仅打包 | Default is 0 (no compression, just archive) Compress=0 -; 等待压缩的最长时间,单位:秒,如果为0则无限等待 +; 等待压缩的最长时间,单位:秒,如果为0则无限等待 | Maximum wait time for compression in seconds (0=unlimited) MaxWaitForZip=1800 + +; 定期自动备份间隔时间(单位:小时),设为0则禁用自动备份 | Automatic backup interval in hours (0=disabled) +; 注意:服务器重启不会影响定时器 | Note: Server restart does not affect the timer +BackupInterval=24 ``` diff --git a/assets/config.ini b/assets/config.ini index 93addae..7eaf2b9 100644 --- a/assets/config.ini +++ b/assets/config.ini @@ -2,7 +2,7 @@ ; 备份存档保存的最长时间,单位:天 | Maximum backup storage time in days MaxStorageTime=7 -; 备份文件夹位置,可以是相对路径或绝对路径 | Backup folder location (relative or absolute path) +; 备份文件夹位置(相对于服务器根目录) | Backup folder location (relative to server root) BackupPath=.\backup ; 备份文件压缩等级,可选等级有0,1,3,5,7,9 | Backup compression level (0=store only, 1=fastest, 9=best) @@ -13,4 +13,5 @@ Compress=0 MaxWaitForZip=1800 ; 定期自动备份间隔时间(单位:小时),设为0则禁用自动备份 | Automatic backup interval in hours (0=disabled) +; 注意:服务器重启不会影响定时器 | Note: Server restart does not affect the timer BackupInterval=24 \ No newline at end of file From dbe300434f01bca440e5338836c29ee42b8b4180 Mon Sep 17 00:00:00 2001 From: leaftail1880 <110915645+leaftail1880@users.noreply.github.com> Date: Tue, 9 Sep 2025 21:02:40 +0300 Subject: [PATCH 3/4] fix interval time save --- src/OldBackupHelper/Backup.cpp | 2 -- src/OldBackupHelper/Interval.cpp | 7 ++----- src/OldBackupHelper/Interval.h | 3 +-- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/OldBackupHelper/Backup.cpp b/src/OldBackupHelper/Backup.cpp index c2c2626..bde9ce8 100644 --- a/src/OldBackupHelper/Backup.cpp +++ b/src/OldBackupHelper/Backup.cpp @@ -1,6 +1,5 @@ #include "Backup.h" #include "Entry.h" -#include "Interval.h" #include "Tools.h" #include "ll/api/chrono/GameChrono.h" #include "ll/api/coro/CoroTask.h" @@ -49,7 +48,6 @@ void ResumeBackup(); void SuccessEnd() { SendFeedback(playerUuid, "Backup ended successfully"_tr()); playerUuid = mce::UUID::EMPTY(); - IntervalOnBackupFinished(); // The isWorking assignment here has been moved to line 321 } diff --git a/src/OldBackupHelper/Interval.cpp b/src/OldBackupHelper/Interval.cpp index b0e2525..1a159b5 100644 --- a/src/OldBackupHelper/Interval.cpp +++ b/src/OldBackupHelper/Interval.cpp @@ -12,11 +12,6 @@ std::chrono::seconds GetNow() { return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); } -void IntervalOnBackupFinished() { - backup_helper::getConfig().SetLongValue("BackFile", "lastTime", GetNow().count()); - backup_helper::getConfig().SaveFile(backup_helper::getConfigPath().c_str()); -} - void StartInterval() { long intervalHours = backup_helper::getConfig().GetLongValue("Main", "BackupInterval", 0); if (intervalHours == 0) { @@ -39,6 +34,8 @@ void StartInterval() { if (!GetIsWorking()) { StartBackup(); + backup_helper::getConfig().SetLongValue("BackFile", "lastTime", GetNow().count()); + backup_helper::getConfig().SaveFile(backup_helper::getConfigPath().c_str()); } co_await ll::chrono::game::ticks(20); diff --git a/src/OldBackupHelper/Interval.h b/src/OldBackupHelper/Interval.h index d974083..5019230 100644 --- a/src/OldBackupHelper/Interval.h +++ b/src/OldBackupHelper/Interval.h @@ -1,5 +1,4 @@ #pragma once void StartInterval(); -void StopInterval(); -void IntervalOnBackupFinished(); \ No newline at end of file +void StopInterval(); \ No newline at end of file From 9285cc845788f9ed5b00259a4e0abc3ff0debe87 Mon Sep 17 00:00:00 2001 From: leaftail1880 <110915645+leaftail1880@users.noreply.github.com> Date: Tue, 9 Sep 2025 21:10:09 +0300 Subject: [PATCH 4/4] improve interval --- src/OldBackupHelper/Interval.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OldBackupHelper/Interval.cpp b/src/OldBackupHelper/Interval.cpp index 1a159b5..b0525c3 100644 --- a/src/OldBackupHelper/Interval.cpp +++ b/src/OldBackupHelper/Interval.cpp @@ -4,9 +4,10 @@ #include "ll/api/chrono/GameChrono.h" #include "ll/api/coro/CoroTask.h" #include "ll/api/thread/ServerThreadExecutor.h" +#include #include -bool isRunning = false; +std::atomic_bool isRunning = false; std::chrono::seconds GetNow() { return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch());