From 2e7d4aa52b04edebe315f078a94bb17bda6c1d83 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Mon, 6 Jan 2025 06:33:45 +0100 Subject: [PATCH] update --- config.toml | 6 ++++++ src/backup.rs | 7 +++++++ src/borg.rs | 27 ++++++++++++++++++++++++--- src/config.rs | 2 ++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/config.toml b/config.toml index 8c90f0b..44e9926 100644 --- a/config.toml +++ b/config.toml @@ -59,6 +59,12 @@ comment = "Backup of /home/me/" # Compression compression = "zstd,10" +# Ensure directory exists before backup +ensure_exists = "/home/me" + +# Make a CephFS snapshot before backup +cephfs_snap = true + # Borg Check Operation [[borg_check]] # Repository to check diff --git a/src/backup.rs b/src/backup.rs index e3c4cf4..1bd43bb 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -116,3 +116,10 @@ pub fn cephfs_snap_remove(dir: &str, snap: &str) { println!("--> Removing snapshot {} on {}", snap, dir); std::fs::remove_dir(snap_dir).unwrap() } + +pub fn cephfs_snap_remove_dir(dir: &str) { + let path = std::path::Path::new(dir); + + println!("--> Removing snapshot {}", path.to_str().unwrap()); + std::fs::remove_dir(path).unwrap() +} diff --git a/src/borg.rs b/src/borg.rs index 2556271..b93bf2c 100644 --- a/src/borg.rs +++ b/src/borg.rs @@ -1,7 +1,7 @@ use yansi::{Color, Paint}; use crate::{ - backup::nowtime, + backup::{cephfs_snap_create, cephfs_snap_remove_dir, ensure_exists, nowtime}, config::{BorgCheckConfig, BorgConfig, BorgPruneConfig}, run_command, }; @@ -24,6 +24,10 @@ pub fn create_archive(conf: &BorgConfig) { nowtime() ); + if let Some(dir) = &conf.ensure_exists { + ensure_exists(dir); + } + println!( "--> Running backup for {}", conf.src.join(",").paint(Color::Yellow), @@ -76,11 +80,28 @@ pub fn create_archive(conf: &BorgConfig) { cmd.push(&repo); - for path in &conf.src { - cmd.push(path); + let mut snaps = Vec::new(); + + if conf.cephfs_snap.unwrap_or_default() { + for path in &conf.src { + let snap = cephfs_snap_create(&path); + snaps.push(snap); + } + } else { + for path in &conf.src { + cmd.push(path); + } } + let snap_dirs = snaps.iter().map(|x| x.0.as_str()).collect::>(); + cmd.extend(snap_dirs); + run_command(&cmd, conf.passphrase.clone()); + + for cleanup in &snaps { + cephfs_snap_remove_dir(&cleanup.0); + println!("--> Cleaning up snap {}", cleanup.0); + } } pub fn prune_archive(conf: &BorgPruneConfig) { diff --git a/src/config.rs b/src/config.rs index 56dff47..a01d5a1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -37,6 +37,8 @@ pub struct BorgConfig { pub no_xattrs: Option, pub comment: Option, pub compression: Option, + pub ensure_exists: Option, + pub cephfs_snap: Option, } #[derive(Debug, Clone, Deserialize)]