Class BackupManager

java.lang.Object
org.kingdoms.managers.backup.BackupManager
Direct Known Subclasses:
KingdomsBackup

public abstract class BackupManager extends Object
BackupManager - v1.0 - A powerful concurrent file compression using NIO.2. Java 8 NIO.2: https://docs.oracle.com/javase/tutorial/essential/io/fileio.html

The simplest way of doing this is to use takeBackup(). You can also override the method and handle it yourself.

  • Field Details

    • DATE_PATTERN

      protected static final DateTimeFormatter DATE_PATTERN
    • useMultiBackups

      protected static boolean useMultiBackups
    • backups

      protected final Path backups
    • toBackup

      protected final Path toBackup
  • Constructor Details

    • BackupManager

      public BackupManager(Path backups, Path toBackup, Executor executor)
  • Method Details

    • getDate

      public String getDate()
      Gets the concurrent date using DATE_PATTERN format.
      Returns:
      today's date.
      Since:
      1.0.0
    • unzipFiles

      public CompletableFuture<Void> unzipFiles(Path zip, Path unzipTo)
      Uncompresses all the files from the given path to the given directory asynchronous.
      Parameters:
      zip - the ZIP file path.
      unzipTo - the directory to copy the uncompressed files to.
      Since:
      1.0.0
      See Also:
    • takeBackup

      public void takeBackup()
      The default method to take a clean backup. You can override for a custom handler.
      Since:
      1.0.0
    • zipFiles

      public CompletableFuture<Integer> zipFiles()
      Compresses all the files in the given directory of toBackup asynchronous.
      Returns:
      the amount of files that were successfully backed up.
      Since:
      1.0.0
      See Also:
    • getMultiZipName

      public Path getMultiZipName()
      Gets a new path with the format of getZipPath() including an additional
      number with the format of (x) where x is starts from 1 only if
      getZipPath() already exists.
      Returns:
      a new path if getZipPath() already exists, otherwise getZipPath() with no additional number.
      Since:
      1.0.0
    • isWhitelistedDirectory

      public abstract BackupManager.DirVisitState isWhitelistedDirectory(Path file)
      When taking a backup from the files in the directory, only the contents inside these
      directories will be checked and passed to isWhitelistedFile(Path)
      Parameters:
      file - the directory that we're about to enter.
      Since:
      1.0.0
    • isBlacklistedDirectory

      public abstract boolean isBlacklistedDirectory(Path file)
    • isWhitelistedFile

      public abstract boolean isWhitelistedFile(Path file)
      When taking a backup from the files in the directory, only these files are accepted.
      Parameters:
      file - the file that we're about to backup.
      Returns:
      true if the specified path should be accepted.
      Since:
      1.0.0
    • addToZip

      public abstract Collection<Path> addToZip()
      Can be either a Path or InputStream
      Returns:
    • shouldBeDeleted

      public boolean shouldBeDeleted(Path path, int time, TimeUnit timeUnit)
      Checks if the specified backup file should be deleted if it's an old backup.
      Parameters:
      path - the backup file's path.
      time - any files older than this time.
      timeUnit - the time unit for time parameter.
      Returns:
      true if this backup should be deleted, otherwise false.
      Since:
      1.0.0
    • deleteOldBackups

      public CompletableFuture<Void> deleteOldBackups(int time, TimeUnit timeUnit)
      Deletes all the backups that are older than the specified time.

      Note that getMultiZipName() will replace the deleted files when
      checking through the files. It's recommended to change DATE_PATTERN to use
      "yyyy-MM-dd hh" to display the hours as well. This task is asynchronous.
      Parameters:
      time - any files older than this time.
      timeUnit - the time unit for time parameter.
      Since:
      1.0.0
    • getZipPath

      public Path getZipPath()
      Gets the backup ZIP path for today.
      Returns:
      a path including today's date with ZIP file extension.
      Since:
      1.0.0
    • getZip

      public Path getZip()
      Gets the final ZIP path.
      Returns:
      default ZIP path or multi ZIP path if enabled.
      Since:
      1.0.0
      See Also:
    • hasBackupToday

      public boolean hasBackupToday()
      Checks if the backup file for today exists considering
      multi backup files if enabled. So you shouldn't use this method
      when taking a backup if you enabled multi backups.
      Returns:
      true if the backup file exists, otherwise false.
      Since:
      1.0.0