]>
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::*; |
247cdbce | 11 | use proxmox_backup::backup::*; |
fe0e04c6 DM |
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 | 20 | |
247cdbce DM |
21 | fn backup_directory( |
22 | repo: &BackupRepository, | |
23 | body: Body, | |
24 | archive_name: &str, | |
25 | chunk_size: Option<u64>, | |
26 | ) -> Result<(), Error> { | |
33d64b81 | 27 | |
0dffe3f9 | 28 | let client = HttpClient::new(&repo.host, &repo.user); |
fb8365b7 | 29 | |
ff3d3100 DM |
30 | let epoch = std::time::SystemTime::now().duration_since( |
31 | std::time::SystemTime::UNIX_EPOCH)?.as_secs(); | |
32 | ||
247cdbce DM |
33 | let mut query = url::form_urlencoded::Serializer::new(String::new()); |
34 | ||
35 | query | |
ff3d3100 DM |
36 | .append_pair("archive_name", archive_name) |
37 | .append_pair("type", "host") | |
38 | .append_pair("id", &tools::nodename()) | |
247cdbce DM |
39 | .append_pair("time", &epoch.to_string()); |
40 | ||
41 | if let Some(size) = chunk_size { | |
42 | query.append_pair("chunk-size", &size.to_string()); | |
43 | } | |
44 | ||
45 | let query = query.finish(); | |
ff3d3100 | 46 | |
33d64b81 | 47 | let path = format!("api2/json/admin/datastore/{}/catar?{}", repo.store, query); |
5e7a09be | 48 | |
83bdac1e | 49 | client.upload("application/x-proxmox-backup-catar", body, &path)?; |
bcd879cf DM |
50 | |
51 | Ok(()) | |
52 | } | |
53 | ||
23bb8780 | 54 | /**** |
bcd879cf DM |
55 | fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> { |
56 | ||
23bb8780 | 57 | let mut target = PathBuf::from(target); |
bcd879cf DM |
58 | |
59 | if let Some(ext) = target.extension() { | |
91a905b6 DM |
60 | if ext != "fidx" { |
61 | bail!("got wrong file extension - expected '.fidx'"); | |
bcd879cf DM |
62 | } |
63 | } else { | |
91a905b6 | 64 | target.set_extension("fidx"); |
bcd879cf DM |
65 | } |
66 | ||
67 | let mut index = datastore.create_image_writer(&target, size, chunk_size)?; | |
68 | ||
69 | tools::file_chunker(file, chunk_size, |pos, chunk| { | |
70 | index.add_chunk(pos, chunk)?; | |
71 | Ok(true) | |
72 | })?; | |
73 | ||
74 | index.close()?; // commit changes | |
75 | ||
76 | Ok(()) | |
77 | } | |
23bb8780 | 78 | */ |
bcd879cf | 79 | |
6049b71f DM |
80 | fn list_backups( |
81 | param: Value, | |
82 | _info: &ApiMethod, | |
83 | _rpcenv: &mut RpcEnvironment, | |
84 | ) -> Result<Value, Error> { | |
41c039e1 | 85 | |
33d64b81 DM |
86 | let repo_url = tools::required_string_param(¶m, "repository")?; |
87 | let repo = BackupRepository::parse(repo_url)?; | |
41c039e1 | 88 | |
0dffe3f9 | 89 | let client = HttpClient::new(&repo.host, &repo.user); |
41c039e1 | 90 | |
33d64b81 | 91 | let path = format!("api2/json/admin/datastore/{}/backups", repo.store); |
41c039e1 DM |
92 | |
93 | let result = client.get(&path)?; | |
94 | ||
95 | Ok(result) | |
96 | } | |
97 | ||
33d64b81 | 98 | |
6049b71f DM |
99 | fn create_backup( |
100 | param: Value, | |
101 | _info: &ApiMethod, | |
102 | _rpcenv: &mut RpcEnvironment, | |
103 | ) -> Result<Value, Error> { | |
ff5d3707 | 104 | |
0fe5d605 | 105 | let filename = tools::required_string_param(¶m, "filename")?; |
33d64b81 | 106 | let repo_url = tools::required_string_param(¶m, "repository")?; |
0fe5d605 | 107 | let target = tools::required_string_param(¶m, "target")?; |
a914a774 | 108 | |
33d64b81 DM |
109 | let repo = BackupRepository::parse(repo_url)?; |
110 | ||
247cdbce | 111 | let chunk_size_opt = param["chunk-size"].as_u64().map(|v| v*1024); |
2d9d143a | 112 | |
247cdbce DM |
113 | if let Some(size) = chunk_size_opt { |
114 | verify_chunk_size(size)?; | |
2d9d143a DM |
115 | } |
116 | ||
23bb8780 DM |
117 | let stat = match nix::sys::stat::stat(filename) { |
118 | Ok(s) => s, | |
119 | Err(err) => bail!("unable to access '{}' - {}", filename, err), | |
120 | }; | |
a914a774 | 121 | |
bcd879cf | 122 | if (stat.st_mode & libc::S_IFDIR) != 0 { |
33d64b81 | 123 | println!("Backup directory '{}' to '{:?}'", filename, repo); |
bcd879cf | 124 | |
23bb8780 DM |
125 | let stream = CaTarBackupStream::open(filename)?; |
126 | ||
127 | let body = Body::wrap_stream(stream); | |
fb8365b7 | 128 | |
247cdbce | 129 | backup_directory(&repo, body, target, chunk_size_opt)?; |
bcd879cf DM |
130 | |
131 | } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 { | |
33d64b81 | 132 | println!("Backup image '{}' to '{:?}'", filename, repo); |
606ce64b | 133 | |
4818c8b6 | 134 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } |
728797d0 | 135 | let _size = stat.st_size as usize; |
a914a774 | 136 | |
23bb8780 DM |
137 | panic!("implement me"); |
138 | ||
139 | //backup_image(&datastore, &file, size, &target, chunk_size)?; | |
d62e6e22 | 140 | |
594fa520 DM |
141 | // let idx = datastore.open_image_reader(target)?; |
142 | // idx.print_info(); | |
4818c8b6 | 143 | |
bcd879cf DM |
144 | } else { |
145 | bail!("unsupported file type (expected a directory, file or block device)"); | |
4818c8b6 DM |
146 | } |
147 | ||
f0819fe5 | 148 | //datastore.garbage_collection()?; |
3d5c11e5 | 149 | |
ff5d3707 | 150 | Ok(Value::Null) |
151 | } | |
152 | ||
ff5d3707 | 153 | fn main() { |
154 | ||
33d64b81 DM |
155 | let repo_url_schema: Arc<Schema> = Arc::new( |
156 | StringSchema::new("Repository URL.") | |
157 | .format(BACKUP_REPO_URL.clone()) | |
158 | .max_length(256) | |
159 | .into() | |
160 | ); | |
161 | ||
41c039e1 | 162 | let create_cmd_def = CliCommand::new( |
ff5d3707 | 163 | ApiMethod::new( |
bcd879cf DM |
164 | create_backup, |
165 | ObjectSchema::new("Create backup.") | |
33d64b81 | 166 | .required("repository", repo_url_schema.clone()) |
bcd879cf | 167 | .required("filename", StringSchema::new("Source name (file or directory name)")) |
c34eb166 | 168 | .required("target", StringSchema::new("Target name.")) |
2d9d143a DM |
169 | .optional( |
170 | "chunk-size", | |
171 | IntegerSchema::new("Chunk size in KB. Must be a power of 2.") | |
172 | .minimum(64) | |
173 | .maximum(4096) | |
174 | .default(4096) | |
175 | ) | |
ff5d3707 | 176 | )) |
33d64b81 DM |
177 | .arg_param(vec!["repository", "filename", "target"]) |
178 | .completion_cb("filename", tools::complete_file_name); | |
f8838fe9 | 179 | |
41c039e1 DM |
180 | let list_cmd_def = CliCommand::new( |
181 | ApiMethod::new( | |
182 | list_backups, | |
183 | ObjectSchema::new("List backups.") | |
33d64b81 | 184 | .required("repository", repo_url_schema.clone()) |
41c039e1 | 185 | )) |
33d64b81 | 186 | .arg_param(vec!["repository"]); |
41c039e1 DM |
187 | |
188 | let cmd_def = CliCommandMap::new() | |
189 | .insert("create".to_owned(), create_cmd_def.into()) | |
190 | .insert("list".to_owned(), list_cmd_def.into()); | |
a914a774 | 191 | |
ff5d3707 | 192 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
193 | eprintln!("Error: {}", err); | |
4968bc3a WB |
194 | if err.downcast::<UsageError>().is_ok() { |
195 | print_cli_usage(); | |
196 | } | |
ff5d3707 | 197 | std::process::exit(-1); |
198 | } | |
199 | ||
200 | } |