Skip to content

Commit 534efbd

Browse files
jullanggitsimonsan
andauthored
feat: Add a "minutely" timeline (#374)
## Disclaimer I am not very experienced with contributing to projects like rustic, so please correct me if I make any mistakes or could improve something about this PR! In this case, I am particularly unsure about the commit message and how to add tests, so any tips there would be appreciated 😄 ## Motivation As creating a rustic snapshot can be really fast if not much has changed (less than 10s), I have started to backup-up my files every ten minutes, but noticed that rustic doesn't yet have any keep-options for sub-hourly snapshots, so I decided to add them! --------- Co-authored-by: simonsan <[email protected]>
1 parent 37cdbe5 commit 534efbd

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

crates/core/src/commands/forget.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ pub struct KeepOptions {
130130
#[cfg_attr(feature = "merge", merge(strategy = conflate::option::overwrite_none))]
131131
pub keep_last: Option<i32>,
132132

133+
/// Keep the last N minutely snapshots (N == -1: keep all minutely snapshots)
134+
#[cfg_attr(
135+
feature = "clap",
136+
clap(long, short = 'M', value_name = "N", allow_hyphen_values = true, value_parser = clap::value_parser!(i32).range(-1..))
137+
)]
138+
#[cfg_attr(feature = "merge", merge(strategy = conflate::option::overwrite_none))]
139+
pub keep_minutely: Option<i32>,
140+
133141
/// Keep the last N hourly snapshots (N == -1: keep all hourly snapshots)
134142
#[cfg_attr(
135143
feature = "clap",
@@ -192,6 +200,12 @@ pub struct KeepOptions {
192200
#[cfg_attr(feature = "merge", merge(strategy = conflate::option::overwrite_none))]
193201
pub keep_within: Option<humantime::Duration>,
194202

203+
/// Keep minutely snapshots newer than DURATION relative to latest snapshot
204+
#[cfg_attr(feature = "clap", clap(long, value_name = "DURATION"))]
205+
#[serde_as(as = "Option<DisplayFromStr>")]
206+
#[cfg_attr(feature = "merge", merge(strategy = conflate::option::overwrite_none))]
207+
pub keep_within_minutely: Option<humantime::Duration>,
208+
195209
/// Keep hourly snapshots newer than DURATION relative to latest snapshot
196210
#[cfg_attr(feature = "clap", clap(long, value_name = "DURATION"))]
197211
#[serde_as(as = "Option<DisplayFromStr>")]
@@ -356,12 +370,31 @@ fn equal_hour(sn1: &SnapshotFile, sn2: &SnapshotFile) -> bool {
356370
t1.year() == t2.year() && t1.ordinal() == t2.ordinal() && t1.hour() == t2.hour()
357371
}
358372

373+
/// Evaluate the minutes of the given snapshots
374+
///
375+
/// # Arguments
376+
///
377+
/// * `sn1` - The first snapshot
378+
/// * `sn2` - The second snapshot
379+
///
380+
/// # Returns
381+
///
382+
/// Whether the minutes of the snapshots are equal
383+
fn equal_minute(sn1: &SnapshotFile, sn2: &SnapshotFile) -> bool {
384+
let (t1, t2) = (sn1.time, sn2.time);
385+
t1.year() == t2.year()
386+
&& t1.ordinal() == t2.ordinal()
387+
&& t1.hour() == t2.hour()
388+
&& t1.minute() == t2.minute()
389+
}
390+
359391
impl KeepOptions {
360392
/// Check if `KeepOptions` are valid, i.e. if at least one keep-* option is given.
361393
fn is_valid(&self) -> bool {
362394
!self.keep_tags.is_empty()
363395
|| !self.keep_ids.is_empty()
364396
|| self.keep_last.is_some()
397+
|| self.keep_minutely.is_some()
365398
|| self.keep_hourly.is_some()
366399
|| self.keep_daily.is_some()
367400
|| self.keep_weekly.is_some()
@@ -370,6 +403,7 @@ impl KeepOptions {
370403
|| self.keep_half_yearly.is_some()
371404
|| self.keep_within.is_some()
372405
|| self.keep_yearly.is_some()
406+
|| self.keep_within_minutely.is_some()
373407
|| self.keep_within_hourly.is_some()
374408
|| self.keep_within_daily.is_some()
375409
|| self.keep_within_weekly.is_some()
@@ -392,6 +426,7 @@ impl KeepOptions {
392426
/// # Returns
393427
///
394428
/// The list of reasons why the snapshot should be kept
429+
#[allow(clippy::too_many_lines)]
395430
fn matches(
396431
&mut self,
397432
sn: &SnapshotFile,
@@ -422,14 +457,21 @@ impl KeepOptions {
422457
reason.push("tags");
423458
}
424459

425-
let keep_checks: [MatchParameters<'_>; 8] = [
460+
let keep_checks: [MatchParameters<'_>; 9] = [
426461
(
427462
always_false,
428463
&mut self.keep_last,
429464
"last",
430465
self.keep_within,
431466
"within",
432467
),
468+
(
469+
equal_minute,
470+
&mut self.keep_minutely,
471+
"minutely",
472+
self.keep_within_minutely,
473+
"within minutely",
474+
),
433475
(
434476
equal_hour,
435477
&mut self.keep_hourly,

0 commit comments

Comments
 (0)