Skip to content

Commit

Permalink
backup: add --init option
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome authored and simonsan committed Sep 9, 2023
1 parent 05d088e commit 0227e9c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
3 changes: 2 additions & 1 deletion changelog/new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ New features:
- Creation of new keys now enforces confirmation of entered key. This helps to prevent mistype of passwords during the initial entry
- Check: Add check if time is set for packs-to-delete
- ls: Options --long (-l) and --summary (-s) have been added.
- forget: Option --json has been added.
- forget: Option --json has been added.
- backup: New option --init to initialize repository if it doesn't exist yet.
2 changes: 2 additions & 0 deletions config/full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ exclude-if-present = [".nobackup", "CACHEDIR.TAG"] # Default: not set
one-file-system = false
exclude-larger-than = "100MB" # Default: not set
json = false
init = false

# Backup options for specific sources - all above options are also available here and replace them for the given source
[[backup.sources]]
Expand Down Expand Up @@ -104,6 +105,7 @@ exclude-if-present = [".nobackup", "CACHEDIR.TAG"] # Default: not set
one-file-system = false
exclude-larger-than = "100MB" # Default: not set
json = false
init = false

[[backup.sources]]
source = "/path/to/source2 /second/path" # multiple local paths are allowd within one source
Expand Down
37 changes: 34 additions & 3 deletions src/commands/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ use merge::Merge;
use serde::Deserialize;

use rustic_core::{
BackupOptions, LocalSourceFilterOptions, LocalSourceSaveOptions, ParentOptions, PathList,
SnapshotOptions,
BackupOptions, ConfigOptions, KeyOptions, LocalSourceFilterOptions, LocalSourceSaveOptions,
ParentOptions, PathList, Repository, SnapshotOptions,
};

use super::init::init;

/// `backup` subcommand
#[derive(Clone, Command, Default, Debug, clap::Parser, Deserialize, Merge)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
Expand Down Expand Up @@ -56,6 +58,11 @@ pub struct BackupCmd {
#[merge(strategy = merge::bool::overwrite_false)]
json: bool,

/// Initialize repository, if it doesn't exist yet
#[clap(long)]
#[merge(strategy = merge::bool::overwrite_false)]
init: bool,

#[clap(flatten, next_help_heading = "Options for parent processing")]
#[serde(flatten)]
parent_opts: ParentOptions,
Expand All @@ -68,6 +75,16 @@ pub struct BackupCmd {
#[serde(flatten)]
snap_opts: SnapshotOptions,

#[clap(flatten, next_help_heading = "Key options (when using --init)")]
#[serde(skip)]
#[merge(skip)]
key_opts: KeyOptions,

#[clap(flatten, next_help_heading = "Config options (when using --init)")]
#[serde(skip)]
#[merge(skip)]
config_opts: ConfigOptions,

#[clap(skip)]
#[merge(strategy = merge_sources)]
sources: Vec<BackupCmd>,
Expand Down Expand Up @@ -98,7 +115,21 @@ impl BackupCmd {
fn inner_run(&self) -> Result<()> {
let config = RUSTIC_APP.config();

let repo = open_repository(&config)?.to_indexed_ids()?;
let po = config.global.progress_options;
let repo = Repository::new_with_progress(&config.repository, po)?;
// Initialize repository if --init is set and it is not yet initialized
let repo = if self.init && repo.config_id()?.is_none() {
if config.global.dry_run {
bail!(
"cannot initialize repository {} in dry-run mode!",
repo.name
);
}
init(repo, &self.key_opts, &self.config_opts)?
} else {
open_repository(&config)?
}
.to_indexed_ids()?;

// manually check for a "source" field, check is not done by serde, see above.
if !config.backup.source.is_empty() {
Expand Down
12 changes: 6 additions & 6 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{Application, RUSTIC_APP};

use dialoguer::Password;

use rustic_core::{ConfigOptions, KeyOptions, Repository};
use rustic_core::{ConfigOptions, KeyOptions, OpenStatus, Repository};

/// `init` subcommand
#[derive(clap::Parser, Command, Debug)]
Expand Down Expand Up @@ -42,15 +42,17 @@ impl InitCmd {
if repo.config_id()?.is_some() {
bail!("Config file already exists. Aborting.");
}
init(repo, &self.key_opts, &self.config_opts)

let _ = init(repo, &self.key_opts, &self.config_opts)?;
Ok(())
}
}

pub(crate) fn init<P, S>(
repo: Repository<P, S>,
key_opts: &KeyOptions,
config_opts: &ConfigOptions,
) -> Result<()> {
) -> Result<Repository<P, OpenStatus>> {
let pass = repo.password()?.unwrap_or_else(|| {
match Password::new()
.with_prompt("enter password for new key")
Expand All @@ -66,7 +68,5 @@ pub(crate) fn init<P, S>(
}
});

let _ = repo.init_with_password(&pass, key_opts, config_opts)?;

Ok(())
Ok(repo.init_with_password(&pass, key_opts, config_opts)?)
}

0 comments on commit 0227e9c

Please sign in to comment.