1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! `backup` subcommand

use std::path::PathBuf;

use crate::{
    commands::{get_repository, init::init, open_repository},
    helpers::bytes_size_to_string,
    status_err, Application, RUSTIC_APP,
};

use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::{bail, Context, Result};
use clap::ValueHint;
use log::{debug, info, warn};
use merge::Merge;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, OneOrMany};

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

/// `backup` subcommand
#[serde_as]
#[derive(Clone, Command, Default, Debug, clap::Parser, Serialize, Deserialize, Merge)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
// Note: using cli_sources, sources and source within this struct is a hack to support serde(deny_unknown_fields)
// for deserializing the backup options from TOML
// Unfortunately we cannot work with nested flattened structures, see
// https://github.com/serde-rs/serde/issues/1547
// A drawback is that a wrongly set "source(s) = ..." won't get correct error handling and need to be manually checked, see below.
#[allow(clippy::struct_excessive_bools)]
pub struct BackupCmd {
    /// Backup source (can be specified multiple times), use - for stdin. If no source is given, uses all
    /// sources defined in the config file
    #[clap(value_name = "SOURCE", value_hint = ValueHint::AnyPath)]
    #[merge(skip)]
    #[serde(skip)]
    cli_sources: Vec<String>,

    /// Set filename to be used when backing up from stdin
    #[clap(long, value_name = "FILENAME", default_value = "stdin", value_hint = ValueHint::FilePath)]
    #[merge(skip)]
    stdin_filename: String,

    /// Manually set backup path in snapshot
    #[clap(long, value_name = "PATH", value_hint = ValueHint::DirPath)]
    as_path: Option<PathBuf>,

    /// Ignore save options
    #[clap(flatten)]
    #[serde(flatten)]
    ignore_save_opts: LocalSourceSaveOptions,

    /// Don't scan the backup source for its size - this disables ETA estimation for backup.
    #[clap(long)]
    #[merge(strategy = merge::bool::overwrite_false)]
    pub no_scan: bool,

    /// Output generated snapshot in json format
    #[clap(long)]
    #[merge(strategy = merge::bool::overwrite_false)]
    json: bool,

    /// Don't show any output
    #[clap(long, conflicts_with = "json")]
    #[merge(strategy = merge::bool::overwrite_false)]
    quiet: bool,

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

    /// Parent processing options
    #[clap(flatten, next_help_heading = "Options for parent processing")]
    #[serde(flatten)]
    parent_opts: ParentOptions,

    /// Exclude options
    #[clap(flatten, next_help_heading = "Exclude options")]
    #[serde(flatten)]
    ignore_filter_opts: LocalSourceFilterOptions,

    /// Snapshot options
    #[clap(flatten, next_help_heading = "Snapshot options")]
    #[serde(flatten)]
    snap_opts: SnapshotOptions,

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

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

    /// Backup sources
    #[clap(skip)]
    #[merge(strategy = merge_sources)]
    sources: Vec<BackupCmd>,

    /// Backup source, used within config file
    #[clap(skip)]
    #[merge(skip)]
    #[serde_as(as = "OneOrMany<_>")]
    source: Vec<String>,
}

/// Merge backup sources
///
/// If a source is already defined on left, use that. Else add it.
///
/// # Arguments
///
/// * `left` - Vector of backup sources
pub(crate) fn merge_sources(left: &mut Vec<BackupCmd>, mut right: Vec<BackupCmd>) {
    left.append(&mut right);
    left.sort_by(|opt1, opt2| opt1.source.cmp(&opt2.source));
    left.dedup_by(|opt1, opt2| opt1.source == opt2.source);
}

impl Runnable for BackupCmd {
    fn run(&self) {
        if let Err(err) = self.inner_run() {
            status_err!("{}", err);
            RUSTIC_APP.shutdown(Shutdown::Crash);
        };
    }
}

impl BackupCmd {
    fn inner_run(&self) -> Result<()> {
        let config = RUSTIC_APP.config();
        let repo = get_repository(&config.repository)?;
        // 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.repository)?
        }
        .to_indexed_ids()?;

        // manually check for a "source" field, check is not done by serde, see above.
        if !config.backup.source.is_empty() {
            bail!("key \"source\" is not valid in the [backup] section!");
        }

        let config_opts = &config.backup.sources;

        // manually check for a "sources" field, check is not done by serde, see above.
        if config_opts.iter().any(|opt| !opt.sources.is_empty()) {
            bail!("key \"sources\" is not valid in a [[backup.sources]] section!");
        }

        let config_sources: Vec<_> = config_opts
            .iter()
            .map(|opt| -> Result<_> {
                Ok(PathList::from_iter(&opt.source)
                    .sanitize()
                    .with_context(|| {
                        format!(
                            "error sanitizing source=\"{:?}\" in config file",
                            opt.source
                        )
                    })?
                    .merge())
            })
            .filter_map(|p| match p {
                Ok(paths) => Some(paths),
                Err(err) => {
                    warn!("{err}");
                    None
                }
            })
            .collect();

        let sources = match (self.cli_sources.is_empty(), config_opts.is_empty()) {
            (false, _) => {
                let item = PathList::from_iter(&self.cli_sources).sanitize()?;
                vec![item]
            }
            (true, false) => {
                info!("using all backup sources from config file.");
                config_sources.clone()
            }
            (true, true) => {
                bail!("no backup source given.");
            }
        };

        for source in sources {
            let mut opts = self.clone();

            // merge Options from config file, if given
            if let Some(idx) = config_sources.iter().position(|s| s == &source) {
                info!("merging source={source} section from config file");
                opts.merge(config_opts[idx].clone());
            }
            if let Some(path) = &opts.as_path {
                // as_path only works in combination with a single target
                if source.len() > 1 {
                    bail!("as-path only works with a single target!");
                }
                // merge Options from config file using as_path, if given
                if let Some(path) = path.as_os_str().to_str() {
                    if let Some(idx) = config_opts.iter().position(|opt| opt.source == vec![path]) {
                        info!("merging source=\"{path}\" section from config file");
                        opts.merge(config_opts[idx].clone());
                    }
                }
            }

            // merge "backup" section from config file, if given
            opts.merge(config.backup.clone());

            let backup_opts = BackupOptions::default()
                .stdin_filename(opts.stdin_filename)
                .as_path(opts.as_path)
                .parent_opts(opts.parent_opts)
                .ignore_save_opts(opts.ignore_save_opts)
                .ignore_filter_opts(opts.ignore_filter_opts)
                .no_scan(opts.no_scan)
                .dry_run(config.global.dry_run);
            let snap = repo.backup(&backup_opts, &source, opts.snap_opts.to_snapshot()?)?;

            if opts.json {
                let mut stdout = std::io::stdout();
                serde_json::to_writer_pretty(&mut stdout, &snap)?;
            } else if !opts.quiet {
                let summary = snap.summary.unwrap();
                println!(
                    "Files:       {} new, {} changed, {} unchanged",
                    summary.files_new, summary.files_changed, summary.files_unmodified
                );
                println!(
                    "Dirs:        {} new, {} changed, {} unchanged",
                    summary.dirs_new, summary.dirs_changed, summary.dirs_unmodified
                );
                debug!("Data Blobs:  {} new", summary.data_blobs);
                debug!("Tree Blobs:  {} new", summary.tree_blobs);
                println!(
                    "Added to the repo: {} (raw: {})",
                    bytes_size_to_string(summary.data_added_packed),
                    bytes_size_to_string(summary.data_added)
                );

                println!(
                    "processed {} files, {}",
                    summary.total_files_processed,
                    bytes_size_to_string(summary.total_bytes_processed)
                );
                println!("snapshot {} successfully saved.", snap.id);
            }

            info!("backup of {source} done.");
        }

        Ok(())
    }
}