-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new command to delete selected backup increments safely (#244)
- Adds new command to delete selected backup increments - Implements parser and properties for the new command - Moves increment deletion logic to ManifestManager to make it reusable - Implements new controller to take care of increment deletions - Adds new tests - Updates documentation Resolves #190 {minor} Signed-off-by: Esta Nagy <[email protected]>
- Loading branch information
Showing
17 changed files
with
483 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,5 @@ out/ | |
|
||
### VS Code ### | ||
*/.vscode/ | ||
|
||
file-barj.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...e/src/main/java/com/github/nagyesta/filebarj/core/delete/IncrementDeletionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.github.nagyesta.filebarj.core.delete; | ||
|
||
import com.github.nagyesta.filebarj.core.common.ManifestManager; | ||
import com.github.nagyesta.filebarj.core.common.ManifestManagerImpl; | ||
import com.github.nagyesta.filebarj.core.model.BackupIncrementManifest; | ||
import com.github.nagyesta.filebarj.core.model.enums.BackupType; | ||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.nio.file.Path; | ||
import java.security.PrivateKey; | ||
import java.util.Comparator; | ||
import java.util.SortedMap; | ||
|
||
/** | ||
* Controller for the backup increment deletion task. | ||
*/ | ||
@Slf4j | ||
public class IncrementDeletionController { | ||
|
||
private final SortedMap<Long, BackupIncrementManifest> manifests; | ||
private final @NonNull Path backupDirectory; | ||
private final ManifestManager manifestManager; | ||
|
||
/** | ||
* Creates a new instance and initializes it for the specified job. | ||
* | ||
* @param backupDirectory the directory where the backup files are located | ||
* @param fileNamePrefix the prefix of the backup file names | ||
* @param kek The key encryption key we want to use to decrypt the files (optional). | ||
* If null, no decryption will be performed. | ||
*/ | ||
public IncrementDeletionController( | ||
@NonNull final Path backupDirectory, | ||
@NonNull final String fileNamePrefix, | ||
@Nullable final PrivateKey kek) { | ||
this.manifestManager = new ManifestManagerImpl(); | ||
this.backupDirectory = backupDirectory; | ||
this.manifests = this.manifestManager.loadAll(this.backupDirectory, fileNamePrefix, kek); | ||
} | ||
|
||
/** | ||
* Deletes the incremental backups which were created after the specified time until the next | ||
* full backup. | ||
* | ||
* @param startingWithEpochSeconds the start time of the first deleted increment | ||
*/ | ||
public void deleteIncrementsUntilNextFullBackupAfter(final long startingWithEpochSeconds) { | ||
final var incrementsStartingWithThreshold = this.manifests.values().stream() | ||
.sorted(Comparator.comparing(BackupIncrementManifest::getStartTimeUtcEpochSeconds)) | ||
.filter(manifest -> manifest.getStartTimeUtcEpochSeconds() >= startingWithEpochSeconds) | ||
.toList(); | ||
if (incrementsStartingWithThreshold.isEmpty()) { | ||
throw new IllegalArgumentException("No backups found after: " + startingWithEpochSeconds); | ||
} | ||
if (incrementsStartingWithThreshold.get(0).getStartTimeUtcEpochSeconds() != startingWithEpochSeconds) { | ||
throw new IllegalArgumentException("Unable to find backup which started at: " + startingWithEpochSeconds); | ||
} | ||
for (final var current : incrementsStartingWithThreshold) { | ||
if (current.getStartTimeUtcEpochSeconds() > startingWithEpochSeconds && current.getBackupType() == BackupType.FULL) { | ||
break; | ||
} | ||
manifestManager.deleteIncrement(backupDirectory, current); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.