]>
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::*; | |
ef2f2efb | 8 | use proxmox_backup::api_schema::*; |
dc9a007b | 9 | use proxmox_backup::api_schema::router::*; |
151c6ce2 | 10 | use proxmox_backup::client::*; |
fe0e04c6 DM |
11 | //use proxmox_backup::backup::chunk_store::*; |
12 | //use proxmox_backup::backup::image_index::*; | |
13 | //use proxmox_backup::config::datastore; | |
23bb8780 | 14 | //use proxmox_backup::catar::encoder::*; |
728797d0 | 15 | //use proxmox_backup::backup::datastore::*; |
23bb8780 | 16 | |
43eeef28 | 17 | use serde_json::{Value}; |
23bb8780 | 18 | use hyper::Body; |
33d64b81 | 19 | use std::sync::Arc; |
33d64b81 DM |
20 | |
21 | fn backup_directory(repo: &BackupRepository, body: Body, archive_name: &str) -> Result<(), Error> { | |
22 | ||
0dffe3f9 | 23 | let client = HttpClient::new(&repo.host, &repo.user); |
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 | ||
33d64b81 | 35 | let path = format!("api2/json/admin/datastore/{}/catar?{}", repo.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() { | |
91a905b6 DM |
48 | if ext != "fidx" { |
49 | bail!("got wrong file extension - expected '.fidx'"); | |
bcd879cf DM |
50 | } |
51 | } else { | |
91a905b6 | 52 | target.set_extension("fidx"); |
bcd879cf DM |
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 | 73 | |
33d64b81 DM |
74 | let repo_url = tools::required_string_param(¶m, "repository")?; |
75 | let repo = BackupRepository::parse(repo_url)?; | |
41c039e1 | 76 | |
0dffe3f9 | 77 | let client = HttpClient::new(&repo.host, &repo.user); |
41c039e1 | 78 | |
33d64b81 | 79 | let path = format!("api2/json/admin/datastore/{}/backups", repo.store); |
41c039e1 DM |
80 | |
81 | let result = client.get(&path)?; | |
82 | ||
83 | Ok(result) | |
84 | } | |
85 | ||
33d64b81 | 86 | |
6049b71f DM |
87 | fn create_backup( |
88 | param: Value, | |
89 | _info: &ApiMethod, | |
90 | _rpcenv: &mut RpcEnvironment, | |
91 | ) -> Result<Value, Error> { | |
ff5d3707 | 92 | |
0fe5d605 | 93 | let filename = tools::required_string_param(¶m, "filename")?; |
33d64b81 | 94 | let repo_url = tools::required_string_param(¶m, "repository")?; |
0fe5d605 | 95 | let target = tools::required_string_param(¶m, "target")?; |
a914a774 | 96 | |
33d64b81 DM |
97 | let repo = BackupRepository::parse(repo_url)?; |
98 | ||
728797d0 | 99 | let mut _chunk_size = 4*1024*1024; |
2d9d143a DM |
100 | |
101 | if let Some(size) = param["chunk-size"].as_u64() { | |
102 | static SIZES: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096]; | |
103 | ||
104 | if SIZES.contains(&size) { | |
728797d0 | 105 | _chunk_size = (size as usize) * 1024; |
2d9d143a DM |
106 | } else { |
107 | bail!("Got unsupported chunk size '{}'", size); | |
108 | } | |
109 | } | |
110 | ||
23bb8780 DM |
111 | let stat = match nix::sys::stat::stat(filename) { |
112 | Ok(s) => s, | |
113 | Err(err) => bail!("unable to access '{}' - {}", filename, err), | |
114 | }; | |
a914a774 | 115 | |
bcd879cf | 116 | if (stat.st_mode & libc::S_IFDIR) != 0 { |
33d64b81 | 117 | println!("Backup directory '{}' to '{:?}'", filename, repo); |
bcd879cf | 118 | |
23bb8780 DM |
119 | let stream = CaTarBackupStream::open(filename)?; |
120 | ||
121 | let body = Body::wrap_stream(stream); | |
fb8365b7 | 122 | |
33d64b81 | 123 | backup_directory(&repo, body, target)?; |
bcd879cf DM |
124 | |
125 | } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 { | |
33d64b81 | 126 | println!("Backup image '{}' to '{:?}'", filename, repo); |
606ce64b | 127 | |
4818c8b6 | 128 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } |
728797d0 | 129 | let _size = stat.st_size as usize; |
a914a774 | 130 | |
23bb8780 DM |
131 | panic!("implement me"); |
132 | ||
133 | //backup_image(&datastore, &file, size, &target, chunk_size)?; | |
d62e6e22 | 134 | |
594fa520 DM |
135 | // let idx = datastore.open_image_reader(target)?; |
136 | // idx.print_info(); | |
4818c8b6 | 137 | |
bcd879cf DM |
138 | } else { |
139 | bail!("unsupported file type (expected a directory, file or block device)"); | |
4818c8b6 DM |
140 | } |
141 | ||
f0819fe5 | 142 | //datastore.garbage_collection()?; |
3d5c11e5 | 143 | |
ff5d3707 | 144 | Ok(Value::Null) |
145 | } | |
146 | ||
ff5d3707 | 147 | fn main() { |
148 | ||
33d64b81 DM |
149 | let repo_url_schema: Arc<Schema> = Arc::new( |
150 | StringSchema::new("Repository URL.") | |
151 | .format(BACKUP_REPO_URL.clone()) | |
152 | .max_length(256) | |
153 | .into() | |
154 | ); | |
155 | ||
41c039e1 | 156 | let create_cmd_def = CliCommand::new( |
ff5d3707 | 157 | ApiMethod::new( |
bcd879cf DM |
158 | create_backup, |
159 | ObjectSchema::new("Create backup.") | |
33d64b81 | 160 | .required("repository", repo_url_schema.clone()) |
bcd879cf | 161 | .required("filename", StringSchema::new("Source name (file or directory name)")) |
c34eb166 | 162 | .required("target", StringSchema::new("Target name.")) |
2d9d143a DM |
163 | .optional( |
164 | "chunk-size", | |
165 | IntegerSchema::new("Chunk size in KB. Must be a power of 2.") | |
166 | .minimum(64) | |
167 | .maximum(4096) | |
168 | .default(4096) | |
169 | ) | |
ff5d3707 | 170 | )) |
33d64b81 DM |
171 | .arg_param(vec!["repository", "filename", "target"]) |
172 | .completion_cb("filename", tools::complete_file_name); | |
f8838fe9 | 173 | |
41c039e1 DM |
174 | let list_cmd_def = CliCommand::new( |
175 | ApiMethod::new( | |
176 | list_backups, | |
177 | ObjectSchema::new("List backups.") | |
33d64b81 | 178 | .required("repository", repo_url_schema.clone()) |
41c039e1 | 179 | )) |
33d64b81 | 180 | .arg_param(vec!["repository"]); |
41c039e1 DM |
181 | |
182 | let cmd_def = CliCommandMap::new() | |
183 | .insert("create".to_owned(), create_cmd_def.into()) | |
184 | .insert("list".to_owned(), list_cmd_def.into()); | |
a914a774 | 185 | |
ff5d3707 | 186 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
187 | eprintln!("Error: {}", err); | |
4968bc3a WB |
188 | if err.downcast::<UsageError>().is_ok() { |
189 | print_cli_usage(); | |
190 | } | |
ff5d3707 | 191 | std::process::exit(-1); |
192 | } | |
193 | ||
194 | } |