Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ chrono = { version = "0.4", default-features = false, features = ["clock", "std"
clap = { version = "4", features = ["derive"] }
dotenvy = "0.15"
flate2 = "1"
indicatif = "0.18"
regex = "1"
schemars = { version = "1", features = ["derive"] }
serde = { version = "1", features = ["derive"] }
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,25 @@ gsbt backup --quiet
- `--verbose` / `-v` – Enable debug logging and show metadata
- `--quiet` / `-q` – Only show errors
- `--sequential` – Run backups one server at a time (default is parallel)
- `--fancy <bool>` – Show fancy per-server progress bars when the terminal supports it (default `true`; use `--fancy=false` to disable)

Archives are stored at `{backup_location}/{timestamp}.tar.gz` with temp files under `{backup_location}/.tmp/`.

When running on an interactive terminal, each server gets its own progress bar (they are not shown when output is piped, in `--quiet`/`--output json` mode, or with `--fancy=false`). A plain-text transfer log is always written next to each archive at `{backup_location}/{timestamp}.log`, listing the server, totals, and every transferred file, so the details remain available even while bars are drawn.

```text
# example transfer log
# gsbt transfer log
server: my-ftp
archive: /srv/backups/my-ftp/2026-01-15_154500.tar.gz
files: 5
bytes: 2400000
duration_sec: 3.201
---
saves/world.dat 1200000
config/server.ini 10240
```

### Output Modes

**Text mode** (default):
Expand Down
57 changes: 54 additions & 3 deletions src/backup/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

use std::fs::{self, File};
use std::io::{self, Write};
use std::path::Path;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use anyhow::{bail, Context, Result};

use crate::backup::archive::{create_archive, timestamped_filename};
use crate::connector::Connector;
use crate::connector::{Connector, FileInfo};
use crate::progress::Reporter;

/// Summary of a backup run.
Expand All @@ -26,6 +26,7 @@ pub struct Stats {
pub struct Manager {
pub temp_dir: String,
pub backup_location: String,
pub server_name: String,
pub progress: Reporter,
}

Expand Down Expand Up @@ -110,10 +111,51 @@ impl Manager {
create_archive(Path::new(&temp_dir), &archive_path).context("create archive")?;

stats.duration = start.elapsed();
Ok((archive_path.to_string_lossy().into_owned(), stats))

let archive_path = archive_path.to_string_lossy().into_owned();
// Best-effort: write a readable transfer log next to the archive so the
// per-file details remain available even when a progress bar is shown.
let log_path = transfer_log_path(&archive_path);
let _ = write_transfer_log(&log_path, &self.server_name, &archive_path, &files, &stats);

Ok((archive_path, stats))
}
}

/// Returns the transfer-log path for an archive
/// (`foo.tar.gz` becomes `foo.log`, next to the archive).
pub fn transfer_log_path(archive_path: &str) -> PathBuf {
match archive_path.strip_suffix(".tar.gz") {
Some(base) => PathBuf::from(format!("{base}.log")),
None => Path::new(archive_path).with_extension("log"),
}
}

fn write_transfer_log(
log_path: &Path,
server: &str,
archive_path: &str,
files: &[FileInfo],
stats: &Stats,
) -> io::Result<()> {
let mut file = File::create(log_path)?;
writeln!(file, "# gsbt transfer log")?;
writeln!(file, "server: {server}")?;
writeln!(file, "archive: {archive_path}")?;
writeln!(file, "files: {}", stats.files)?;
writeln!(file, "bytes: {}", stats.bytes)?;
writeln!(file, "duration_sec: {:.3}", stats.duration.as_secs_f64())?;
writeln!(file, "---")?;

for entry in files {
if !entry.is_dir {
writeln!(file, "{}\t{}", entry.path, entry.size)?;
}
}

Ok(())
}

/// Wraps a writer to report incremental bytes written.
struct ProgressWriter<'a> {
inner: &'a mut dyn Write,
Expand Down Expand Up @@ -212,6 +254,7 @@ mod tests {
let mut manager = Manager {
temp_dir: String::new(),
backup_location: tmp.path().to_string_lossy().into_owned(),
server_name: "test-server".to_string(),
progress: crate::progress::new(&crate::log::Logger::new(), "json"),
};

Expand All @@ -221,6 +264,13 @@ mod tests {
assert_eq!(Path::new(&archive_path).parent().unwrap(), tmp.path());
assert_eq!(stats.files, 2);
assert_eq!(stats.bytes, ("hello".len() + "world".len()) as i64);

// A transfer log is written next to the archive.
let log_path = transfer_log_path(&archive_path);
let log = std::fs::read_to_string(&log_path).expect("transfer log");
assert!(log.contains("server: test-server"));
assert!(log.contains("file1.txt"));
assert!(log.contains("nested/file2.txt"));
}

#[test]
Expand All @@ -229,6 +279,7 @@ mod tests {
let mut manager = Manager {
temp_dir: String::new(),
backup_location: String::new(),
server_name: "test-server".to_string(),
progress: crate::progress::new(&crate::log::Logger::new(), "json"),
};
assert!(manager.backup(&mut conn).is_err());
Expand Down
2 changes: 1 addition & 1 deletion src/backup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pub mod archive;
pub mod manager;

pub use archive::{create_archive, timestamped_filename};
pub use manager::{Manager, Stats};
pub use manager::{transfer_log_path, Manager, Stats};
20 changes: 15 additions & 5 deletions src/cli/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ pub struct BackupOptions {
}

/// Runs the backup command using the supplied connector factory.
pub fn run_backup(opts: &BackupOptions, logger: &Logger, factory: &ConnectorFactory) -> Result<()> {
pub fn run_backup(
opts: &BackupOptions,
logger: &Logger,
progress_factory: &progress::ProgressFactory,
factory: &ConnectorFactory,
) -> Result<()> {
let cfg_path = crate::config::find_config_file(&opts.config)?;
let cfg = crate::config::load_config(&cfg_path)?;

Expand All @@ -36,8 +41,6 @@ pub fn run_backup(opts: &BackupOptions, logger: &Logger, factory: &ConnectorFact
bail!("no servers configured");
}

let format = opts.output.as_str();

let run_one = |server: &Server| -> bool {
let server_logger =
logger.with_prefix(format!("[bold][cyan]{}[/cyan][/bold]", server.name));
Expand All @@ -62,21 +65,26 @@ pub fn run_backup(opts: &BackupOptions, logger: &Logger, factory: &ConnectorFact
let mut manager = Manager {
backup_location: server.get_backup_location(&cfg.defaults),
temp_dir: cfg.defaults.temp_dir.clone(),
progress: progress::new(&server_logger, format),
server_name: server.name.clone(),
progress: progress_factory.reporter(&server_logger, &server.name),
};

let start = Instant::now();
match manager.backup(&mut *conn) {
Ok((archive_path, stats)) => {
let log_path = crate::backup::transfer_log_path(&archive_path)
.to_string_lossy()
.into_owned();
server_logger.info_with(
format!(
"[green]saved[/green] {archive_path} ({} files, {:.1} MB, {:.1}s)",
"[green]saved[/green] {archive_path} ({} files, {:.1} MB, {:.1}s); log: {log_path}",
stats.files,
stats.bytes as f64 / 1e6,
start.elapsed().as_secs_f64()
),
crate::meta! {
"archive_path" => archive_path,
"log_path" => log_path,
"files" => stats.files,
"bytes" => stats.bytes,
"duration_sec" => start.elapsed().as_secs_f64(),
Expand Down Expand Up @@ -120,6 +128,8 @@ pub fn run_backup(opts: &BackupOptions, logger: &Logger, factory: &ConnectorFact
)
};

progress_factory.finish();

if failures > 0 {
bail!("backup complete with failures: {successes} success, {failures} failed");
}
Expand Down
Loading
Loading