]> git.proxmox.com Git - proxmox-backup.git/blob - src/bin/proxmox-backup-manager.rs
pull: allow pulling groups selectively
[proxmox-backup.git] / src / bin / proxmox-backup-manager.rs
1 use std::collections::HashMap;
2 use std::io::{self, Write};
3
4 use anyhow::{format_err, Error};
5 use serde_json::{json, Value};
6
7 use proxmox::tools::fs::CreateOptions;
8 use proxmox_router::{cli::*, RpcEnvironment};
9 use proxmox_schema::api;
10
11 use pbs_client::{display_task_log, view_task_result};
12 use pbs_tools::percent_encoding::percent_encode_component;
13 use pbs_tools::json::required_string_param;
14 use pbs_api_types::{
15 GroupFilter,
16 DATASTORE_SCHEMA, GROUP_FILTER_LIST_SCHEMA, IGNORE_VERIFIED_BACKUPS_SCHEMA, REMOTE_ID_SCHEMA,
17 REMOVE_VANISHED_BACKUPS_SCHEMA, UPID_SCHEMA, VERIFICATION_OUTDATED_AFTER_SCHEMA,
18 };
19
20 use proxmox_rest_server::wait_for_local_worker;
21
22 use proxmox_backup::api2;
23 use proxmox_backup::client_helpers::connect_to_localhost;
24 use proxmox_backup::config;
25
26 mod proxmox_backup_manager;
27 use proxmox_backup_manager::*;
28
29 #[api(
30 input: {
31 properties: {
32 store: {
33 schema: DATASTORE_SCHEMA,
34 },
35 "output-format": {
36 schema: OUTPUT_FORMAT,
37 optional: true,
38 },
39 }
40 }
41 )]
42 /// Start garbage collection for a specific datastore.
43 async fn start_garbage_collection(param: Value) -> Result<Value, Error> {
44
45 let output_format = get_output_format(&param);
46
47 let store = required_string_param(&param, "store")?;
48
49 let mut client = connect_to_localhost()?;
50
51 let path = format!("api2/json/admin/datastore/{}/gc", store);
52
53 let result = client.post(&path, None).await?;
54
55 view_task_result(&mut client, result, &output_format).await?;
56
57 Ok(Value::Null)
58 }
59
60 #[api(
61 input: {
62 properties: {
63 store: {
64 schema: DATASTORE_SCHEMA,
65 },
66 "output-format": {
67 schema: OUTPUT_FORMAT,
68 optional: true,
69 },
70 }
71 }
72 )]
73 /// Show garbage collection status for a specific datastore.
74 async fn garbage_collection_status(param: Value) -> Result<Value, Error> {
75
76 let output_format = get_output_format(&param);
77
78 let store = required_string_param(&param, "store")?;
79
80 let client = connect_to_localhost()?;
81
82 let path = format!("api2/json/admin/datastore/{}/gc", store);
83
84 let mut result = client.get(&path, None).await?;
85 let mut data = result["data"].take();
86 let return_type = &api2::admin::datastore::API_METHOD_GARBAGE_COLLECTION_STATUS.returns;
87
88 let options = default_table_format_options();
89
90 format_and_print_result_full(&mut data, return_type, &output_format, &options);
91
92 Ok(Value::Null)
93 }
94
95 fn garbage_collection_commands() -> CommandLineInterface {
96
97 let cmd_def = CliCommandMap::new()
98 .insert("status",
99 CliCommand::new(&API_METHOD_GARBAGE_COLLECTION_STATUS)
100 .arg_param(&["store"])
101 .completion_cb("store", pbs_config::datastore::complete_datastore_name)
102 )
103 .insert("start",
104 CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION)
105 .arg_param(&["store"])
106 .completion_cb("store", pbs_config::datastore::complete_datastore_name)
107 );
108
109 cmd_def.into()
110 }
111
112 #[api(
113 input: {
114 properties: {
115 limit: {
116 description: "The maximal number of tasks to list.",
117 type: Integer,
118 optional: true,
119 minimum: 1,
120 maximum: 1000,
121 default: 50,
122 },
123 "output-format": {
124 schema: OUTPUT_FORMAT,
125 optional: true,
126 },
127 all: {
128 type: Boolean,
129 description: "Also list stopped tasks.",
130 optional: true,
131 }
132 }
133 }
134 )]
135 /// List running server tasks.
136 async fn task_list(param: Value) -> Result<Value, Error> {
137
138 let output_format = get_output_format(&param);
139
140 let client = connect_to_localhost()?;
141
142 let limit = param["limit"].as_u64().unwrap_or(50) as usize;
143 let running = !param["all"].as_bool().unwrap_or(false);
144 let args = json!({
145 "running": running,
146 "start": 0,
147 "limit": limit,
148 });
149 let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?;
150
151 let mut data = result["data"].take();
152 let return_type = &api2::node::tasks::API_METHOD_LIST_TASKS.returns;
153
154 use pbs_tools::format::{render_epoch, render_task_status};
155 let options = default_table_format_options()
156 .column(ColumnConfig::new("starttime").right_align(false).renderer(render_epoch))
157 .column(ColumnConfig::new("endtime").right_align(false).renderer(render_epoch))
158 .column(ColumnConfig::new("upid"))
159 .column(ColumnConfig::new("status").renderer(render_task_status));
160
161 format_and_print_result_full(&mut data, return_type, &output_format, &options);
162
163 Ok(Value::Null)
164 }
165
166 #[api(
167 input: {
168 properties: {
169 upid: {
170 schema: UPID_SCHEMA,
171 },
172 }
173 }
174 )]
175 /// Display the task log.
176 async fn task_log(param: Value) -> Result<Value, Error> {
177
178 let upid = required_string_param(&param, "upid")?;
179
180 let mut client = connect_to_localhost()?;
181
182 display_task_log(&mut client, upid, true).await?;
183
184 Ok(Value::Null)
185 }
186
187 #[api(
188 input: {
189 properties: {
190 upid: {
191 schema: UPID_SCHEMA,
192 },
193 }
194 }
195 )]
196 /// Try to stop a specific task.
197 async fn task_stop(param: Value) -> Result<Value, Error> {
198
199 let upid_str = required_string_param(&param, "upid")?;
200
201 let mut client = connect_to_localhost()?;
202
203 let path = format!("api2/json/nodes/localhost/tasks/{}", percent_encode_component(upid_str));
204 let _ = client.delete(&path, None).await?;
205
206 Ok(Value::Null)
207 }
208
209 fn task_mgmt_cli() -> CommandLineInterface {
210
211 let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG)
212 .arg_param(&["upid"]);
213
214 let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP)
215 .arg_param(&["upid"]);
216
217 let cmd_def = CliCommandMap::new()
218 .insert("list", CliCommand::new(&API_METHOD_TASK_LIST))
219 .insert("log", task_log_cmd_def)
220 .insert("stop", task_stop_cmd_def);
221
222 cmd_def.into()
223 }
224
225 // fixme: avoid API redefinition
226 #[api(
227 input: {
228 properties: {
229 "local-store": {
230 schema: DATASTORE_SCHEMA,
231 },
232 remote: {
233 schema: REMOTE_ID_SCHEMA,
234 },
235 "remote-store": {
236 schema: DATASTORE_SCHEMA,
237 },
238 "remove-vanished": {
239 schema: REMOVE_VANISHED_BACKUPS_SCHEMA,
240 optional: true,
241 },
242 "groups": {
243 schema: GROUP_FILTER_LIST_SCHEMA,
244 optional: true,
245 },
246 "output-format": {
247 schema: OUTPUT_FORMAT,
248 optional: true,
249 },
250 }
251 }
252 )]
253 /// Sync datastore from another repository
254 async fn pull_datastore(
255 remote: String,
256 remote_store: String,
257 local_store: String,
258 remove_vanished: Option<bool>,
259 groups: Option<Vec<GroupFilter>>,
260 param: Value,
261 ) -> Result<Value, Error> {
262
263 let output_format = get_output_format(&param);
264
265 let mut client = connect_to_localhost()?;
266
267 let mut args = json!({
268 "store": local_store,
269 "remote": remote,
270 "remote-store": remote_store,
271 });
272
273 if groups.is_some() {
274 args["groups"] = json!(groups);
275 }
276
277 if let Some(remove_vanished) = remove_vanished {
278 args["remove-vanished"] = Value::from(remove_vanished);
279 }
280
281 let result = client.post("api2/json/pull", Some(args)).await?;
282
283 view_task_result(&mut client, result, &output_format).await?;
284
285 Ok(Value::Null)
286 }
287
288 #[api(
289 input: {
290 properties: {
291 "store": {
292 schema: DATASTORE_SCHEMA,
293 },
294 "ignore-verified": {
295 schema: IGNORE_VERIFIED_BACKUPS_SCHEMA,
296 optional: true,
297 },
298 "outdated-after": {
299 schema: VERIFICATION_OUTDATED_AFTER_SCHEMA,
300 optional: true,
301 },
302 "output-format": {
303 schema: OUTPUT_FORMAT,
304 optional: true,
305 },
306 }
307 }
308 )]
309 /// Verify backups
310 async fn verify(
311 store: String,
312 mut param: Value,
313 ) -> Result<Value, Error> {
314
315 let output_format = extract_output_format(&mut param);
316
317 let mut client = connect_to_localhost()?;
318
319 let args = json!(param);
320
321 let path = format!("api2/json/admin/datastore/{}/verify", store);
322
323 let result = client.post(&path, Some(args)).await?;
324
325 view_task_result(&mut client, result, &output_format).await?;
326
327 Ok(Value::Null)
328 }
329
330 #[api()]
331 /// System report
332 async fn report() -> Result<Value, Error> {
333 let report = proxmox_backup::server::generate_report();
334 io::stdout().write_all(report.as_bytes())?;
335 Ok(Value::Null)
336 }
337
338 #[api(
339 input: {
340 properties: {
341 verbose: {
342 type: Boolean,
343 optional: true,
344 default: false,
345 description: "Output verbose package information. It is ignored if output-format is specified.",
346 },
347 "output-format": {
348 schema: OUTPUT_FORMAT,
349 optional: true,
350 }
351 }
352 }
353 )]
354 /// List package versions for important Proxmox Backup Server packages.
355 async fn get_versions(verbose: bool, param: Value) -> Result<Value, Error> {
356 let output_format = get_output_format(&param);
357
358 let packages = crate::api2::node::apt::get_versions()?;
359 let mut packages = json!(if verbose { &packages[..] } else { &packages[1..2] });
360
361 let options = default_table_format_options()
362 .disable_sort()
363 .noborder(true) // just not helpful for version info which gets copy pasted often
364 .column(ColumnConfig::new("Package"))
365 .column(ColumnConfig::new("Version"))
366 .column(ColumnConfig::new("ExtraInfo").header("Extra Info"))
367 ;
368 let return_type = &crate::api2::node::apt::API_METHOD_GET_VERSIONS.returns;
369
370 format_and_print_result_full(&mut packages, return_type, &output_format, &options);
371
372 Ok(Value::Null)
373 }
374
375 async fn run() -> Result<(), Error> {
376
377 let cmd_def = CliCommandMap::new()
378 .insert("acl", acl_commands())
379 .insert("datastore", datastore_commands())
380 .insert("disk", disk_commands())
381 .insert("dns", dns_commands())
382 .insert("network", network_commands())
383 .insert("node", node_commands())
384 .insert("user", user_commands())
385 .insert("openid", openid_commands())
386 .insert("remote", remote_commands())
387 .insert("traffic-control", traffic_control_commands())
388 .insert("garbage-collection", garbage_collection_commands())
389 .insert("acme", acme_mgmt_cli())
390 .insert("cert", cert_mgmt_cli())
391 .insert("subscription", subscription_commands())
392 .insert("sync-job", sync_job_commands())
393 .insert("verify-job", verify_job_commands())
394 .insert("task", task_mgmt_cli())
395 .insert(
396 "pull",
397 CliCommand::new(&API_METHOD_PULL_DATASTORE)
398 .arg_param(&["remote", "remote-store", "local-store"])
399 .completion_cb("local-store", pbs_config::datastore::complete_datastore_name)
400 .completion_cb("remote", pbs_config::remote::complete_remote_name)
401 .completion_cb("remote-store", complete_remote_datastore_name)
402 )
403 .insert(
404 "verify",
405 CliCommand::new(&API_METHOD_VERIFY)
406 .arg_param(&["store"])
407 .completion_cb("store", pbs_config::datastore::complete_datastore_name)
408 )
409 .insert("report",
410 CliCommand::new(&API_METHOD_REPORT)
411 )
412 .insert("versions",
413 CliCommand::new(&API_METHOD_GET_VERSIONS)
414 );
415
416 let args: Vec<String> = std::env::args().take(2).collect();
417 let avoid_init = args.len() >= 2 && (args[1] == "bashcomplete" || args[1] == "printdoc");
418
419 if !avoid_init {
420 let backup_user = pbs_config::backup_user()?;
421 let file_opts = CreateOptions::new().owner(backup_user.uid).group(backup_user.gid);
422 proxmox_rest_server::init_worker_tasks(pbs_buildcfg::PROXMOX_BACKUP_LOG_DIR_M!().into(), file_opts.clone())?;
423
424 let mut commando_sock = proxmox_rest_server::CommandSocket::new(proxmox_rest_server::our_ctrl_sock(), backup_user.gid);
425 proxmox_rest_server::register_task_control_commands(&mut commando_sock)?;
426 commando_sock.spawn()?;
427 }
428
429 let mut rpcenv = CliEnvironment::new();
430 rpcenv.set_auth_id(Some(String::from("root@pam")));
431
432 run_async_cli_command(cmd_def, rpcenv).await; // this call exit(-1) on error
433
434 Ok(())
435 }
436
437 fn main() -> Result<(), Error> {
438
439 proxmox_backup::tools::setup_safe_path_env();
440
441 pbs_runtime::main(run())
442 }
443
444 // shell completion helper
445 pub fn complete_remote_datastore_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
446
447 let mut list = Vec::new();
448
449 let _ = proxmox_lang::try_block!({
450 let remote = param.get("remote").ok_or_else(|| format_err!("no remote"))?;
451
452 let data = pbs_runtime::block_on(async move {
453 crate::api2::config::remote::scan_remote_datastores(remote.clone()).await
454 })?;
455
456 for item in data {
457 list.push(item.store);
458 }
459
460 Ok(())
461 }).map_err(|_err: Error| { /* ignore */ });
462
463 list
464 }