1 extern crate proxmox_backup
;
4 //use std::os::unix::io::AsRawFd;
5 use chrono
::{Local, TimeZone}
;
7 use proxmox_backup
::tools
;
8 use proxmox_backup
::cli
::command
::*;
9 use proxmox_backup
::api_schema
::*;
10 use proxmox_backup
::api_schema
::router
::*;
11 use proxmox_backup
::client
::*;
12 use proxmox_backup
::backup
::*;
13 //use proxmox_backup::backup::image_index::*;
14 //use proxmox_backup::config::datastore;
15 //use proxmox_backup::catar::encoder::*;
16 //use proxmox_backup::backup::datastore::*;
18 use serde_json
::{Value}
;
23 repo
: &BackupRepository
,
26 chunk_size
: Option
<u64>,
27 ) -> Result
<(), Error
> {
29 let client
= HttpClient
::new(&repo
.host
, &repo
.user
);
31 let epoch
= std
::time
::SystemTime
::now().duration_since(
32 std
::time
::SystemTime
::UNIX_EPOCH
)?
.as_secs();
34 let mut query
= url
::form_urlencoded
::Serializer
::new(String
::new());
37 .append_pair("archive_name", archive_name
)
38 .append_pair("type", "host")
39 .append_pair("id", &tools
::nodename())
40 .append_pair("time", &epoch
.to_string());
42 if let Some(size
) = chunk_size
{
43 query
.append_pair("chunk-size", &size
.to_string());
46 let query
= query
.finish();
48 let path
= format
!("api2/json/admin/datastore/{}/catar?{}", repo
.store
, query
);
50 client
.upload("application/x-proxmox-backup-catar", body
, &path
)?
;
56 fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> {
58 let mut target = PathBuf::from(target);
60 if let Some(ext) = target.extension() {
62 bail!("got wrong file extension - expected '.fidx'");
65 target.set_extension("fidx");
68 let mut index = datastore.create_image_writer(&target, size, chunk_size)?;
70 tools::file_chunker(file, chunk_size, |pos, chunk| {
71 index.add_chunk(pos, chunk)?;
75 index.close()?; // commit changes
84 _rpcenv
: &mut RpcEnvironment
,
85 ) -> Result
<Value
, Error
> {
87 let repo_url
= tools
::required_string_param(¶m
, "repository")?
;
88 let repo
= BackupRepository
::parse(repo_url
)?
;
90 let client
= HttpClient
::new(&repo
.host
, &repo
.user
);
92 let path
= format
!("api2/json/admin/datastore/{}/backups", repo
.store
);
94 let result
= client
.get(&path
)?
;
96 // fixme: implement and use output formatter instead ..
97 let list
= result
["data"].as_array().unwrap();
101 let id
= item
["backup_id"].as_str(). unwrap();
102 let btype
= item
["backup_type"].as_str(). unwrap();
103 let epoch
= item
["backup_time"].as_i64(). unwrap();
105 let time_str
= Local
.timestamp(epoch
, 0).format("%c");
107 let files
= item
["files"].as_array().unwrap();
110 let filename
= file
.as_str().unwrap();
111 println
!("| {} | {} | {} | {}", btype
, id
, time_str
, filename
);
123 _rpcenv
: &mut RpcEnvironment
,
124 ) -> Result
<Value
, Error
> {
126 let filename
= tools
::required_string_param(¶m
, "filename")?
;
127 let repo_url
= tools
::required_string_param(¶m
, "repository")?
;
128 let target
= tools
::required_string_param(¶m
, "target")?
;
130 let repo
= BackupRepository
::parse(repo_url
)?
;
132 let chunk_size_opt
= param
["chunk-size"].as_u64().map(|v
| v
*1024);
134 if let Some(size
) = chunk_size_opt
{
135 verify_chunk_size(size
)?
;
138 let stat
= match nix
::sys
::stat
::stat(filename
) {
140 Err(err
) => bail
!("unable to access '{}' - {}", filename
, err
),
143 if (stat
.st_mode
& libc
::S_IFDIR
) != 0 {
144 println
!("Backup directory '{}' to '{:?}'", filename
, repo
);
146 let stream
= CaTarBackupStream
::open(filename
)?
;
148 let body
= Body
::wrap_stream(stream
);
150 backup_directory(&repo
, body
, target
, chunk_size_opt
)?
;
152 } else if (stat
.st_mode
& (libc
::S_IFREG
|libc
::S_IFBLK
)) != 0 {
153 println
!("Backup image '{}' to '{:?}'", filename
, repo
);
155 if stat
.st_size
<= 0 { bail!("got strange file size '{}'
", stat.st_size); }
156 let _size = stat.st_size as usize;
158 panic!("implement me
");
160 //backup_image(&datastore, &file, size, &target, chunk_size)?;
162 // let idx = datastore.open_image_reader(target)?;
166 bail!("unsupported file
type (expected a directory
, file or block device
)");
169 //datastore.garbage_collection()?;
176 let repo_url_schema: Arc<Schema> = Arc::new(
177 StringSchema::new("Repository URL
.")
178 .format(BACKUP_REPO_URL.clone())
183 let create_cmd_def = CliCommand::new(
186 ObjectSchema::new("Create backup
.")
187 .required("repository
", repo_url_schema.clone())
188 .required("filename
", StringSchema::new("Source
name (file or directory name
)"))
189 .required("target
", StringSchema::new("Target name
."))
192 IntegerSchema::new("Chunk size
in KB
. Must be a power of
2.")
198 .arg_param(vec!["repository
", "filename
", "target
"])
199 .completion_cb("filename
", tools::complete_file_name);
201 let list_cmd_def = CliCommand::new(
204 ObjectSchema::new("List backups
.")
205 .required("repository
", repo_url_schema.clone())
207 .arg_param(vec!["repository
"]);
209 let cmd_def = CliCommandMap::new()
210 .insert("create
".to_owned(), create_cmd_def.into())
211 .insert("list
".to_owned(), list_cmd_def.into());
213 if let Err(err) = run_cli_command(&cmd_def.into()) {
214 eprintln!("Error
: {}
", err);
215 if err.downcast::<UsageError>().is_ok() {
218 std::process::exit(-1);