helix-toolbox 1.23.0

Build, configure and optimize a Linux machine from a terminal menu: installs, dotfiles, updates, backups and health checks, every change planned and confirmed first.
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! `helix-toolbox restic`: Restic backups of your own files on the command
//! line (helix_toolbox_engine::restic). Showing, browsing and a file's
//! history only read; checking, backing up, restoring and recovery tests
//! are plans, confirmed like any other.

use anyhow::{Result, bail};
use clap::Subcommand;
use helix_toolbox_catalog::Catalog;
use helix_toolbox_core::printable;
use helix_toolbox_engine::{
    history, notifications,
    restic::{self, Config, Every, Found, Here},
};
use helix_toolbox_platform::LinuxInfo;
use serde_json::json;
use std::path::{Path, PathBuf};

#[derive(Subcommand)]
pub enum ResticCommand {
    /// The repository, how restic finds its password, and the snapshots
    /// (the default).
    Show,
    /// What a folder of a snapshot holds.
    Files {
        /// The snapshot's id, or latest.
        snapshot: String,
        /// The folder, a full path; the snapshot's first folder when left
        /// out.
        folder: Option<String>,
    },
    /// Every version of the files a name or path matches, across all
    /// snapshots, with which changed.
    History {
        /// A file name or path, with * wildcards, such as notes.txt or
        /// ~/Documents/*.odt.
        pattern: String,
        #[arg(long)]
        ignore_case: bool,
    },
    /// Check the repository's structure; confirmed by typing CHECK.
    Check {
        #[arg(long)]
        dry_run: bool,
        #[arg(long)]
        yes: bool,
    },
    /// Back up the folders `[restic]` in settings.toml names (the home
    /// folder unless set); confirmed by typing BACKUP.
    Backup {
        #[arg(long)]
        dry_run: bool,
        #[arg(long)]
        yes: bool,
    },
    /// Restore a snapshot, or one path of it, into a new folder of your
    /// home folder; nothing is overwritten. Confirmed by typing RESTORE.
    Restore {
        snapshot: String,
        /// The new folder to restore into.
        target: PathBuf,
        /// Only this path of the snapshot, a full path.
        #[arg(long)]
        include: Option<String>,
        #[arg(long)]
        dry_run: bool,
        #[arg(long)]
        yes: bool,
    },
    /// Record a file's SHA-256 as a baseline to test restores against; with
    /// no file, list the baselines.
    Baseline { file: Option<PathBuf> },
    /// Restore a baseline's file from a snapshot into a temporary folder
    /// and compare its SHA-256; confirmed by typing TEST.
    Test {
        /// The baseline: its number in `restic baseline`, or its id.
        baseline: String,
        /// The snapshot; the latest when left out.
        snapshot: Option<String>,
        #[arg(long)]
        dry_run: bool,
        #[arg(long)]
        yes: bool,
    },
    /// Back up on a timer of your systemd user session, or stop.
    Timer {
        switch: Switch,
        /// Every week instead of every day.
        #[arg(long)]
        weekly: bool,
    },
}

#[derive(Clone, Copy, clap::ValueEnum)]
pub enum Switch {
    On,
    Off,
}

fn home() -> Result<PathBuf> {
    std::env::var_os("HOME")
        .map(PathBuf::from)
        .ok_or_else(|| anyhow::anyhow!("there is no home folder"))
}

fn state() -> Result<PathBuf> {
    history::state_dir().ok_or_else(|| anyhow::anyhow!("cannot find the state folder"))
}

/// The repository set up, or why there is none.
fn ready() -> Result<Config> {
    match restic::here() {
        Found::Ready(config) => Ok(config),
        Found::Problem(why) => bail!("{why}"),
        Found::Missing => bail!(
            "no restic repository is set up: add [restic] with repository and password_file to {}",
            helix_toolbox_engine::settings::path()
                .map(|path| path.display().to_string())
                .unwrap_or_else(|| "settings.toml".into())
        ),
    }
}

/// A path typed in: `~/` for the home folder, relative to here otherwise.
fn typed(path: &Path) -> Result<PathBuf> {
    let home = home()?;
    Ok(match path.strip_prefix("~") {
        Ok(rest) => home.join(rest),
        Err(_) if path.is_absolute() => path.to_path_buf(),
        Err(_) => std::env::current_dir()?.join(path),
    })
}

/// What the overview shows, as JSON: also the window's.
pub fn overview(
    found: &Found,
    snapshots: Option<&Result<Vec<restic::Snapshot>, String>>,
) -> serde_json::Value {
    let timer = restic::units_here().and_then(|units| restic::timer(&units));
    let (config, problem) = match found {
        Found::Ready(config) => (Some(config), None),
        Found::Problem(why) => (None, Some(why.clone())),
        Found::Missing => (None, None),
    };
    json!({
        "configured": config.is_some(),
        "repository": config.map(|config| &config.repository),
        "origin": config.map(|config| config.origin),
        "password": config.map(|config| config.password.describe()),
        "paths": config.map(|config| &config.paths).cloned().unwrap_or_default(),
        "excludes": config.map(|config| &config.excludes).cloned().unwrap_or_default(),
        "problem": problem,
        "snapshots": snapshots.and_then(|read| read.as_ref().ok()).cloned().unwrap_or_default(),
        "snapshots_problem": snapshots.and_then(|read| read.as_ref().err()),
        "timer": timer,
    })
}

pub fn restic(
    command: Option<ResticCommand>,
    catalog: &Catalog,
    platform: &LinuxInfo,
    as_json: bool,
) -> Result<()> {
    match command.unwrap_or(ResticCommand::Show) {
        ResticCommand::Show => show(as_json),
        ResticCommand::Files { snapshot, folder } => {
            let config = ready()?;
            let folder = match folder {
                Some(folder) => folder,
                None if snapshot == "latest" => config
                    .paths
                    .first()
                    .map(|path| path.display().to_string())
                    .unwrap_or_else(|| "/".into()),
                None => restic::snapshots(&Here, &config)
                    .map_err(anyhow::Error::msg)?
                    .iter()
                    .find(|found| found.id.starts_with(&snapshot))
                    .and_then(|found| found.paths.first().cloned())
                    .unwrap_or_else(|| "/".into()),
            };
            let nodes =
                restic::files(&Here, &config, &snapshot, &folder).map_err(anyhow::Error::msg)?;
            if as_json {
                crate::json::print(
                    "restic-files",
                    json!({"snapshot": snapshot, "folder": folder, "nodes": nodes}),
                );
                return Ok(());
            }
            println!(
                "{} in snapshot {}:",
                printable(&folder),
                printable(&snapshot)
            );
            for node in &nodes {
                if node.is_dir() {
                    println!("  {}/", printable(&node.name));
                } else {
                    println!(
                        "  {:<48} {}",
                        printable(&node.name),
                        node.size.map(size).unwrap_or_default()
                    );
                }
            }
            if nodes.is_empty() {
                println!("  (nothing)");
            }
            Ok(())
        }
        ResticCommand::History {
            pattern,
            ignore_case,
        } => {
            let config = ready()?;
            let found = restic::history(&Here, &config, &home()?, &pattern, ignore_case)
                .map_err(anyhow::Error::msg)?;
            if as_json {
                crate::json::print(
                    "restic-history",
                    json!({"pattern": pattern, "files": found}),
                );
                return Ok(());
            }
            if found.is_empty() {
                println!("No snapshot has a file matching {}.", printable(&pattern));
            }
            for file in &found {
                println!("{}", printable(&file.path));
                for version in &file.versions {
                    println!(
                        "  {}  {}  {:<10} {}",
                        version.short_id,
                        version
                            .time
                            .replace('T', " ")
                            .get(..16)
                            .unwrap_or(&version.time),
                        version.size.map(size).unwrap_or_default(),
                        match version.change {
                            restic::Change::First => "first",
                            restic::Change::Changed => "changed",
                            restic::Change::Same => "same",
                        }
                    );
                }
                if file.missing_from > 0 {
                    println!(
                        "  Not in the {} newer snapshot(s) of the same folder: deleted or moved since.",
                        file.missing_from
                    );
                }
            }
            if !found.is_empty() {
                println!(
                    "helix-toolbox restic restore <snapshot> <new folder> --include <path> brings one back."
                );
            }
            Ok(())
        }
        ResticCommand::Check { dry_run, yes } => crate::confirm_and_run(
            catalog,
            platform,
            &restic::plan_check(&ready()?),
            dry_run,
            yes,
            "CHECK",
        ),
        ResticCommand::Backup { dry_run, yes } => crate::confirm_and_run(
            catalog,
            platform,
            &restic::plan_backup(&ready()?),
            dry_run,
            yes,
            "BACKUP",
        ),
        ResticCommand::Restore {
            snapshot,
            target,
            include,
            dry_run,
            yes,
        } => {
            let config = ready()?;
            if !restic::restores_safely(&Here) {
                bail!(
                    "this restic cannot restore without overwriting and check what it wrote; restic 0.17 or later can"
                );
            }
            let target =
                restic::check_target(&home()?, &typed(&target)?).map_err(anyhow::Error::msg)?;
            let include = include
                .map(|path| typed(Path::new(&path)).map(|path| path.display().to_string()))
                .transpose()?;
            let planned = restic::plan_restore(&config, &snapshot, &target, include.as_deref())
                .map_err(anyhow::Error::msg)?;
            crate::confirm_and_run(catalog, platform, &planned, dry_run, yes, "RESTORE")
        }
        ResticCommand::Baseline { file: Some(file) } => {
            let recorded = restic::record(&state()?, &home()?, &typed(&file)?, history::now())
                .map_err(anyhow::Error::msg)?;
            if as_json {
                crate::json::print("restic-baseline", json!(recorded));
                return Ok(());
            }
            println!("Recorded {}: SHA-256 {}.", recorded.path, recorded.sha256);
            println!(
                "Back it up, then helix-toolbox restic test 1 restores it from the latest snapshot and compares."
            );
            Ok(())
        }
        ResticCommand::Baseline { file: None } => {
            let all = restic::baselines(&state()?);
            if as_json {
                crate::json::print("restic-baselines", json!({"baselines": all}));
                return Ok(());
            }
            if all.is_empty() {
                println!("No baselines yet: helix-toolbox restic baseline <file> records one.");
            }
            for (place, baseline) in all.iter().enumerate() {
                println!(
                    "{:>2}. {}  {}  {}",
                    place + 1,
                    crate::when(baseline.recorded),
                    &baseline.sha256[..12.min(baseline.sha256.len())],
                    printable(&baseline.path)
                );
            }
            Ok(())
        }
        ResticCommand::Test {
            baseline,
            snapshot,
            dry_run,
            yes,
        } => {
            let config = ready()?;
            let all = restic::baselines(&state()?);
            let Some(chosen) = restic::find_baseline(&all, &baseline) else {
                bail!("no baseline {baseline}: helix-toolbox restic baseline lists them");
            };
            let planned =
                restic::plan_test(&config, chosen, snapshot.as_deref().unwrap_or("latest"))
                    .map_err(anyhow::Error::msg)?;
            crate::confirm_and_run(catalog, platform, &planned, dry_run, yes, "TEST")
        }
        ResticCommand::Timer { switch, weekly } => {
            let units = restic::units_here()
                .ok_or_else(|| anyhow::anyhow!("cannot find your systemd folder"))?;
            match switch {
                Switch::On => {
                    let config = ready()?;
                    let program = std::env::current_exe()?;
                    let every = if weekly { Every::Week } else { Every::Day };
                    let started = restic::timer_on(
                        &units,
                        &program,
                        &config,
                        every,
                        &notifications::systemctl,
                    )
                    .map_err(anyhow::Error::msg)?;
                    println!(
                        "The backup runs {}{}.",
                        every.label(),
                        match started {
                            notifications::Started::Now => "",
                            notifications::Started::AtLogin => ", from the next login",
                        }
                    );
                }
                Switch::Off => {
                    restic::timer_off(&units, &notifications::systemctl)
                        .map_err(anyhow::Error::msg)?;
                    println!("The timed backup is off.");
                }
            }
            Ok(())
        }
    }
}

fn size(bytes: u64) -> String {
    helix_toolbox_engine::storage::shown(bytes)
}

fn show(as_json: bool) -> Result<()> {
    let found = restic::here();
    let snapshots = match &found {
        Found::Ready(config) => Some(restic::snapshots(&Here, config)),
        _ => None,
    };
    if as_json {
        crate::json::print("restic", overview(&found, snapshots.as_ref()));
        return Ok(());
    }
    let config = match &found {
        Found::Ready(config) => config,
        Found::Problem(why) => bail!("{why}"),
        Found::Missing => {
            println!("No restic repository is set up. Add to settings.toml:");
            println!();
            println!("[restic]");
            println!("repository = \"/path/to/the/repository\"");
            println!("password_file = \"~/.config/restic/password\"");
            println!("# paths = [\"~/Documents\"]  (the home folder when left out)");
            println!("# excludes = [\"~/.cache\"]");
            println!();
            println!("RESTIC_REPOSITORY and RESTIC_PASSWORD_FILE are read too.");
            return Ok(());
        }
    };
    println!(
        "Repository: {} (from {})",
        printable(&config.repository),
        config.origin.label()
    );
    println!("Password: from {}", config.password.describe());
    println!(
        "Backs up: {}{}",
        config
            .paths
            .iter()
            .map(|path| path.display().to_string())
            .collect::<Vec<_>>()
            .join(", "),
        if config.excludes.is_empty() {
            String::new()
        } else {
            format!(", leaving out {}", config.excludes.join(", "))
        }
    );
    println!(
        "Timer: {}",
        restic::units_here()
            .and_then(|units| restic::timer(&units))
            .map(Every::label)
            .unwrap_or("off (helix-toolbox restic timer on)")
    );
    match snapshots.unwrap_or_else(|| Ok(Vec::new())) {
        Ok(listed) => {
            println!();
            println!("Snapshots ({}):", listed.len());
            for snapshot in &listed {
                println!(
                    "  {}  {}  {:<16} {}",
                    snapshot.short_id,
                    snapshot.when(),
                    printable(&snapshot.hostname),
                    printable(&snapshot.paths.join(", "))
                );
            }
            Ok(())
        }
        Err(why) => bail!("restic could not read the repository: {why}"),
    }
}