Skip to content

Commit

Permalink
Save the OS name in the backup (#152)
Browse files Browse the repository at this point in the history
- Defines OS enum
- Implements OS name parsing logic
- Adds OS name to manifests
- Shows OS name during restore

Resolves #151
{minor}

Signed-off-by: Esta Nagy <[email protected]>
  • Loading branch information
nagyesta authored Feb 21, 2024
1 parent 1c723b9 commit f7df8ae
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.github.nagyesta.filebarj.core.model.*;
import com.github.nagyesta.filebarj.core.model.enums.BackupType;
import com.github.nagyesta.filebarj.core.model.enums.Change;
import com.github.nagyesta.filebarj.core.model.enums.OperatingSystem;
import com.github.nagyesta.filebarj.core.util.LogUtil;
import com.github.nagyesta.filebarj.core.util.OsUtil;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -49,6 +51,7 @@ public BackupIncrementManifest generateManifest(
.archivedEntries(new ConcurrentHashMap<>())
.startTimeUtcEpochSeconds(startTimeEpochSecond)
.fileNamePrefix(fileNamePrefix)
.operatingSystem(OsUtil.getRawOsName())
.build();
Optional.ofNullable(jobConfiguration.getEncryptionKey())
.ifPresent(manifest::generateDataEncryptionKeys);
Expand Down Expand Up @@ -171,6 +174,7 @@ public RestoreManifest mergeForRestore(
.versions(versions)
.configuration(lastIncrementManifest.getConfiguration())
.fileNamePrefixes(fileNamePrefixes)
.operatingSystem(OperatingSystem.forOsName(lastIncrementManifest.getOperatingSystem()))
.files(files)
.archivedEntries(archivedEntries)
.encryptionKeys(keys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public String convertToSummaryString(final @NonNull BackupIncrementManifest mani
+ "\tStarted at : " + Instant.ofEpochSecond(epochSeconds) + " (Epoch seconds: " + epochSeconds + ")\n"
+ "\tContains " + manifest.getFiles().size() + " files (" + totalSize + " MiB)\n"
+ "\tVersions : " + manifest.getVersions() + "\n"
+ "\tOS name : " + Optional.ofNullable(manifest.getOperatingSystem()).orElse("unknown") + "\n"
+ "\tEncrypted : " + Optional.ofNullable(manifest.getConfiguration().getEncryptionKey()).isPresent() + "\n"
+ "\tHash alg. : " + manifest.getConfiguration().getHashAlgorithm().name() + "\n"
+ "\tCompression: " + manifest.getConfiguration().getCompression().name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public class BackupIncrementManifest extends EncryptionKeyStore {
@NonNull
@JsonProperty("backup_type")
private BackupType backupType;
/**
* The OS of the backup.
*/
@JsonProperty("operating_system")
private String operatingSystem;
/**
* The snapshot of the backup configuration at the time of backup.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.nagyesta.filebarj.core.config.BackupJobConfiguration;
import com.github.nagyesta.filebarj.core.model.enums.Change;
import com.github.nagyesta.filebarj.core.model.enums.FileType;
import com.github.nagyesta.filebarj.core.model.enums.OperatingSystem;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
Expand Down Expand Up @@ -41,6 +42,8 @@ public class RestoreManifest extends EncryptionKeyStore {
*/
@NonNull
private final BackupJobConfiguration configuration;
@NonNull
private final OperatingSystem operatingSystem;
/**
* The map of matching files identified during backup keyed by filename and Id.
*/
Expand Down Expand Up @@ -70,6 +73,7 @@ protected RestoreManifest(final RestoreManifestBuilder<?, ?> builder) {
this.lastStartTimeUtcEpochSeconds = builder.lastStartTimeUtcEpochSeconds;
this.fileNamePrefixes = Collections.unmodifiableSortedMap(builder.fileNamePrefixes);
this.configuration = builder.configuration;
this.operatingSystem = builder.operatingSystem;
this.files = builder.files.entrySet().stream()
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, entry -> Collections.unmodifiableMap(entry.getValue())));
this.archivedEntries = builder.archivedEntries.entrySet().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.nagyesta.filebarj.core.model.enums;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Optional;

/**
* Represents the current OS.
*/
public enum OperatingSystem {
/**
* Windows.
*/
WINDOWS("win"),
/**
* Mac OS.
*/
MAC("mac"),
/**
* Linux.
*/
LINUX("linux"),
/**
* Catch-all for any OS not matching the above.
*/
UNKNOWN(null);

private final String nameToken;

/**
* Creates a new instance.
*
* @param nameToken the OS name token we need to search for in the OS name.
*/
OperatingSystem(final String nameToken) {
this.nameToken = nameToken;
}

/**
* Parses the OS name.
*
* @param name the OS name
* @return the matching constant or UNKNOWN
*/
@NotNull
public static OperatingSystem forOsName(@Nullable final String name) {
final var osName = Optional.ofNullable(name)
.map(String::toLowerCase)
.orElse("unknown");
return Arrays.stream(values())
.filter(os -> os.nameToken != null && osName.contains(os.nameToken))
.findFirst()
.orElse(UNKNOWN);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.nagyesta.filebarj.core.util;

import com.github.nagyesta.filebarj.core.model.enums.OperatingSystem;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.NotNull;

/**
* Utility class for OS specific operations.
Expand All @@ -15,6 +17,27 @@ public final class OsUtil {
* @return true if the OS is Windows
*/
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().startsWith("win");
return getOs() == OperatingSystem.WINDOWS;
}

/**
* Parses the current OS name.
*
* @return the current OS
*/
@NotNull
public static OperatingSystem getOs() {
return OperatingSystem.forOsName(getRawOsName());
}


/**
* Returns the raw OS name.
*
* @return the raw OS name
*/
@NotNull
public static String getRawOsName() {
return System.getProperty("os.name");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void testConvertToSummaryStringShouldReturnExpectedValueWhenCalledWithValidManif
.versions(new TreeSet<>(Set.of(0)))
.fileNamePrefix("prefix")
.startTimeUtcEpochSeconds(1L)
.operatingSystem(null)
.build();
//when
final var actual = underTest.convertToSummaryString(manifest);
Expand All @@ -92,6 +93,7 @@ void testConvertToSummaryStringShouldReturnExpectedValueWhenCalledWithValidManif
\tStarted at : 1970-01-01T00:00:01Z (Epoch seconds: 1)
\tContains 1 files (2 MiB)
\tVersions : [0]
\tOS name : unknown
\tEncrypted : true
\tHash alg. : SHA256
\tCompression: GZIP""", actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.github.nagyesta.filebarj.core.model.enums.BackupType;
import com.github.nagyesta.filebarj.core.model.enums.Change;
import com.github.nagyesta.filebarj.core.model.enums.FileType;
import com.github.nagyesta.filebarj.core.util.OsUtil;
import com.github.nagyesta.filebarj.io.stream.crypto.EncryptionUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -82,6 +83,7 @@ void testDeserializeShouldRecreatePreviousStateWhenCalledOnSerializedStateOfFull
.appVersion(new AppVersion(0, 0, 1))
.versions(new TreeSet<>(Set.of(0)))
.backupType(BackupType.FULL)
.operatingSystem(OsUtil.getRawOsName())
.encryptionKeys(Map.of(0, Map.of(0, dek)))
.startTimeUtcEpochSeconds(Instant.now().getEpochSecond())
.fileNamePrefix("backup-")
Expand Down

0 comments on commit f7df8ae

Please sign in to comment.