]> git.proxmox.com Git - proxmox-backup.git/blob - src/bin/proxmox-backup-client.rs
src/backup/chunker.rs: start() - correctly store hash in self.h
[proxmox-backup.git] / src / bin / proxmox-backup-client.rs
1 extern crate proxmox_backup;
2
3 use failure::*;
4 //use std::os::unix::io::AsRawFd;
5
6 use proxmox_backup::tools;
7 use proxmox_backup::cli::command::*;
8 use proxmox_backup::api_schema::*;
9 use proxmox_backup::api_schema::router::*;
10 use proxmox_backup::client::*;
11 //use proxmox_backup::backup::chunk_store::*;
12 //use proxmox_backup::backup::image_index::*;
13 //use proxmox_backup::config::datastore;
14 //use proxmox_backup::catar::encoder::*;
15 //use proxmox_backup::backup::datastore::*;
16
17 use serde_json::{Value};
18 use hyper::Body;
19 use std::sync::Arc;
20
21 fn backup_directory(repo: &BackupRepository, body: Body, archive_name: &str) -> Result<(), Error> {
22
23 let client = HttpClient::new(&repo.host, &repo.user);
24
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
35 let path = format!("api2/json/admin/datastore/{}/catar?{}", repo.store, query);
36
37 client.upload("application/x-proxmox-backup-catar", body, &path)?;
38
39 Ok(())
40 }
41
42 /****
43 fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> {
44
45 let mut target = PathBuf::from(target);
46
47 if let Some(ext) = target.extension() {
48 if ext != "fidx" {
49 bail!("got wrong file extension - expected '.fidx'");
50 }
51 } else {
52 target.set_extension("fidx");
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 }
66 */
67
68 fn list_backups(
69 param: Value,
70 _info: &ApiMethod,
71 _rpcenv: &mut RpcEnvironment,
72 ) -> Result<Value, Error> {
73
74 let repo_url = tools::required_string_param(&param, "repository")?;
75 let repo = BackupRepository::parse(repo_url)?;
76
77 let client = HttpClient::new(&repo.host, &repo.user);
78
79 let path = format!("api2/json/admin/datastore/{}/backups", repo.store);
80
81 let result = client.get(&path)?;
82
83 Ok(result)
84 }
85
86
87 fn create_backup(
88 param: Value,
89 _info: &ApiMethod,
90 _rpcenv: &mut RpcEnvironment,
91 ) -> Result<Value, Error> {
92
93 let filename = tools::required_string_param(&param, "filename")?;
94 let repo_url = tools::required_string_param(&param, "repository")?;
95 let target = tools::required_string_param(&param, "target")?;
96
97 let repo = BackupRepository::parse(repo_url)?;
98
99 let mut _chunk_size = 4*1024*1024;
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) {
105 _chunk_size = (size as usize) * 1024;
106 } else {
107 bail!("Got unsupported chunk size '{}'", size);
108 }
109 }
110
111 let stat = match nix::sys::stat::stat(filename) {
112 Ok(s) => s,
113 Err(err) => bail!("unable to access '{}' - {}", filename, err),
114 };
115
116 if (stat.st_mode & libc::S_IFDIR) != 0 {
117 println!("Backup directory '{}' to '{:?}'", filename, repo);
118
119 let stream = CaTarBackupStream::open(filename)?;
120
121 let body = Body::wrap_stream(stream);
122
123 backup_directory(&repo, body, target)?;
124
125 } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 {
126 println!("Backup image '{}' to '{:?}'", filename, repo);
127
128 if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
129 let _size = stat.st_size as usize;
130
131 panic!("implement me");
132
133 //backup_image(&datastore, &file, size, &target, chunk_size)?;
134
135 // let idx = datastore.open_image_reader(target)?;
136 // idx.print_info();
137
138 } else {
139 bail!("unsupported file type (expected a directory, file or block device)");
140 }
141
142 //datastore.garbage_collection()?;
143
144 Ok(Value::Null)
145 }
146
147 fn main() {
148
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
156 let create_cmd_def = CliCommand::new(
157 ApiMethod::new(
158 create_backup,
159 ObjectSchema::new("Create backup.")
160 .required("repository", repo_url_schema.clone())
161 .required("filename", StringSchema::new("Source name (file or directory name)"))
162 .required("target", StringSchema::new("Target name."))
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 )
170 ))
171 .arg_param(vec!["repository", "filename", "target"])
172 .completion_cb("filename", tools::complete_file_name);
173
174 let list_cmd_def = CliCommand::new(
175 ApiMethod::new(
176 list_backups,
177 ObjectSchema::new("List backups.")
178 .required("repository", repo_url_schema.clone())
179 ))
180 .arg_param(vec!["repository"]);
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());
185
186 if let Err(err) = run_cli_command(&cmd_def.into()) {
187 eprintln!("Error: {}", err);
188 if err.downcast::<UsageError>().is_ok() {
189 print_cli_usage();
190 }
191 std::process::exit(-1);
192 }
193
194 }