]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/backup-client.rs
src/backup/archive_index.rs: implement flush()
[proxmox-backup.git] / src / bin / backup-client.rs
CommitLineData
fe0e04c6 1extern crate proxmox_backup;
ff5d3707 2
3use failure::*;
606ce64b 4use 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::*;
10//use proxmox_backup::backup::chunk_store::*;
11//use proxmox_backup::backup::image_index::*;
12//use proxmox_backup::config::datastore;
fb8365b7 13use proxmox_backup::catar::encoder::*;
fe0e04c6 14use proxmox_backup::backup::datastore::*;
43eeef28 15use serde_json::{Value};
ff5d3707 16
a914a774 17fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str {
18 param[name].as_str().expect(&format!("missing parameter '{}'", name))
19}
20
fb8365b7
DM
21fn backup_dir(
22 datastore: &DataStore,
23 path: &str,
24 dir: &mut nix::dir::Dir,
25 target: &str,
26 chunk_size: usize,
27) -> Result<(), Error> {
bcd879cf
DM
28
29 let mut target = std::path::PathBuf::from(target);
30
31 if let Some(ext) = target.extension() {
32 if ext != "aidx" {
33 bail!("got wrong file extension - expected '.aidx'");
34 }
35 } else {
36 target.set_extension("aidx");
37 }
38
fb8365b7 39 // fixme: implement chunked writer
1c287cb1
DM
40 // let writer = std::fs::OpenOptions::new()
41 // .create(true)
42 // .write(true)
43 // .truncate(true)
44 // .open("mytest.catar")?;
45
94a882e9 46 let index = datastore.create_archive_writer(&target, chunk_size)?;
fb8365b7
DM
47
48 let path = std::path::PathBuf::from(path);
49
0433db19 50 CaTarEncoder::encode(path, dir, index)?;
bcd879cf
DM
51
52 Ok(())
53}
54
55fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> {
56
57 let mut target = std::path::PathBuf::from(target);
58
59 if let Some(ext) = target.extension() {
60 if ext != "iidx" {
61 bail!("got wrong file extension - expected '.iidx'");
62 }
63 } else {
64 target.set_extension("iidx");
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}
78
79fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
ff5d3707 80
a914a774 81 let filename = required_string_param(&param, "filename");
82 let store = required_string_param(&param, "store");
c34eb166 83 let target = required_string_param(&param, "target");
a914a774 84
2d9d143a
DM
85 let mut chunk_size = 4*1024*1024;
86
87 if let Some(size) = param["chunk-size"].as_u64() {
88 static SIZES: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096];
89
90 if SIZES.contains(&size) {
91 chunk_size = (size as usize) * 1024;
92 } else {
93 bail!("Got unsupported chunk size '{}'", size);
94 }
95 }
96
bcd879cf 97 let datastore = DataStore::open(store)?;
a914a774 98
bcd879cf 99 let file = std::fs::File::open(filename)?;
fb8365b7
DM
100 let rawfd = file.as_raw_fd();
101 let stat = nix::sys::stat::fstat(rawfd)?;
a914a774 102
bcd879cf
DM
103 if (stat.st_mode & libc::S_IFDIR) != 0 {
104 println!("Backup directory '{}' to '{}'", filename, store);
105
fb8365b7
DM
106 let mut dir = nix::dir::Dir::from_fd(rawfd)?;
107
108 backup_dir(&datastore, &filename, &mut dir, &target, chunk_size)?;
bcd879cf
DM
109
110 } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 {
111 println!("Backup file '{}' to '{}'", filename, store);
606ce64b 112
4818c8b6
DM
113 if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
114 let size = stat.st_size as usize;
a914a774 115
bcd879cf 116 backup_image(&datastore, &file, size, &target, chunk_size)?;
d62e6e22 117
bcd879cf
DM
118 let idx = datastore.open_image_reader(target)?;
119 idx.print_info();
4818c8b6 120
bcd879cf
DM
121 } else {
122 bail!("unsupported file type (expected a directory, file or block device)");
4818c8b6
DM
123 }
124
f0819fe5 125 //datastore.garbage_collection()?;
3d5c11e5 126
ff5d3707 127 Ok(Value::Null)
128}
129
130
131fn main() {
132
133 let cmd_def = CliCommand::new(
134 ApiMethod::new(
bcd879cf
DM
135 create_backup,
136 ObjectSchema::new("Create backup.")
137 .required("filename", StringSchema::new("Source name (file or directory name)"))
ff5d3707 138 .required("store", StringSchema::new("Datastore name."))
c34eb166 139 .required("target", StringSchema::new("Target name."))
2d9d143a
DM
140 .optional(
141 "chunk-size",
142 IntegerSchema::new("Chunk size in KB. Must be a power of 2.")
143 .minimum(64)
144 .maximum(4096)
145 .default(4096)
146 )
ff5d3707 147 ))
c34eb166 148 .arg_param(vec!["filename", "target"])
fe0e04c6 149 .completion_cb("store", proxmox_backup::config::datastore::complete_datastore_name);
f8838fe9 150
a914a774 151
ff5d3707 152 if let Err(err) = run_cli_command(&cmd_def.into()) {
153 eprintln!("Error: {}", err);
154 print_cli_usage();
155 std::process::exit(-1);
156 }
157
158}