]>
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 DM |
7 | use proxmox_backup::tools; |
8 | use proxmox_backup::cli::command::*; | |
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 | ||
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 | ||
247cdbce | 132 | let chunk_size_opt = param["chunk-size"].as_u64().map(|v| v*1024); |
2d9d143a | 133 | |
247cdbce DM |
134 | if let Some(size) = chunk_size_opt { |
135 | verify_chunk_size(size)?; | |
2d9d143a DM |
136 | } |
137 | ||
23bb8780 DM |
138 | let stat = match nix::sys::stat::stat(filename) { |
139 | Ok(s) => s, | |
140 | Err(err) => bail!("unable to access '{}' - {}", filename, err), | |
141 | }; | |
a914a774 | 142 | |
bcd879cf | 143 | if (stat.st_mode & libc::S_IFDIR) != 0 { |
33d64b81 | 144 | println!("Backup directory '{}' to '{:?}'", filename, repo); |
bcd879cf | 145 | |
23bb8780 DM |
146 | let stream = CaTarBackupStream::open(filename)?; |
147 | ||
148 | let body = Body::wrap_stream(stream); | |
fb8365b7 | 149 | |
247cdbce | 150 | backup_directory(&repo, body, target, chunk_size_opt)?; |
bcd879cf DM |
151 | |
152 | } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 { | |
33d64b81 | 153 | println!("Backup image '{}' to '{:?}'", filename, repo); |
606ce64b | 154 | |
4818c8b6 | 155 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } |
728797d0 | 156 | let _size = stat.st_size as usize; |
a914a774 | 157 | |
23bb8780 DM |
158 | panic!("implement me"); |
159 | ||
160 | //backup_image(&datastore, &file, size, &target, chunk_size)?; | |
d62e6e22 | 161 | |
594fa520 DM |
162 | // let idx = datastore.open_image_reader(target)?; |
163 | // idx.print_info(); | |
4818c8b6 | 164 | |
bcd879cf DM |
165 | } else { |
166 | bail!("unsupported file type (expected a directory, file or block device)"); | |
4818c8b6 DM |
167 | } |
168 | ||
f0819fe5 | 169 | //datastore.garbage_collection()?; |
3d5c11e5 | 170 | |
ff5d3707 | 171 | Ok(Value::Null) |
172 | } | |
173 | ||
ff5d3707 | 174 | fn main() { |
175 | ||
33d64b81 DM |
176 | let repo_url_schema: Arc<Schema> = Arc::new( |
177 | StringSchema::new("Repository URL.") | |
178 | .format(BACKUP_REPO_URL.clone()) | |
179 | .max_length(256) | |
180 | .into() | |
181 | ); | |
182 | ||
41c039e1 | 183 | let create_cmd_def = CliCommand::new( |
ff5d3707 | 184 | ApiMethod::new( |
bcd879cf DM |
185 | create_backup, |
186 | ObjectSchema::new("Create backup.") | |
33d64b81 | 187 | .required("repository", repo_url_schema.clone()) |
bcd879cf | 188 | .required("filename", StringSchema::new("Source name (file or directory name)")) |
c34eb166 | 189 | .required("target", StringSchema::new("Target name.")) |
2d9d143a DM |
190 | .optional( |
191 | "chunk-size", | |
192 | IntegerSchema::new("Chunk size in KB. Must be a power of 2.") | |
193 | .minimum(64) | |
194 | .maximum(4096) | |
195 | .default(4096) | |
196 | ) | |
ff5d3707 | 197 | )) |
33d64b81 DM |
198 | .arg_param(vec!["repository", "filename", "target"]) |
199 | .completion_cb("filename", tools::complete_file_name); | |
f8838fe9 | 200 | |
41c039e1 DM |
201 | let list_cmd_def = CliCommand::new( |
202 | ApiMethod::new( | |
203 | list_backups, | |
204 | ObjectSchema::new("List backups.") | |
33d64b81 | 205 | .required("repository", repo_url_schema.clone()) |
41c039e1 | 206 | )) |
33d64b81 | 207 | .arg_param(vec!["repository"]); |
41c039e1 DM |
208 | |
209 | let cmd_def = CliCommandMap::new() | |
210 | .insert("create".to_owned(), create_cmd_def.into()) | |
211 | .insert("list".to_owned(), list_cmd_def.into()); | |
a914a774 | 212 | |
ff5d3707 | 213 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
214 | eprintln!("Error: {}", err); | |
4968bc3a WB |
215 | if err.downcast::<UsageError>().is_ok() { |
216 | print_cli_usage(); | |
217 | } | |
ff5d3707 | 218 | std::process::exit(-1); |
219 | } | |
220 | ||
221 | } |