summaryrefslogtreecommitdiff
path: root/src/backup.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/backup.rs')
-rw-r--r--src/backup.rs37
1 files changed, 18 insertions, 19 deletions
diff --git a/src/backup.rs b/src/backup.rs
index 3d07ace..b468917 100644
--- a/src/backup.rs
+++ b/src/backup.rs
@@ -2,18 +2,18 @@ use std::{
2 fs::{create_dir_all, File}, 2 fs::{create_dir_all, File},
3 io::{Read, Write}, 3 io::{Read, Write},
4 path::PathBuf, 4 path::PathBuf,
5 time::{SystemTime, UNIX_EPOCH}, 5 time::SystemTime,
6}; 6};
7 7
8use serde::{Deserialize, Serialize}; 8use serde::{Deserialize, Serialize};
9use tracing::info; 9use tracing::{debug, info};
10use uuid::Uuid; 10use uuid::Uuid;
11 11
12use crate::{ 12use crate::{
13 config::Config, 13 config::Config,
14 error::{Error, Result}, 14 error::{Error, Result},
15 packages::{Manager, PackageList}, 15 packages::PackageList,
16 pathinfo::PathInfo, 16 pathinfo::PathInfo, send_notification,
17}; 17};
18 18
19pub type Id = String; 19pub type Id = String;
@@ -28,16 +28,20 @@ pub struct Backup {
28} 28}
29 29
30impl Backup { 30impl Backup {
31 pub fn create(config: &Config, manager: Option<Manager>) -> Result<Self> { 31 pub fn create(config: &Config) -> Result<Self> {
32 let mut files: Vec<PathInfo> = Vec::new(); 32 let mut files: Vec<PathInfo> = Vec::new();
33 for dir in &config.directories { 33 for dir in &config.directories {
34 files.push(PathInfo::from_path(config, dir)?); 34 files.push(PathInfo::from_path(config, dir)?);
35 } 35 }
36 Ok(Self { 36 Ok(Self {
37 // TODO: UUID not really needed, maybe a shorter hash
38 id: Uuid::new_v4().to_string(), 37 id: Uuid::new_v4().to_string(),
39 timestamp: Self::get_timestamp(), 38 timestamp: SystemTime::now()
40 packages: Manager::get_manager(manager)?.get_installed()?, 39 .duration_since(SystemTime::UNIX_EPOCH)?
40 .as_secs(),
41 packages: config
42 .package_manager
43 .to_package_manager()
44 .get_installed()?,
41 files, 45 files,
42 device: config.device.clone(), 46 device: config.device.clone(),
43 }) 47 })
@@ -58,12 +62,14 @@ impl Backup {
58 path.save(&backup_root)?; 62 path.save(&backup_root)?;
59 } 63 }
60 64
65 send_notification("Backup created" , "", notify_rust::Urgency::Normal)?;
66
61 Ok(()) 67 Ok(())
62 } 68 }
63 69
64 pub fn get_last(config: &Config) -> Result<Option<Self>> { 70 pub fn get_last(config: &Config) -> Result<Option<Self>> {
65 let backup_index_root = format!("{}/index.json", config.root); 71 let backup_index_root = format!("{}/index.json", config.root);
66 info!(?backup_index_root, "backup index location:"); 72 debug!(?backup_index_root, "backup index location:");
67 let list: Vec<IndexEntry> = match Self::get_json_content(&backup_index_root) { 73 let list: Vec<IndexEntry> = match Self::get_json_content(&backup_index_root) {
68 Ok(list) => list, 74 Ok(list) => list,
69 Err(err) => { 75 Err(err) => {
@@ -74,8 +80,6 @@ impl Backup {
74 } 80 }
75 }; 81 };
76 82
77 info!(?list, "backup index:");
78
79 Ok(Some(Self::from_index( 83 Ok(Some(Self::from_index(
80 config, 84 config,
81 &list.last().ok_or(Error::BackupNotFound)?.id, 85 &list.last().ok_or(Error::BackupNotFound)?.id,
@@ -122,6 +126,8 @@ impl Backup {
122 path.restore(config, &backup_root)?; 126 path.restore(config, &backup_root)?;
123 } 127 }
124 128
129 send_notification("Backup restored" , "", notify_rust::Urgency::Normal)?;
130
125 Ok(()) 131 Ok(())
126 } 132 }
127 133
@@ -131,13 +137,6 @@ impl Backup {
131 file.read_to_string(&mut content)?; 137 file.read_to_string(&mut content)?;
132 Ok(serde_json::from_str(&content)?) 138 Ok(serde_json::from_str(&content)?)
133 } 139 }
134
135 fn get_timestamp() -> u64 {
136 SystemTime::now()
137 .duration_since(UNIX_EPOCH)
138 .unwrap()
139 .as_secs()
140 }
141} 140}
142 141
143#[derive(Debug, Clone, Serialize, Deserialize)] 142#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -165,7 +164,7 @@ impl IndexEntry {
165 164
166 f.write_all(&serde_json::to_vec(&loc)?)?; 165 f.write_all(&serde_json::to_vec(&loc)?)?;
167 } else { 166 } else {
168 create_dir_all(&config.root).unwrap(); 167 create_dir_all(&config.root)?;
169 let mut f = File::create(backup_index_root)?; 168 let mut f = File::create(backup_index_root)?;
170 f.write_all(&serde_json::to_vec(&vec![self])?)?; 169 f.write_all(&serde_json::to_vec(&vec![self])?)?;
171 }; 170 };