]>
Commit | Line | Data |
---|---|---|
fe0e04c6 | 1 | extern crate proxmox_backup; |
ff5d3707 | 2 | |
3 | use failure::*; | |
728797d0 | 4 | //use std::os::unix::io::AsRawFd; |
ff5d3707 | 5 | |
fe0e04c6 DM |
6 | use proxmox_backup::tools; |
7 | use proxmox_backup::cli::command::*; | |
8 | use proxmox_backup::api::schema::*; | |
9 | use proxmox_backup::api::router::*; | |
23bb8780 DM |
10 | use proxmox_backup::client::http_client::*; |
11 | use proxmox_backup::client::catar_backup_stream::*; | |
fe0e04c6 DM |
12 | //use proxmox_backup::backup::chunk_store::*; |
13 | //use proxmox_backup::backup::image_index::*; | |
14 | //use proxmox_backup::config::datastore; | |
23bb8780 | 15 | //use proxmox_backup::catar::encoder::*; |
728797d0 | 16 | //use proxmox_backup::backup::datastore::*; |
23bb8780 | 17 | |
43eeef28 | 18 | use serde_json::{Value}; |
23bb8780 | 19 | use hyper::Body; |
33d64b81 DM |
20 | use std::sync::Arc; |
21 | use lazy_static::lazy_static; | |
22 | use regex::Regex; | |
23bb8780 | 23 | |
33d64b81 DM |
24 | lazy_static! { |
25 | // user@host:datastore | |
26 | pub static ref BACKUP_REPO_URL_REGEX: Regex = Regex::new(r"^(?:(?:([\w@]+)@)?(\w+):)?(\w+)$").unwrap(); | |
fb8365b7 | 27 | |
33d64b81 DM |
28 | pub static ref BACKUP_REPO_URL: Arc<ApiStringFormat> = |
29 | ApiStringFormat::Pattern(&BACKUP_REPO_URL_REGEX).into(); | |
30 | } | |
31 | ||
32 | #[derive(Debug)] | |
33 | pub struct BackupRepository { | |
34 | pub user: String, | |
35 | pub host: String, | |
36 | pub store: String, | |
37 | } | |
38 | ||
39 | impl BackupRepository { | |
40 | ||
41 | pub fn parse(url: &str) -> Result<Self, Error> { | |
42 | ||
43 | let cap = BACKUP_REPO_URL_REGEX.captures(url) | |
44 | .ok_or_else(|| format_err!("unable to parse reepository url '{}'", url))?; | |
45 | ||
46 | Ok(BackupRepository { | |
47 | user: cap.get(1).map_or("root@pam", |m| m.as_str()).to_owned(), | |
48 | host: cap.get(2).map_or("localhost", |m| m.as_str()).to_owned(), | |
49 | store: cap[3].to_owned(), | |
50 | }) | |
51 | } | |
52 | } | |
53 | ||
54 | fn backup_directory(repo: &BackupRepository, body: Body, archive_name: &str) -> Result<(), Error> { | |
55 | ||
0dffe3f9 | 56 | let client = HttpClient::new(&repo.host, &repo.user); |
fb8365b7 | 57 | |
ff3d3100 DM |
58 | let epoch = std::time::SystemTime::now().duration_since( |
59 | std::time::SystemTime::UNIX_EPOCH)?.as_secs(); | |
60 | ||
61 | let query = url::form_urlencoded::Serializer::new(String::new()) | |
62 | .append_pair("archive_name", archive_name) | |
63 | .append_pair("type", "host") | |
64 | .append_pair("id", &tools::nodename()) | |
65 | .append_pair("time", &epoch.to_string()) | |
66 | .finish(); | |
67 | ||
33d64b81 | 68 | let path = format!("api2/json/admin/datastore/{}/catar?{}", repo.store, query); |
5e7a09be | 69 | |
83bdac1e | 70 | client.upload("application/x-proxmox-backup-catar", body, &path)?; |
bcd879cf DM |
71 | |
72 | Ok(()) | |
73 | } | |
74 | ||
23bb8780 | 75 | /**** |
bcd879cf DM |
76 | fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> { |
77 | ||
23bb8780 | 78 | let mut target = PathBuf::from(target); |
bcd879cf DM |
79 | |
80 | if let Some(ext) = target.extension() { | |
91a905b6 DM |
81 | if ext != "fidx" { |
82 | bail!("got wrong file extension - expected '.fidx'"); | |
bcd879cf DM |
83 | } |
84 | } else { | |
91a905b6 | 85 | target.set_extension("fidx"); |
bcd879cf DM |
86 | } |
87 | ||
88 | let mut index = datastore.create_image_writer(&target, size, chunk_size)?; | |
89 | ||
90 | tools::file_chunker(file, chunk_size, |pos, chunk| { | |
91 | index.add_chunk(pos, chunk)?; | |
92 | Ok(true) | |
93 | })?; | |
94 | ||
95 | index.close()?; // commit changes | |
96 | ||
97 | Ok(()) | |
98 | } | |
23bb8780 | 99 | */ |
bcd879cf | 100 | |
6049b71f DM |
101 | fn list_backups( |
102 | param: Value, | |
103 | _info: &ApiMethod, | |
104 | _rpcenv: &mut RpcEnvironment, | |
105 | ) -> Result<Value, Error> { | |
41c039e1 | 106 | |
33d64b81 DM |
107 | let repo_url = tools::required_string_param(¶m, "repository")?; |
108 | let repo = BackupRepository::parse(repo_url)?; | |
41c039e1 | 109 | |
0dffe3f9 | 110 | let client = HttpClient::new(&repo.host, &repo.user); |
41c039e1 | 111 | |
33d64b81 | 112 | let path = format!("api2/json/admin/datastore/{}/backups", repo.store); |
41c039e1 DM |
113 | |
114 | let result = client.get(&path)?; | |
115 | ||
116 | Ok(result) | |
117 | } | |
118 | ||
33d64b81 | 119 | |
6049b71f DM |
120 | fn create_backup( |
121 | param: Value, | |
122 | _info: &ApiMethod, | |
123 | _rpcenv: &mut RpcEnvironment, | |
124 | ) -> Result<Value, Error> { | |
ff5d3707 | 125 | |
0fe5d605 | 126 | let filename = tools::required_string_param(¶m, "filename")?; |
33d64b81 | 127 | let repo_url = tools::required_string_param(¶m, "repository")?; |
0fe5d605 | 128 | let target = tools::required_string_param(¶m, "target")?; |
a914a774 | 129 | |
33d64b81 DM |
130 | let repo = BackupRepository::parse(repo_url)?; |
131 | ||
728797d0 | 132 | let mut _chunk_size = 4*1024*1024; |
2d9d143a DM |
133 | |
134 | if let Some(size) = param["chunk-size"].as_u64() { | |
135 | static SIZES: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096]; | |
136 | ||
137 | if SIZES.contains(&size) { | |
728797d0 | 138 | _chunk_size = (size as usize) * 1024; |
2d9d143a DM |
139 | } else { |
140 | bail!("Got unsupported chunk size '{}'", size); | |
141 | } | |
142 | } | |
143 | ||
23bb8780 DM |
144 | let stat = match nix::sys::stat::stat(filename) { |
145 | Ok(s) => s, | |
146 | Err(err) => bail!("unable to access '{}' - {}", filename, err), | |
147 | }; | |
a914a774 | 148 | |
bcd879cf | 149 | if (stat.st_mode & libc::S_IFDIR) != 0 { |
33d64b81 | 150 | println!("Backup directory '{}' to '{:?}'", filename, repo); |
bcd879cf | 151 | |
23bb8780 DM |
152 | let stream = CaTarBackupStream::open(filename)?; |
153 | ||
154 | let body = Body::wrap_stream(stream); | |
fb8365b7 | 155 | |
33d64b81 | 156 | backup_directory(&repo, body, target)?; |
bcd879cf DM |
157 | |
158 | } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 { | |
33d64b81 | 159 | println!("Backup image '{}' to '{:?}'", filename, repo); |
606ce64b | 160 | |
4818c8b6 | 161 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } |
728797d0 | 162 | let _size = stat.st_size as usize; |
a914a774 | 163 | |
23bb8780 DM |
164 | panic!("implement me"); |
165 | ||
166 | //backup_image(&datastore, &file, size, &target, chunk_size)?; | |
d62e6e22 | 167 | |
594fa520 DM |
168 | // let idx = datastore.open_image_reader(target)?; |
169 | // idx.print_info(); | |
4818c8b6 | 170 | |
bcd879cf DM |
171 | } else { |
172 | bail!("unsupported file type (expected a directory, file or block device)"); | |
4818c8b6 DM |
173 | } |
174 | ||
f0819fe5 | 175 | //datastore.garbage_collection()?; |
3d5c11e5 | 176 | |
ff5d3707 | 177 | Ok(Value::Null) |
178 | } | |
179 | ||
ff5d3707 | 180 | fn main() { |
181 | ||
33d64b81 DM |
182 | let repo_url_schema: Arc<Schema> = Arc::new( |
183 | StringSchema::new("Repository URL.") | |
184 | .format(BACKUP_REPO_URL.clone()) | |
185 | .max_length(256) | |
186 | .into() | |
187 | ); | |
188 | ||
41c039e1 | 189 | let create_cmd_def = CliCommand::new( |
ff5d3707 | 190 | ApiMethod::new( |
bcd879cf DM |
191 | create_backup, |
192 | ObjectSchema::new("Create backup.") | |
33d64b81 | 193 | .required("repository", repo_url_schema.clone()) |
bcd879cf | 194 | .required("filename", StringSchema::new("Source name (file or directory name)")) |
c34eb166 | 195 | .required("target", StringSchema::new("Target name.")) |
2d9d143a DM |
196 | .optional( |
197 | "chunk-size", | |
198 | IntegerSchema::new("Chunk size in KB. Must be a power of 2.") | |
199 | .minimum(64) | |
200 | .maximum(4096) | |
201 | .default(4096) | |
202 | ) | |
ff5d3707 | 203 | )) |
33d64b81 DM |
204 | .arg_param(vec!["repository", "filename", "target"]) |
205 | .completion_cb("filename", tools::complete_file_name); | |
f8838fe9 | 206 | |
41c039e1 DM |
207 | let list_cmd_def = CliCommand::new( |
208 | ApiMethod::new( | |
209 | list_backups, | |
210 | ObjectSchema::new("List backups.") | |
33d64b81 | 211 | .required("repository", repo_url_schema.clone()) |
41c039e1 | 212 | )) |
33d64b81 | 213 | .arg_param(vec!["repository"]); |
41c039e1 DM |
214 | |
215 | let cmd_def = CliCommandMap::new() | |
216 | .insert("create".to_owned(), create_cmd_def.into()) | |
217 | .insert("list".to_owned(), list_cmd_def.into()); | |
a914a774 | 218 | |
ff5d3707 | 219 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
220 | eprintln!("Error: {}", err); | |
4968bc3a WB |
221 | if err.downcast::<UsageError>().is_ok() { |
222 | print_cli_usage(); | |
223 | } | |
ff5d3707 | 224 | std::process::exit(-1); |
225 | } | |
226 | ||
227 | } |