diff --git a/config.toml b/config.toml index 2b28851..705d362 100644 --- a/config.toml +++ b/config.toml @@ -65,6 +65,9 @@ # Make a CephFS snapshot before backup # cephfs_snap = true +# Ensure the file paths stay the same between backups +# same_path = true + # Borg Check Operation # [[borg_check]] # Repository to check diff --git a/src/borg.rs b/src/borg.rs index b93bf2c..af16e1d 100644 --- a/src/borg.rs +++ b/src/borg.rs @@ -10,6 +10,14 @@ pub fn init_repo(path: &str) { run_command(&["borg", "init", "--encryption=repokey-blake2", path], None); } +pub fn bind_mount(src: &str, dst: &str) { + run_command(&["mount", "--bind", src, dst], None); +} + +pub fn umount(mount: &str) { + run_command(&["umount", mount], None); +} + pub fn create_archive(conf: &BorgConfig) { let archive_name = format!( "BK_{}_{}_{}", @@ -87,14 +95,28 @@ pub fn create_archive(conf: &BorgConfig) { 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); + let mut dirs = if snaps.is_empty() { + conf.src.clone() + } else { + snaps.iter().map(|x| x.0.clone()).collect::>() + }; + + let mut mounts = Vec::new(); + if conf.same_path.unwrap_or_default() { + for path in &dirs { + let name = path.replace("/", "_"); + println!("--> Creating consistent path /bk/{}", name); + std::fs::create_dir_all(&format!("/bk/{name}")).unwrap(); + bind_mount(path, &format!("/bk/{name}")); + mounts.push(format!["/bk/{name}"]); + } + + dirs = mounts.clone(); + } + + cmd.extend(dirs.iter().map(|x| x.as_str())); run_command(&cmd, conf.passphrase.clone()); @@ -102,6 +124,11 @@ pub fn create_archive(conf: &BorgConfig) { cephfs_snap_remove_dir(&cleanup.0); println!("--> Cleaning up snap {}", cleanup.0); } + + for cleanup in &mounts { + println!("--> Cleaning up mount {}", cleanup); + umount(&cleanup); + } } pub fn prune_archive(conf: &BorgPruneConfig) { diff --git a/src/config.rs b/src/config.rs index d443cdd..5b211fc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -84,7 +84,10 @@ pub struct BorgConfig { pub ensure_exists: Option, /// Create CephFS snapshots before the backup. - pub cephfs_snap: Option + pub cephfs_snap: Option, + + /// Bind mount to consistent path + pub same_path: Option, } /// Configuration for a Borg repository integrity check job.