2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2019-05-07 09:24:44 +00:00
|
|
|
use futures::*;
|
2019-04-27 06:57:35 +00:00
|
|
|
use hyper::header::{HeaderValue, UPGRADE};
|
|
|
|
use hyper::http::request::Parts;
|
2019-11-21 13:53:15 +00:00
|
|
|
use hyper::{Body, Response, StatusCode};
|
2019-05-10 08:25:40 +00:00
|
|
|
use serde_json::{json, Value};
|
2019-04-27 06:57:35 +00:00
|
|
|
|
2020-01-21 11:28:01 +00:00
|
|
|
use proxmox::{sortable, identity, list_subdirs_api_method};
|
2020-04-17 12:40:20 +00:00
|
|
|
use proxmox::api::{ApiResponseFuture, ApiHandler, ApiMethod, Router, RpcEnvironment, Permission};
|
2019-11-21 13:53:15 +00:00
|
|
|
use proxmox::api::router::SubdirMap;
|
|
|
|
use proxmox::api::schema::*;
|
2019-11-21 12:10:49 +00:00
|
|
|
|
2020-06-25 10:23:30 +00:00
|
|
|
use crate::tools;
|
2019-06-26 15:29:12 +00:00
|
|
|
use crate::server::{WorkerTask, H2Service};
|
2019-05-09 11:06:09 +00:00
|
|
|
use crate::backup::*;
|
2019-06-13 05:30:42 +00:00
|
|
|
use crate::api2::types::*;
|
2020-04-28 08:11:15 +00:00
|
|
|
use crate::config::acl::PRIV_DATASTORE_BACKUP;
|
2020-04-18 06:23:04 +00:00
|
|
|
use crate::config::cached_user_info::CachedUserInfo;
|
2019-04-27 06:57:35 +00:00
|
|
|
|
2019-05-08 10:41:58 +00:00
|
|
|
mod environment;
|
|
|
|
use environment::*;
|
|
|
|
|
2019-05-09 11:06:09 +00:00
|
|
|
mod upload_chunk;
|
|
|
|
use upload_chunk::*;
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.upgrade(&API_METHOD_UPGRADE_BACKUP);
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const API_METHOD_UPGRADE_BACKUP: ApiMethod = ApiMethod::new(
|
2019-11-23 08:03:21 +00:00
|
|
|
&ApiHandler::AsyncHttp(&upgrade_to_backup_protocol),
|
2019-11-21 08:36:41 +00:00
|
|
|
&ObjectSchema::new(
|
|
|
|
concat!("Upgraded to backup protocol ('", PROXMOX_BACKUP_PROTOCOL_ID_V1!(), "')."),
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-12-11 12:45:23 +00:00
|
|
|
("store", false, &DATASTORE_SCHEMA),
|
2019-11-21 08:36:41 +00:00
|
|
|
("backup-type", false, &BACKUP_TYPE_SCHEMA),
|
|
|
|
("backup-id", false, &BACKUP_ID_SCHEMA),
|
|
|
|
("backup-time", false, &BACKUP_TIME_SCHEMA),
|
|
|
|
("debug", true, &BooleanSchema::new("Enable verbose debug logging.").schema()),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-04-27 06:57:35 +00:00
|
|
|
)
|
2020-04-18 06:23:04 +00:00
|
|
|
).access(
|
|
|
|
// Note: parameter 'store' is no uri parameter, so we need to test inside function body
|
2020-04-28 08:11:15 +00:00
|
|
|
Some("The user needs Datastore.Backup privilege on /datastore/{store} and needs to own the backup group."),
|
2020-04-18 06:23:04 +00:00
|
|
|
&Permission::Anybody
|
|
|
|
);
|
2019-04-27 06:57:35 +00:00
|
|
|
|
2019-05-08 11:17:23 +00:00
|
|
|
fn upgrade_to_backup_protocol(
|
2019-04-27 06:57:35 +00:00
|
|
|
parts: Parts,
|
|
|
|
req_body: Body,
|
2019-05-08 11:17:23 +00:00
|
|
|
param: Value,
|
2019-11-21 08:36:41 +00:00
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
rpcenv: Box<dyn RpcEnvironment>,
|
2019-12-16 08:59:45 +00:00
|
|
|
) -> ApiResponseFuture {
|
2019-05-08 11:17:23 +00:00
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
async move {
|
2019-05-29 07:35:21 +00:00
|
|
|
let debug = param["debug"].as_bool().unwrap_or(false);
|
|
|
|
|
2020-04-18 06:23:04 +00:00
|
|
|
let username = rpcenv.get_user().unwrap();
|
|
|
|
|
2019-05-15 08:09:46 +00:00
|
|
|
let store = tools::required_string_param(¶m, "store")?.to_owned();
|
2020-04-18 06:23:04 +00:00
|
|
|
|
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-04-28 08:11:15 +00:00
|
|
|
user_info.check_privs(&username, &["datastore", &store], PRIV_DATASTORE_BACKUP, false)?;
|
2020-04-18 06:23:04 +00:00
|
|
|
|
2019-05-15 08:09:46 +00:00
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
2019-05-09 11:06:09 +00:00
|
|
|
|
2019-05-08 11:17:23 +00:00
|
|
|
let backup_type = tools::required_string_param(¶m, "backup-type")?;
|
|
|
|
let backup_id = tools::required_string_param(¶m, "backup-id")?;
|
2019-07-25 11:44:01 +00:00
|
|
|
let backup_time = tools::required_integer_param(¶m, "backup-time")?;
|
2019-04-27 06:57:35 +00:00
|
|
|
|
|
|
|
let protocols = parts
|
|
|
|
.headers
|
|
|
|
.get("UPGRADE")
|
|
|
|
.ok_or_else(|| format_err!("missing Upgrade header"))?
|
|
|
|
.to_str()?;
|
|
|
|
|
2019-06-05 06:41:20 +00:00
|
|
|
if protocols != PROXMOX_BACKUP_PROTOCOL_ID_V1!() {
|
2019-04-27 06:57:35 +00:00
|
|
|
bail!("invalid protocol name");
|
|
|
|
}
|
|
|
|
|
2019-05-06 08:29:34 +00:00
|
|
|
if parts.version >= http::version::Version::HTTP_2 {
|
|
|
|
bail!("unexpected http version '{:?}' (expected version < 2)", parts.version);
|
|
|
|
}
|
|
|
|
|
2019-05-08 11:17:23 +00:00
|
|
|
let worker_id = format!("{}_{}_{}", store, backup_type, backup_id);
|
2019-05-07 11:42:00 +00:00
|
|
|
|
2019-05-08 09:26:54 +00:00
|
|
|
let env_type = rpcenv.env_type();
|
2019-05-07 09:24:44 +00:00
|
|
|
|
2019-05-11 10:07:09 +00:00
|
|
|
let backup_group = BackupGroup::new(backup_type, backup_id);
|
2020-04-28 08:11:15 +00:00
|
|
|
let owner = datastore.create_backup_group(&backup_group, &username)?;
|
|
|
|
// permission check
|
|
|
|
if owner != username { // only the owner is allowed to create additional snapshots
|
|
|
|
bail!("backup owner check failed ({} != {})", username, owner);
|
|
|
|
}
|
|
|
|
|
2019-05-13 05:06:56 +00:00
|
|
|
let last_backup = BackupInfo::last_backup(&datastore.base_path(), &backup_group).unwrap_or(None);
|
2019-07-25 11:44:01 +00:00
|
|
|
let backup_dir = BackupDir::new_with_group(backup_group, backup_time);
|
|
|
|
|
|
|
|
if let Some(last) = &last_backup {
|
|
|
|
if backup_dir.backup_time() <= last.backup_dir.backup_time() {
|
|
|
|
bail!("backup timestamp is older than last backup.");
|
|
|
|
}
|
2019-12-05 10:18:10 +00:00
|
|
|
// fixme: abort if last backup is still running - howto test?
|
|
|
|
// Idea: write upid into a file inside snapshot dir. then test if
|
|
|
|
// it is still running here.
|
2019-07-25 11:44:01 +00:00
|
|
|
}
|
2019-05-10 08:25:40 +00:00
|
|
|
|
2019-05-15 08:09:46 +00:00
|
|
|
let (path, is_new) = datastore.create_backup_dir(&backup_dir)?;
|
2020-05-30 14:37:33 +00:00
|
|
|
if !is_new { bail!("backup directory already exists."); }
|
2019-05-10 08:25:40 +00:00
|
|
|
|
2019-05-08 11:17:23 +00:00
|
|
|
WorkerTask::spawn("backup", Some(worker_id), &username.clone(), true, move |worker| {
|
2019-05-15 08:09:46 +00:00
|
|
|
let mut env = BackupEnvironment::new(
|
2019-05-11 10:13:44 +00:00
|
|
|
env_type, username.clone(), worker.clone(), datastore, backup_dir);
|
2019-05-11 09:21:13 +00:00
|
|
|
|
2019-05-29 07:35:21 +00:00
|
|
|
env.debug = debug;
|
2019-05-15 08:09:46 +00:00
|
|
|
env.last_backup = last_backup;
|
2019-05-11 09:21:13 +00:00
|
|
|
|
2019-05-15 08:09:46 +00:00
|
|
|
env.log(format!("starting new backup on datastore '{}': {:?}", store, path));
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
let service = H2Service::new(env.clone(), worker.clone(), &BACKUP_API_ROUTER, debug);
|
2019-05-07 12:07:00 +00:00
|
|
|
|
2019-05-08 05:14:40 +00:00
|
|
|
let abort_future = worker.abort_future();
|
|
|
|
|
2019-05-15 10:58:55 +00:00
|
|
|
let env2 = env.clone();
|
2019-05-15 08:09:46 +00:00
|
|
|
|
2020-03-18 10:32:51 +00:00
|
|
|
let mut req_fut = req_body
|
2019-04-27 06:57:35 +00:00
|
|
|
.on_upgrade()
|
2019-05-07 09:24:44 +00:00
|
|
|
.map_err(Error::from)
|
2019-04-27 06:57:35 +00:00
|
|
|
.and_then(move |conn| {
|
2020-03-18 10:32:51 +00:00
|
|
|
env2.debug("protocol upgrade done");
|
2019-05-07 09:24:44 +00:00
|
|
|
|
|
|
|
let mut http = hyper::server::conn::Http::new();
|
|
|
|
http.http2_only(true);
|
2019-05-16 08:24:23 +00:00
|
|
|
// increase window size: todo - find optiomal size
|
2019-05-27 05:40:50 +00:00
|
|
|
let window_size = 32*1024*1024; // max = (1 << 31) - 2
|
|
|
|
http.http2_initial_stream_window_size(window_size);
|
|
|
|
http.http2_initial_connection_window_size(window_size);
|
2019-05-07 09:24:44 +00:00
|
|
|
|
2019-05-07 11:42:00 +00:00
|
|
|
http.serve_connection(conn, service)
|
|
|
|
.map_err(Error::from)
|
2019-08-28 07:58:39 +00:00
|
|
|
});
|
2020-03-18 10:32:51 +00:00
|
|
|
let mut abort_future = abort_future
|
2019-08-28 07:58:39 +00:00
|
|
|
.map(|_| Err(format_err!("task aborted")));
|
|
|
|
|
2020-03-18 10:32:51 +00:00
|
|
|
async move {
|
|
|
|
let res = select!{
|
|
|
|
req = req_fut => req,
|
|
|
|
abrt = abort_future => abrt,
|
|
|
|
};
|
|
|
|
|
|
|
|
match (res, env.ensure_finished()) {
|
|
|
|
(Ok(_), Ok(())) => {
|
2020-05-30 14:37:33 +00:00
|
|
|
env.log("backup finished successfully");
|
2020-03-18 10:32:51 +00:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
(Err(err), Ok(())) => {
|
|
|
|
// ignore errors after finish
|
|
|
|
env.log(format!("backup had errors but finished: {}", err));
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
(Ok(_), Err(err)) => {
|
|
|
|
env.log(format!("backup ended and finish failed: {}", err));
|
|
|
|
env.log("removing unfinished backup");
|
|
|
|
env.remove_backup()?;
|
|
|
|
Err(err)
|
|
|
|
},
|
|
|
|
(Err(err), Err(_)) => {
|
|
|
|
env.log(format!("backup failed: {}", err));
|
|
|
|
env.log("removing failed backup");
|
|
|
|
env.remove_backup()?;
|
|
|
|
Err(err)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-05-08 06:21:18 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
let response = Response::builder()
|
|
|
|
.status(StatusCode::SWITCHING_PROTOCOLS)
|
2019-06-05 06:41:20 +00:00
|
|
|
.header(UPGRADE, HeaderValue::from_static(PROXMOX_BACKUP_PROTOCOL_ID_V1!()))
|
2019-05-08 06:21:18 +00:00
|
|
|
.body(Body::empty())?;
|
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
Ok(response)
|
|
|
|
}.boxed()
|
2019-04-27 06:57:35 +00:00
|
|
|
}
|
2019-05-07 09:24:44 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const BACKUP_API_SUBDIRS: SubdirMap = &[
|
|
|
|
(
|
|
|
|
"blob", &Router::new()
|
|
|
|
.upload(&API_METHOD_UPLOAD_BLOB)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"dynamic_chunk", &Router::new()
|
|
|
|
.upload(&API_METHOD_UPLOAD_DYNAMIC_CHUNK)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"dynamic_close", &Router::new()
|
|
|
|
.post(&API_METHOD_CLOSE_DYNAMIC_INDEX)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"dynamic_index", &Router::new()
|
|
|
|
.post(&API_METHOD_CREATE_DYNAMIC_INDEX)
|
|
|
|
.put(&API_METHOD_DYNAMIC_APPEND)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"finish", &Router::new()
|
|
|
|
.post(
|
|
|
|
&ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&finish_backup),
|
|
|
|
&ObjectSchema::new("Mark backup as finished.", &[])
|
2019-05-15 10:58:55 +00:00
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"fixed_chunk", &Router::new()
|
|
|
|
.upload(&API_METHOD_UPLOAD_FIXED_CHUNK)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"fixed_close", &Router::new()
|
|
|
|
.post(&API_METHOD_CLOSE_FIXED_INDEX)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"fixed_index", &Router::new()
|
|
|
|
.post(&API_METHOD_CREATE_FIXED_INDEX)
|
|
|
|
.put(&API_METHOD_FIXED_APPEND)
|
|
|
|
),
|
2020-06-25 10:23:30 +00:00
|
|
|
(
|
|
|
|
"previous", &Router::new()
|
|
|
|
.download(&API_METHOD_DOWNLOAD_PREVIOUS)
|
|
|
|
),
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"speedtest", &Router::new()
|
|
|
|
.upload(&API_METHOD_UPLOAD_SPEEDTEST)
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
pub const BACKUP_API_ROUTER: Router = Router::new()
|
|
|
|
.get(&list_subdirs_api_method!(BACKUP_API_SUBDIRS))
|
|
|
|
.subdirs(BACKUP_API_SUBDIRS);
|
|
|
|
|
2019-12-04 13:18:11 +00:00
|
|
|
#[sortable]
|
|
|
|
pub const API_METHOD_CREATE_DYNAMIC_INDEX: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&create_dynamic_index),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Create dynamic chunk index file.",
|
|
|
|
&sorted!([
|
|
|
|
("archive-name", false, &crate::api2::types::BACKUP_ARCHIVE_NAME_SCHEMA),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-05-10 08:25:40 +00:00
|
|
|
fn create_dynamic_index(
|
|
|
|
param: Value,
|
2019-12-04 13:18:11 +00:00
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-05-10 08:25:40 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let env: &BackupEnvironment = rpcenv.as_ref();
|
|
|
|
|
2019-05-23 06:50:36 +00:00
|
|
|
let name = tools::required_string_param(¶m, "archive-name")?.to_owned();
|
2019-05-10 08:25:40 +00:00
|
|
|
|
2019-08-07 08:10:14 +00:00
|
|
|
let archive_name = name.clone();
|
2019-11-08 09:28:14 +00:00
|
|
|
if !archive_name.ends_with(".didx") {
|
2019-05-28 04:18:55 +00:00
|
|
|
bail!("wrong archive extension: '{}'", archive_name);
|
2019-05-10 08:25:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-11 10:13:44 +00:00
|
|
|
let mut path = env.backup_dir.relative_path();
|
2019-05-10 08:25:40 +00:00
|
|
|
path.push(archive_name);
|
|
|
|
|
2019-05-29 06:49:57 +00:00
|
|
|
let index = env.datastore.create_dynamic_writer(&path)?;
|
2019-05-23 06:50:36 +00:00
|
|
|
let wid = env.register_dynamic_writer(index, name)?;
|
2019-05-10 08:25:40 +00:00
|
|
|
|
2019-05-15 08:09:46 +00:00
|
|
|
env.log(format!("created new dynamic index {} ({:?})", wid, path));
|
2019-05-10 08:25:40 +00:00
|
|
|
|
2019-05-15 08:09:46 +00:00
|
|
|
Ok(json!(wid))
|
2019-05-10 08:25:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const API_METHOD_CREATE_FIXED_INDEX: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&create_fixed_index),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Create fixed chunk index file.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
("archive-name", false, &crate::api2::types::BACKUP_ARCHIVE_NAME_SCHEMA),
|
|
|
|
("size", false, &IntegerSchema::new("File size.")
|
|
|
|
.minimum(1)
|
|
|
|
.schema()
|
|
|
|
),
|
2020-06-23 12:43:10 +00:00
|
|
|
("reuse-csum", true, &StringSchema::new("If set, compare last backup's \
|
|
|
|
csum and reuse index for incremental backup if it matches.").schema()),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-05-28 04:18:55 +00:00
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
);
|
2019-05-28 04:18:55 +00:00
|
|
|
|
|
|
|
fn create_fixed_index(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-05-28 04:18:55 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let env: &BackupEnvironment = rpcenv.as_ref();
|
|
|
|
|
|
|
|
let name = tools::required_string_param(¶m, "archive-name")?.to_owned();
|
|
|
|
let size = tools::required_integer_param(¶m, "size")? as usize;
|
2020-06-23 12:43:10 +00:00
|
|
|
let reuse_csum = param["reuse-csum"].as_str();
|
2019-05-28 04:18:55 +00:00
|
|
|
|
2019-08-07 08:10:14 +00:00
|
|
|
let archive_name = name.clone();
|
2019-11-08 09:28:14 +00:00
|
|
|
if !archive_name.ends_with(".fidx") {
|
2019-05-28 04:18:55 +00:00
|
|
|
bail!("wrong archive extension: '{}'", archive_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut path = env.backup_dir.relative_path();
|
2020-06-23 12:43:10 +00:00
|
|
|
path.push(&archive_name);
|
2019-05-28 04:18:55 +00:00
|
|
|
|
|
|
|
let chunk_size = 4096*1024; // todo: ??
|
|
|
|
|
2020-06-23 12:43:10 +00:00
|
|
|
// do incremental backup if csum is set
|
|
|
|
let mut reader = None;
|
|
|
|
let mut incremental = false;
|
|
|
|
if let Some(csum) = reuse_csum {
|
|
|
|
incremental = true;
|
|
|
|
let last_backup = match &env.last_backup {
|
|
|
|
Some(info) => info,
|
|
|
|
None => {
|
|
|
|
bail!("cannot reuse index - no previous backup exists");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut last_path = last_backup.backup_dir.relative_path();
|
|
|
|
last_path.push(&archive_name);
|
|
|
|
|
|
|
|
let index = match env.datastore.open_fixed_reader(last_path) {
|
|
|
|
Ok(index) => index,
|
|
|
|
Err(_) => {
|
|
|
|
bail!("cannot reuse index - no previous backup exists for archive");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let (old_csum, _) = index.compute_csum();
|
|
|
|
let old_csum = proxmox::tools::digest_to_hex(&old_csum);
|
|
|
|
if old_csum != csum {
|
|
|
|
bail!("expected csum ({}) doesn't match last backup's ({}), cannot do incremental backup",
|
|
|
|
csum, old_csum);
|
|
|
|
}
|
|
|
|
|
|
|
|
reader = Some(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut writer = env.datastore.create_fixed_writer(&path, size, chunk_size)?;
|
|
|
|
|
|
|
|
if let Some(reader) = reader {
|
|
|
|
writer.clone_data_from(&reader)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let wid = env.register_fixed_writer(writer, name, size, chunk_size as u32, incremental)?;
|
2019-05-28 04:18:55 +00:00
|
|
|
|
|
|
|
env.log(format!("created new fixed index {} ({:?})", wid, path));
|
|
|
|
|
|
|
|
Ok(json!(wid))
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const API_METHOD_DYNAMIC_APPEND: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&dynamic_append),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Append chunk to dynamic index writer.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"wid",
|
|
|
|
false,
|
|
|
|
&IntegerSchema::new("Dynamic writer ID.")
|
|
|
|
.minimum(1)
|
|
|
|
.maximum(256)
|
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"digest-list",
|
|
|
|
false,
|
|
|
|
&ArraySchema::new("Chunk digest list.", &CHUNK_DIGEST_SCHEMA).schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"offset-list",
|
|
|
|
false,
|
|
|
|
&ArraySchema::new(
|
|
|
|
"Chunk offset list.",
|
|
|
|
&IntegerSchema::new("Corresponding chunk offsets.")
|
|
|
|
.minimum(0)
|
|
|
|
.schema()
|
|
|
|
).schema()
|
|
|
|
),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-05-20 12:19:24 +00:00
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
);
|
2019-05-20 12:19:24 +00:00
|
|
|
|
|
|
|
fn dynamic_append (
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-05-20 12:19:24 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let wid = tools::required_integer_param(¶m, "wid")? as usize;
|
2019-05-23 10:29:33 +00:00
|
|
|
let digest_list = tools::required_array_param(¶m, "digest-list")?;
|
2019-05-24 08:05:22 +00:00
|
|
|
let offset_list = tools::required_array_param(¶m, "offset-list")?;
|
2019-05-23 10:29:33 +00:00
|
|
|
|
2019-05-24 08:05:22 +00:00
|
|
|
if offset_list.len() != digest_list.len() {
|
|
|
|
bail!("offset list has wrong length ({} != {})", offset_list.len(), digest_list.len());
|
|
|
|
}
|
|
|
|
|
2019-05-20 12:19:24 +00:00
|
|
|
let env: &BackupEnvironment = rpcenv.as_ref();
|
|
|
|
|
2019-05-29 08:17:38 +00:00
|
|
|
env.debug(format!("dynamic_append {} chunks", digest_list.len()));
|
|
|
|
|
2019-05-24 08:05:22 +00:00
|
|
|
for (i, item) in digest_list.iter().enumerate() {
|
2019-05-23 10:29:33 +00:00
|
|
|
let digest_str = item.as_str().unwrap();
|
2019-06-14 09:40:04 +00:00
|
|
|
let digest = proxmox::tools::hex_to_digest(digest_str)?;
|
2019-05-24 08:05:22 +00:00
|
|
|
let offset = offset_list[i].as_u64().unwrap();
|
2019-05-23 10:29:33 +00:00
|
|
|
let size = env.lookup_chunk(&digest).ok_or_else(|| format_err!("no such chunk {}", digest_str))?;
|
2019-05-29 08:17:38 +00:00
|
|
|
|
2019-05-24 08:05:22 +00:00
|
|
|
env.dynamic_writer_append_chunk(wid, offset, size, &digest)?;
|
2019-05-20 12:19:24 +00:00
|
|
|
|
2020-05-30 14:37:33 +00:00
|
|
|
env.debug(format!("successfully added chunk {} to dynamic index {} (offset {}, size {})", digest_str, wid, offset, size));
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
2019-05-20 12:19:24 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const API_METHOD_FIXED_APPEND: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&fixed_append),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Append chunk to fixed index writer.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"wid",
|
|
|
|
false,
|
|
|
|
&IntegerSchema::new("Fixed writer ID.")
|
|
|
|
.minimum(1)
|
|
|
|
.maximum(256)
|
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"digest-list",
|
|
|
|
false,
|
|
|
|
&ArraySchema::new("Chunk digest list.", &CHUNK_DIGEST_SCHEMA).schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"offset-list",
|
|
|
|
false,
|
|
|
|
&ArraySchema::new(
|
|
|
|
"Chunk offset list.",
|
|
|
|
&IntegerSchema::new("Corresponding chunk offsets.")
|
|
|
|
.minimum(0)
|
|
|
|
.schema()
|
|
|
|
).schema()
|
2019-05-28 04:18:55 +00:00
|
|
|
)
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-05-28 04:18:55 +00:00
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
);
|
2019-05-28 04:18:55 +00:00
|
|
|
|
|
|
|
fn fixed_append (
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-05-28 04:18:55 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let wid = tools::required_integer_param(¶m, "wid")? as usize;
|
|
|
|
let digest_list = tools::required_array_param(¶m, "digest-list")?;
|
|
|
|
let offset_list = tools::required_array_param(¶m, "offset-list")?;
|
|
|
|
|
|
|
|
if offset_list.len() != digest_list.len() {
|
|
|
|
bail!("offset list has wrong length ({} != {})", offset_list.len(), digest_list.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
let env: &BackupEnvironment = rpcenv.as_ref();
|
|
|
|
|
2019-05-29 08:17:38 +00:00
|
|
|
env.debug(format!("fixed_append {} chunks", digest_list.len()));
|
|
|
|
|
2019-05-28 04:18:55 +00:00
|
|
|
for (i, item) in digest_list.iter().enumerate() {
|
|
|
|
let digest_str = item.as_str().unwrap();
|
2019-06-14 09:40:04 +00:00
|
|
|
let digest = proxmox::tools::hex_to_digest(digest_str)?;
|
2019-05-28 04:18:55 +00:00
|
|
|
let offset = offset_list[i].as_u64().unwrap();
|
|
|
|
let size = env.lookup_chunk(&digest).ok_or_else(|| format_err!("no such chunk {}", digest_str))?;
|
2019-05-29 08:17:38 +00:00
|
|
|
|
2019-05-28 04:18:55 +00:00
|
|
|
env.fixed_writer_append_chunk(wid, offset, size, &digest)?;
|
|
|
|
|
2020-05-30 14:37:33 +00:00
|
|
|
env.debug(format!("successfully added chunk {} to fixed index {} (offset {}, size {})", digest_str, wid, offset, size));
|
2019-05-28 04:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const API_METHOD_CLOSE_DYNAMIC_INDEX: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&close_dynamic_index),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Close dynamic index writer.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"wid",
|
|
|
|
false,
|
|
|
|
&IntegerSchema::new("Dynamic writer ID.")
|
|
|
|
.minimum(1)
|
|
|
|
.maximum(256)
|
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"chunk-count",
|
|
|
|
false,
|
|
|
|
&IntegerSchema::new("Chunk count. This is used to verify that the server got all chunks.")
|
|
|
|
.minimum(1)
|
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"size",
|
|
|
|
false,
|
|
|
|
&IntegerSchema::new("File size. This is used to verify that the server got all data.")
|
|
|
|
.minimum(1)
|
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
("csum", false, &StringSchema::new("Digest list checksum.").schema()),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-05-15 05:58:05 +00:00
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
);
|
2019-05-15 05:58:05 +00:00
|
|
|
|
|
|
|
fn close_dynamic_index (
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-05-15 05:58:05 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let wid = tools::required_integer_param(¶m, "wid")? as usize;
|
2019-05-23 06:50:36 +00:00
|
|
|
let chunk_count = tools::required_integer_param(¶m, "chunk-count")? as u64;
|
|
|
|
let size = tools::required_integer_param(¶m, "size")? as u64;
|
2019-09-23 08:56:53 +00:00
|
|
|
let csum_str = tools::required_string_param(¶m, "csum")?;
|
|
|
|
let csum = proxmox::tools::hex_to_digest(csum_str)?;
|
2019-05-15 05:58:05 +00:00
|
|
|
|
|
|
|
let env: &BackupEnvironment = rpcenv.as_ref();
|
|
|
|
|
2019-09-23 08:56:53 +00:00
|
|
|
env.dynamic_writer_close(wid, chunk_count, size, csum)?;
|
2019-05-15 05:58:05 +00:00
|
|
|
|
2020-05-30 14:37:33 +00:00
|
|
|
env.log(format!("successfully closed dynamic index {}", wid));
|
2019-05-15 08:09:46 +00:00
|
|
|
|
2019-05-15 05:58:05 +00:00
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const API_METHOD_CLOSE_FIXED_INDEX: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&close_fixed_index),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Close fixed index writer.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"wid",
|
|
|
|
false,
|
|
|
|
&IntegerSchema::new("Fixed writer ID.")
|
|
|
|
.minimum(1)
|
|
|
|
.maximum(256)
|
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"chunk-count",
|
|
|
|
false,
|
2020-06-23 12:43:10 +00:00
|
|
|
&IntegerSchema::new("Chunk count. This is used to verify that the server got all chunks. Ignored for incremental backups.")
|
|
|
|
.minimum(0)
|
2019-11-21 08:36:41 +00:00
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"size",
|
|
|
|
false,
|
2020-06-23 12:43:10 +00:00
|
|
|
&IntegerSchema::new("File size. This is used to verify that the server got all data. Ignored for incremental backups.")
|
|
|
|
.minimum(0)
|
2019-11-21 08:36:41 +00:00
|
|
|
.schema()
|
|
|
|
),
|
|
|
|
("csum", false, &StringSchema::new("Digest list checksum.").schema()),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-05-28 04:18:55 +00:00
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
);
|
2019-05-28 04:18:55 +00:00
|
|
|
|
|
|
|
fn close_fixed_index (
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-05-28 04:18:55 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let wid = tools::required_integer_param(¶m, "wid")? as usize;
|
|
|
|
let chunk_count = tools::required_integer_param(¶m, "chunk-count")? as u64;
|
|
|
|
let size = tools::required_integer_param(¶m, "size")? as u64;
|
2019-09-23 08:56:53 +00:00
|
|
|
let csum_str = tools::required_string_param(¶m, "csum")?;
|
|
|
|
let csum = proxmox::tools::hex_to_digest(csum_str)?;
|
2019-05-28 04:18:55 +00:00
|
|
|
|
|
|
|
let env: &BackupEnvironment = rpcenv.as_ref();
|
|
|
|
|
2019-09-23 08:56:53 +00:00
|
|
|
env.fixed_writer_close(wid, chunk_count, size, csum)?;
|
2019-05-28 04:18:55 +00:00
|
|
|
|
2020-05-30 14:37:33 +00:00
|
|
|
env.log(format!("successfully closed fixed index {}", wid));
|
2019-05-28 04:18:55 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
2019-05-15 05:58:05 +00:00
|
|
|
|
2019-05-15 10:58:55 +00:00
|
|
|
fn finish_backup (
|
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-05-15 10:58:55 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let env: &BackupEnvironment = rpcenv.as_ref();
|
|
|
|
|
|
|
|
env.finish_backup()?;
|
2020-05-30 14:37:33 +00:00
|
|
|
env.log("successfully finished backup");
|
2019-05-15 10:58:55 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
2019-05-15 05:58:05 +00:00
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2020-06-25 10:23:30 +00:00
|
|
|
pub const API_METHOD_DOWNLOAD_PREVIOUS: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::AsyncHttp(&download_previous),
|
2019-11-21 08:36:41 +00:00
|
|
|
&ObjectSchema::new(
|
2020-06-25 10:23:30 +00:00
|
|
|
"Download archive from previous backup.",
|
2019-11-21 12:10:49 +00:00
|
|
|
&sorted!([
|
|
|
|
("archive-name", false, &crate::api2::types::BACKUP_ARCHIVE_NAME_SCHEMA)
|
|
|
|
]),
|
2019-05-28 04:18:55 +00:00
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
);
|
2019-05-28 04:18:55 +00:00
|
|
|
|
2020-06-25 10:23:30 +00:00
|
|
|
fn download_previous(
|
2019-05-11 14:05:50 +00:00
|
|
|
_parts: Parts,
|
|
|
|
_req_body: Body,
|
|
|
|
param: Value,
|
2019-11-21 08:36:41 +00:00
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
rpcenv: Box<dyn RpcEnvironment>,
|
2019-12-16 08:59:45 +00:00
|
|
|
) -> ApiResponseFuture {
|
2019-05-11 14:05:50 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
async move {
|
|
|
|
let env: &BackupEnvironment = rpcenv.as_ref();
|
2019-05-11 14:05:50 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
let archive_name = tools::required_string_param(¶m, "archive-name")?.to_owned();
|
2019-05-11 14:05:50 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
let last_backup = match &env.last_backup {
|
|
|
|
Some(info) => info,
|
2020-06-25 10:23:30 +00:00
|
|
|
None => bail!("no previous backup"),
|
2019-11-22 12:02:05 +00:00
|
|
|
};
|
|
|
|
|
2020-06-25 10:23:30 +00:00
|
|
|
let mut path = env.datastore.snapshot_path(&last_backup.backup_dir);
|
2019-11-22 12:02:05 +00:00
|
|
|
path.push(&archive_name);
|
|
|
|
|
2020-06-26 06:22:46 +00:00
|
|
|
{
|
|
|
|
let index: Option<Box<dyn IndexFile>> = match archive_type(&archive_name)? {
|
|
|
|
ArchiveType::FixedIndex => {
|
|
|
|
let index = env.datastore.open_fixed_reader(&path)?;
|
|
|
|
Some(Box::new(index))
|
|
|
|
}
|
|
|
|
ArchiveType::DynamicIndex => {
|
|
|
|
let index = env.datastore.open_dynamic_reader(&path)?;
|
|
|
|
Some(Box::new(index))
|
|
|
|
}
|
|
|
|
_ => { None }
|
|
|
|
};
|
|
|
|
if let Some(index) = index {
|
|
|
|
env.log(format!("register chunks in '{}' from previous backup.", archive_name));
|
|
|
|
|
|
|
|
for pos in 0..index.index_count() {
|
|
|
|
let info = index.chunk_info(pos).unwrap();
|
|
|
|
let size = info.range.end - info.range.start;
|
|
|
|
env.register_chunk(info.digest, size as u32)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
env.log(format!("download '{}' from previous backup.", archive_name));
|
2020-06-25 10:23:30 +00:00
|
|
|
crate::api2::helpers::create_download_response(path).await
|
2019-11-22 12:02:05 +00:00
|
|
|
}.boxed()
|
2019-05-28 04:18:55 +00:00
|
|
|
}
|