1 extern crate proxmox_backup
;
4 //use std::os::unix::io::AsRawFd;
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::*;
17 use serde_json
::{Value}
;
21 fn backup_directory(repo
: &BackupRepository
, body
: Body
, archive_name
: &str) -> Result
<(), Error
> {
23 let client
= HttpClient
::new(&repo
.host
, &repo
.user
);
25 let epoch
= std
::time
::SystemTime
::now().duration_since(
26 std
::time
::SystemTime
::UNIX_EPOCH
)?
.as_secs();
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())
35 let path
= format
!("api2/json/admin/datastore/{}/catar?{}", repo
.store
, query
);
37 client
.upload("application/x-proxmox-backup-catar", body
, &path
)?
;
43 fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> {
45 let mut target = PathBuf::from(target);
47 if let Some(ext) = target.extension() {
49 bail!("got wrong file extension - expected '.fidx'");
52 target.set_extension("fidx");
55 let mut index = datastore.create_image_writer(&target, size, chunk_size)?;
57 tools::file_chunker(file, chunk_size, |pos, chunk| {
58 index.add_chunk(pos, chunk)?;
62 index.close()?; // commit changes
71 _rpcenv
: &mut RpcEnvironment
,
72 ) -> Result
<Value
, Error
> {
74 let repo_url
= tools
::required_string_param(¶m
, "repository")?
;
75 let repo
= BackupRepository
::parse(repo_url
)?
;
77 let client
= HttpClient
::new(&repo
.host
, &repo
.user
);
79 let path
= format
!("api2/json/admin/datastore/{}/backups", repo
.store
);
81 let result
= client
.get(&path
)?
;
90 _rpcenv
: &mut RpcEnvironment
,
91 ) -> Result
<Value
, Error
> {
93 let filename
= tools
::required_string_param(¶m
, "filename")?
;
94 let repo_url
= tools
::required_string_param(¶m
, "repository")?
;
95 let target
= tools
::required_string_param(¶m
, "target")?
;
97 let repo
= BackupRepository
::parse(repo_url
)?
;
99 let mut _chunk_size
= 4*1024*1024;
101 if let Some(size
) = param
["chunk-size"].as_u64() {
102 static SIZES
: [u64; 7] = [64, 128, 256, 512, 1024, 2048, 4096];
104 if SIZES
.contains(&size
) {
105 _chunk_size
= (size
as usize) * 1024;
107 bail
!("Got unsupported chunk size '{}'", size
);
111 let stat
= match nix
::sys
::stat
::stat(filename
) {
113 Err(err
) => bail
!("unable to access '{}' - {}", filename
, err
),
116 if (stat
.st_mode
& libc
::S_IFDIR
) != 0 {
117 println
!("Backup directory '{}' to '{:?}'", filename
, repo
);
119 let stream
= CaTarBackupStream
::open(filename
)?
;
121 let body
= Body
::wrap_stream(stream
);
123 backup_directory(&repo
, body
, target
)?
;
125 } else if (stat
.st_mode
& (libc
::S_IFREG
|libc
::S_IFBLK
)) != 0 {
126 println
!("Backup image '{}' to '{:?}'", filename
, repo
);
128 if stat
.st_size
<= 0 { bail!("got strange file size '{}'
", stat.st_size); }
129 let _size = stat.st_size as usize;
131 panic!("implement me
");
133 //backup_image(&datastore, &file, size, &target, chunk_size)?;
135 // let idx = datastore.open_image_reader(target)?;
139 bail!("unsupported file
type (expected a directory
, file or block device
)");
142 //datastore.garbage_collection()?;
149 let repo_url_schema: Arc<Schema> = Arc::new(
150 StringSchema::new("Repository URL
.")
151 .format(BACKUP_REPO_URL.clone())
156 let create_cmd_def = CliCommand::new(
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
."))
165 IntegerSchema::new("Chunk size
in KB
. Must be a power of
2.")
171 .arg_param(vec!["repository
", "filename
", "target
"])
172 .completion_cb("filename
", tools::complete_file_name);
174 let list_cmd_def = CliCommand::new(
177 ObjectSchema::new("List backups
.")
178 .required("repository
", repo_url_schema.clone())
180 .arg_param(vec!["repository
"]);
182 let cmd_def = CliCommandMap::new()
183 .insert("create
".to_owned(), create_cmd_def.into())
184 .insert("list
".to_owned(), list_cmd_def.into());
186 if let Err(err) = run_cli_command(&cmd_def.into()) {
187 eprintln!("Error
: {}
", err);
188 if err.downcast::<UsageError>().is_ok() {
191 std::process::exit(-1);