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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
#![feature(iterator_try_collect, never_type)]
pub mod client;
pub mod server;
use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use client::Client;
use log::info;
use serde::Deserialize;
use server::server;
use std::{
fs::{read_to_string, File},
net::SocketAddr,
os::unix::fs::MetadataExt,
path::PathBuf,
};
pub type Serial = u64;
#[derive(Parser)]
/// A tool for safely automating offsite backups over a network
struct Args {
/// Path to the configuration file
config: PathBuf,
#[clap(subcommand)]
action: Action,
}
#[derive(Subcommand)]
enum Action {
/// Run as a server
Daemon,
/// List backups stored on other peers
List {
/// Which peers to list backups of; all if not specified
peer: Option<String>,
},
/// Download a backup
Download {
path: PathBuf,
/// Which peer to download from, any if not specified
peer: Option<String>,
/// Serial of the backup to download, latest if not specified
serial: Option<Serial>,
},
/// Upload a backup to one or more peers
Upload {
/// Path to backup file
path: PathBuf,
/// Which peer to upload to, all if not specified
peer: Option<String>,
},
}
#[derive(Deserialize)]
pub struct Config {
storage: StorageConfig,
server: ServerConfig,
peer: Vec<PeerConfig>,
}
#[derive(Deserialize)]
pub struct PeerConfig {
name: String,
address: SocketAddr,
shared_secret: String,
}
#[derive(Deserialize)]
pub struct ServerConfig {
address: String,
}
#[derive(Deserialize)]
pub struct StorageConfig {
root: PathBuf,
size: u64,
versions: usize,
upload_cooldown: u64,
download_cooldown: u64,
upload_speed: usize,
download_speed: usize,
}
fn main() -> Result<()> {
env_logger::init_from_env("LOG");
let args = Args::parse();
let config = read_to_string(&args.config)?;
let config = toml::from_str::<Config>(&config)?;
match args.action {
Action::Daemon => server(config.into())?,
Action::List { peer } => {
let peers = config.peer.iter().filter(|p| {
if let Some(pn) = &peer {
pn == &p.name
} else {
true
}
});
for p in peers {
println!("peer {:?}", p.name);
let mut client = Client::new(p.address, &p.shared_secret)?;
for (mtime, size, serial) in client.list()? {
println!("\tserial={serial} mtime={mtime} size={size}")
}
client.quit()?;
}
}
Action::Download { path, serial, peer } => {
let mut peers = config.peer.iter().filter(|p| {
if let Some(pn) = &peer {
pn == &p.name
} else {
true
}
});
let peer = peers.next().ok_or(anyhow!("no matching peer"))?;
info!("connecting to {:?}", peer.name);
let mut client = Client::new(peer.address, &peer.shared_secret)?;
let file = File::create_new(&path)?;
let serial = if let Some(serial) = serial {
serial
} else {
client.list()?.last().ok_or(anyhow!("no backups stored"))?.2
};
println!("downloading serial={serial} from {}", peer.name);
client.download(serial, file)?;
info!("upload successful");
client.quit()?;
println!("success")
}
Action::Upload { peer, path } => {
let peers = config.peer.iter().filter(|p| {
if let Some(pn) = &peer {
pn == &p.name
} else {
true
}
});
for peer in peers {
info!("connecting to {:?}", peer.name);
println!("uploading to {}", peer.name);
let mut client = Client::new(peer.address, &peer.shared_secret)?;
let file = File::open(&path)?;
let size = file.metadata()?.size();
client.upload(size, file)?;
info!("upload successful");
client.quit()?;
}
println!("success")
}
}
Ok(())
}
|