summaryrefslogtreecommitdiff
path: root/src/backup.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/backup.rs')
-rw-r--r--src/backup.rs28
1 files changed, 14 insertions, 14 deletions
diff --git a/src/backup.rs b/src/backup.rs
index 675f020..e463593 100644
--- a/src/backup.rs
+++ b/src/backup.rs
@@ -6,6 +6,7 @@ use std::{
6}; 6};
7 7
8use serde::{Deserialize, Serialize}; 8use serde::{Deserialize, Serialize};
9use tracing::info;
9use uuid::Uuid; 10use uuid::Uuid;
10 11
11use crate::{ 12use crate::{
@@ -15,7 +16,7 @@ use crate::{
15 pathinfo::PathInfo, 16 pathinfo::PathInfo,
16}; 17};
17 18
18pub type BackupId = String; 19pub type Id = String;
19 20
20#[derive(Debug, Serialize, Deserialize)] 21#[derive(Debug, Serialize, Deserialize)]
21pub struct Backup { 22pub struct Backup {
@@ -43,13 +44,12 @@ impl Backup {
43 } 44 }
44 45
45 pub fn save(&self, config: &Config) -> Result<()> { 46 pub fn save(&self, config: &Config) -> Result<()> {
46 println!("Save Backup {:?}", self.get_location(config)); 47 info!("Save Backup {:?}", self.get_location(config));
47 // println!("{self:#?}");
48 self.get_location(config).append_to_root(config)?; 48 self.get_location(config).append_to_root(config)?;
49 49
50 let backup_root = self.get_location(config).get_absolute_dir(config); 50 let backup_root = self.get_location(config).get_absolute_dir(config);
51 create_dir_all(&backup_root).unwrap(); 51 create_dir_all(&backup_root).unwrap();
52 let path = format!("{}/index.json", backup_root); 52 let path = format!("{backup_root}/index.json");
53 let mut f = File::create(path).unwrap(); 53 let mut f = File::create(path).unwrap();
54 f.write_all(&serde_json::to_vec(self).unwrap()).unwrap(); 54 f.write_all(&serde_json::to_vec(self).unwrap()).unwrap();
55 55
@@ -62,7 +62,7 @@ impl Backup {
62 62
63 pub fn get_last(config: &Config) -> Result<Option<Self>> { 63 pub fn get_last(config: &Config) -> Result<Option<Self>> {
64 let backup_index_root = format!("{}/index.json", config.root); 64 let backup_index_root = format!("{}/index.json", config.root);
65 let list: Vec<BackupLocation> = match Self::get_json_content(&backup_index_root) { 65 let list: Vec<IndexEntry> = match Self::get_json_content(&backup_index_root) {
66 Ok(list) => list, 66 Ok(list) => list,
67 Err(err) => { 67 Err(err) => {
68 if err.to_string() == "io: No such file or directory (os error 2)" { 68 if err.to_string() == "io: No such file or directory (os error 2)" {
@@ -78,9 +78,9 @@ impl Backup {
78 )?)) 78 )?))
79 } 79 }
80 80
81 pub fn from_index(config: &Config, id: &BackupId) -> Result<Self> { 81 pub fn from_index(config: &Config, id: &Id) -> Result<Self> {
82 let backup_index_root = format!("{}/index.json", config.root); 82 let backup_index_root = format!("{}/index.json", config.root);
83 let list: Vec<BackupLocation> = Self::get_json_content(&backup_index_root)?; 83 let list: Vec<IndexEntry> = Self::get_json_content(&backup_index_root)?;
84 let index_loc = list 84 let index_loc = list
85 .iter() 85 .iter()
86 .find(|bl| &bl.id == id) 86 .find(|bl| &bl.id == id)
@@ -94,10 +94,10 @@ impl Backup {
94 Ok(index_file) 94 Ok(index_file)
95 } 95 }
96 96
97 pub fn get_location(&self, config: &Config) -> BackupLocation { 97 pub fn get_location(&self, config: &Config) -> IndexEntry {
98 let rel_location = format!("{}_{}", config.device, self.timestamp); 98 let rel_location = format!("{}_{}", config.device, self.timestamp);
99 99
100 BackupLocation { 100 IndexEntry {
101 id: self.id.to_string(), 101 id: self.id.to_string(),
102 rel_location, 102 rel_location,
103 } 103 }
@@ -106,7 +106,7 @@ impl Backup {
106 pub fn get_absolute_file_location(&self, config: &Config, rel_location: &str) -> String { 106 pub fn get_absolute_file_location(&self, config: &Config, rel_location: &str) -> String {
107 let loc = self.get_location(config).get_absolute_dir(config); 107 let loc = self.get_location(config).get_absolute_dir(config);
108 108
109 format!("{}/{}", loc, rel_location) 109 format!("{loc}/{rel_location}")
110 } 110 }
111 111
112 fn get_json_content<T: for<'a> Deserialize<'a>>(path: &str) -> Result<T> { 112 fn get_json_content<T: for<'a> Deserialize<'a>>(path: &str) -> Result<T> {
@@ -125,12 +125,12 @@ impl Backup {
125} 125}
126 126
127#[derive(Debug, Clone, Serialize, Deserialize)] 127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct BackupLocation { 128pub struct IndexEntry {
129 id: BackupId, 129 id: Id,
130 rel_location: String, 130 rel_location: String,
131} 131}
132 132
133impl BackupLocation { 133impl IndexEntry {
134 pub fn get_absolute_dir(&self, config: &Config) -> String { 134 pub fn get_absolute_dir(&self, config: &Config) -> String {
135 format!("{}/{}", config.root, self.rel_location) 135 format!("{}/{}", config.root, self.rel_location)
136 } 136 }
@@ -142,7 +142,7 @@ impl BackupLocation {
142 let mut f = File::open(&path)?; 142 let mut f = File::open(&path)?;
143 let mut content = String::new(); 143 let mut content = String::new();
144 f.read_to_string(&mut content)?; 144 f.read_to_string(&mut content)?;
145 let mut loc: Vec<BackupLocation> = serde_json::from_str(&content)?; 145 let mut loc: Vec<IndexEntry> = serde_json::from_str(&content)?;
146 146
147 let mut f = File::create(path)?; 147 let mut f = File::create(path)?;
148 loc.push(self.clone()); 148 loc.push(self.clone());