]> git.proxmox.com Git - proxmox-backup.git/blame - src/api2/admin/datastore.rs
pxar: introduce fs_feature_flags obtained from filesystem magic in Encoder
[proxmox-backup.git] / src / api2 / admin / datastore.rs
CommitLineData
15e9b4ed
DM
1use failure::*;
2
184f17af 3use crate::tools;
ef2f2efb 4use crate::api_schema::*;
dc9a007b 5use crate::api_schema::router::*;
2085142e 6//use crate::server::rest::*;
15e9b4ed 7use serde_json::{json, Value};
8f579717 8use std::collections::{HashSet, HashMap};
875fb1c0 9use chrono::{DateTime, Datelike, Local};
38b0dfa5 10use std::path::PathBuf;
812c6f87 11use std::sync::Arc;
15e9b4ed 12
2085142e
DM
13//use hyper::StatusCode;
14//use hyper::rt::{Future, Stream};
7e21da6e 15
15e9b4ed
DM
16use crate::config::datastore;
17
e5064ba6 18use crate::backup::*;
0f778e06 19use crate::server::WorkerTask;
15e9b4ed 20
8968258b 21mod pxar;
1629d2ad 22
8f579717
DM
23fn group_backups(backup_list: Vec<BackupInfo>) -> HashMap<String, Vec<BackupInfo>> {
24
25 let mut group_hash = HashMap::new();
26
27 for info in backup_list {
9b492eb2 28 let group_id = info.backup_dir.group().group_path().to_str().unwrap().to_owned();
8f579717
DM
29 let time_list = group_hash.entry(group_id).or_insert(vec![]);
30 time_list.push(info);
31 }
32
33 group_hash
34}
35
36fn mark_selections<F: Fn(DateTime<Local>, &BackupInfo) -> String> (
38b0dfa5 37 mark: &mut HashSet<PathBuf>,
8f579717
DM
38 list: &Vec<BackupInfo>,
39 keep: usize,
40 select_id: F,
41){
42 let mut hash = HashSet::new();
43 for info in list {
9b492eb2 44 let local_time = info.backup_dir.backup_time().with_timezone(&Local);
8f579717 45 if hash.len() >= keep as usize { break; }
38b0dfa5 46 let backup_id = info.backup_dir.relative_path();
8f579717
DM
47 let sel_id: String = select_id(local_time, &info);
48 if !hash.contains(&sel_id) {
49 hash.insert(sel_id);
50 //println!(" KEEP ID {} {}", backup_id, local_time.format("%c"));
51 mark.insert(backup_id);
52 }
53 }
54}
55
ad20d198 56fn list_groups(
812c6f87
DM
57 param: Value,
58 _info: &ApiMethod,
59 _rpcenv: &mut RpcEnvironment,
60) -> Result<Value, Error> {
61
62 let store = param["store"].as_str().unwrap();
63
64 let datastore = DataStore::lookup_datastore(store)?;
65
c0977501 66 let backup_list = BackupInfo::list_backups(&datastore.base_path())?;
812c6f87
DM
67
68 let group_hash = group_backups(backup_list);
69
70 let mut groups = vec![];
71
72 for (_group_id, mut list) in group_hash {
73
2b01a225 74 BackupInfo::sort_list(&mut list, false);
812c6f87
DM
75
76 let info = &list[0];
9b492eb2 77 let group = info.backup_dir.group();
812c6f87
DM
78
79 groups.push(json!({
1e9a94e5
DM
80 "backup-type": group.backup_type(),
81 "backup-id": group.backup_id(),
9b492eb2 82 "last-backup": info.backup_dir.backup_time().timestamp(),
ad20d198
DM
83 "backup-count": list.len() as u64,
84 "files": info.files,
812c6f87
DM
85 }));
86 }
87
88 Ok(json!(groups))
89}
8f579717 90
01a13423
DM
91fn list_snapshot_files (
92 param: Value,
93 _info: &ApiMethod,
94 _rpcenv: &mut RpcEnvironment,
95) -> Result<Value, Error> {
96
97 let store = tools::required_string_param(&param, "store")?;
98 let backup_type = tools::required_string_param(&param, "backup-type")?;
99 let backup_id = tools::required_string_param(&param, "backup-id")?;
100 let backup_time = tools::required_integer_param(&param, "backup-time")?;
101
102 let snapshot = BackupDir::new(backup_type, backup_id, backup_time);
103
104 let datastore = DataStore::lookup_datastore(store)?;
105
c0977501
DM
106 let path = datastore.base_path();
107 let files = BackupInfo::list_files(&path, &snapshot)?;
01a13423
DM
108
109 Ok(json!(files))
110}
111
6f62c924
DM
112fn delete_snapshots (
113 param: Value,
114 _info: &ApiMethod,
115 _rpcenv: &mut RpcEnvironment,
116) -> Result<Value, Error> {
117
118 let store = tools::required_string_param(&param, "store")?;
119 let backup_type = tools::required_string_param(&param, "backup-type")?;
120 let backup_id = tools::required_string_param(&param, "backup-id")?;
121 let backup_time = tools::required_integer_param(&param, "backup-time")?;
6f62c924 122
391d3107 123 let snapshot = BackupDir::new(backup_type, backup_id, backup_time);
6f62c924
DM
124
125 let datastore = DataStore::lookup_datastore(store)?;
126
127 datastore.remove_backup_dir(&snapshot)?;
128
129 Ok(Value::Null)
130}
131
184f17af
DM
132fn list_snapshots (
133 param: Value,
134 _info: &ApiMethod,
135 _rpcenv: &mut RpcEnvironment,
136) -> Result<Value, Error> {
137
138 let store = tools::required_string_param(&param, "store")?;
139 let backup_type = tools::required_string_param(&param, "backup-type")?;
140 let backup_id = tools::required_string_param(&param, "backup-id")?;
141
1e9a94e5 142 let group = BackupGroup::new(backup_type, backup_id);
184f17af
DM
143
144 let datastore = DataStore::lookup_datastore(store)?;
145
c0977501 146 let base_path = datastore.base_path();
184f17af 147
c0977501 148 let backup_list = group.list_backups(&base_path)?;
184f17af
DM
149
150 let mut snapshots = vec![];
151
c0977501 152 for info in backup_list {
184f17af 153 snapshots.push(json!({
1e9a94e5
DM
154 "backup-type": group.backup_type(),
155 "backup-id": group.backup_id(),
9b492eb2 156 "backup-time": info.backup_dir.backup_time().timestamp(),
184f17af
DM
157 "files": info.files,
158 }));
159 }
160
161 Ok(json!(snapshots))
162}
163
83b7db02
DM
164fn prune(
165 param: Value,
166 _info: &ApiMethod,
167 _rpcenv: &mut RpcEnvironment,
168) -> Result<Value, Error> {
169
170 let store = param["store"].as_str().unwrap();
171
172 let datastore = DataStore::lookup_datastore(store)?;
173
174 println!("Starting prune on store {}", store);
175
c0977501 176 let backup_list = BackupInfo::list_backups(&datastore.base_path())?;
8f579717
DM
177
178 let group_hash = group_backups(backup_list);
179
180 for (_group_id, mut list) in group_hash {
181
182 let mut mark = HashSet::new();
183
2b01a225 184 BackupInfo::sort_list(&mut list, false);
8f579717
DM
185
186 if let Some(keep_last) = param["keep-last"].as_u64() {
187 list.iter().take(keep_last as usize).for_each(|info| {
38b0dfa5 188 mark.insert(info.backup_dir.relative_path());
8f579717
DM
189 });
190 }
191
192 if let Some(keep_daily) = param["keep-daily"].as_u64() {
193 mark_selections(&mut mark, &list, keep_daily as usize, |local_time, _info| {
194 format!("{}/{}/{}", local_time.year(), local_time.month(), local_time.day())
195 });
196 }
83b7db02 197
8f579717
DM
198 if let Some(keep_weekly) = param["keep-weekly"].as_u64() {
199 mark_selections(&mut mark, &list, keep_weekly as usize, |local_time, _info| {
200 format!("{}/{}", local_time.year(), local_time.iso_week().week())
201 });
202 }
203
204 if let Some(keep_monthly) = param["keep-monthly"].as_u64() {
205 mark_selections(&mut mark, &list, keep_monthly as usize, |local_time, _info| {
206 format!("{}/{}", local_time.year(), local_time.month())
207 });
208 }
209
210 if let Some(keep_yearly) = param["keep-yearly"].as_u64() {
211 mark_selections(&mut mark, &list, keep_yearly as usize, |local_time, _info| {
212 format!("{}/{}", local_time.year(), local_time.year())
213 });
214 }
215
2b01a225 216 let mut remove_list: Vec<BackupInfo> = list.into_iter()
38b0dfa5 217 .filter(|info| !mark.contains(&info.backup_dir.relative_path())).collect();
8f579717 218
2b01a225 219 BackupInfo::sort_list(&mut remove_list, true);
8f579717
DM
220
221 for info in remove_list {
38b0dfa5 222 datastore.remove_backup_dir(&info.backup_dir)?;
8f579717
DM
223 }
224 }
83b7db02
DM
225
226 Ok(json!(null))
227}
228
229pub fn add_common_prune_prameters(schema: ObjectSchema) -> ObjectSchema {
230
231 schema
8f579717
DM
232 .optional(
233 "keep-last",
234 IntegerSchema::new("Number of backups to keep.")
235 .minimum(1)
236 )
83b7db02
DM
237 .optional(
238 "keep-daily",
8f579717
DM
239 IntegerSchema::new("Number of daily backups to keep.")
240 .minimum(1)
241 )
242 .optional(
243 "keep-weekly",
244 IntegerSchema::new("Number of weekly backups to keep.")
245 .minimum(1)
246 )
247 .optional(
248 "keep-monthly",
249 IntegerSchema::new("Number of monthly backups to keep.")
250 .minimum(1)
251 )
252 .optional(
253 "keep-yearly",
254 IntegerSchema::new("Number of yearly backups to keep.")
255 .minimum(1)
83b7db02
DM
256 )
257}
258
259fn api_method_prune() -> ApiMethod {
260 ApiMethod::new(
261 prune,
262 add_common_prune_prameters(
263 ObjectSchema::new("Prune the datastore.")
264 .required(
265 "store",
266 StringSchema::new("Datastore name.")
267 )
268 )
269 )
270}
271
6049b71f
DM
272fn start_garbage_collection(
273 param: Value,
274 _info: &ApiMethod,
0f778e06 275 rpcenv: &mut RpcEnvironment,
6049b71f 276) -> Result<Value, Error> {
15e9b4ed 277
3e6a7dee 278 let store = param["store"].as_str().unwrap().to_string();
15e9b4ed 279
3e6a7dee 280 let datastore = DataStore::lookup_datastore(&store)?;
15e9b4ed 281
5a778d92 282 println!("Starting garbage collection on store {}", store);
15e9b4ed 283
0f778e06 284 let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
15e9b4ed 285
0f778e06
DM
286 let upid_str = WorkerTask::new_thread(
287 "garbage_collection", Some(store.clone()), "root@pam", to_stdout, move |worker|
288 {
289 worker.log(format!("starting garbage collection on store {}", store));
d4b59ae0 290 datastore.garbage_collection(worker)
0f778e06
DM
291 })?;
292
293 Ok(json!(upid_str))
15e9b4ed
DM
294}
295
691c89a0
DM
296pub fn api_method_start_garbage_collection() -> ApiMethod {
297 ApiMethod::new(
298 start_garbage_collection,
299 ObjectSchema::new("Start garbage collection.")
5a778d92 300 .required("store", StringSchema::new("Datastore name."))
691c89a0
DM
301 )
302}
303
6049b71f
DM
304fn garbage_collection_status(
305 param: Value,
306 _info: &ApiMethod,
307 _rpcenv: &mut RpcEnvironment,
308) -> Result<Value, Error> {
691c89a0 309
5a778d92 310 let store = param["store"].as_str().unwrap();
691c89a0 311
f2b99c34
DM
312 let datastore = DataStore::lookup_datastore(&store)?;
313
5a778d92 314 println!("Garbage collection status on store {}", store);
691c89a0 315
f2b99c34 316 let status = datastore.last_gc_status();
691c89a0 317
f2b99c34 318 Ok(serde_json::to_value(&status)?)
691c89a0
DM
319}
320
321pub fn api_method_garbage_collection_status() -> ApiMethod {
322 ApiMethod::new(
323 garbage_collection_status,
324 ObjectSchema::new("Garbage collection status.")
5a778d92 325 .required("store", StringSchema::new("Datastore name."))
691c89a0
DM
326 )
327}
328
6049b71f
DM
329fn get_backup_list(
330 param: Value,
331 _info: &ApiMethod,
332 _rpcenv: &mut RpcEnvironment,
333) -> Result<Value, Error> {
83dbd80b 334
9f49fe1d 335 //let config = datastore::config()?;
83dbd80b
DM
336
337 let store = param["store"].as_str().unwrap();
338
339 let datastore = DataStore::lookup_datastore(store)?;
340
341 let mut list = vec![];
342
c0977501
DM
343 let backup_list = BackupInfo::list_backups(&datastore.base_path())?;
344
345 for info in backup_list {
83dbd80b 346 list.push(json!({
9b492eb2
DM
347 "backup-type": info.backup_dir.group().backup_type(),
348 "backup-id": info.backup_dir.group().backup_id(),
349 "backup-time": info.backup_dir.backup_time().timestamp(),
8c75372b 350 "files": info.files,
83dbd80b
DM
351 }));
352 }
353
354 let result = json!(list);
355
356 Ok(result)
357}
7e21da6e 358
6049b71f
DM
359fn get_datastore_list(
360 _param: Value,
361 _info: &ApiMethod,
362 _rpcenv: &mut RpcEnvironment,
363) -> Result<Value, Error> {
15e9b4ed
DM
364
365 let config = datastore::config()?;
366
5a778d92 367 Ok(config.convert_to_array("store"))
15e9b4ed
DM
368}
369
691c89a0 370
15e9b4ed
DM
371pub fn router() -> Router {
372
812c6f87
DM
373 let store_schema: Arc<Schema> = Arc::new(
374 StringSchema::new("Datastore name.").into()
375 );
376
15e9b4ed 377 let datastore_info = Router::new()
83dbd80b
DM
378 .subdir(
379 "backups",
380 Router::new()
381 .get(ApiMethod::new(
382 get_backup_list,
383 ObjectSchema::new("List backups.")
812c6f87 384 .required("store", store_schema.clone()))))
264f52cf 385 .subdir(
8968258b 386 "pxar",
264f52cf 387 Router::new()
8968258b
DM
388 .download(pxar::api_method_download_pxar())
389 .upload(pxar::api_method_upload_pxar()))
15e9b4ed
DM
390 .subdir(
391 "gc",
392 Router::new()
691c89a0 393 .get(api_method_garbage_collection_status())
83b7db02 394 .post(api_method_start_garbage_collection()))
01a13423
DM
395 .subdir(
396 "files",
397 Router::new()
398 .get(
399 ApiMethod::new(
400 list_snapshot_files,
401 ObjectSchema::new("List snapshot files.")
402 .required("store", store_schema.clone())
403 .required("backup-type", StringSchema::new("Backup type."))
404 .required("backup-id", StringSchema::new("Backup ID."))
405 .required("backup-time", IntegerSchema::new("Backup time (Unix epoch.)")
406 .minimum(1547797308))
407 )
408 )
409 )
812c6f87
DM
410 .subdir(
411 "groups",
412 Router::new()
413 .get(ApiMethod::new(
ad20d198 414 list_groups,
812c6f87
DM
415 ObjectSchema::new("List backup groups.")
416 .required("store", store_schema.clone()))))
184f17af
DM
417 .subdir(
418 "snapshots",
419 Router::new()
6f62c924
DM
420 .get(
421 ApiMethod::new(
422 list_snapshots,
423 ObjectSchema::new("List backup groups.")
424 .required("store", store_schema.clone())
425 .required("backup-type", StringSchema::new("Backup type."))
426 .required("backup-id", StringSchema::new("Backup ID."))
427 )
428 )
429 .delete(
430 ApiMethod::new(
431 delete_snapshots,
432 ObjectSchema::new("Delete backup snapshot.")
433 .required("store", store_schema.clone())
434 .required("backup-type", StringSchema::new("Backup type."))
435 .required("backup-id", StringSchema::new("Backup ID."))
436 .required("backup-time", IntegerSchema::new("Backup time (Unix epoch.)")
437 .minimum(1547797308))
438 )
439 )
440 )
83b7db02
DM
441 .subdir(
442 "prune",
443 Router::new()
13f1cc17
DM
444 .post(api_method_prune())
445 )
446 .list_subdirs();
7e21da6e 447
15e9b4ed
DM
448
449
450 let route = Router::new()
451 .get(ApiMethod::new(
452 get_datastore_list,
453 ObjectSchema::new("Directory index.")))
5a778d92 454 .match_all("store", datastore_info);
15e9b4ed
DM
455
456
457
458 route
459}