mirror of
https://github.com/qwjyh/xdbm
synced 2025-06-26 09:49:24 +09:00
add backup related types and cmd arguments
- fix some typos & format
This commit is contained in:
parent
d1f7a4787e
commit
56563a0e8b
10 changed files with 244 additions and 34 deletions
55
src/backups.rs
Normal file
55
src/backups.rs
Normal file
|
@ -0,0 +1,55 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use chrono::{DateTime, Local};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::storages::Storage;
|
||||
|
||||
/// Targets for backup source or destination.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct BackupTarget {
|
||||
storage: Storage,
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
/// Type of backup commands.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum BackupCommand {
|
||||
ExternallyInvoked(ExternallyInvoked),
|
||||
}
|
||||
|
||||
/// Backup commands which is not invoked from xdbm itself.
|
||||
/// Call xdbm externally to record backup datetime and status.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ExternallyInvoked {
|
||||
name: String,
|
||||
pub note: String,
|
||||
}
|
||||
|
||||
/// Backup execution log.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct BackupLog {
|
||||
datetime: DateTime<Local>,
|
||||
status: BackupResult,
|
||||
log: String,
|
||||
}
|
||||
|
||||
/// Result of backup.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum BackupResult {
|
||||
Success,
|
||||
Failure,
|
||||
}
|
||||
|
||||
/// Backup source, destination, command and logs.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Backup {
|
||||
/// must be unique
|
||||
name: String,
|
||||
/// name of [`crate::Device`]
|
||||
device: String,
|
||||
from: BackupTarget,
|
||||
to: BackupTarget,
|
||||
command: BackupCommand,
|
||||
logs: Vec<BackupLog>,
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
//! CLI arguments
|
||||
|
||||
use crate::StorageType;
|
||||
use crate::path;
|
||||
use crate::PathBuf;
|
||||
use clap::Args;
|
||||
|
@ -41,6 +40,10 @@ pub(crate) enum Commands {
|
|||
/// Manage storages.
|
||||
Storage(StorageArgs),
|
||||
|
||||
/// Manage backups.
|
||||
#[command(subcommand)]
|
||||
Backup(BackupSubCommands),
|
||||
|
||||
/// Print config dir.
|
||||
Path {},
|
||||
|
||||
|
@ -129,5 +132,40 @@ pub(crate) enum StorageAddCommands {
|
|||
/// Device specific alias for the storage.
|
||||
#[arg(short, long)]
|
||||
alias: String,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
pub(crate) enum BackupSubCommands {
|
||||
/// Add new backup config.
|
||||
Add {
|
||||
name: String,
|
||||
/// Source of the data backuped.
|
||||
#[arg(short, long)]
|
||||
src: PathBuf,
|
||||
/// Destination of the backuped data.
|
||||
#[arg(short, long)]
|
||||
dest: PathBuf,
|
||||
#[command(subcommand)]
|
||||
cmd: BackupAddCommands,
|
||||
},
|
||||
/// Print configured backups.
|
||||
List {},
|
||||
/// Record xdbm that the backup with the name has finished right now.
|
||||
Done {
|
||||
name: String,
|
||||
succeeded: bool,
|
||||
#[arg(short, long)]
|
||||
log: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
pub(crate) enum BackupAddCommands {
|
||||
/// Invoke logging via cli of xdbm. The simplest one.
|
||||
External {
|
||||
name: String,
|
||||
#[arg(default_value = "")]
|
||||
note: String,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -82,7 +82,7 @@ pub(crate) fn cmd_init(
|
|||
}
|
||||
// validate device name
|
||||
if device_name.chars().count() == 0 {
|
||||
log::error!("Device name cannnot by empty");
|
||||
log::error!("Device name cannot by empty");
|
||||
return Err(anyhow!("Device name is empty"));
|
||||
}
|
||||
// get repo or initialize it
|
||||
|
|
|
@ -12,7 +12,7 @@ pub const DEVICESFILE: &str = "devices.yml";
|
|||
|
||||
/// Represents each devices.
|
||||
/// Identified by name, which is accessible from `name()`.
|
||||
/// Store os name, os version and hostname as supplimental information.
|
||||
/// Store os name, os version and hostname as supplemental information.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Device {
|
||||
name: String,
|
||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -15,21 +15,18 @@ extern crate dirs;
|
|||
use anyhow::{anyhow, Context, Result};
|
||||
use clap::{CommandFactory, Parser};
|
||||
use git2::{Commit, Oid, Repository};
|
||||
use inquire::{min_length, Confirm, CustomType, Select};
|
||||
use inquire::{validator::Validation, Text};
|
||||
use serde_yaml;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::path::{self, PathBuf};
|
||||
use storages::Storages;
|
||||
|
||||
use crate::cmd_args::{Cli, Commands, StorageCommands};
|
||||
use crate::storages::online_storage;
|
||||
use crate::storages::{
|
||||
directory, local_info, physical_drive_partition, Storage, StorageExt, StorageType, STORAGESFILE,
|
||||
directory, local_info, online_storage, physical_drive_partition, Storage, StorageExt,
|
||||
StorageType, STORAGESFILE,
|
||||
};
|
||||
use devices::{Device, DEVICESFILE, *};
|
||||
|
||||
mod backups;
|
||||
mod cmd_args;
|
||||
mod cmd_init;
|
||||
mod cmd_storage;
|
||||
|
@ -38,8 +35,6 @@ mod devices;
|
|||
mod inquire_filepath_completer;
|
||||
mod storages;
|
||||
|
||||
struct BackupLog {}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
env_logger::Builder::new()
|
||||
|
@ -98,6 +93,7 @@ fn main() -> Result<()> {
|
|||
let _storages = Storages::read(&config_dir)?;
|
||||
todo!()
|
||||
}
|
||||
Commands::Backup(_) => todo!(),
|
||||
}
|
||||
full_status(&Repository::open(&config_dir)?)?;
|
||||
Ok(())
|
||||
|
|
|
@ -8,13 +8,7 @@ use anyhow::{anyhow, Context, Result};
|
|||
use clap::ValueEnum;
|
||||
use core::panic;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::ffi::OsString;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
ffi,
|
||||
fmt::{self, format},
|
||||
fs, io, path, u64,
|
||||
};
|
||||
use std::{collections::HashMap, fmt, fs, io, path, u64};
|
||||
|
||||
/// YAML file to store known storages..
|
||||
pub const STORAGESFILE: &str = "storages.yml";
|
||||
|
@ -74,11 +68,7 @@ impl StorageExt for Storage {
|
|||
}
|
||||
}
|
||||
|
||||
fn mount_path(
|
||||
&self,
|
||||
device: &devices::Device,
|
||||
storages: &Storages,
|
||||
) -> Result<path::PathBuf> {
|
||||
fn mount_path(&self, device: &devices::Device, storages: &Storages) -> Result<path::PathBuf> {
|
||||
match self {
|
||||
Self::PhysicalStorage(s) => s.mount_path(&device, &storages),
|
||||
Self::SubDirectory(s) => s.mount_path(&device, &storages),
|
||||
|
@ -145,11 +135,7 @@ pub trait StorageExt {
|
|||
|
||||
/// Get mount path of `self` on `device`.
|
||||
/// `storages` is a `HashMap` with key of storage name and value of the storage.
|
||||
fn mount_path(
|
||||
&self,
|
||||
device: &devices::Device,
|
||||
storages: &Storages,
|
||||
) -> Result<path::PathBuf>;
|
||||
fn mount_path(&self, device: &devices::Device, storages: &Storages) -> Result<path::PathBuf>;
|
||||
|
||||
/// Add local info of `device` to `self`.
|
||||
fn bound_on_device(
|
||||
|
@ -226,7 +212,7 @@ impl Storages {
|
|||
let storages_file = config_dir.join(STORAGESFILE);
|
||||
if !storages_file.exists() {
|
||||
warn!("No storages file found.");
|
||||
return Err(anyhow!("Couln't find {}", STORAGESFILE))
|
||||
return Err(anyhow!("Couln't find {}", STORAGESFILE));
|
||||
}
|
||||
trace!("Reading {:?}", storages_file);
|
||||
let f = fs::File::open(storages_file)?;
|
||||
|
@ -237,8 +223,10 @@ impl Storages {
|
|||
}
|
||||
|
||||
pub fn write(self, config_dir: &path::Path) -> Result<()> {
|
||||
let f = fs::File::create(config_dir.join(STORAGESFILE)).context("Failed to open storages file")?;
|
||||
let f = fs::File::create(config_dir.join(STORAGESFILE))
|
||||
.context("Failed to open storages file")?;
|
||||
let writer = io::BufWriter::new(f);
|
||||
serde_yaml::to_writer(writer, &self).context(format!("Failed to writing to {:?}", STORAGESFILE))
|
||||
serde_yaml::to_writer(writer, &self)
|
||||
.context(format!("Failed to writing to {:?}", STORAGESFILE))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ impl Directory {
|
|||
/// - `name`: id
|
||||
/// - `parent`: where the directory locates.
|
||||
/// - `relative_path`: path from root of the parent storage.
|
||||
/// - `notes`: supplimental notes.
|
||||
/// - `notes`: supplemental notes.
|
||||
fn new(
|
||||
name: String,
|
||||
parent: String,
|
||||
|
|
|
@ -192,7 +192,7 @@ pub fn select_physical_storage(
|
|||
device: Device,
|
||||
) -> Result<PhysicalDrivePartition> {
|
||||
trace!("select_physical_storage");
|
||||
// get disk info fron sysinfo
|
||||
// get disk info from sysinfo
|
||||
let sys_disks =
|
||||
sysinfo::System::new_with_specifics(sysinfo::RefreshKind::new().with_disks_list());
|
||||
trace!("refresh");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue