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
#![allow(clippy::let_and_return, clippy::let_unit_value)]
#[macro_use]
mod redefine;
mod args;
#[doc(hidden)]
pub mod btrfs;
mod repo;
#[doc(hidden)]
pub mod snapshot;
#[cfg(any(test, feature = "test"))]
pub mod test;
#[doc(hidden)]
pub mod util;
use std::borrow::Cow;
use std::ffi::OsString;
use std::path::Path;
use std::path::PathBuf;
use anyhow::Context as _;
use anyhow::Result;
use clap::Parser as _;
use time::Duration;
use crate::args::Args;
use crate::args::Backup;
use crate::args::Command;
use crate::args::Purge;
use crate::args::Restore;
use crate::args::Snapshot;
use crate::btrfs::trace_commands;
use crate::repo::backup as backup_subvol;
use crate::repo::restore as restore_subvol;
use crate::repo::Repo;
use crate::snapshot::current_time;
use crate::snapshot::Subvol;
use crate::util::canonicalize_non_strict;
fn create_repo(directory: &Path) -> Result<Repo> {
let repo = Repo::new(directory).with_context(|| {
format!(
"failed to create btrfs snapshot repository at {}",
directory.display()
)
})?;
Ok(repo)
}
fn with_repo_and_subvols<F>(repo_path: Option<&Path>, subvols: &[PathBuf], f: F) -> Result<()>
where
F: FnMut(&Repo, &Path) -> Result<()>,
{
let mut f = f;
let repo = if let Some(repo_path) = repo_path {
Some(create_repo(repo_path)?)
} else {
None
};
let () = subvols.iter().try_for_each::<_, Result<()>>(|subvol| {
let repo = if let Some(repo) = &repo {
Cow::Borrowed(repo)
} else {
let directory = subvol.parent().with_context(|| {
format!(
"subvolume {} does not have a parent; need repository path",
subvol.display()
)
})?;
Cow::Owned(create_repo(directory)?)
};
f(&repo, subvol)
})?;
Ok(())
}
fn backup(backup: Backup) -> Result<()> {
let Backup {
subvolumes,
destination,
source,
} = backup;
let dst = create_repo(&destination)?;
with_repo_and_subvols(source.as_deref(), subvolumes.as_slice(), |src, subvol| {
let _snapshot = backup_subvol(src, &dst, subvol)
.with_context(|| format!("failed to backup subvolume {}", subvol.display()))?;
Ok(())
})
}
fn restore(restore: Restore) -> Result<()> {
let Restore {
subvolumes,
destination,
source,
snapshots_only,
} = restore;
let src = create_repo(&source)?;
with_repo_and_subvols(
destination.as_deref(),
subvolumes.as_slice(),
|dst, subvol| {
let _snapshot = restore_subvol(&src, dst, subvol, snapshots_only)
.with_context(|| format!("failed to restore subvolume {}", subvol.display()))?;
Ok(())
},
)
}
fn purge(purge: Purge) -> Result<()> {
fn purge_subvol(repo: &Repo, subvol: &Path, keep_for: Duration) -> Result<()> {
let subvol = canonicalize_non_strict(subvol)?;
let snapshots = repo
.snapshots()
.context("failed to list snapshots")?
.into_iter()
.map(|(snapshot, _generation)| snapshot)
.filter(|snapshot| snapshot.subvol == Subvol::new(&subvol));
let current_time = current_time();
let mut to_purge = snapshots
.clone()
.filter(|snapshot| current_time > snapshot.timestamp + keep_for);
if to_purge.clone().count() == snapshots.clone().count() {
let _skipped = to_purge.next_back();
}
let () = to_purge.try_for_each(|snapshot| {
repo.delete(&snapshot).with_context(|| {
format!(
"failed to delete snapshot {} in {}",
snapshot,
repo.path().display()
)
})
})?;
Ok(())
}
let Purge {
subvolumes,
source,
destination,
keep_for,
} = purge;
if let Some(destination) = destination {
let repo = create_repo(&destination)?;
let () = subvolumes
.iter()
.try_for_each(|subvol| purge_subvol(&repo, subvol, keep_for))?;
}
with_repo_and_subvols(source.as_deref(), subvolumes.as_slice(), |repo, subvol| {
purge_subvol(repo, subvol, keep_for)
})
}
fn snapshot(snapshot: Snapshot) -> Result<()> {
let Snapshot {
repository,
subvolumes,
} = snapshot;
with_repo_and_subvols(
repository.as_deref(),
subvolumes.as_slice(),
|repo, subvol| {
let _snapshot = repo
.snapshot(subvol)
.with_context(|| format!("failed to snapshot subvolume {}", subvol.display()))?;
Ok(())
},
)
}
pub fn run<A, T>(args: A) -> Result<()>
where
A: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let args = Args::try_parse_from(args).context("failed to parse program arguments")?;
if args.trace {
let () = trace_commands();
}
match args.command {
Command::Backup(backup) => self::backup(backup),
Command::Purge(purge) => self::purge(purge),
Command::Restore(restore) => self::restore(restore),
Command::Snapshot(snapshot) => self::snapshot(snapshot),
}
}