Compare commits
3 Commits
v0.2.2
...
87cc05f33a
| Author | SHA1 | Date | |
|---|---|---|---|
| 87cc05f33a | |||
| 2784bb678a | |||
| 1baeb9f465 |
3
.github/workflows/goreleaser.yaml
vendored
3
.github/workflows/goreleaser.yaml
vendored
@@ -5,6 +5,9 @@ on:
|
|||||||
tags:
|
tags:
|
||||||
- "*"
|
- "*"
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|||||||
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -413,7 +413,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zfsbackup"
|
name = "zfsbackup"
|
||||||
version = "0.2.2"
|
version = "0.2.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "zfsbackup"
|
name = "zfsbackup"
|
||||||
version = "0.2.2"
|
version = "0.2.3"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
|
|||||||
282
src/job.rs
282
src/job.rs
@@ -5,106 +5,8 @@ use chrono::{Local, NaiveDateTime};
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
pub struct JobBuilder {
|
|
||||||
sources: Vec<String>,
|
|
||||||
target: String,
|
|
||||||
source_zfs_command: Vec<String>,
|
|
||||||
target_zfs_command: Vec<String>,
|
|
||||||
dryrun: bool,
|
|
||||||
retain: usize,
|
|
||||||
sender: Option<Sender<BackupEvent>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_command(commandstr: &str) -> Vec<String> {
|
|
||||||
commandstr
|
|
||||||
.split_whitespace()
|
|
||||||
.map(|s| s.to_string())
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
impl JobBuilder {
|
|
||||||
pub fn new(sources: Vec<String>, target: String) -> Self {
|
|
||||||
JobBuilder {
|
|
||||||
sources,
|
|
||||||
target,
|
|
||||||
source_zfs_command: vec!["zfs".to_string()],
|
|
||||||
target_zfs_command: vec!["zfs".to_string()],
|
|
||||||
dryrun: false,
|
|
||||||
retain: 2,
|
|
||||||
sender: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn source_zfs_command(mut self, commandstr: &str) -> Self {
|
|
||||||
let command = parse_command(commandstr);
|
|
||||||
self.source_zfs_command = command;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn target_zfs_command(mut self, commandstr: &str) -> Self {
|
|
||||||
let command = parse_command(commandstr);
|
|
||||||
self.target_zfs_command = command;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn zfs_command(mut self, commandstr: &str) -> Self {
|
|
||||||
let command = parse_command(commandstr);
|
|
||||||
self.source_zfs_command = command.clone();
|
|
||||||
self.target_zfs_command = command;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn dryrun(mut self) -> Self {
|
|
||||||
self.dryrun = true;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn retain(mut self, retain: usize) -> Self {
|
|
||||||
self.retain = retain;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sender(mut self, sender: Sender<BackupEvent>) -> Self {
|
|
||||||
self.sender = Some(sender);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn build(self) -> Result<Job, String> {
|
|
||||||
let mut job = Job {
|
|
||||||
datasets: vec![],
|
|
||||||
target: self.target,
|
|
||||||
source_zfs_command: self.source_zfs_command,
|
|
||||||
target_zfs_command: self.target_zfs_command,
|
|
||||||
dryrun: self.dryrun,
|
|
||||||
retain: self.retain,
|
|
||||||
sender: self.sender,
|
|
||||||
};
|
|
||||||
let mut datasets: Vec<String> = vec![];
|
|
||||||
for source in &self.sources {
|
|
||||||
let recurse = source.ends_with("/...");
|
|
||||||
let source = source.trim_end_matches("/...");
|
|
||||||
|
|
||||||
let mut args = vec!["list", "-H", "-o", "name"];
|
|
||||||
if recurse {
|
|
||||||
args.push("-r");
|
|
||||||
}
|
|
||||||
args.push(source);
|
|
||||||
|
|
||||||
let mut cmd = job.get_side_command(JobSide::Source);
|
|
||||||
cmd.extend(args);
|
|
||||||
let output = command::exec_command(&cmd)?;
|
|
||||||
datasets.extend(output.lines().map(str::to_string));
|
|
||||||
}
|
|
||||||
if datasets.is_empty() {
|
|
||||||
return Err(String::from("no matching source datasets found"));
|
|
||||||
}
|
|
||||||
job.datasets = datasets;
|
|
||||||
Ok(job)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Job {
|
pub struct Job {
|
||||||
datasets: Vec<String>,
|
datasets: HashSet<String>,
|
||||||
target: String,
|
target: String,
|
||||||
source_zfs_command: Vec<String>,
|
source_zfs_command: Vec<String>,
|
||||||
target_zfs_command: Vec<String>,
|
target_zfs_command: Vec<String>,
|
||||||
@@ -358,3 +260,185 @@ impl Job {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct JobBuilder {
|
||||||
|
sources: Vec<String>,
|
||||||
|
target: String,
|
||||||
|
source_zfs_command: Vec<String>,
|
||||||
|
target_zfs_command: Vec<String>,
|
||||||
|
dryrun: bool,
|
||||||
|
retain: usize,
|
||||||
|
sender: Option<Sender<BackupEvent>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a command string into a vector of strings, splitting on whitespace.
|
||||||
|
/// Quoting is supported using ' or ".
|
||||||
|
fn parse_command(commandstr: &str) -> Vec<String> {
|
||||||
|
let mut arg = String::new();
|
||||||
|
let mut command: Vec<String> = Vec::new();
|
||||||
|
let mut in_single_quote = false;
|
||||||
|
let mut in_double_quote = false;
|
||||||
|
|
||||||
|
for c in commandstr.chars() {
|
||||||
|
match c {
|
||||||
|
'\'' => {
|
||||||
|
if !in_double_quote {
|
||||||
|
in_single_quote = !in_single_quote;
|
||||||
|
} else {
|
||||||
|
arg.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'"' => {
|
||||||
|
if !in_single_quote {
|
||||||
|
in_double_quote = !in_double_quote;
|
||||||
|
} else {
|
||||||
|
arg.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
' ' => {
|
||||||
|
if in_single_quote || in_double_quote {
|
||||||
|
arg.push(c);
|
||||||
|
} else if !arg.is_empty() {
|
||||||
|
command.push(arg.clone());
|
||||||
|
arg.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
arg.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !arg.is_empty() {
|
||||||
|
command.push(arg);
|
||||||
|
}
|
||||||
|
command
|
||||||
|
}
|
||||||
|
|
||||||
|
impl JobBuilder {
|
||||||
|
/// Create a new JobBuilder with the given sources and target. The sources should be ZFS
|
||||||
|
/// datasets, optionally ending with "/..." to include all child datasets. The target should be
|
||||||
|
/// a ZFS dataset.
|
||||||
|
pub fn new(sources: Vec<String>, target: String) -> Self {
|
||||||
|
JobBuilder {
|
||||||
|
sources,
|
||||||
|
target,
|
||||||
|
source_zfs_command: vec!["zfs".to_string()],
|
||||||
|
target_zfs_command: vec!["zfs".to_string()],
|
||||||
|
dryrun: false,
|
||||||
|
retain: 2,
|
||||||
|
sender: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn source_zfs_command(mut self, commandstr: &str) -> Self {
|
||||||
|
let command = parse_command(commandstr);
|
||||||
|
self.source_zfs_command = command;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn target_zfs_command(mut self, commandstr: &str) -> Self {
|
||||||
|
let command = parse_command(commandstr);
|
||||||
|
self.target_zfs_command = command;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn zfs_command(mut self, commandstr: &str) -> Self {
|
||||||
|
let command = parse_command(commandstr);
|
||||||
|
self.source_zfs_command = command.clone();
|
||||||
|
self.target_zfs_command = command;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dryrun(mut self) -> Self {
|
||||||
|
self.dryrun = true;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn retain(mut self, retain: usize) -> Self {
|
||||||
|
self.retain = retain;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sender(mut self, sender: Sender<BackupEvent>) -> Self {
|
||||||
|
self.sender = Some(sender);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(self) -> Result<Job, String> {
|
||||||
|
let mut datasets: HashSet<String> = HashSet::new();
|
||||||
|
for source in &self.sources {
|
||||||
|
let recurse = source.ends_with("/...");
|
||||||
|
let source = source.trim_end_matches("/...");
|
||||||
|
|
||||||
|
let mut args = vec!["list", "-H", "-o", "name"];
|
||||||
|
if recurse {
|
||||||
|
args.push("-r");
|
||||||
|
}
|
||||||
|
args.push(source);
|
||||||
|
|
||||||
|
let mut cmd: Vec<&str> = self.source_zfs_command.iter().map(|s| s.as_str()).collect();
|
||||||
|
cmd.extend(args);
|
||||||
|
let output = command::exec_command(&cmd)?;
|
||||||
|
datasets.extend(output.lines().map(str::to_string));
|
||||||
|
}
|
||||||
|
if datasets.is_empty() {
|
||||||
|
return Err(String::from("no matching source datasets found"));
|
||||||
|
}
|
||||||
|
Ok(Job {
|
||||||
|
datasets: datasets,
|
||||||
|
target: self.target,
|
||||||
|
source_zfs_command: self.source_zfs_command,
|
||||||
|
target_zfs_command: self.target_zfs_command,
|
||||||
|
dryrun: self.dryrun,
|
||||||
|
retain: self.retain,
|
||||||
|
sender: self.sender,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_command_simple() {
|
||||||
|
assert_eq!(parse_command("zfs list"), vec!["zfs", "list"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_command_quoted() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_command("ssh host sudo zfs"),
|
||||||
|
vec!["ssh", "host", "sudo", "zfs"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_command_with_quotes() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_command(r#"ssh "my host" sudo zfs"#),
|
||||||
|
vec!["ssh", "my host", "sudo", "zfs"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_command_with_nested_quotes() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_command(r#"ssh 'my " host' "su'do" zfs"#),
|
||||||
|
vec!["ssh", "my \" host", "su'do", "zfs"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_command_extra_spaces() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_command(" ssh \" ho st \" sudo zfs "),
|
||||||
|
vec!["ssh", " ho st ", "sudo", "zfs"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_command_empty() {
|
||||||
|
assert_eq!(parse_command(""), Vec::<String>::new());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user