From b171d46ce07a9ed9a426779fcff92dd1f56b3dd1 Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 15 Feb 2021 18:13:44 +0800 Subject: Minor bugfix: incremental backup without `backups` directory created will fail. --- .../autobackup/AutoBackupDispatcher.java | 50 ++++++++++++++++++++++ .../autobackup/AutoBackupScheduler.java | 50 ---------------------- .../backup/incremental/manager/IncCopyResult.java | 48 ++++++++++----------- .../method/ConfiguredIncrementalBackupMethod.java | 13 ++++-- 4 files changed, 84 insertions(+), 77 deletions(-) create mode 100644 src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java (limited to 'src/main') diff --git a/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java new file mode 100644 index 0000000..32d626e --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java @@ -0,0 +1,50 @@ +package com.keuin.kbackupfabric.autobackup; + +import java.util.Optional; +import java.util.Timer; +import java.util.TimerTask; +import java.util.logging.Logger; + +public class AutoBackupDispatcher { + + private Timer timer = null; + private final Logger logger = Logger.getLogger(AutoBackupDispatcher.class.getName()); + private boolean skipIfNoPlayerLoggedIn; + private final PlayerActivityTracker playerActivityTracker; + + public AutoBackupDispatcher(int intervalSeconds, boolean skipIfNoPlayerLoggedIn, PlayerActivityTracker playerActivityTracker) { + this.skipIfNoPlayerLoggedIn = skipIfNoPlayerLoggedIn; + this.playerActivityTracker = playerActivityTracker; + if (intervalSeconds > 0) + setInterval(intervalSeconds); + } + + public synchronized void setInterval(int intervalSeconds) { + Optional.ofNullable(timer).ifPresent(Timer::cancel); + Timer newTimer = new Timer("AutoBackupTimer"); + newTimer.schedule(new TimerTask() { + @Override + public void run() { + toggleBackup(); + } + }, 0L, intervalSeconds * 1000L); + timer = newTimer; + } + + public void setSkipIfNoPlayerLoggedIn(boolean skipIfNoPlayerLoggedIn) { + this.skipIfNoPlayerLoggedIn = skipIfNoPlayerLoggedIn; + } + + public synchronized void stop() { + timer.cancel(); + timer = null; + } + + private void toggleBackup() { + if (playerActivityTracker.getCheckpoint() || !skipIfNoPlayerLoggedIn) { + logger.info("Interval backup event is triggered."); + // TODO: perform a backup + } + } + +} diff --git a/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java deleted file mode 100644 index c35a35d..0000000 --- a/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.keuin.kbackupfabric.autobackup; - -import java.util.Optional; -import java.util.Timer; -import java.util.TimerTask; -import java.util.logging.Logger; - -public class AutoBackupScheduler { - - private Timer timer = null; - private final Logger logger = Logger.getLogger(AutoBackupScheduler.class.getName()); - private boolean skipIfNoPlayerLoggedIn; - private final PlayerActivityTracker playerActivityTracker; - - public AutoBackupScheduler(int intervalSeconds, boolean skipIfNoPlayerLoggedIn, PlayerActivityTracker playerActivityTracker) { - this.skipIfNoPlayerLoggedIn = skipIfNoPlayerLoggedIn; - this.playerActivityTracker = playerActivityTracker; - if (intervalSeconds > 0) - setInterval(intervalSeconds); - } - - public synchronized void setInterval(int intervalSeconds) { - Optional.ofNullable(timer).ifPresent(Timer::cancel); - Timer newTimer = new Timer("AutoBackupTimer"); - newTimer.schedule(new TimerTask() { - @Override - public void run() { - toggleBackup(); - } - }, 0L, intervalSeconds * 1000L); - timer = newTimer; - } - - public void setSkipIfNoPlayerLoggedIn(boolean skipIfNoPlayerLoggedIn) { - this.skipIfNoPlayerLoggedIn = skipIfNoPlayerLoggedIn; - } - - public synchronized void stop() { - timer.cancel(); - timer = null; - } - - private void toggleBackup() { - if (playerActivityTracker.getCheckpoint() || !skipIfNoPlayerLoggedIn) { - logger.info("Interval backup event is triggered."); - // TODO: perform a backup - } - } - -} diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncCopyResult.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncCopyResult.java index 6661a27..3323348 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncCopyResult.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncCopyResult.java @@ -11,17 +11,17 @@ import java.util.Objects; public class IncCopyResult { private final int totalFiles; - private final int filesCopied; - private final long bytesCopied; - private final long bytesTotal; + private final int copiedFiles; + private final long copiedBytes; + private final long totalBytes; public static final IncCopyResult ZERO = new IncCopyResult(0, 0, 0, 0); - public IncCopyResult(int totalFiles, int filesCopied, long bytesCopied, long bytesTotal) { + public IncCopyResult(int totalFiles, int copiedFiles, long copiedBytes, long totalBytes) { this.totalFiles = totalFiles; - this.filesCopied = filesCopied; - this.bytesCopied = bytesCopied; - this.bytesTotal = bytesTotal; + this.copiedFiles = copiedFiles; + this.copiedBytes = copiedBytes; + this.totalBytes = totalBytes; } /** @@ -38,8 +38,8 @@ public class IncCopyResult { * * @return file count. */ - public int getFilesCopied() { - return filesCopied; + public int getCopiedFiles() { + return copiedFiles; } /** @@ -47,8 +47,8 @@ public class IncCopyResult { * * @return bytes. */ - public long getBytesCopied() { - return bytesCopied; + public long getCopiedBytes() { + return copiedBytes; } /** @@ -56,8 +56,8 @@ public class IncCopyResult { * * @return bytes. */ - public long getBytesTotal() { - return bytesTotal; + public long getTotalBytes() { + return totalBytes; } /** @@ -70,9 +70,9 @@ public class IncCopyResult { Objects.requireNonNull(a); return new IncCopyResult( totalFiles + a.totalFiles, - filesCopied + a.filesCopied, - bytesCopied + a.bytesCopied, - bytesTotal + a.bytesTotal + copiedFiles + a.copiedFiles, + copiedBytes + a.copiedBytes, + totalBytes + a.totalBytes ); } @@ -82,25 +82,25 @@ public class IncCopyResult { if (o == null || getClass() != o.getClass()) return false; IncCopyResult that = (IncCopyResult) o; return totalFiles == that.totalFiles && - filesCopied == that.filesCopied && - bytesCopied == that.bytesCopied && - bytesTotal == that.bytesTotal; + copiedFiles == that.copiedFiles && + copiedBytes == that.copiedBytes && + totalBytes == that.totalBytes; } @Override public int hashCode() { - return Objects.hash(totalFiles, filesCopied, bytesCopied, bytesTotal); + return Objects.hash(totalFiles, copiedFiles, copiedBytes, totalBytes); } @Override public String toString() { return String.format( "File(s) added: %d (%s in size, totally %d files). Total backup-ed files size: %s (%.2f%% reused)", - filesCopied, - BackupFilesystemUtil.getFriendlyFileSizeString(bytesCopied), + copiedFiles, + BackupFilesystemUtil.getFriendlyFileSizeString(copiedBytes), totalFiles, - BackupFilesystemUtil.getFriendlyFileSizeString(bytesTotal), - (1 - 1.0f * bytesCopied / bytesTotal) * 100 + BackupFilesystemUtil.getFriendlyFileSizeString(totalBytes), + (1 - 1.0f * copiedBytes / totalBytes) * 100 ); } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java index 5fe532a..0c202e1 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java @@ -75,6 +75,13 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod // legacy index file // ObjectCollectionSerializer.toFile(collection, new File(backupIndexFileSaveDirectory, backupIndexFileName)); + // create directory + final File indexDirectoryFile = new File(backupIndexFileSaveDirectory); + if (indexDirectoryFile.isFile()) + throw new IOException("There is a file which has the same name with index directory"); + if (!indexDirectoryFile.isDirectory() && !indexDirectoryFile.mkdirs()) + throw new IOException("Backup index save directory does not exist and we failed to create it"); + // newer saved info (with metadata) File indexFile = new File(backupIndexFileSaveDirectory, backupIndexFileName); BackupFileNameEncoder.BackupBasicInformation info = IncrementalBackupFileNameEncoder.INSTANCE.decode(backupIndexFileName); @@ -82,9 +89,9 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod collection, info.customName, info.time.atZone(ZoneId.systemDefault()), - copyResult.getBytesTotal(), - copyResult.getBytesCopied(), - copyResult.getFilesCopied(), + copyResult.getTotalBytes(), + copyResult.getCopiedBytes(), + copyResult.getCopiedFiles(), copyResult.getTotalFiles() )); -- cgit v1.2.3