| Crates.io | obsidian-backups |
| lib.rs | obsidian-backups |
| version | 0.1.3 |
| created_at | 2025-10-02 18:59:31.606464+00 |
| updated_at | 2025-10-12 05:14:38.573674+00 |
| description | A Git-based backup library for Rust applications. Originally designed for the Obsidian Minecraft Server Panel, but generic enough to be used in any project requiring file backup management. |
| homepage | |
| repository | https://github.com/Obsidian-Minecraft-Server-Portal/obsidian-backup-system.git |
| max_upload_size | |
| id | 1864853 |
| size | 241,751 |
A Git-based backup library for Rust applications. Originally designed for the Obsidian Minecraft Server Panel, but generic enough to be used in any project requiring file backup management.
This library uses Git under the hood to provide efficient, version-controlled backups with diff capabilities.
zip feature)This crate is not yet published on crates.io. To use it, add the following to your Cargo.toml:
[dependencies]
obsidian-backups = { git = "https://github.com/Obsidian-Minecraft-Server-Portal/obsidian-backup-system.git" }
[dependencies]
obsidian-backups = { git = "https://github.com/Obsidian-Minecraft-Server-Portal/obsidian-backup-system.git", features = ["serde", "logging", "zip"] }
| Feature | Description | Dependencies |
|---|---|---|
serde |
Enables serialization/deserialization support for backup items | serde |
logging |
Enables internal logging using the log crate |
log |
zip |
Enables exporting backups as 7z compressed archives | sevenz-rust2 |
cli |
Builds the command-line interface application | clap, serde_json, pretty_env_logger |
use obsidian_backups::BackupManager;
// Create a backup manager
// store_directory: where backup metadata is stored (.git repository)
// working_directory: the directory you want to back up
let manager = BackupManager::new("./backups", "./my_data")
.expect("Failed to initialize BackupManager");
// Create a backup without description
let backup_id = manager.backup(None)
.expect("Failed to create backup");
println!("Created backup with ID: {}", backup_id);
// Create a backup with description
let backup_id = manager.backup(Some("Before major update".to_string()))
.expect("Failed to create backup");
Note: Backups automatically exclude common system and temporary files:
.git.DS_Store, Thumbs.db, desktop.ini, $RECYCLE.BIN*.tmp, *.swp, ~*, Office temp files (~$*)__pycache__let backups = manager.list()
.expect("Failed to list backups");
for backup in backups {
println!("ID: {}", backup.id);
println!("Timestamp: {}", backup.timestamp);
println!("Description: {}", backup.description);
println!("---");
}
if let Some(last_backup) = manager.last().expect("Failed to get last backup") {
println!("Last backup: {}", last_backup.description);
println!("Created at: {}", last_backup.timestamp);
} else {
println!("No backups found");
}
// Restore using backup ID
manager.restore( & backup_id)
.expect("Failed to restore backup");
Safety Note: The restore operation uses a safe atomic approach:
// Get differences between a backup and its parent
let modified_files = manager.diff( & backup_id)
.expect("Failed to get diff");
for file in modified_files {
println!("File: {}", file.path);
match ( &file.content_before, &file.content_after) {
(Some(_), Some(_)) => println ! (" Modified"),
(None, Some(_)) => println ! (" Added"),
(Some(_), None) => println ! (" Deleted"),
_ => {}
}
}
zip feature)#[cfg(feature = "zip")]
{
// Export a backup as a 7z archive to a file
// compression_level: 0-9 (0 = no compression, 9 = maximum compression)
// Values outside this range are automatically clamped
manager.export(&backup_id, "./backup.7z", 5)
.expect("Failed to export backup");
}
For scenarios where you need to stream the archive directly (e.g., HTTP responses, in-memory processing):
#[cfg(feature = "zip")]
{
use std::io::Cursor;
// Export to an in-memory buffer
let mut buffer = Cursor::new(Vec::new());
manager.export_to_stream(&backup_id, &mut buffer, 5)
.expect("Failed to export backup to stream");
// Get the archive bytes
let archive_bytes = buffer.into_inner();
println!("Archive size: {} bytes", archive_bytes.len());
// You can also use any other writer that implements Write + Seek:
// - File handles
// - TCP streams with buffering
// - Custom buffers
}
Note:
Write and Seek traits.The backup system provides three strategies for managing backup retention and preventing unlimited growth:
// Keep only the 10 most recent backups, remove all older ones
manager.purge_backups_over_count(10)
.expect("Failed to purge old backups");
This method:
use chrono::Duration;
// Remove backups older than 30 days
manager.purge_backups_older_than(Duration::days(30))
.expect("Failed to purge old backups");
// Remove backups older than 7 days
manager.purge_backups_older_than(Duration::days(7))
.expect("Failed to purge old backups");
// Remove backups older than 2 hours
manager.purge_backups_older_than(Duration::hours(2))
.expect("Failed to purge old backups");
This method:
// Keep repository under 100MB
manager.purge_backups_over_size(100 * 1024 * 1024)
.expect("Failed to reduce repository size");
// Keep repository under 1GB
manager.purge_backups_over_size(1024 * 1024 * 1024)
.expect("Failed to reduce repository size");
This method:
Important Notes:
use obsidian_backups::BackupManager;
use std::fs;
fn main() -> anyhow::Result<()> {
// Initialize directories
fs::create_dir_all("./backups")?;
fs::create_dir_all("./my_data")?;
// Create some content
fs::write("./my_data/file.txt", "Initial content")?;
// Initialize backup manager
let manager = BackupManager::new("./backups", "./my_data")?;
// Create first backup
let backup1 = manager.backup(Some("Initial state".to_string()))?;
println!("Created backup: {}", backup1);
// Modify content
fs::write("./my_data/file.txt", "Modified content")?;
// Create second backup
let backup2 = manager.backup(Some("After modifications".to_string()))?;
// View changes
let diffs = manager.diff(&backup2)?;
println!("\nChanges in last backup:");
for diff in diffs {
println!(" {}: modified", diff.path);
}
// List all backups
println!("\nAll backups:");
for backup in manager.list()? {
println!(" {}: {}", backup.timestamp, backup.description);
}
// Restore first backup
manager.restore(&backup1)?;
println!("\nRestored to initial state");
Ok(())
}
use obsidian_backups::BackupManager;
use chrono::Duration;
use std::fs;
fn main() -> anyhow::Result<()> {
// Initialize backup manager
let manager = BackupManager::new("./backups", "./my_data")?;
// Create multiple backups over time
fs::write("./my_data/file.txt", "Version 1")?;
manager.backup(Some("Version 1".to_string()))?;
fs::write("./my_data/file.txt", "Version 2")?;
manager.backup(Some("Version 2".to_string()))?;
fs::write("./my_data/file.txt", "Version 3")?;
manager.backup(Some("Version 3".to_string()))?;
// List all backups before purging
let backups_before = manager.list()?;
println!("Backups before purge: {}", backups_before.len());
// Strategy 1: Keep only the 2 most recent backups
manager.purge_backups_over_count(2)?;
println!("Kept only 2 most recent backups");
// Strategy 2: Remove backups older than 7 days
// manager.purge_backups_older_than(Duration::days(7))?;
// Strategy 3: Keep repository under 50MB
// manager.purge_backups_over_size(50 * 1024 * 1024)?;
// List backups after purging
let backups_after = manager.list()?;
println!("Backups after purge: {}", backups_after.len());
for backup in backups_after {
println!(" {}: {}", backup.timestamp, backup.description);
}
Ok(())
}
logging feature)#[cfg(feature = "logging")]
use log::{info, LevelFilter};
#[cfg(feature = "logging")]
fn main() {
// Initialize logger
pretty_env_logger::env_logger::builder()
.filter_level(LevelFilter::Debug)
.init();
let manager = BackupManager::new("./backups", "./my_data")
.expect("Failed to create BackupManager");
info!("Creating backup...");
let backup_id = manager.backup(Some("Logged backup".to_string()))
.expect("Failed to create backup");
info!("Backup created: {}", backup_id);
}
The obackup command-line tool provides a convenient way to manage backups directly from the terminal without writing any code.
The CLI requires the cli, serde, logging, and optionally zip features to be enabled. Build it using:
cargo build --bin obackup --features cli,serde,logging,zip --release
The binary will be located at ./target/release/obackup (or ./target/release/obackup.exe on Windows).
After building, you can copy the binary to a location in your PATH:
# Linux/macOS
sudo cp ./target/release/obackup /usr/local/bin/
# Windows (PowerShell, as administrator)
Copy-Item .\target\release\obackup.exe C:\Windows\System32\
obackup [OPTIONS] <COMMAND>
-s, --store-directory <PATH> - Store directory for backup repository (default: ./backup_store)-w, --working-directory <PATH> - Working directory to backup (default: .)-v, --verbose - Increase verbosity level (can be repeated: -v, -vv, -vvv)-h, --help - Print help information-V, --version - Print version informationinit - Initialize a new backup repositoryInitializes a new backup repository in the specified store directory.
obackup -s ./backups -w ./my_data init
backup - Create a new backupCreates a new backup of the working directory.
Options:
-d, --description <TEXT> - Description for the backupExamples:
# Create a backup without description
obackup -s ./backups -w ./my_data backup
# Create a backup with description
obackup -s ./backups -w ./my_data backup -d "Before major update"
list - List all backupsLists all available backups with their IDs, timestamps, and descriptions.
Options:
-j, --json - Output in JSON formatExamples:
# List backups in human-readable format
obackup -s ./backups -w ./my_data list
# List backups in JSON format
obackup -s ./backups -w ./my_data list --json
last - Show the most recent backupDisplays information about the most recent backup.
Options:
-j, --json - Output in JSON formatExamples:
# Show last backup
obackup -s ./backups -w ./my_data last
# Show last backup in JSON format
obackup -s ./backups -w ./my_data last --json
restore - Restore a backup by IDRestores the working directory to the state of the specified backup.
Arguments:
<BACKUP_ID> - The backup ID to restore (obtained from list or last)Example:
obackup -s ./backups -w ./my_data restore abc123def456
⚠️ Warning: This will replace all files in the working directory with the backup contents.
export - Export a backup to a 7z archiveExports a backup as a compressed 7z archive. Requires the zip feature.
Arguments:
<BACKUP_ID> - The backup ID to exportOptions:
-o, --output <PATH> - Output path for the archive-l, --level <0-9> - Compression level (default: 5)Example:
# Export with default compression
obackup -s ./backups -w ./my_data export abc123def456 -o backup.7z
# Export with maximum compression
obackup -s ./backups -w ./my_data export abc123def456 -o backup.7z -l 9
diff - Show changes in a specific backupShows what files were added, modified, or deleted in a specific backup.
Arguments:
<BACKUP_ID> - The backup ID to diffOptions:
-j, --json - Output in JSON format-c, --show-content - Show file contentsExamples:
# Show changes summary
obackup -s ./backups -w ./my_data diff abc123def456
# Show changes with file contents
obackup -s ./backups -w ./my_data diff abc123def456 --show-content
# Show changes in JSON format
obackup -s ./backups -w ./my_data diff abc123def456 --json
# Initialize backup repository
obackup -s ./my_backups -w ./project init
# Create first backup
obackup -s ./my_backups -w ./project backup -d "Initial state"
# Make some changes to your project files...
# Create another backup
obackup -s ./my_backups -w ./project backup -d "After feature implementation"
# View all backups
obackup -s ./my_backups -w ./project list
# Check what changed in the last backup
obackup -s ./my_backups -w ./project last
obackup -s ./my_backups -w ./project diff <BACKUP_ID>
# Restore to a previous state if needed
obackup -s ./my_backups -w ./project restore <BACKUP_ID>
If you're working from within your project directory, you can use relative paths:
cd /path/to/project
# Initialize (stores backup data in ./backups, tracks current directory)
obackup -s ./backups -w . init
# Create backups
obackup -s ./backups -w . backup -d "Checkpoint 1"
obackup -s ./backups -w . backup -d "Checkpoint 2"
# List and inspect
obackup -s ./backups -w . list
Use -v flags to see detailed logging:
# Info level logging
obackup -v -s ./backups -w . backup -d "Debug backup"
# Debug level logging
obackup -vv -s ./backups -w . backup -d "More details"
# Trace level logging (very detailed)
obackup -vvv -s ./backups -w . backup -d "All the details"
# Get the ID of the last backup
obackup -s ./backups -w . last
# Export it as a compressed archive
obackup -s ./backups -w . export <BACKUP_ID> -o ./archives/backup-2024-09-30.7z -l 9
BackupManagerThe main struct for managing backups.
new(store_directory, working_directory) -> Result<Self> - Initialize a new backup managersetup_ignore_file(ignore_file: impl AsRef<Path>) -> Result<()> - Configure ignore patterns from a .gitignore-style filebackup(description: Option<String>) -> Result<String> - Create a new backup, returns backup IDlist() -> Result<Vec<BackupItem>> - List all available backupslast() -> Result<Option<BackupItem>> - Get the most recent backuprestore(backup_id: impl AsRef<str>) -> Result<()> - Restore a specific backupdiff(backup_id: impl AsRef<str>) -> Result<Vec<ModifiedFile>> - Get changes in a backupexport(backup_id, output_path, level: u8) -> Result<()> - Export backup as 7z archive to file (requires zip feature)export_to_stream<W: Write + Seek>(backup_id, writer: W, level: u8) -> Result<()> - Export backup as 7z archive to a stream (requires zip feature)purge_backups_over_count(count: usize) -> Result<()> - Keep only the N most recent backups, remove older onespurge_backups_older_than(period: chrono::Duration) -> Result<()> - Remove backups older than specified durationpurge_backups_over_size(size: usize) -> Result<()> - Remove old backups to keep repository under size limit (in bytes)BackupItemRepresents a backup point with metadata.
pub struct BackupItem {
pub id: String, // Git commit ID
pub timestamp: DateTime<Utc>, // When the backup was created
pub description: String, // User-provided description
}
ModifiedFileRepresents a file that changed in a backup.
pub struct ModifiedFile {
pub path: String, // Path to the file
pub content_before: Option<Vec<u8>>, // Content before change (None if added)
pub content_after: Option<Vec<u8>>, // Content after change (None if deleted)
}
Contributions are welcome! Please feel free to submit issues or pull requests.