]>
Commit | Line | Data |
---|---|---|
fe0e04c6 | 1 | extern crate proxmox_backup; |
ff5d3707 | 2 | |
3 | use failure::*; | |
728797d0 | 4 | //use std::os::unix::io::AsRawFd; |
8c75372b | 5 | use chrono::{Local, TimeZone}; |
ff5d3707 | 6 | |
fe0e04c6 | 7 | use proxmox_backup::tools; |
4de0e142 | 8 | use proxmox_backup::cli::*; |
ef2f2efb | 9 | use proxmox_backup::api_schema::*; |
dc9a007b | 10 | use proxmox_backup::api_schema::router::*; |
151c6ce2 | 11 | use proxmox_backup::client::*; |
247cdbce | 12 | use proxmox_backup::backup::*; |
fe0e04c6 DM |
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 | 20 | use std::sync::Arc; |
33d64b81 | 21 | |
247cdbce DM |
22 | fn backup_directory( |
23 | repo: &BackupRepository, | |
24 | body: Body, | |
25 | archive_name: &str, | |
26 | chunk_size: Option<u64>, | |
27 | ) -> Result<(), Error> { | |
33d64b81 | 28 | |
0dffe3f9 | 29 | let client = HttpClient::new(&repo.host, &repo.user); |
fb8365b7 | 30 | |
ff3d3100 DM |
31 | let epoch = std::time::SystemTime::now().duration_since( |
32 | std::time::SystemTime::UNIX_EPOCH)?.as_secs(); | |
33 | ||
247cdbce DM |
34 | let mut query = url::form_urlencoded::Serializer::new(String::new()); |
35 | ||
36 | query | |
ff3d3100 DM |
37 | .append_pair("archive_name", archive_name) |
38 | .append_pair("type", "host") | |
39 | .append_pair("id", &tools::nodename()) | |
247cdbce DM |
40 | .append_pair("time", &epoch.to_string()); |
41 | ||
42 | if let Some(size) = chunk_size { | |
43 | query.append_pair("chunk-size", &size.to_string()); | |
44 | } | |
45 | ||
46 | let query = query.finish(); | |
ff3d3100 | 47 | |
33d64b81 | 48 | let path = format!("api2/json/admin/datastore/{}/catar?{}", repo.store, query); |
5e7a09be | 49 | |
83bdac1e | 50 | client.upload("application/x-proxmox-backup-catar", body, &path)?; |
bcd879cf DM |
51 | |
52 | Ok(()) | |
53 | } | |
54 | ||
23bb8780 | 55 | /**** |
bcd879cf DM |
56 | fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> { |
57 | ||
23bb8780 | 58 | let mut target = PathBuf::from(target); |
bcd879cf DM |
59 | |
60 | if let Some(ext) = target.extension() { | |
91a905b6 DM |
61 | if ext != "fidx" { |
62 | bail!("got wrong file extension - expected '.fidx'"); | |
bcd879cf DM |
63 | } |
64 | } else { | |
91a905b6 | 65 | target.set_extension("fidx"); |
bcd879cf DM |
66 | } |
67 | ||
68 | let mut index = datastore.create_image_writer(&target, size, chunk_size)?; | |
69 | ||
70 | tools::file_chunker(file, chunk_size, |pos, chunk| { | |
71 | index.add_chunk(pos, chunk)?; | |
72 | Ok(true) | |
73 | })?; | |
74 | ||
75 | index.close()?; // commit changes | |
76 | ||
77 | Ok(()) | |
78 | } | |
23bb8780 | 79 | */ |
bcd879cf | 80 | |
6049b71f DM |
81 | fn list_backups( |
82 | param: Value, | |
83 | _info: &ApiMethod, | |
84 | _rpcenv: &mut RpcEnvironment, | |
85 | ) -> Result<Value, Error> { | |
41c039e1 | 86 | |
33d64b81 DM |
87 | let repo_url = tools::required_string_param(¶m, "repository")?; |
88 | let repo = BackupRepository::parse(repo_url)?; | |
41c039e1 | 89 | |
0dffe3f9 | 90 | let client = HttpClient::new(&repo.host, &repo.user); |
41c039e1 | 91 | |
33d64b81 | 92 | let path = format!("api2/json/admin/datastore/{}/backups", repo.store); |
41c039e1 DM |
93 | |
94 | let result = client.get(&path)?; | |
95 | ||
8c75372b DM |
96 | // fixme: implement and use output formatter instead .. |
97 | let list = result["data"].as_array().unwrap(); | |
98 | ||
99 | for item in list { | |
100 | ||
101 | let id = item["backup_id"].as_str(). unwrap(); | |
102 | let btype = item["backup_type"].as_str(). unwrap(); | |
103 | let epoch = item["backup_time"].as_i64(). unwrap(); | |
104 | ||
105 | let time_str = Local.timestamp(epoch, 0).format("%c"); | |
106 | ||
107 | let files = item["files"].as_array().unwrap(); | |
108 | ||
109 | for file in files { | |
110 | let filename = file.as_str().unwrap(); | |
111 | println!("| {} | {} | {} | {}", btype, id, time_str, filename); | |
112 | } | |
113 | } | |
114 | ||
115 | //Ok(result) | |
116 | Ok(Value::Null) | |
41c039e1 DM |
117 | } |
118 | ||
8cc0d6af DM |
119 | fn start_garbage_collection( |
120 | param: Value, | |
121 | _info: &ApiMethod, | |
122 | _rpcenv: &mut RpcEnvironment, | |
123 | ) -> Result<Value, Error> { | |
124 | ||
125 | let repo_url = tools::required_string_param(¶m, "repository")?; | |
126 | let repo = BackupRepository::parse(repo_url)?; | |
127 | ||
128 | let client = HttpClient::new(&repo.host, &repo.user); | |
129 | ||
130 | let path = format!("api2/json/admin/datastore/{}/gc", repo.store); | |
131 | ||
132 | let result = client.post(&path)?; | |
133 | ||
134 | Ok(result) | |
135 | } | |
33d64b81 | 136 | |
6049b71f DM |
137 | fn create_backup( |
138 | param: Value, | |
139 | _info: &ApiMethod, | |
140 | _rpcenv: &mut RpcEnvironment, | |
141 | ) -> Result<Value, Error> { | |
ff5d3707 | 142 | |
0fe5d605 | 143 | let filename = tools::required_string_param(¶m, "filename")?; |
33d64b81 | 144 | let repo_url = tools::required_string_param(¶m, "repository")?; |
0fe5d605 | 145 | let target = tools::required_string_param(¶m, "target")?; |
a914a774 | 146 | |
33d64b81 DM |
147 | let repo = BackupRepository::parse(repo_url)?; |
148 | ||
247cdbce | 149 | let chunk_size_opt = param["chunk-size"].as_u64().map(|v| v*1024); |
2d9d143a | 150 | |
247cdbce DM |
151 | if let Some(size) = chunk_size_opt { |
152 | verify_chunk_size(size)?; | |
2d9d143a DM |
153 | } |
154 | ||
23bb8780 DM |
155 | let stat = match nix::sys::stat::stat(filename) { |
156 | Ok(s) => s, | |
157 | Err(err) => bail!("unable to access '{}' - {}", filename, err), | |
158 | }; | |
a914a774 | 159 | |
bcd879cf | 160 | if (stat.st_mode & libc::S_IFDIR) != 0 { |
33d64b81 | 161 | println!("Backup directory '{}' to '{:?}'", filename, repo); |
bcd879cf | 162 | |
23bb8780 DM |
163 | let stream = CaTarBackupStream::open(filename)?; |
164 | ||
165 | let body = Body::wrap_stream(stream); | |
fb8365b7 | 166 | |
247cdbce | 167 | backup_directory(&repo, body, target, chunk_size_opt)?; |
bcd879cf DM |
168 | |
169 | } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 { | |
33d64b81 | 170 | println!("Backup image '{}' to '{:?}'", filename, repo); |
606ce64b | 171 | |
4818c8b6 | 172 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } |
728797d0 | 173 | let _size = stat.st_size as usize; |
a914a774 | 174 | |
23bb8780 DM |
175 | panic!("implement me"); |
176 | ||
177 | //backup_image(&datastore, &file, size, &target, chunk_size)?; | |
d62e6e22 | 178 | |
594fa520 DM |
179 | // let idx = datastore.open_image_reader(target)?; |
180 | // idx.print_info(); | |
4818c8b6 | 181 | |
bcd879cf DM |
182 | } else { |
183 | bail!("unsupported file type (expected a directory, file or block device)"); | |
4818c8b6 DM |
184 | } |
185 | ||
f0819fe5 | 186 | //datastore.garbage_collection()?; |
3d5c11e5 | 187 | |
ff5d3707 | 188 | Ok(Value::Null) |
189 | } | |
190 | ||
ff5d3707 | 191 | fn main() { |
192 | ||
33d64b81 DM |
193 | let repo_url_schema: Arc<Schema> = Arc::new( |
194 | StringSchema::new("Repository URL.") | |
195 | .format(BACKUP_REPO_URL.clone()) | |
196 | .max_length(256) | |
197 | .into() | |
198 | ); | |
199 | ||
41c039e1 | 200 | let create_cmd_def = CliCommand::new( |
ff5d3707 | 201 | ApiMethod::new( |
bcd879cf DM |
202 | create_backup, |
203 | ObjectSchema::new("Create backup.") | |
33d64b81 | 204 | .required("repository", repo_url_schema.clone()) |
8b6dd224 | 205 | .required("filename", StringSchema::new("Source name (file or directory name).")) |
c34eb166 | 206 | .required("target", StringSchema::new("Target name.")) |
2d9d143a DM |
207 | .optional( |
208 | "chunk-size", | |
209 | IntegerSchema::new("Chunk size in KB. Must be a power of 2.") | |
210 | .minimum(64) | |
211 | .maximum(4096) | |
212 | .default(4096) | |
213 | ) | |
ff5d3707 | 214 | )) |
33d64b81 DM |
215 | .arg_param(vec!["repository", "filename", "target"]) |
216 | .completion_cb("filename", tools::complete_file_name); | |
f8838fe9 | 217 | |
41c039e1 DM |
218 | let list_cmd_def = CliCommand::new( |
219 | ApiMethod::new( | |
220 | list_backups, | |
221 | ObjectSchema::new("List backups.") | |
33d64b81 | 222 | .required("repository", repo_url_schema.clone()) |
41c039e1 | 223 | )) |
33d64b81 | 224 | .arg_param(vec!["repository"]); |
41c039e1 | 225 | |
8cc0d6af DM |
226 | let garbage_collect_cmd_def = CliCommand::new( |
227 | ApiMethod::new( | |
228 | start_garbage_collection, | |
229 | ObjectSchema::new("Start garbage collection for a specific repository.") | |
230 | .required("repository", repo_url_schema.clone()) | |
231 | )) | |
232 | .arg_param(vec!["repository"]); | |
233 | ||
41c039e1 DM |
234 | let cmd_def = CliCommandMap::new() |
235 | .insert("create".to_owned(), create_cmd_def.into()) | |
8cc0d6af | 236 | .insert("garbage-collect".to_owned(), garbage_collect_cmd_def.into()) |
41c039e1 | 237 | .insert("list".to_owned(), list_cmd_def.into()); |
a914a774 | 238 | |
698d9d44 | 239 | run_cli_command(cmd_def.into()); |
ff5d3707 | 240 | } |