summaryrefslogtreecommitdiff
path: root/src/backup.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2024-09-06 13:47:47 +0200
committerfxqnlr <[email protected]>2024-09-06 13:47:47 +0200
commitab7f99e061e54924899b778e929dd2e17c8792d9 (patch)
tree644dc766f954b7a5e04cf7658821a5b5694f764b /src/backup.rs
parent3e1cb020d5449849b37874f91cadfa4a9c878747 (diff)
downloadarbs-ab7f99e061e54924899b778e929dd2e17c8792d9.tar
arbs-ab7f99e061e54924899b778e929dd2e17c8792d9.tar.gz
arbs-ab7f99e061e54924899b778e929dd2e17c8792d9.zip
add root index and directory creation
Diffstat (limited to 'src/backup.rs')
-rw-r--r--src/backup.rs89
1 files changed, 80 insertions, 9 deletions
diff --git a/src/backup.rs b/src/backup.rs
index 4e74c97..69bc2ea 100644
--- a/src/backup.rs
+++ b/src/backup.rs
@@ -1,9 +1,20 @@
1use std::time::{SystemTime, UNIX_EPOCH}; 1use std::{
2 fs::{create_dir_all, File, OpenOptions},
3 io::{ErrorKind, Read, Write},
4 path::PathBuf,
5 time::{SystemTime, UNIX_EPOCH},
6};
2 7
8use gethostname::gethostname;
3use serde::{Deserialize, Serialize}; 9use serde::{Deserialize, Serialize};
4use uuid::Uuid; 10use uuid::Uuid;
5 11
6use crate::{config::Config, pathinfo::PathInfo, packages::Package, error::Result}; 12use crate::{
13 config::Config,
14 error::{Error, Result},
15 packages::Package,
16 pathinfo::PathInfo,
17};
7 18
8pub type BackupId = String; 19pub type BackupId = String;
9 20
@@ -24,21 +35,81 @@ impl Backup {
24 Ok(Self { 35 Ok(Self {
25 // UUID not really needed, maybe a shorter hash 36 // UUID not really needed, maybe a shorter hash
26 id: Uuid::new_v4().to_string(), 37 id: Uuid::new_v4().to_string(),
27 timestamp: SystemTime::now() 38 timestamp: Self::get_timestamp(),
28 .duration_since(UNIX_EPOCH)
29 .unwrap()
30 .as_secs(),
31 packages, 39 packages,
32 files, 40 files,
33 }) 41 })
34 } 42 }
35 43
36 44 pub fn save(&self, config: &Config) -> Result<()> {
45 let rel_location = format!(
46 "bu_{}_{}",
47 gethostname()
48 .into_string()
49 .map_err(|_| Error::InvalidOsString)?,
50 Self::get_timestamp()
51 );
52
53 let bl = BackupLocation {
54 id: self.id.to_string(),
55 rel_location,
56 };
57
58 Self::append_to_root_index(config, bl.clone())?;
59
60 let backup_root = format!("{}/{}", config.root, bl.rel_location);
61 create_dir_all(&backup_root).unwrap();
62 let path = format!("{}/index.json", backup_root);
63 let mut f = File::create(path).unwrap();
64 f.write_all(&serde_json::to_vec(self).unwrap()).unwrap();
65
66 Ok(())
67 }
68
69 pub fn get(config: &Config, _id: Option<BackupId>) -> Result<()> {
70 let backup_index_root = format!("{}/index.json", config.root);
71 let mut file = File::open(backup_index_root)?;
72 let mut content = String::new();
73 file.read_to_string(&mut content)?;
74 let list: Vec<BackupLocation> = serde_json::from_str(&content)?;
75 println!("{list:#?}");
76
77 todo!();
78
79 Ok(())
80 }
81
82 fn append_to_root_index(config: &Config, new_backup: BackupLocation) -> Result<()> {
83 let backup_index_root = format!("{}/index.json", config.root);
84 let path = PathBuf::from(&backup_index_root);
85 if path.exists() {
86 let mut f = File::open(&path)?;
87 let mut content = String::new();
88 f.read_to_string(&mut content)?;
89 let mut loc: Vec<BackupLocation> = serde_json::from_str(&content)?;
90
91 let mut f = File::create(path)?;
92 loc.push(new_backup);
93
94 f.write_all(&serde_json::to_vec(&loc)?)?;
95 } else {
96 let mut f = File::create(backup_index_root)?;
97 f.write_all(&serde_json::to_vec(&vec![new_backup])?)?;
98 };
99
100 Ok(())
101 }
102
103 fn get_timestamp() -> u64 {
104 SystemTime::now()
105 .duration_since(UNIX_EPOCH)
106 .unwrap()
107 .as_secs()
108 }
37} 109}
38 110
111#[derive(Debug, Clone, Serialize, Deserialize)]
39struct BackupLocation { 112struct BackupLocation {
40 id: BackupId, 113 id: BackupId,
41 rel_location: String, 114 rel_location: String,
42} 115}
43
44type BackupList = Vec<BackupLocation>;