feat: add wip stage 3

This commit is contained in:
Xory 2024-09-10 21:28:32 +03:00
parent 28f0988f5f
commit 1aa73c0088
6 changed files with 46 additions and 10 deletions

View file

@ -5,4 +5,5 @@ edition = "2021"
[dependencies] [dependencies]
serde = { version = "1.0.210", features = [ "derive" ] } serde = { version = "1.0.210", features = [ "derive" ] }
tar = "0.4.41"
toml = "0.8.19" toml = "0.8.19"

View file

@ -1,4 +0,0 @@
I: Begin backup log - 1725955295
I: Beginning stage 1 backup.
I: Beginning stage 2 backup.

View file

@ -2,3 +2,4 @@
rm -r testfiles/dir/* rm -r testfiles/dir/*
echo "" > log.txt echo "" > log.txt
rm -r /run/media/xory/Ventoy/backup/* rm -r /run/media/xory/Ventoy/backup/*
rm backup.tar

View file

@ -1,6 +1,7 @@
use serde::Deserialize; use serde::Deserialize;
use toml; use toml;
use std::{fs, io, path::Path}; use std::{fs, io, net::TcpStream, path::Path};
use std::io::{prelude::*, BufReader, SeekFrom};
mod logger; mod logger;
use logger::{log, LogLevel}; use logger::{log, LogLevel};
@ -12,11 +13,19 @@ pub struct StageConfig {
pub recursive: bool pub recursive: bool
} }
#[derive(Deserialize, Debug, PartialEq)]
pub struct Stage3Config {
pub src_files: Vec<String>,
pub target_server: String,
pub recursive: bool
}
#[derive(Deserialize, Debug, PartialEq)] #[derive(Deserialize, Debug, PartialEq)]
pub struct Config { pub struct Config {
pub stage_1: StageConfig, pub stage_1: StageConfig,
pub stage_2: StageConfig pub stage_2: StageConfig,
pub stage_3: Stage3Config
} }
impl Config { impl Config {
@ -101,6 +110,21 @@ pub fn stage2(config: &Config) -> io::Result<()> {
Ok(()) Ok(())
} }
pub fn stage_3(config: &Config) -> io::Result<()> {
{
let mut archivefile = tar::Builder::new(fs::File::create_new("backup.tar")?);
for file in &config.stage_3.src_files {
archivefile.append_path(file)?;
}
}
let archivefile = fs::File::open("backup.tar")?;
let mut backup_server_connection = TcpStream::connect(format!("{}:1024", &config.stage_3.target_server))?;
let buffer = vec![0_u8; archivefile.metadata()?.len().try_into().unwrap()];
backup_server_connection.write_all(&buffer)?;
backup_server_connection.flush()?;
Ok(())
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -115,6 +139,10 @@ mod tests {
src_files = [ './testfiles/src/file1.txt', './testfiles/src/file2.txt', './testfiles/src/a' ] src_files = [ './testfiles/src/file1.txt', './testfiles/src/file2.txt', './testfiles/src/a' ]
target_dir = '/run/media/xory/Ventoy/backup' target_dir = '/run/media/xory/Ventoy/backup'
recursive = true recursive = true
[stage_3]
src_files = [ './testfiles/src/file1.txt', './testfiles/src/file2.txt', './testfiles/src/a' ]
target_server = '192.168.1.204'
recursive = true
"; ";
let parsed_config = Config::new(toml_config.to_string()); let parsed_config = Config::new(toml_config.to_string());
@ -129,6 +157,11 @@ mod tests {
src_files: vec![ "./testfiles/src/file1.txt".to_string(), "./testfiles/src/file2.txt".to_string(), "./testfiles/src/a".to_string() ], src_files: vec![ "./testfiles/src/file1.txt".to_string(), "./testfiles/src/file2.txt".to_string(), "./testfiles/src/a".to_string() ],
target_dir: "/run/media/xory/Ventoy/backup".to_string(), target_dir: "/run/media/xory/Ventoy/backup".to_string(),
recursive: true recursive: true
},
stage_3: Stage3Config {
src_files: vec![ "./testfiles/src/file1.txt".to_string(), "./testfiles/src/file2.txt".to_string(), "./testfiles/src/a".to_string() ],
target_server: "192.168.1.204".to_string(),
recursive: true
} }
}, },
parsed_config parsed_config

View file

@ -1,4 +1,4 @@
use cynewave::{Config, stage1, stage2}; use cynewave::{Config, stage1, stage2, stage_3};
mod logger; mod logger;
use logger::{log, LogLevel}; use logger::{log, LogLevel};
use std::fs; use std::fs;
@ -12,7 +12,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
dbg!(&config); dbg!(&config);
log("./log.txt".to_string(), "Beginning stage 1 backup.".to_string(), LogLevel::Info)?; log("./log.txt".to_string(), "Beginning stage 1 backup.".to_string(), LogLevel::Info)?;
stage1(&config)?; stage1(&config)?;
log("./log.txt".to_string(), "Beginning stage 2 backup.".to_string(), LogLevel::Info)?; // log("./log.txt".to_string(), "Beginning stage 2 backup.".to_string(), LogLevel::Info)?;
stage2(&config)?; // stage2(&config)?;
log("./log.txt".to_string(), "Beginning stage 3 backup.".to_string(), LogLevel::Info)?;
stage_3(&config)?;
Ok(()) Ok(())
} }

View file

@ -8,4 +8,7 @@ src_files = [ "./testfiles/src/file1.txt", "./testfiles/src/file2.txt", "./testf
target_dir = "/run/media/xory/Ventoy/backup" target_dir = "/run/media/xory/Ventoy/backup"
recursive = true recursive = true
[stage_3]
src_files = [ "./testfiles/src/file1.txt", "./testfiles/src/file2.txt", "./testfiles/src/a" ]
target_server = "127.0.0.1"
recursive = true