Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/himansaBro/JungleConfig/llms.txt

Use this file to discover all available pages before exploring further.

JungleConfig.Backup(File path, boolean override) copies the current serialized configuration to a destination file you specify. It returns true when the copy succeeds and false when the destination already exists and override is false. The method is available across all storage modes — file-backed, encrypted, in-memory, and FlatJson — making it a consistent way to snapshot configuration state regardless of how the instance was created.
import java.io.File;
import java.time.LocalDate;

File backup = new File("/backups/app-" + LocalDate.now() + ".conf");
boolean saved = config.Backup(backup, false);

if (!saved) {
    System.out.println("Backup already exists for today, skipping.");
}

Override behaviour

The override flag controls what happens when the destination file already exists:
  • override = false — if the destination file already exists, the method returns false immediately without writing anything. The existing destination file is left completely untouched. Use this when you want idempotent daily snapshots (one file per day at most) or when losing a previous backup would be unacceptable.
  • override = true — if the destination file exists, it is overwritten with the current configuration contents. The destination path must point to a regular file (not a directory) and the process must have write permission on it.
// Safe snapshot — never overwrites an existing file
boolean created = config.Backup(new File("snapshot.conf"), false);

// Force-overwrite a standing backup location
config.Backup(new File("/var/backup/latest.conf"), true);

Creating a new backup file

When the destination file does not exist, NativeIOHandler.Backup calls path.canWrite() before attempting path.createNewFile(). On some JVMs, canWrite() returns false for a path that does not yet exist (even when the parent directory is writable), which causes Backup to throw an IOException with the message "No permission to Write" before the file is ever created. If you encounter this, ensure the parent directory exists and is writable. If canWrite() still returns false on your JVM for a non-existent path, pre-create the file manually before calling Backup. After the file is created, Backup writes the current serialized contents to it with writeToFile. The parent directory of the destination path must already exist — Backup does not create intermediate directories automatically.

Encrypted backups

When the JungleConfig instance was created with EncryptedConfig, the backup file contains the same Base64-encoded ciphertext that the primary file holds at the moment Backup is called. The backup is fully encrypted with the same password — there is no plaintext copy produced at any point during the backup operation. To read the backup later, open it with EncryptedConfig using the same password.
JungleConfig config = JungleConfig.EncryptedConfig(
    new File("secrets.conf"),
    "my-strong-password"
);

config.Set("api.key", "abc-def-xyz");

// Backup is also encrypted — same format, same password
config.Backup(new File("secrets.backup.conf"), true);

// Restore by opening the backup with the same password
JungleConfig restored = JungleConfig.EncryptedConfig(
    new File("secrets.backup.conf"),
    "my-strong-password"
);
String key = restored.get("api.key", String.class); // "abc-def-xyz"

In-memory and FlatJson modes

Backup is available on all JungleConfig modes. For InMemoryConfig and FlatJsonConfig instances, the current serialized representation of the in-memory store is written to the destination file in its respective format. This means:
  • An InMemoryConfig backup produces a file in the standard JConfig001 format that can be opened with new JungleConfig(File).
  • A FlatJsonConfig backup produces a flat JSON file.
These backups can serve as a way to persist an in-memory config to disk on shutdown or at a checkpoint, even though the live instance itself never writes to a file.
Schedule a backup before any major configuration change by calling Backup with a timestamped path. This gives you a point-in-time snapshot to roll back to if the update produces unexpected behaviour:
String timestamp = LocalDateTime.now()
    .format(DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"));
config.Backup(new File("backups/pre-update-" + timestamp + ".conf"), false);

// Now apply the configuration change
config.Set("rollout.enabled", true);
NativeIOHandler.Backup wraps any IOException in a RuntimeException. Common causes include the destination directory not existing, insufficient write permissions on the path, canWrite() returning false for a non-existent path on certain JVMs, or the destination pointing to a directory rather than a file. Ensure the parent directory exists and is writable before calling Backup — JungleConfig will not create missing directories automatically.
File backupDir = new File("/backups");
if (!backupDir.exists()) {
    backupDir.mkdirs(); // create the directory first
}
config.Backup(new File(backupDir, "app.conf"), false);

Build docs developers (and LLMs) love