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 878e93f..0589c3a 100644 --- a/assets/config.ini +++ b/assets/config.ini @@ -1,15 +1,19 @@ [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 + CommandPermissionLevel=1 \ No newline at end of file diff --git a/src/OldBackupHelper/Entry.cpp b/src/OldBackupHelper/Entry.cpp index f60b6f8..3eeff6b 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(), ""); 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..b0525c3 --- /dev/null +++ b/src/OldBackupHelper/Interval.cpp @@ -0,0 +1,47 @@ +#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 +#include + +std::atomic_bool isRunning = false; + +std::chrono::seconds GetNow() { + return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); +} + +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(); + backup_helper::getConfig().SetLongValue("BackFile", "lastTime", GetNow().count()); + backup_helper::getConfig().SaveFile(backup_helper::getConfigPath().c_str()); + } + + 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..5019230 --- /dev/null +++ b/src/OldBackupHelper/Interval.h @@ -0,0 +1,4 @@ +#pragma once + +void StartInterval(); +void StopInterval(); \ No newline at end of file