Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands)!: copy: Use config profile as target #1131

Merged
merged 7 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,13 @@ source-specific option and then only apply to this source.
| keep-none | Allow to keep no snapshots. | false | true |
| prune | If set to true, prune the repository after snapshots have been removed. | false | |

### Copy Targets `[[copy.targets]]`

**Note**: Copy-targets are simply repositories with the same defaults as within
the repository section.

| Attribute | Description | Default Value | Example Value |
| ---------------- | ----------------------------------------------------------------- | ------------------------ | ---------------------- |
| cache-dir | Path to the cache directory for the target repository. | ~/.cache/rustic/$REPO_ID | ~/.cache/my_own_cache/ |
| no-cache | If true, disables caching for the target repository. | false | |
| password | The password for the target repository. | Not set | |
| password-file | Path to a file containing the password for the target repository. | Not set | |
| password-command | Command to retrieve the password for the target repository. | Not set | |
| repository | The path or URL to the target repository. | Not set | |
| repo-hot | The path or URL to the hot target repository. | Not set | |
| warm-up | If true, warms up the target repository by file access. | Not set | |
| warm-up-command | Command to warm up the target repository. | Not set | |
| warm-up-wait | The wait time for warming up the target repository. | Not set | |
### Copy Targets `[copy]`

**Note**: Copy-targets must be defined in their own config profile files.

| Attribute | Description | Default Value | Example Value |
| --------- | ------------------ | ------------- | ---------------------------------------- |
| target | One or more target | Not set | "remote_host" / ["profile1", "profile2"] |

### WebDAV Options `[webdav]`

Expand Down
14 changes: 3 additions & 11 deletions config/copy_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
repository = "/tmp/repo"
password = "test"

# you can specify multiple targets
[[copy.targets]]
repository = "/tmp/repo2"
password = "test"
no-cache = true

[[copy.targets]]
repository = "rclone:ovh:backup"
repo-hot = "clone:ovh:backup-hot"
password-file = "/root/key-rustic-ovh"
cache-dir = "/var/lib/cache/rustic" # explicitly specify cache dir for remote repository
# you can specify multiple targets. Note that each target must be configured via a config profile file
[copy]
target = ["full", "rustic"]
21 changes: 2 additions & 19 deletions config/full-one.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,8 @@ keep-withing-quarter-yearly = "0 year"
keep-withing-half-yearly = "1 year"
keep-within-yearly = "10 years"

# Multiple targets are available for the copy command. Each specify a repository with exactly identical options as in
# the [repository] section.
[[copy.targets]]
repository = "/repo/rustic" # Must be set
repo-hot = "/my/hot/repo" # Default: not set
# one of the three password options must be set
password = "mySecretPassword"
password-file = "/my/password.txt"
password-command = "my_command.sh"
no-cache = false
cache-dir = "/my/rustic/cachedir" # Default: Applications default cache dir, e.g. ~/.cache/rustic
# use either warm-up (warm-up by file access) or warm-up-command to specify warming up
warm-up = false
warm-up-command = "warmup.sh %id" # Default: not set
warm-up-wait = "10min" # Default: not set

[[copy.targets]]
repository = "/repo/rustic2" # Must be set
# ...
[copy]
target = "target_profile" # Default: not set

[webdav]
address = "localhost:8000"
Expand Down
21 changes: 2 additions & 19 deletions config/full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,8 @@ keep-withing-quarter-yearly = "0 year"
keep-withing-half-yearly = "1 year"
keep-within-yearly = "10 years"

# Multiple targets are available for the copy command. Each specify a repository with exactly identical options as in
# the [repository] section.
[[copy.targets]]
repository = "/repo/rustic" # Must be set
repo-hot = "/my/hot/repo" # Default: not set
# one of the three password options must be set
password = "mySecretPassword"
password-file = "/my/password.txt"
password-command = "my_command.sh"
no-cache = false
cache-dir = "/my/rustic/cachedir" # Default: Applications default cache dir, e.g. ~/.cache/rustic
# use either warm-up (warm-up by file access) or warm-up-command to specify warming up
warm-up = false
warm-up-command = "warmup.sh %id" # Default: not set
warm-up-wait = "10min" # Default: not set

[[copy.targets]]
repository = "/repo/rustic2" # Must be set
# ...
[copy]
target = ["profile1", "profile2"] # Default: not set

[webdav]
address = "localhost:8000"
Expand Down
1 change: 1 addition & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ impl Configurable<RusticConfig> for EntryPoint {

match &self.commands {
RusticCmd::Forget(cmd) => cmd.override_config(config),
RusticCmd::Copy(cmd) => cmd.override_config(config),
#[cfg(feature = "webdav")]
RusticCmd::Webdav(cmd) => cmd.override_config(config),

Expand Down
58 changes: 42 additions & 16 deletions src/commands/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,58 @@

use crate::{
commands::{get_repository, init::init_password, open_repository, open_repository_indexed},
config::AllRepositoryOptions,
helpers::table_with_titles,
status_err, Application, RUSTIC_APP,
status_err, Application, RusticConfig, RUSTIC_APP,
};
use abscissa_core::{Command, Runnable, Shutdown};
use abscissa_core::{config::Override, Command, FrameworkError, Runnable, Shutdown};
use anyhow::{bail, Result};
use log::{error, info};
use log::{error, info, log, Level};
use merge::Merge;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, OneOrMany};

use rustic_core::{CopySnapshot, Id, KeyOptions};

/// `copy` subcommand
#[derive(clap::Parser, Command, Debug)]
pub(crate) struct CopyCmd {
#[serde_as]
#[derive(clap::Parser, Command, Default, Clone, Debug, Serialize, Deserialize, Merge)]
pub struct CopyCmd {
/// Snapshots to copy. If none is given, use filter options to filter from all snapshots.
#[clap(value_name = "ID")]
#[serde(skip)]
#[merge(skip)]
ids: Vec<String>,

/// Initialize non-existing target repositories
#[clap(long)]
#[serde(skip)]
#[merge(skip)]
init: bool,

/// Target repository (can be specified multiple times)
#[clap(long)]
#[merge(strategy = merge::vec::overwrite_empty)]
#[serde_as(as = "OneOrMany<_>")]
target: Vec<String>,

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

/// Target repository options
#[derive(Default, Clone, Debug, Serialize, Deserialize, Merge)]
pub struct Targets {
/// Target repositories
#[merge(strategy = merge::vec::overwrite_empty)]
targets: Vec<AllRepositoryOptions>,
impl Override<RusticConfig> for CopyCmd {
// Process the given command line options, overriding settings from
// a configuration file using explicit flags taken from command-line
// arguments.
fn override_config(&self, mut config: RusticConfig) -> Result<RusticConfig, FrameworkError> {
let mut self_config = self.clone();
// merge "copy" section from config file, if given
self_config.merge(config.copy);
config.copy = self_config;
Ok(config)
}
}

impl Runnable for CopyCmd {
Expand All @@ -51,9 +69,8 @@ impl CopyCmd {
fn inner_run(&self) -> Result<()> {
let config = RUSTIC_APP.config();

if config.copy.targets.is_empty() {
status_err!("no [[copy.targets]] section in config file found!");
RUSTIC_APP.shutdown(Shutdown::Crash);
if config.copy.target.is_empty() {
bail!("No target given. Please specify at least 1 target either in the profile or using --target!");
}

let repo = open_repository_indexed(&config.repository)?;
Expand All @@ -66,7 +83,16 @@ impl CopyCmd {
snapshots.sort_unstable();

let poly = repo.config().poly()?;
for target_opt in &config.copy.targets {
for target in &config.copy.target {
let mut merge_logs = Vec::new();
let mut target_config = RusticConfig::default();
target_config.merge_profile(target, &mut merge_logs, Level::Error)?;
// display logs from merging
for (level, merge_log) in merge_logs {
log!(level, "{}", merge_log);
}
let target_opt = &target_config.repository;

let repo_dest = get_repository(target_opt)?;

info!("copying to target {}...", repo_dest.name);
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde_with::{serde_as, OneOrMany};
#[cfg(feature = "webdav")]
use crate::commands::webdav::WebDavCmd;
use crate::{
commands::{backup::BackupCmd, copy::Targets, forget::ForgetOptions},
commands::{backup::BackupCmd, copy::CopyCmd, forget::ForgetOptions},
config::progress_options::ProgressOptions,
filtering::SnapshotFilter,
};
Expand Down Expand Up @@ -56,7 +56,7 @@ pub struct RusticConfig {

/// Copy options
#[clap(skip)]
pub copy: Targets,
pub copy: CopyCmd,

/// Forget options
#[clap(skip)]
Expand Down
2 changes: 1 addition & 1 deletion tests/show-config-fixtures/empty.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sources = []
source = []

[copy]
targets = []
target = []
aawsome marked this conversation as resolved.
Show resolved Hide resolved

[forget]
prune = false
Expand Down
Loading