]>
Commit | Line | Data |
---|---|---|
fe0e04c6 | 1 | extern crate proxmox_backup; |
ff5d3707 | 2 | |
3 | use failure::*; | |
606ce64b | 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::*; |
fe0e04c6 | 16 | use proxmox_backup::backup::datastore::*; |
23bb8780 | 17 | |
43eeef28 | 18 | use serde_json::{Value}; |
23bb8780 DM |
19 | use hyper::Body; |
20 | ||
23bb8780 | 21 | fn backup_directory(body: Body, store: &str, archive_name: &str) -> Result<(), Error> { |
fb8365b7 | 22 | |
23bb8780 | 23 | let client = HttpClient::new("localhost"); |
fb8365b7 | 24 | |
ff3d3100 DM |
25 | let epoch = std::time::SystemTime::now().duration_since( |
26 | std::time::SystemTime::UNIX_EPOCH)?.as_secs(); | |
27 | ||
28 | let query = url::form_urlencoded::Serializer::new(String::new()) | |
29 | .append_pair("archive_name", archive_name) | |
30 | .append_pair("type", "host") | |
31 | .append_pair("id", &tools::nodename()) | |
32 | .append_pair("time", &epoch.to_string()) | |
33 | .finish(); | |
34 | ||
35 | let path = format!("api3/json/admin/datastore/{}/upload_catar?{}", store, query); | |
5e7a09be | 36 | |
83bdac1e | 37 | client.upload("application/x-proxmox-backup-catar", body, &path)?; |
bcd879cf DM |
38 | |
39 | Ok(()) | |
40 | } | |
41 | ||
23bb8780 | 42 | /**** |
bcd879cf DM |
43 | fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> { |
44 | ||
23bb8780 | 45 | let mut target = PathBuf::from(target); |
bcd879cf DM |
46 | |
47 | if let Some(ext) = target.extension() { | |
48 | if ext != "iidx" { | |
49 | bail!("got wrong file extension - expected '.iidx'"); | |
50 | } | |
51 | } else { | |
52 | target.set_extension("iidx"); | |
53 | } | |
54 | ||
55 | let mut index = datastore.create_image_writer(&target, size, chunk_size)?; | |
56 | ||
57 | tools::file_chunker(file, chunk_size, |pos, chunk| { | |
58 | index.add_chunk(pos, chunk)?; | |
59 | Ok(true) | |
60 | })?; | |
61 | ||
62 | index.close()?; // commit changes | |
63 | ||
64 | Ok(()) | |
65 | } | |
23bb8780 | 66 | */ |
bcd879cf DM |
67 | |
68 | fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> { | |
ff5d3707 | 69 | |
0fe5d605 DM |
70 | let filename = tools::required_string_param(¶m, "filename")?; |
71 | let store = tools::required_string_param(¶m, "store")?; | |
72 | let target = tools::required_string_param(¶m, "target")?; | |
a914a774 | 73 | |
2d9d143a DM |
74 | let mut chunk_size = 4*1024*1024; |
75 | ||
76 | if let Some(size) = param["chunk-size"].as_u64() { | |
77 | static SIZES: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096]; | |
78 | ||
79 | if SIZES.contains(&size) { | |
80 | chunk_size = (size as usize) * 1024; | |
81 | } else { | |
82 | bail!("Got unsupported chunk size '{}'", size); | |
83 | } | |
84 | } | |
85 | ||
23bb8780 DM |
86 | let stat = match nix::sys::stat::stat(filename) { |
87 | Ok(s) => s, | |
88 | Err(err) => bail!("unable to access '{}' - {}", filename, err), | |
89 | }; | |
a914a774 | 90 | |
bcd879cf DM |
91 | if (stat.st_mode & libc::S_IFDIR) != 0 { |
92 | println!("Backup directory '{}' to '{}'", filename, store); | |
93 | ||
23bb8780 DM |
94 | let stream = CaTarBackupStream::open(filename)?; |
95 | ||
96 | let body = Body::wrap_stream(stream); | |
fb8365b7 | 97 | |
23bb8780 | 98 | backup_directory(body, store, target)?; |
bcd879cf DM |
99 | |
100 | } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 { | |
23bb8780 | 101 | println!("Backup image '{}' to '{}'", filename, store); |
606ce64b | 102 | |
4818c8b6 DM |
103 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } |
104 | let size = stat.st_size as usize; | |
a914a774 | 105 | |
23bb8780 DM |
106 | panic!("implement me"); |
107 | ||
108 | //backup_image(&datastore, &file, size, &target, chunk_size)?; | |
d62e6e22 | 109 | |
594fa520 DM |
110 | // let idx = datastore.open_image_reader(target)?; |
111 | // idx.print_info(); | |
4818c8b6 | 112 | |
bcd879cf DM |
113 | } else { |
114 | bail!("unsupported file type (expected a directory, file or block device)"); | |
4818c8b6 DM |
115 | } |
116 | ||
f0819fe5 | 117 | //datastore.garbage_collection()?; |
3d5c11e5 | 118 | |
ff5d3707 | 119 | Ok(Value::Null) |
120 | } | |
121 | ||
122 | ||
244d9b17 DM |
123 | pub fn complete_file_name(arg: &str) -> Vec<String> { |
124 | ||
125 | let mut result = vec![]; | |
126 | ||
127 | use nix::fcntl::OFlag; | |
128 | use nix::sys::stat::Mode; | |
129 | use nix::fcntl::AtFlags; | |
130 | ||
131 | let mut dirname = std::path::PathBuf::from(arg); | |
132 | ||
73e844db DM |
133 | let is_dir = match nix::sys::stat::fstatat(libc::AT_FDCWD, &dirname, AtFlags::empty()) { |
134 | Ok(stat) => (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR, | |
135 | Err(_) => false, | |
136 | }; | |
244d9b17 | 137 | |
73e844db | 138 | if !is_dir { |
244d9b17 DM |
139 | if let Some(parent) = dirname.parent() { |
140 | dirname = parent.to_owned(); | |
141 | } | |
142 | } | |
143 | ||
144 | let mut dir = match nix::dir::Dir::openat(libc::AT_FDCWD, &dirname, OFlag::O_DIRECTORY, Mode::empty()) { | |
145 | Ok(d) => d, | |
146 | Err(err) => { | |
147 | return result; | |
148 | } | |
149 | }; | |
150 | ||
151 | for item in dir.iter() { | |
152 | if let Ok(entry) = item { | |
153 | if let Ok(name) = entry.file_name().to_str() { | |
154 | if name == "." || name == ".." { continue; } | |
155 | let mut newpath = dirname.clone(); | |
156 | newpath.push(name); | |
157 | ||
158 | if let Ok(stat) = nix::sys::stat::fstatat(libc::AT_FDCWD, &newpath, AtFlags::empty()) { | |
159 | if (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR { | |
160 | newpath.push(""); | |
161 | if let Some(newpath) = newpath.to_str() { | |
162 | result.push(newpath.to_owned()); | |
163 | } | |
164 | continue; | |
165 | } | |
166 | } | |
167 | if let Some(newpath) = newpath.to_str() { | |
168 | result.push(newpath.to_owned()); | |
169 | } | |
170 | ||
171 | } | |
172 | } | |
173 | } | |
174 | ||
175 | result | |
176 | } | |
177 | ||
ff5d3707 | 178 | fn main() { |
179 | ||
180 | let cmd_def = CliCommand::new( | |
181 | ApiMethod::new( | |
bcd879cf DM |
182 | create_backup, |
183 | ObjectSchema::new("Create backup.") | |
184 | .required("filename", StringSchema::new("Source name (file or directory name)")) | |
ff5d3707 | 185 | .required("store", StringSchema::new("Datastore name.")) |
c34eb166 | 186 | .required("target", StringSchema::new("Target name.")) |
2d9d143a DM |
187 | .optional( |
188 | "chunk-size", | |
189 | IntegerSchema::new("Chunk size in KB. Must be a power of 2.") | |
190 | .minimum(64) | |
191 | .maximum(4096) | |
192 | .default(4096) | |
193 | ) | |
ff5d3707 | 194 | )) |
c34eb166 | 195 | .arg_param(vec!["filename", "target"]) |
244d9b17 | 196 | .completion_cb("filename", complete_file_name) |
fe0e04c6 | 197 | .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name); |
f8838fe9 | 198 | |
a914a774 | 199 | |
ff5d3707 | 200 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
201 | eprintln!("Error: {}", err); | |
4968bc3a WB |
202 | if err.downcast::<UsageError>().is_ok() { |
203 | print_cli_usage(); | |
204 | } | |
ff5d3707 | 205 | std::process::exit(-1); |
206 | } | |
207 | ||
208 | } |