]>
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 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 | ||
576e3bf2 | 35 | let path = format!("api2/json/admin/datastore/{}/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 | 67 | |
6049b71f DM |
68 | fn list_backups( |
69 | param: Value, | |
70 | _info: &ApiMethod, | |
71 | _rpcenv: &mut RpcEnvironment, | |
72 | ) -> Result<Value, Error> { | |
41c039e1 DM |
73 | |
74 | let store = tools::required_string_param(¶m, "store")?; | |
75 | ||
76 | let client = HttpClient::new("localhost"); | |
77 | ||
576e3bf2 | 78 | let path = format!("api2/json/admin/datastore/{}/backups", store); |
41c039e1 DM |
79 | |
80 | let result = client.get(&path)?; | |
81 | ||
82 | Ok(result) | |
83 | } | |
84 | ||
6049b71f DM |
85 | fn create_backup( |
86 | param: Value, | |
87 | _info: &ApiMethod, | |
88 | _rpcenv: &mut RpcEnvironment, | |
89 | ) -> Result<Value, Error> { | |
ff5d3707 | 90 | |
0fe5d605 DM |
91 | let filename = tools::required_string_param(¶m, "filename")?; |
92 | let store = tools::required_string_param(¶m, "store")?; | |
93 | let target = tools::required_string_param(¶m, "target")?; | |
a914a774 | 94 | |
728797d0 | 95 | let mut _chunk_size = 4*1024*1024; |
2d9d143a DM |
96 | |
97 | if let Some(size) = param["chunk-size"].as_u64() { | |
98 | static SIZES: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096]; | |
99 | ||
100 | if SIZES.contains(&size) { | |
728797d0 | 101 | _chunk_size = (size as usize) * 1024; |
2d9d143a DM |
102 | } else { |
103 | bail!("Got unsupported chunk size '{}'", size); | |
104 | } | |
105 | } | |
106 | ||
23bb8780 DM |
107 | let stat = match nix::sys::stat::stat(filename) { |
108 | Ok(s) => s, | |
109 | Err(err) => bail!("unable to access '{}' - {}", filename, err), | |
110 | }; | |
a914a774 | 111 | |
bcd879cf DM |
112 | if (stat.st_mode & libc::S_IFDIR) != 0 { |
113 | println!("Backup directory '{}' to '{}'", filename, store); | |
114 | ||
23bb8780 DM |
115 | let stream = CaTarBackupStream::open(filename)?; |
116 | ||
117 | let body = Body::wrap_stream(stream); | |
fb8365b7 | 118 | |
23bb8780 | 119 | backup_directory(body, store, target)?; |
bcd879cf DM |
120 | |
121 | } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 { | |
23bb8780 | 122 | println!("Backup image '{}' to '{}'", filename, store); |
606ce64b | 123 | |
4818c8b6 | 124 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } |
728797d0 | 125 | let _size = stat.st_size as usize; |
a914a774 | 126 | |
23bb8780 DM |
127 | panic!("implement me"); |
128 | ||
129 | //backup_image(&datastore, &file, size, &target, chunk_size)?; | |
d62e6e22 | 130 | |
594fa520 DM |
131 | // let idx = datastore.open_image_reader(target)?; |
132 | // idx.print_info(); | |
4818c8b6 | 133 | |
bcd879cf DM |
134 | } else { |
135 | bail!("unsupported file type (expected a directory, file or block device)"); | |
4818c8b6 DM |
136 | } |
137 | ||
f0819fe5 | 138 | //datastore.garbage_collection()?; |
3d5c11e5 | 139 | |
ff5d3707 | 140 | Ok(Value::Null) |
141 | } | |
142 | ||
ff5d3707 | 143 | fn main() { |
144 | ||
41c039e1 | 145 | let create_cmd_def = CliCommand::new( |
ff5d3707 | 146 | ApiMethod::new( |
bcd879cf DM |
147 | create_backup, |
148 | ObjectSchema::new("Create backup.") | |
149 | .required("filename", StringSchema::new("Source name (file or directory name)")) | |
ff5d3707 | 150 | .required("store", StringSchema::new("Datastore name.")) |
c34eb166 | 151 | .required("target", StringSchema::new("Target name.")) |
2d9d143a DM |
152 | .optional( |
153 | "chunk-size", | |
154 | IntegerSchema::new("Chunk size in KB. Must be a power of 2.") | |
155 | .minimum(64) | |
156 | .maximum(4096) | |
157 | .default(4096) | |
158 | ) | |
ff5d3707 | 159 | )) |
c34eb166 | 160 | .arg_param(vec!["filename", "target"]) |
383e8577 | 161 | .completion_cb("filename", tools::complete_file_name) |
fe0e04c6 | 162 | .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name); |
f8838fe9 | 163 | |
41c039e1 DM |
164 | let list_cmd_def = CliCommand::new( |
165 | ApiMethod::new( | |
166 | list_backups, | |
167 | ObjectSchema::new("List backups.") | |
168 | .required("store", StringSchema::new("Datastore name.")) | |
169 | )) | |
170 | .arg_param(vec!["store"]) | |
171 | .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name); | |
172 | ||
173 | ||
174 | let cmd_def = CliCommandMap::new() | |
175 | .insert("create".to_owned(), create_cmd_def.into()) | |
176 | .insert("list".to_owned(), list_cmd_def.into()); | |
a914a774 | 177 | |
ff5d3707 | 178 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
179 | eprintln!("Error: {}", err); | |
4968bc3a WB |
180 | if err.downcast::<UsageError>().is_ok() { |
181 | print_cli_usage(); | |
182 | } | |
ff5d3707 | 183 | std::process::exit(-1); |
184 | } | |
185 | ||
186 | } |