1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
use log::{debug, error, info, LevelFilter};
use log4rs::append::file::FileAppender;
use log4rs::config::{Appender, Config, Logger, Root};
use obnam::client::ClientConfig;
use obnam::cmd::{backup, list, list_files, restore};
use std::path::{Path, PathBuf};
use structopt::StructOpt;
const BUFFER_SIZE: usize = 1024 * 1024;
fn main() -> anyhow::Result<()> {
let opt = Opt::from_args();
let config = ClientConfig::read_config(&opt.config)?;
if let Some(ref log) = config.log {
setup_logging(&log)?;
}
info!("client starts");
debug!("{:?}", opt);
let result = match opt.cmd {
Command::Backup => backup(&config, BUFFER_SIZE),
Command::List => list(&config),
Command::ListFiles { gen_id } => list_files(&config, &gen_id),
Command::Restore { gen_id, to } => restore(&config, &gen_id, &to),
};
if let Err(ref e) = result {
error!("{}", e);
eprintln!("ERROR: {}", e);
return result;
}
info!("client ends successfully");
Ok(())
}
#[derive(Debug, StructOpt)]
#[structopt(name = "obnam-backup", about = "Simplistic backup client")]
struct Opt {
#[structopt(long, short, parse(from_os_str))]
config: PathBuf,
#[structopt(subcommand)]
cmd: Command,
}
#[derive(Debug, StructOpt)]
enum Command {
Backup,
List,
ListFiles {
#[structopt(default_value = "latest")]
gen_id: String,
},
Restore {
#[structopt()]
gen_id: String,
#[structopt(parse(from_os_str))]
to: PathBuf,
},
}
fn setup_logging(filename: &Path) -> anyhow::Result<()> {
let logfile = FileAppender::builder().build(filename)?;
let config = Config::builder()
.appender(Appender::builder().build("obnam", Box::new(logfile)))
.logger(Logger::builder().build("obnam", LevelFilter::Debug))
.build(Root::builder().appender("obnam").build(LevelFilter::Debug))?;
log4rs::init_config(config)?;
Ok(())
}
|