Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
14 changes: 9 additions & 5 deletions assets/config.ini
Original file line number Diff line number Diff line change
@@ -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
14 changes: 10 additions & 4 deletions src/OldBackupHelper/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <filesystem>
#include <magic_enum.hpp>
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down
47 changes: 47 additions & 0 deletions src/OldBackupHelper/Interval.cpp
Original file line number Diff line number Diff line change
@@ -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 <atomic>
#include <chrono>

std::atomic_bool isRunning = false;

std::chrono::seconds GetNow() {
return std::chrono::duration_cast<std::chrono::seconds>(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; };
4 changes: 4 additions & 0 deletions src/OldBackupHelper/Interval.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

void StartInterval();
void StopInterval();