generated from cosmic-utils/cosmic-app-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ahoneybun/backend
feat: add init and snapshot functionality
- Loading branch information
Showing
8 changed files
with
137 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod init; | ||
pub mod restore; | ||
pub mod snapshot; |
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,36 @@ | ||
use rustic_backend::BackendOptions; | ||
use rustic_core::{ConfigOptions, KeyOptions, Repository, RepositoryOptions}; | ||
use std::error::Error; | ||
|
||
pub fn init(repository: &str, password: &str) -> Result<(), Box<dyn Error>> { | ||
// Initialize Backends | ||
let backends = BackendOptions::default() | ||
.repository(repository) | ||
.to_backends()?; | ||
|
||
// Init repository | ||
let repo_opts = RepositoryOptions::default().password(password); | ||
let key_opts = KeyOptions::default(); | ||
let config_opts = ConfigOptions::default(); | ||
|
||
if Repository::new(&repo_opts, backends.clone())? | ||
.open() | ||
.is_err() | ||
{ | ||
Repository::new(&repo_opts, backends)?.init(&key_opts, &config_opts)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_init() { | ||
let repository = "/tmp/test"; | ||
let password = "password"; | ||
|
||
assert!(init(repository, password).is_ok()); | ||
} | ||
} |
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,40 @@ | ||
use rustic_backend::BackendOptions; | ||
use rustic_core::{LocalDestination, LsOptions, Repository, RepositoryOptions, RestoreOptions}; | ||
use std::error::Error; | ||
|
||
fn restore( | ||
repository: &str, | ||
password: &str, | ||
snap_path: &str, | ||
restore_destination: &str, | ||
) -> Result<(), Box<dyn Error>> { | ||
// Initialize Backends | ||
let backends = BackendOptions::default() | ||
.repository(repository) | ||
.to_backends()?; | ||
|
||
// Open repository | ||
let repo_opts = RepositoryOptions::default().password(password); | ||
let repo = Repository::new(&repo_opts, backends)? | ||
.open()? | ||
.to_indexed()?; | ||
|
||
// use latest snapshot without filtering snapshots | ||
let node = repo.node_from_snapshot_path(snap_path, |_| true)?; | ||
|
||
// use list of the snapshot contents using no additional filtering | ||
let streamer_opts = LsOptions::default(); | ||
let ls = repo.ls(&node, &streamer_opts)?; | ||
|
||
let destination = restore_destination; // restore to this destination dir | ||
let create = true; // create destination dir, if it doesn't exist | ||
let dest = LocalDestination::new(destination, create, !node.is_dir())?; | ||
|
||
let opts = RestoreOptions::default(); | ||
let dry_run = false; | ||
// create restore infos. Note: this also already creates needed dirs in the destination | ||
let restore_infos = repo.prepare_restore(&opts, ls.clone(), &dest, dry_run)?; | ||
|
||
repo.restore(restore_infos, &opts, ls, &dest)?; | ||
Ok(()) | ||
} |
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,50 @@ | ||
use rustic_backend::BackendOptions; | ||
use rustic_core::{BackupOptions, PathList, Repository, RepositoryOptions, SnapshotOptions}; | ||
use std::error::Error; | ||
|
||
pub fn snapshot(repository: &str, password: &str, paths: Vec<&str>) -> Result<(), Box<dyn Error>> { | ||
// Initialize Backends | ||
let backends = BackendOptions::default() | ||
.repository(repository) | ||
.to_backends()?; | ||
|
||
println!("successfully initialized backends:\n{backends:#?}"); | ||
|
||
// Open repository | ||
let repo_opts = RepositoryOptions::default().password(password); | ||
|
||
let repo = Repository::new(&repo_opts, backends)? | ||
.open()? | ||
.to_indexed_ids()?; | ||
|
||
println!("successfully opened repository:\n{repo:#?}"); | ||
|
||
let backup_opts = BackupOptions::default(); | ||
let source = PathList::from_strings(paths).sanitize()?; | ||
|
||
println!("successfully sanitized paths:\n{source:#?}"); | ||
|
||
let snap = SnapshotOptions::default().to_snapshot()?; | ||
|
||
println!("successfully created snapshot options:\n{snap:#?}"); | ||
|
||
// Create snapshot | ||
let snap = repo.backup(&backup_opts, &source, snap)?; | ||
|
||
println!("successfully created snapshot:\n{snap:#?}"); | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_snapshot() { | ||
let repository = "/tmp/test"; | ||
let password = "password"; | ||
let paths = vec!["/etc"]; | ||
|
||
assert!(snapshot(repository, password, paths).is_ok()); | ||
} | ||
} |
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