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