2019-11-21 13:53:15 +00:00
|
|
|
use std::collections::{HashSet, HashMap};
|
2020-06-23 10:09:53 +00:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
2019-11-21 13:53:15 +00:00
|
|
|
|
2020-06-18 11:55:29 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2019-06-25 08:16:59 +00:00
|
|
|
use futures::*;
|
2019-11-21 13:53:15 +00:00
|
|
|
use hyper::http::request::Parts;
|
|
|
|
use hyper::{header, Body, Response, StatusCode};
|
2018-12-21 12:38:41 +00:00
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
2020-04-17 12:36:27 +00:00
|
|
|
use proxmox::api::{
|
|
|
|
api, ApiResponseFuture, ApiHandler, ApiMethod, Router,
|
2020-08-06 13:46:01 +00:00
|
|
|
RpcEnvironment, RpcEnvironmentType, Permission
|
|
|
|
};
|
2019-11-21 13:53:15 +00:00
|
|
|
use proxmox::api::router::SubdirMap;
|
|
|
|
use proxmox::api::schema::*;
|
2020-06-24 04:58:14 +00:00
|
|
|
use proxmox::tools::fs::{replace_file, CreateOptions};
|
2020-01-21 11:28:01 +00:00
|
|
|
use proxmox::try_block;
|
|
|
|
use proxmox::{http_err, identity, list_subdirs_api_method, sortable};
|
2019-08-03 11:05:38 +00:00
|
|
|
|
2020-06-23 10:09:53 +00:00
|
|
|
use pxar::accessor::aio::Accessor;
|
|
|
|
use pxar::EntryKind;
|
|
|
|
|
2019-11-21 13:53:15 +00:00
|
|
|
use crate::api2::types::*;
|
2020-06-10 10:02:57 +00:00
|
|
|
use crate::api2::node::rrd::create_value_from_rrd;
|
2019-02-12 13:13:31 +00:00
|
|
|
use crate::backup::*;
|
2019-11-21 13:53:15 +00:00
|
|
|
use crate::config::datastore;
|
2020-04-28 08:11:15 +00:00
|
|
|
use crate::config::cached_user_info::CachedUserInfo;
|
|
|
|
|
2019-04-06 09:27:23 +00:00
|
|
|
use crate::server::WorkerTask;
|
2020-06-22 14:44:13 +00:00
|
|
|
use crate::tools::{self, AsyncReaderStream, WrappedReaderStream};
|
2020-04-27 05:13:06 +00:00
|
|
|
use crate::config::acl::{
|
|
|
|
PRIV_DATASTORE_AUDIT,
|
2020-04-28 08:11:15 +00:00
|
|
|
PRIV_DATASTORE_MODIFY,
|
2020-04-27 05:13:06 +00:00
|
|
|
PRIV_DATASTORE_READ,
|
|
|
|
PRIV_DATASTORE_PRUNE,
|
2020-04-28 08:11:15 +00:00
|
|
|
PRIV_DATASTORE_BACKUP,
|
2020-04-27 05:13:06 +00:00
|
|
|
};
|
2019-01-15 10:38:26 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
fn check_backup_owner(
|
|
|
|
store: &DataStore,
|
|
|
|
group: &BackupGroup,
|
|
|
|
userid: &Userid,
|
|
|
|
) -> Result<(), Error> {
|
2020-04-28 08:11:15 +00:00
|
|
|
let owner = store.get_owner(group)?;
|
|
|
|
if &owner != userid {
|
|
|
|
bail!("backup owner check failed ({} != {})", userid, owner);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
fn read_backup_index(
|
|
|
|
store: &DataStore,
|
|
|
|
backup_dir: &BackupDir,
|
|
|
|
) -> Result<(BackupManifest, Vec<BackupContent>), Error> {
|
2019-08-05 11:22:19 +00:00
|
|
|
|
2020-07-31 08:25:30 +00:00
|
|
|
let (manifest, index_size) = store.load_manifest(backup_dir)?;
|
2019-08-05 11:22:19 +00:00
|
|
|
|
2020-01-23 10:16:57 +00:00
|
|
|
let mut result = Vec::new();
|
|
|
|
for item in manifest.files() {
|
|
|
|
result.push(BackupContent {
|
|
|
|
filename: item.filename.clone(),
|
2020-07-07 13:20:20 +00:00
|
|
|
crypt_mode: Some(item.crypt_mode),
|
2020-01-23 10:16:57 +00:00
|
|
|
size: Some(item.size),
|
|
|
|
});
|
2019-08-05 11:22:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 10:16:57 +00:00
|
|
|
result.push(BackupContent {
|
2020-05-30 12:04:15 +00:00
|
|
|
filename: MANIFEST_BLOB_NAME.to_string(),
|
2020-08-10 11:25:09 +00:00
|
|
|
crypt_mode: match manifest.signature {
|
|
|
|
Some(_) => Some(CryptMode::SignOnly),
|
|
|
|
None => Some(CryptMode::None),
|
|
|
|
},
|
2020-01-23 10:16:57 +00:00
|
|
|
size: Some(index_size),
|
|
|
|
});
|
2019-12-04 09:03:52 +00:00
|
|
|
|
2020-07-31 09:31:43 +00:00
|
|
|
Ok((manifest, result))
|
2019-08-05 11:22:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 11:55:24 +00:00
|
|
|
fn get_all_snapshot_files(
|
|
|
|
store: &DataStore,
|
|
|
|
info: &BackupInfo,
|
2020-07-31 09:31:43 +00:00
|
|
|
) -> Result<(BackupManifest, Vec<BackupContent>), Error> {
|
|
|
|
|
|
|
|
let (manifest, mut files) = read_backup_index(&store, &info.backup_dir)?;
|
2020-06-18 11:55:24 +00:00
|
|
|
|
|
|
|
let file_set = files.iter().fold(HashSet::new(), |mut acc, item| {
|
|
|
|
acc.insert(item.filename.clone());
|
|
|
|
acc
|
|
|
|
});
|
|
|
|
|
|
|
|
for file in &info.files {
|
|
|
|
if file_set.contains(file) { continue; }
|
2020-07-07 13:20:20 +00:00
|
|
|
files.push(BackupContent {
|
|
|
|
filename: file.to_string(),
|
|
|
|
size: None,
|
|
|
|
crypt_mode: None,
|
|
|
|
});
|
2020-06-18 11:55:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 09:31:43 +00:00
|
|
|
Ok((manifest, files))
|
2020-06-18 11:55:24 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 11:51:27 +00:00
|
|
|
fn group_backups(backup_list: Vec<BackupInfo>) -> HashMap<String, Vec<BackupInfo>> {
|
|
|
|
|
|
|
|
let mut group_hash = HashMap::new();
|
|
|
|
|
|
|
|
for info in backup_list {
|
2019-03-04 12:51:36 +00:00
|
|
|
let group_id = info.backup_dir.group().group_path().to_str().unwrap().to_owned();
|
2019-02-28 11:51:27 +00:00
|
|
|
let time_list = group_hash.entry(group_id).or_insert(vec![]);
|
|
|
|
time_list.push(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
group_hash
|
|
|
|
}
|
|
|
|
|
2020-01-17 09:17:18 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
type: Array,
|
|
|
|
description: "Returns the list of backup groups.",
|
|
|
|
items: {
|
|
|
|
type: GroupListItem,
|
|
|
|
}
|
|
|
|
},
|
2020-04-17 12:36:27 +00:00
|
|
|
access: {
|
2020-04-28 08:11:15 +00:00
|
|
|
permission: &Permission::Privilege(
|
|
|
|
&["datastore", "{store}"],
|
|
|
|
PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_BACKUP,
|
|
|
|
true),
|
2020-04-17 12:36:27 +00:00
|
|
|
},
|
2020-01-17 09:17:18 +00:00
|
|
|
)]
|
|
|
|
/// List backup groups.
|
2019-03-02 15:28:36 +00:00
|
|
|
fn list_groups(
|
2020-01-17 09:17:18 +00:00
|
|
|
store: String,
|
2020-04-28 08:11:15 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2020-01-17 09:17:18 +00:00
|
|
|
) -> Result<Vec<GroupListItem>, Error> {
|
2019-03-02 10:29:05 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-04-28 08:11:15 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2020-01-17 09:17:18 +00:00
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
2019-03-02 10:29:05 +00:00
|
|
|
|
2019-05-11 08:19:34 +00:00
|
|
|
let backup_list = BackupInfo::list_backups(&datastore.base_path())?;
|
2019-03-02 10:29:05 +00:00
|
|
|
|
|
|
|
let group_hash = group_backups(backup_list);
|
|
|
|
|
2020-01-17 09:17:18 +00:00
|
|
|
let mut groups = Vec::new();
|
2019-03-02 10:29:05 +00:00
|
|
|
|
|
|
|
for (_group_id, mut list) in group_hash {
|
|
|
|
|
2019-03-04 17:20:57 +00:00
|
|
|
BackupInfo::sort_list(&mut list, false);
|
2019-03-02 10:29:05 +00:00
|
|
|
|
|
|
|
let info = &list[0];
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2019-03-04 12:51:36 +00:00
|
|
|
let group = info.backup_dir.group();
|
2019-03-02 10:29:05 +00:00
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
let list_all = (user_privs & PRIV_DATASTORE_AUDIT) != 0;
|
2020-05-29 13:09:01 +00:00
|
|
|
let owner = datastore.get_owner(group)?;
|
2020-04-28 08:11:15 +00:00
|
|
|
if !list_all {
|
2020-08-06 13:46:01 +00:00
|
|
|
if owner != userid { continue; }
|
2020-04-28 08:11:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 09:17:18 +00:00
|
|
|
let result_item = GroupListItem {
|
|
|
|
backup_type: group.backup_type().to_string(),
|
|
|
|
backup_id: group.backup_id().to_string(),
|
|
|
|
last_backup: info.backup_dir.backup_time().timestamp(),
|
|
|
|
backup_count: list.len() as u64,
|
|
|
|
files: info.files.clone(),
|
2020-05-29 13:09:01 +00:00
|
|
|
owner: Some(owner),
|
2020-01-17 09:17:18 +00:00
|
|
|
};
|
|
|
|
groups.push(result_item);
|
2019-03-02 10:29:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 09:17:18 +00:00
|
|
|
Ok(groups)
|
2019-03-02 10:29:05 +00:00
|
|
|
}
|
2019-02-28 11:51:27 +00:00
|
|
|
|
2020-01-23 10:16:57 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-type": {
|
|
|
|
schema: BACKUP_TYPE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-id": {
|
|
|
|
schema: BACKUP_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-time": {
|
|
|
|
schema: BACKUP_TIME_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
type: Array,
|
|
|
|
description: "Returns the list of archive files inside a backup snapshots.",
|
|
|
|
items: {
|
|
|
|
type: BackupContent,
|
|
|
|
}
|
|
|
|
},
|
2020-04-17 12:36:27 +00:00
|
|
|
access: {
|
2020-04-28 08:11:15 +00:00
|
|
|
permission: &Permission::Privilege(
|
|
|
|
&["datastore", "{store}"],
|
|
|
|
PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_READ | PRIV_DATASTORE_BACKUP,
|
|
|
|
true),
|
2020-04-17 12:36:27 +00:00
|
|
|
},
|
2020-01-23 10:16:57 +00:00
|
|
|
)]
|
|
|
|
/// List snapshot files.
|
2020-02-26 12:49:47 +00:00
|
|
|
pub fn list_snapshot_files(
|
2020-01-23 10:16:57 +00:00
|
|
|
store: String,
|
|
|
|
backup_type: String,
|
|
|
|
backup_id: String,
|
|
|
|
backup_time: i64,
|
2019-03-06 09:49:59 +00:00
|
|
|
_info: &ApiMethod,
|
2020-04-28 08:11:15 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2020-01-23 10:16:57 +00:00
|
|
|
) -> Result<Vec<BackupContent>, Error> {
|
2019-03-06 09:49:59 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-04-28 08:11:15 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2020-01-23 10:16:57 +00:00
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2019-03-06 09:49:59 +00:00
|
|
|
let snapshot = BackupDir::new(backup_type, backup_id, backup_time);
|
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
let allowed = (user_privs & (PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_READ)) != 0;
|
2020-08-06 13:46:01 +00:00
|
|
|
if !allowed { check_backup_owner(&datastore, snapshot.group(), &userid)?; }
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2019-08-07 07:16:14 +00:00
|
|
|
let info = BackupInfo::new(&datastore.base_path(), snapshot)?;
|
2019-03-06 09:49:59 +00:00
|
|
|
|
2020-07-31 09:31:43 +00:00
|
|
|
let (_manifest, files) = get_all_snapshot_files(&datastore, &info)?;
|
|
|
|
|
|
|
|
Ok(files)
|
2019-03-06 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 09:16:45 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-type": {
|
|
|
|
schema: BACKUP_TYPE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-id": {
|
|
|
|
schema: BACKUP_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-time": {
|
|
|
|
schema: BACKUP_TIME_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:36:27 +00:00
|
|
|
access: {
|
2020-04-28 08:11:15 +00:00
|
|
|
permission: &Permission::Privilege(
|
|
|
|
&["datastore", "{store}"],
|
|
|
|
PRIV_DATASTORE_MODIFY| PRIV_DATASTORE_PRUNE,
|
|
|
|
true),
|
2020-04-17 12:36:27 +00:00
|
|
|
},
|
2020-01-23 09:16:45 +00:00
|
|
|
)]
|
|
|
|
/// Delete backup snapshot.
|
|
|
|
fn delete_snapshot(
|
|
|
|
store: String,
|
|
|
|
backup_type: String,
|
|
|
|
backup_id: String,
|
|
|
|
backup_time: i64,
|
2019-03-03 10:29:00 +00:00
|
|
|
_info: &ApiMethod,
|
2020-04-28 08:11:15 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-03-03 10:29:00 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-04-28 08:11:15 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2019-03-05 08:16:54 +00:00
|
|
|
let snapshot = BackupDir::new(backup_type, backup_id, backup_time);
|
2019-03-03 10:29:00 +00:00
|
|
|
|
2020-01-23 09:16:45 +00:00
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
2019-03-03 10:29:00 +00:00
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
let allowed = (user_privs & PRIV_DATASTORE_MODIFY) != 0;
|
2020-08-06 13:46:01 +00:00
|
|
|
if !allowed { check_backup_owner(&datastore, snapshot.group(), &userid)?; }
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2020-07-29 12:33:11 +00:00
|
|
|
datastore.remove_backup_dir(&snapshot, false)?;
|
2019-03-03 10:29:00 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2020-01-07 11:52:15 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-type": {
|
|
|
|
optional: true,
|
|
|
|
schema: BACKUP_TYPE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-id": {
|
|
|
|
optional: true,
|
|
|
|
schema: BACKUP_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
type: Array,
|
|
|
|
description: "Returns the list of snapshots.",
|
|
|
|
items: {
|
|
|
|
type: SnapshotListItem,
|
|
|
|
}
|
|
|
|
},
|
2020-04-17 12:36:27 +00:00
|
|
|
access: {
|
2020-04-28 08:11:15 +00:00
|
|
|
permission: &Permission::Privilege(
|
|
|
|
&["datastore", "{store}"],
|
|
|
|
PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_BACKUP,
|
|
|
|
true),
|
2020-04-17 12:36:27 +00:00
|
|
|
},
|
2020-01-07 11:52:15 +00:00
|
|
|
)]
|
|
|
|
/// List backup snapshots.
|
2020-02-27 10:27:44 +00:00
|
|
|
pub fn list_snapshots (
|
2020-04-28 08:11:15 +00:00
|
|
|
store: String,
|
|
|
|
backup_type: Option<String>,
|
|
|
|
backup_id: Option<String>,
|
|
|
|
_param: Value,
|
2019-03-02 15:20:50 +00:00
|
|
|
_info: &ApiMethod,
|
2020-04-28 08:11:15 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2020-01-07 11:52:15 +00:00
|
|
|
) -> Result<Vec<SnapshotListItem>, Error> {
|
2019-03-02 15:20:50 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-04-28 08:11:15 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2019-03-02 15:20:50 +00:00
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
2019-03-02 15:20:50 +00:00
|
|
|
|
2019-05-11 08:19:34 +00:00
|
|
|
let base_path = datastore.base_path();
|
2019-03-02 15:20:50 +00:00
|
|
|
|
2019-07-18 07:11:43 +00:00
|
|
|
let backup_list = BackupInfo::list_backups(&base_path)?;
|
2019-03-02 15:20:50 +00:00
|
|
|
|
|
|
|
let mut snapshots = vec![];
|
|
|
|
|
2019-05-11 08:19:34 +00:00
|
|
|
for info in backup_list {
|
2019-07-18 07:11:43 +00:00
|
|
|
let group = info.backup_dir.group();
|
2020-04-28 08:11:15 +00:00
|
|
|
if let Some(ref backup_type) = backup_type {
|
2019-07-18 07:11:43 +00:00
|
|
|
if backup_type != group.backup_type() { continue; }
|
|
|
|
}
|
2020-04-28 08:11:15 +00:00
|
|
|
if let Some(ref backup_id) = backup_id {
|
2019-07-18 07:11:43 +00:00
|
|
|
if backup_id != group.backup_id() { continue; }
|
|
|
|
}
|
2019-08-06 08:56:21 +00:00
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
let list_all = (user_privs & PRIV_DATASTORE_AUDIT) != 0;
|
2020-05-29 13:09:01 +00:00
|
|
|
let owner = datastore.get_owner(group)?;
|
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
if !list_all {
|
2020-08-06 13:46:01 +00:00
|
|
|
if owner != userid { continue; }
|
2020-04-28 08:11:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 11:55:24 +00:00
|
|
|
let mut size = None;
|
|
|
|
|
2020-07-31 09:31:43 +00:00
|
|
|
let (comment, files) = match get_all_snapshot_files(&datastore, &info) {
|
|
|
|
Ok((manifest, files)) => {
|
2020-06-18 11:55:24 +00:00
|
|
|
size = Some(files.iter().map(|x| x.size.unwrap_or(0)).sum());
|
2020-07-31 09:31:43 +00:00
|
|
|
// extract the first line from notes
|
|
|
|
let comment: Option<String> = manifest.unprotected["notes"]
|
|
|
|
.as_str()
|
|
|
|
.and_then(|notes| notes.lines().next())
|
|
|
|
.map(String::from);
|
|
|
|
|
|
|
|
(comment, files)
|
2020-06-18 11:55:24 +00:00
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("error during snapshot file listing: '{}'", err);
|
2020-07-31 09:31:43 +00:00
|
|
|
(
|
|
|
|
None,
|
|
|
|
info
|
|
|
|
.files
|
|
|
|
.iter()
|
|
|
|
.map(|x| BackupContent {
|
|
|
|
filename: x.to_string(),
|
|
|
|
size: None,
|
|
|
|
crypt_mode: None,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
)
|
2020-06-18 11:55:24 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let result_item = SnapshotListItem {
|
2020-01-07 11:52:15 +00:00
|
|
|
backup_type: group.backup_type().to_string(),
|
|
|
|
backup_id: group.backup_id().to_string(),
|
|
|
|
backup_time: info.backup_dir.backup_time().timestamp(),
|
2020-07-31 09:31:43 +00:00
|
|
|
comment,
|
2020-06-18 11:55:24 +00:00
|
|
|
files,
|
|
|
|
size,
|
2020-05-29 13:09:01 +00:00
|
|
|
owner: Some(owner),
|
2020-01-07 11:52:15 +00:00
|
|
|
};
|
2019-08-06 08:56:21 +00:00
|
|
|
|
|
|
|
snapshots.push(result_item);
|
2019-03-02 15:20:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 11:52:15 +00:00
|
|
|
Ok(snapshots)
|
2019-03-02 15:20:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 11:42:40 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
type: StorageStatus,
|
|
|
|
},
|
2020-04-17 12:36:27 +00:00
|
|
|
access: {
|
2020-04-28 08:11:15 +00:00
|
|
|
permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_BACKUP, true),
|
2020-04-17 12:36:27 +00:00
|
|
|
},
|
2020-01-23 11:42:40 +00:00
|
|
|
)]
|
|
|
|
/// Get datastore status.
|
2020-02-26 12:49:47 +00:00
|
|
|
pub fn status(
|
2020-01-23 11:42:40 +00:00
|
|
|
store: String,
|
2019-07-16 11:34:38 +00:00
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2020-01-23 11:42:40 +00:00
|
|
|
) -> Result<StorageStatus, Error> {
|
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
2020-06-09 08:01:09 +00:00
|
|
|
crate::tools::disks::disk_usage(&datastore.base_path())
|
2019-07-16 11:34:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:11:45 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-type": {
|
|
|
|
schema: BACKUP_TYPE_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"backup-id": {
|
|
|
|
schema: BACKUP_ID_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"backup-time": {
|
|
|
|
schema: BACKUP_TIME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_READ | PRIV_DATASTORE_BACKUP, true), // fixme
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Verify backups.
|
|
|
|
///
|
|
|
|
/// This function can verify a single backup snapshot, all backup from a backup group,
|
|
|
|
/// or all backups in the datastore.
|
|
|
|
pub fn verify(
|
|
|
|
store: String,
|
|
|
|
backup_type: Option<String>,
|
|
|
|
backup_id: Option<String>,
|
|
|
|
backup_time: Option<i64>,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
|
|
|
|
2020-06-25 10:55:34 +00:00
|
|
|
let worker_id;
|
2020-06-24 11:11:45 +00:00
|
|
|
|
|
|
|
let mut backup_dir = None;
|
|
|
|
let mut backup_group = None;
|
|
|
|
|
|
|
|
match (backup_type, backup_id, backup_time) {
|
|
|
|
(Some(backup_type), Some(backup_id), Some(backup_time)) => {
|
2020-06-30 11:11:22 +00:00
|
|
|
worker_id = format!("{}_{}_{}_{:08X}", store, backup_type, backup_id, backup_time);
|
2020-06-24 11:11:45 +00:00
|
|
|
let dir = BackupDir::new(backup_type, backup_id, backup_time);
|
|
|
|
backup_dir = Some(dir);
|
|
|
|
}
|
|
|
|
(Some(backup_type), Some(backup_id), None) => {
|
2020-06-30 11:11:22 +00:00
|
|
|
worker_id = format!("{}_{}_{}", store, backup_type, backup_id);
|
2020-06-24 11:11:45 +00:00
|
|
|
let group = BackupGroup::new(backup_type, backup_id);
|
|
|
|
backup_group = Some(group);
|
|
|
|
}
|
|
|
|
(None, None, None) => {
|
2020-06-25 10:55:34 +00:00
|
|
|
worker_id = store.clone();
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
2020-08-25 12:16:40 +00:00
|
|
|
_ => bail!("parameters do not specify a backup group or snapshot"),
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-06-24 11:11:45 +00:00
|
|
|
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
|
|
|
|
|
|
|
|
let upid_str = WorkerTask::new_thread(
|
2020-08-06 13:46:01 +00:00
|
|
|
"verify",
|
|
|
|
Some(worker_id.clone()),
|
|
|
|
userid,
|
|
|
|
to_stdout,
|
|
|
|
move |worker| {
|
2020-07-30 07:09:05 +00:00
|
|
|
let failed_dirs = if let Some(backup_dir) = backup_dir {
|
2020-07-29 11:29:13 +00:00
|
|
|
let mut verified_chunks = HashSet::with_capacity(1024*16);
|
2020-07-30 07:09:04 +00:00
|
|
|
let mut corrupt_chunks = HashSet::with_capacity(64);
|
2020-07-30 07:09:05 +00:00
|
|
|
let mut res = Vec::new();
|
|
|
|
if !verify_backup_dir(&datastore, &backup_dir, &mut verified_chunks, &mut corrupt_chunks, &worker)? {
|
|
|
|
res.push(backup_dir.to_string());
|
|
|
|
}
|
|
|
|
res
|
2020-06-24 11:11:45 +00:00
|
|
|
} else if let Some(backup_group) = backup_group {
|
2020-06-25 10:55:34 +00:00
|
|
|
verify_backup_group(&datastore, &backup_group, &worker)?
|
2020-06-24 11:11:45 +00:00
|
|
|
} else {
|
2020-06-25 10:55:34 +00:00
|
|
|
verify_all_backups(&datastore, &worker)?
|
2020-06-24 11:11:45 +00:00
|
|
|
};
|
2020-07-30 07:09:05 +00:00
|
|
|
if failed_dirs.len() > 0 {
|
|
|
|
worker.log("Failed to verify following snapshots:");
|
|
|
|
for dir in failed_dirs {
|
|
|
|
worker.log(format!("\t{}", dir));
|
|
|
|
}
|
2020-08-25 16:52:31 +00:00
|
|
|
bail!("verification failed - please check the log for details");
|
2020-06-24 11:11:45 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
2020-08-06 13:46:01 +00:00
|
|
|
},
|
|
|
|
)?;
|
2020-06-24 11:11:45 +00:00
|
|
|
|
|
|
|
Ok(json!(upid_str))
|
|
|
|
}
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! add_common_prune_prameters {
|
2019-11-21 12:10:49 +00:00
|
|
|
( [ $( $list1:tt )* ] ) => {
|
|
|
|
add_common_prune_prameters!([$( $list1 )* ] , [])
|
|
|
|
};
|
|
|
|
( [ $( $list1:tt )* ] , [ $( $list2:tt )* ] ) => {
|
2019-11-21 08:36:41 +00:00
|
|
|
[
|
2019-11-21 12:10:49 +00:00
|
|
|
$( $list1 )*
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
2019-11-21 12:10:49 +00:00
|
|
|
"keep-daily",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
2020-05-20 08:13:38 +00:00
|
|
|
&PRUNE_SCHEMA_KEEP_DAILY,
|
2019-11-21 08:36:41 +00:00
|
|
|
),
|
2019-12-07 10:23:33 +00:00
|
|
|
(
|
|
|
|
"keep-hourly",
|
|
|
|
true,
|
2020-05-20 08:13:38 +00:00
|
|
|
&PRUNE_SCHEMA_KEEP_HOURLY,
|
2019-12-07 10:23:33 +00:00
|
|
|
),
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
2019-11-21 12:10:49 +00:00
|
|
|
"keep-last",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
2020-05-20 08:13:38 +00:00
|
|
|
&PRUNE_SCHEMA_KEEP_LAST,
|
2019-11-21 08:36:41 +00:00
|
|
|
),
|
|
|
|
(
|
2019-11-21 12:10:49 +00:00
|
|
|
"keep-monthly",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
2020-05-20 08:13:38 +00:00
|
|
|
&PRUNE_SCHEMA_KEEP_MONTHLY,
|
2019-11-21 08:36:41 +00:00
|
|
|
),
|
|
|
|
(
|
2019-11-21 12:10:49 +00:00
|
|
|
"keep-weekly",
|
2019-11-21 08:36:41 +00:00
|
|
|
true,
|
2020-05-20 08:13:38 +00:00
|
|
|
&PRUNE_SCHEMA_KEEP_WEEKLY,
|
2019-11-21 08:36:41 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"keep-yearly",
|
|
|
|
true,
|
2020-05-20 08:13:38 +00:00
|
|
|
&PRUNE_SCHEMA_KEEP_YEARLY,
|
2019-11-21 08:36:41 +00:00
|
|
|
),
|
2019-11-21 12:10:49 +00:00
|
|
|
$( $list2 )*
|
2019-11-21 08:36:41 +00:00
|
|
|
]
|
|
|
|
}
|
2019-07-16 11:34:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 04:45:37 +00:00
|
|
|
pub const API_RETURN_SCHEMA_PRUNE: Schema = ArraySchema::new(
|
|
|
|
"Returns the list of snapshots and a flag indicating if there are kept or removed.",
|
2020-07-10 09:05:19 +00:00
|
|
|
&PruneListItem::API_SCHEMA
|
2020-05-05 04:45:37 +00:00
|
|
|
).schema();
|
|
|
|
|
2019-12-06 11:36:41 +00:00
|
|
|
const API_METHOD_PRUNE: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&prune),
|
2019-11-21 08:36:41 +00:00
|
|
|
&ObjectSchema::new(
|
2019-12-06 11:36:41 +00:00
|
|
|
"Prune the datastore.",
|
|
|
|
&add_common_prune_prameters!([
|
|
|
|
("backup-id", false, &BACKUP_ID_SCHEMA),
|
|
|
|
("backup-type", false, &BACKUP_TYPE_SCHEMA),
|
2019-12-08 09:59:47 +00:00
|
|
|
("dry-run", true, &BooleanSchema::new(
|
|
|
|
"Just show what prune would do, but do not delete anything.")
|
|
|
|
.schema()
|
|
|
|
),
|
2019-12-06 11:36:41 +00:00
|
|
|
],[
|
2019-12-11 12:45:23 +00:00
|
|
|
("store", false, &DATASTORE_SCHEMA),
|
2019-12-06 11:36:41 +00:00
|
|
|
])
|
2020-05-05 04:45:37 +00:00
|
|
|
))
|
|
|
|
.returns(&API_RETURN_SCHEMA_PRUNE)
|
|
|
|
.access(None, &Permission::Privilege(
|
2020-04-28 08:11:15 +00:00
|
|
|
&["datastore", "{store}"],
|
|
|
|
PRIV_DATASTORE_MODIFY | PRIV_DATASTORE_PRUNE,
|
|
|
|
true)
|
|
|
|
);
|
2019-11-21 08:36:41 +00:00
|
|
|
|
2019-02-27 15:53:17 +00:00
|
|
|
fn prune(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2020-04-28 08:11:15 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-02-27 15:53:17 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
let store = tools::required_string_param(¶m, "store")?;
|
2019-07-27 06:49:14 +00:00
|
|
|
let backup_type = tools::required_string_param(¶m, "backup-type")?;
|
|
|
|
let backup_id = tools::required_string_param(¶m, "backup-id")?;
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-04-28 08:11:15 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2019-12-08 09:59:47 +00:00
|
|
|
let dry_run = param["dry-run"].as_bool().unwrap_or(false);
|
|
|
|
|
2019-07-27 06:49:14 +00:00
|
|
|
let group = BackupGroup::new(backup_type, backup_id);
|
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
|
|
|
|
|
|
|
let allowed = (user_privs & PRIV_DATASTORE_MODIFY) != 0;
|
2020-08-06 13:46:01 +00:00
|
|
|
if !allowed { check_backup_owner(&datastore, &group, &userid)?; }
|
2019-02-27 15:53:17 +00:00
|
|
|
|
2019-12-06 11:28:31 +00:00
|
|
|
let prune_options = PruneOptions {
|
|
|
|
keep_last: param["keep-last"].as_u64(),
|
2019-12-07 10:23:33 +00:00
|
|
|
keep_hourly: param["keep-hourly"].as_u64(),
|
2019-12-06 11:28:31 +00:00
|
|
|
keep_daily: param["keep-daily"].as_u64(),
|
|
|
|
keep_weekly: param["keep-weekly"].as_u64(),
|
|
|
|
keep_monthly: param["keep-monthly"].as_u64(),
|
|
|
|
keep_yearly: param["keep-yearly"].as_u64(),
|
|
|
|
};
|
2019-02-28 11:51:27 +00:00
|
|
|
|
2019-12-14 15:05:21 +00:00
|
|
|
let worker_id = format!("{}_{}_{}", store, backup_type, backup_id);
|
|
|
|
|
2020-04-01 10:24:28 +00:00
|
|
|
let mut prune_result = Vec::new();
|
|
|
|
|
|
|
|
let list = group.list_backups(&datastore.base_path())?;
|
|
|
|
|
|
|
|
let mut prune_info = compute_prune_info(list, &prune_options)?;
|
|
|
|
|
|
|
|
prune_info.reverse(); // delete older snapshots first
|
|
|
|
|
|
|
|
let keep_all = !prune_options.keeps_something();
|
|
|
|
|
|
|
|
if dry_run {
|
|
|
|
for (info, mut keep) in prune_info {
|
|
|
|
if keep_all { keep = true; }
|
|
|
|
|
|
|
|
let backup_time = info.backup_dir.backup_time();
|
|
|
|
let group = info.backup_dir.group();
|
|
|
|
|
|
|
|
prune_result.push(json!({
|
|
|
|
"backup-type": group.backup_type(),
|
|
|
|
"backup-id": group.backup_id(),
|
|
|
|
"backup-time": backup_time.timestamp(),
|
|
|
|
"keep": keep,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return Ok(json!(prune_result));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-07 15:11:26 +00:00
|
|
|
// We use a WorkerTask just to have a task log, but run synchrounously
|
2020-08-06 13:46:01 +00:00
|
|
|
let worker = WorkerTask::new("prune", Some(worker_id), Userid::root_userid().clone(), true)?;
|
2020-04-01 10:24:28 +00:00
|
|
|
|
2019-06-25 06:12:25 +00:00
|
|
|
let result = try_block! {
|
2020-04-01 10:24:28 +00:00
|
|
|
if keep_all {
|
2019-07-27 06:49:14 +00:00
|
|
|
worker.log("No prune selection - keeping all files.");
|
2019-06-25 06:12:25 +00:00
|
|
|
} else {
|
2019-12-14 15:32:16 +00:00
|
|
|
worker.log(format!("retention options: {}", prune_options.cli_options_string()));
|
2020-04-01 10:24:28 +00:00
|
|
|
worker.log(format!("Starting prune on store \"{}\" group \"{}/{}\"",
|
|
|
|
store, backup_type, backup_id));
|
2019-06-25 06:12:25 +00:00
|
|
|
}
|
2019-02-28 11:51:27 +00:00
|
|
|
|
2020-04-01 10:24:28 +00:00
|
|
|
for (info, mut keep) in prune_info {
|
|
|
|
if keep_all { keep = true; }
|
2019-06-25 06:12:25 +00:00
|
|
|
|
2019-12-08 09:59:47 +00:00
|
|
|
let backup_time = info.backup_dir.backup_time();
|
|
|
|
let timestamp = BackupDir::backup_time_to_string(backup_time);
|
|
|
|
let group = info.backup_dir.group();
|
|
|
|
|
2020-04-01 10:24:28 +00:00
|
|
|
|
2019-12-08 09:59:47 +00:00
|
|
|
let msg = format!(
|
|
|
|
"{}/{}/{} {}",
|
|
|
|
group.backup_type(),
|
|
|
|
group.backup_id(),
|
|
|
|
timestamp,
|
|
|
|
if keep { "keep" } else { "remove" },
|
|
|
|
);
|
|
|
|
|
|
|
|
worker.log(msg);
|
|
|
|
|
2020-04-01 10:24:28 +00:00
|
|
|
prune_result.push(json!({
|
|
|
|
"backup-type": group.backup_type(),
|
|
|
|
"backup-id": group.backup_id(),
|
|
|
|
"backup-time": backup_time.timestamp(),
|
|
|
|
"keep": keep,
|
|
|
|
}));
|
|
|
|
|
2019-12-08 09:59:47 +00:00
|
|
|
if !(dry_run || keep) {
|
2020-07-29 12:33:11 +00:00
|
|
|
datastore.remove_backup_dir(&info.backup_dir, true)?;
|
2019-12-05 12:13:30 +00:00
|
|
|
}
|
2019-02-28 11:51:27 +00:00
|
|
|
}
|
2019-06-25 06:12:25 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
worker.log_result(&result);
|
|
|
|
|
|
|
|
if let Err(err) = result {
|
|
|
|
bail!("prune failed - {}", err);
|
2020-04-01 10:24:28 +00:00
|
|
|
};
|
2019-02-27 15:53:17 +00:00
|
|
|
|
2020-04-01 10:24:28 +00:00
|
|
|
Ok(json!(prune_result))
|
2019-02-27 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 12:01:21 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
2020-04-17 12:36:27 +00:00
|
|
|
access: {
|
2020-04-28 08:11:15 +00:00
|
|
|
permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_MODIFY, false),
|
2020-04-17 12:36:27 +00:00
|
|
|
},
|
2020-01-23 12:01:21 +00:00
|
|
|
)]
|
|
|
|
/// Start garbage collection.
|
2019-01-26 13:50:37 +00:00
|
|
|
fn start_garbage_collection(
|
2020-01-23 12:01:21 +00:00
|
|
|
store: String,
|
2019-01-26 13:50:37 +00:00
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-01-26 13:50:37 +00:00
|
|
|
) -> Result<Value, Error> {
|
2018-12-21 12:38:41 +00:00
|
|
|
|
2019-04-01 10:02:46 +00:00
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
2018-12-21 12:38:41 +00:00
|
|
|
|
2019-01-18 07:33:11 +00:00
|
|
|
println!("Starting garbage collection on store {}", store);
|
2018-12-21 12:38:41 +00:00
|
|
|
|
2019-04-06 09:27:23 +00:00
|
|
|
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
|
2018-12-21 12:38:41 +00:00
|
|
|
|
2019-04-06 09:27:23 +00:00
|
|
|
let upid_str = WorkerTask::new_thread(
|
2020-08-06 13:46:01 +00:00
|
|
|
"garbage_collection",
|
|
|
|
Some(store.clone()),
|
|
|
|
Userid::root_userid().clone(),
|
|
|
|
to_stdout,
|
|
|
|
move |worker| {
|
2019-04-06 09:27:23 +00:00
|
|
|
worker.log(format!("starting garbage collection on store {}", store));
|
2020-05-05 07:06:34 +00:00
|
|
|
datastore.garbage_collection(&worker)
|
2020-08-06 13:46:01 +00:00
|
|
|
},
|
|
|
|
)?;
|
2019-04-06 09:27:23 +00:00
|
|
|
|
|
|
|
Ok(json!(upid_str))
|
2018-12-21 12:38:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 12:31:52 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
type: GarbageCollectionStatus,
|
2020-04-17 12:36:27 +00:00
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT, false),
|
|
|
|
},
|
2020-01-23 12:31:52 +00:00
|
|
|
)]
|
|
|
|
/// Garbage collection status.
|
2020-01-30 12:26:46 +00:00
|
|
|
pub fn garbage_collection_status(
|
2020-01-23 12:31:52 +00:00
|
|
|
store: String,
|
2019-01-26 13:50:37 +00:00
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2020-01-23 12:31:52 +00:00
|
|
|
) -> Result<GarbageCollectionStatus, Error> {
|
2019-01-04 10:33:58 +00:00
|
|
|
|
2019-04-11 10:04:25 +00:00
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
|
|
|
|
|
|
|
let status = datastore.last_gc_status();
|
2019-01-04 10:33:58 +00:00
|
|
|
|
2020-01-23 12:31:52 +00:00
|
|
|
Ok(status)
|
2019-01-04 10:33:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 12:36:27 +00:00
|
|
|
#[api(
|
2020-04-29 07:21:34 +00:00
|
|
|
returns: {
|
|
|
|
description: "List the accessible datastores.",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
description: "Datastore name and description.",
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
comment: {
|
|
|
|
optional: true,
|
|
|
|
schema: SINGLE_LINE_COMMENT_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-04-17 12:36:27 +00:00
|
|
|
access: {
|
2020-04-28 08:11:15 +00:00
|
|
|
permission: &Permission::Anybody,
|
2020-04-17 12:36:27 +00:00
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Datastore list
|
2019-01-26 13:50:37 +00:00
|
|
|
fn get_datastore_list(
|
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
2020-04-28 08:11:15 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
2019-01-26 13:50:37 +00:00
|
|
|
) -> Result<Value, Error> {
|
2018-12-21 12:38:41 +00:00
|
|
|
|
2020-01-14 11:57:03 +00:00
|
|
|
let (config, _digest) = datastore::config()?;
|
2018-12-21 12:38:41 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-04-28 08:11:15 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
|
|
|
|
2020-04-29 07:21:34 +00:00
|
|
|
let mut list = Vec::new();
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2020-04-29 07:21:34 +00:00
|
|
|
for (store, (_, data)) in &config.sections {
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-04-28 08:11:15 +00:00
|
|
|
let allowed = (user_privs & (PRIV_DATASTORE_AUDIT| PRIV_DATASTORE_BACKUP)) != 0;
|
2020-04-29 07:21:34 +00:00
|
|
|
if allowed {
|
|
|
|
let mut entry = json!({ "store": store });
|
|
|
|
if let Some(comment) = data["comment"].as_str() {
|
|
|
|
entry["comment"] = comment.into();
|
|
|
|
}
|
|
|
|
list.push(entry);
|
|
|
|
}
|
2020-04-28 08:11:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 07:21:34 +00:00
|
|
|
Ok(list.into())
|
2018-12-21 12:38:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 11:36:41 +00:00
|
|
|
#[sortable]
|
|
|
|
pub const API_METHOD_DOWNLOAD_FILE: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::AsyncHttp(&download_file),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Download single raw file from backup snapshot.",
|
|
|
|
&sorted!([
|
2019-12-11 12:45:23 +00:00
|
|
|
("store", false, &DATASTORE_SCHEMA),
|
2019-12-06 11:36:41 +00:00
|
|
|
("backup-type", false, &BACKUP_TYPE_SCHEMA),
|
|
|
|
("backup-id", false, &BACKUP_ID_SCHEMA),
|
|
|
|
("backup-time", false, &BACKUP_TIME_SCHEMA),
|
2020-01-23 12:49:30 +00:00
|
|
|
("file-name", false, &BACKUP_ARCHIVE_NAME_SCHEMA),
|
2019-12-06 11:36:41 +00:00
|
|
|
]),
|
|
|
|
)
|
2020-04-28 08:11:15 +00:00
|
|
|
).access(None, &Permission::Privilege(
|
|
|
|
&["datastore", "{store}"],
|
|
|
|
PRIV_DATASTORE_READ | PRIV_DATASTORE_BACKUP,
|
|
|
|
true)
|
|
|
|
);
|
2019-01-04 10:33:58 +00:00
|
|
|
|
2019-06-25 08:16:59 +00:00
|
|
|
fn download_file(
|
|
|
|
_parts: Parts,
|
|
|
|
_req_body: Body,
|
|
|
|
param: Value,
|
2019-11-21 08:36:41 +00:00
|
|
|
_info: &ApiMethod,
|
2020-04-28 08:11:15 +00:00
|
|
|
rpcenv: Box<dyn RpcEnvironment>,
|
2019-12-16 08:59:45 +00:00
|
|
|
) -> ApiResponseFuture {
|
2019-06-25 08:16:59 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
async move {
|
|
|
|
let store = tools::required_string_param(¶m, "store")?;
|
|
|
|
let datastore = DataStore::lookup_datastore(store)?;
|
2019-06-25 10:59:36 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-04-28 08:11:15 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
let file_name = tools::required_string_param(¶m, "file-name")?.to_owned();
|
2019-06-25 08:16:59 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
let backup_type = tools::required_string_param(¶m, "backup-type")?;
|
|
|
|
let backup_id = tools::required_string_param(¶m, "backup-id")?;
|
|
|
|
let backup_time = tools::required_integer_param(¶m, "backup-time")?;
|
2019-06-25 08:16:59 +00:00
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
let backup_dir = BackupDir::new(backup_type, backup_id, backup_time);
|
|
|
|
|
|
|
|
let allowed = (user_privs & PRIV_DATASTORE_READ) != 0;
|
2020-08-06 13:46:01 +00:00
|
|
|
if !allowed { check_backup_owner(&datastore, backup_dir.group(), &userid)?; }
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2020-06-19 06:05:09 +00:00
|
|
|
println!("Download {} from {} ({}/{})", file_name, store, backup_dir, file_name);
|
2019-06-25 08:16:59 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
let mut path = datastore.base_path();
|
|
|
|
path.push(backup_dir.relative_path());
|
|
|
|
path.push(&file_name);
|
|
|
|
|
2020-06-18 11:55:20 +00:00
|
|
|
let file = tokio::fs::File::open(&path)
|
2020-07-29 07:38:11 +00:00
|
|
|
.await
|
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
|
2019-11-22 12:02:05 +00:00
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
|
2020-06-18 11:55:20 +00:00
|
|
|
.map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze()))
|
|
|
|
.map_err(move |err| {
|
|
|
|
eprintln!("error during streaming of '{:?}' - {}", &path, err);
|
|
|
|
err
|
|
|
|
});
|
2019-11-22 12:02:05 +00:00
|
|
|
let body = Body::wrap_stream(payload);
|
2019-06-25 08:16:59 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
// fixme: set other headers ?
|
|
|
|
Ok(Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.header(header::CONTENT_TYPE, "application/octet-stream")
|
|
|
|
.body(body)
|
|
|
|
.unwrap())
|
|
|
|
}.boxed()
|
2019-06-25 08:16:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 11:55:29 +00:00
|
|
|
#[sortable]
|
|
|
|
pub const API_METHOD_DOWNLOAD_FILE_DECODED: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::AsyncHttp(&download_file_decoded),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Download single decoded file from backup snapshot. Only works if it's not encrypted.",
|
|
|
|
&sorted!([
|
|
|
|
("store", false, &DATASTORE_SCHEMA),
|
|
|
|
("backup-type", false, &BACKUP_TYPE_SCHEMA),
|
|
|
|
("backup-id", false, &BACKUP_ID_SCHEMA),
|
|
|
|
("backup-time", false, &BACKUP_TIME_SCHEMA),
|
|
|
|
("file-name", false, &BACKUP_ARCHIVE_NAME_SCHEMA),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
).access(None, &Permission::Privilege(
|
|
|
|
&["datastore", "{store}"],
|
|
|
|
PRIV_DATASTORE_READ | PRIV_DATASTORE_BACKUP,
|
|
|
|
true)
|
|
|
|
);
|
|
|
|
|
|
|
|
fn download_file_decoded(
|
|
|
|
_parts: Parts,
|
|
|
|
_req_body: Body,
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
rpcenv: Box<dyn RpcEnvironment>,
|
|
|
|
) -> ApiResponseFuture {
|
|
|
|
|
|
|
|
async move {
|
|
|
|
let store = tools::required_string_param(¶m, "store")?;
|
|
|
|
let datastore = DataStore::lookup_datastore(store)?;
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-06-18 11:55:29 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-06-18 11:55:29 +00:00
|
|
|
|
|
|
|
let file_name = tools::required_string_param(¶m, "file-name")?.to_owned();
|
|
|
|
|
|
|
|
let backup_type = tools::required_string_param(¶m, "backup-type")?;
|
|
|
|
let backup_id = tools::required_string_param(¶m, "backup-id")?;
|
|
|
|
let backup_time = tools::required_integer_param(¶m, "backup-time")?;
|
|
|
|
|
|
|
|
let backup_dir = BackupDir::new(backup_type, backup_id, backup_time);
|
|
|
|
|
|
|
|
let allowed = (user_privs & PRIV_DATASTORE_READ) != 0;
|
2020-08-06 13:46:01 +00:00
|
|
|
if !allowed { check_backup_owner(&datastore, backup_dir.group(), &userid)?; }
|
2020-06-18 11:55:29 +00:00
|
|
|
|
2020-08-10 11:25:06 +00:00
|
|
|
let (manifest, files) = read_backup_index(&datastore, &backup_dir)?;
|
2020-06-18 11:55:29 +00:00
|
|
|
for file in files {
|
2020-07-07 13:20:20 +00:00
|
|
|
if file.filename == file_name && file.crypt_mode == Some(CryptMode::Encrypt) {
|
2020-06-18 11:55:29 +00:00
|
|
|
bail!("cannot decode '{}' - is encrypted", file_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("Download {} from {} ({}/{})", file_name, store, backup_dir, file_name);
|
|
|
|
|
|
|
|
let mut path = datastore.base_path();
|
|
|
|
path.push(backup_dir.relative_path());
|
|
|
|
path.push(&file_name);
|
|
|
|
|
|
|
|
let extension = file_name.rsplitn(2, '.').next().unwrap();
|
|
|
|
|
|
|
|
let body = match extension {
|
|
|
|
"didx" => {
|
|
|
|
let index = DynamicIndexReader::open(&path)
|
|
|
|
.map_err(|err| format_err!("unable to read dynamic index '{:?}' - {}", &path, err))?;
|
2020-08-10 11:25:06 +00:00
|
|
|
let (csum, size) = index.compute_csum();
|
|
|
|
manifest.verify_file(&file_name, &csum, size)?;
|
2020-06-18 11:55:29 +00:00
|
|
|
|
2020-08-10 11:25:07 +00:00
|
|
|
let chunk_reader = LocalChunkReader::new(datastore, None, CryptMode::None);
|
2020-06-18 11:55:29 +00:00
|
|
|
let reader = AsyncIndexReader::new(index, chunk_reader);
|
2020-06-22 14:44:13 +00:00
|
|
|
Body::wrap_stream(AsyncReaderStream::new(reader)
|
2020-06-18 11:55:29 +00:00
|
|
|
.map_err(move |err| {
|
|
|
|
eprintln!("error during streaming of '{:?}' - {}", path, err);
|
|
|
|
err
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
"fidx" => {
|
|
|
|
let index = FixedIndexReader::open(&path)
|
|
|
|
.map_err(|err| format_err!("unable to read fixed index '{:?}' - {}", &path, err))?;
|
|
|
|
|
2020-08-10 11:25:06 +00:00
|
|
|
let (csum, size) = index.compute_csum();
|
|
|
|
manifest.verify_file(&file_name, &csum, size)?;
|
|
|
|
|
2020-08-10 11:25:07 +00:00
|
|
|
let chunk_reader = LocalChunkReader::new(datastore, None, CryptMode::None);
|
2020-06-18 11:55:29 +00:00
|
|
|
let reader = AsyncIndexReader::new(index, chunk_reader);
|
2020-06-22 14:44:13 +00:00
|
|
|
Body::wrap_stream(AsyncReaderStream::with_buffer_size(reader, 4*1024*1024)
|
2020-06-18 11:55:29 +00:00
|
|
|
.map_err(move |err| {
|
|
|
|
eprintln!("error during streaming of '{:?}' - {}", path, err);
|
|
|
|
err
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
"blob" => {
|
|
|
|
let file = std::fs::File::open(&path)
|
2020-07-29 07:38:11 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
|
2020-06-18 11:55:29 +00:00
|
|
|
|
2020-08-10 11:25:06 +00:00
|
|
|
// FIXME: load full blob to verify index checksum?
|
|
|
|
|
2020-06-18 11:55:29 +00:00
|
|
|
Body::wrap_stream(
|
|
|
|
WrappedReaderStream::new(DataBlobReader::new(file, None)?)
|
|
|
|
.map_err(move |err| {
|
|
|
|
eprintln!("error during streaming of '{:?}' - {}", path, err);
|
|
|
|
err
|
|
|
|
})
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extension => {
|
|
|
|
bail!("cannot download '{}' files", extension);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// fixme: set other headers ?
|
|
|
|
Ok(Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.header(header::CONTENT_TYPE, "application/octet-stream")
|
|
|
|
.body(body)
|
|
|
|
.unwrap())
|
|
|
|
}.boxed()
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-12-06 11:36:41 +00:00
|
|
|
pub const API_METHOD_UPLOAD_BACKUP_LOG: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::AsyncHttp(&upload_backup_log),
|
2019-11-21 08:36:41 +00:00
|
|
|
&ObjectSchema::new(
|
2020-04-28 08:11:15 +00:00
|
|
|
"Upload the client backup log file into a backup snapshot ('client.log.blob').",
|
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),
|
2019-12-06 11:36:41 +00:00
|
|
|
("backup-id", false, &BACKUP_ID_SCHEMA),
|
2019-11-21 08:36:41 +00:00
|
|
|
("backup-time", false, &BACKUP_TIME_SCHEMA),
|
2019-11-21 12:10:49 +00:00
|
|
|
]),
|
2019-06-25 08:16:59 +00:00
|
|
|
)
|
2020-04-28 08:11:15 +00:00
|
|
|
).access(
|
|
|
|
Some("Only the backup creator/owner is allowed to do this."),
|
|
|
|
&Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_BACKUP, false)
|
|
|
|
);
|
2019-06-25 08:16:59 +00:00
|
|
|
|
2019-07-25 08:12:45 +00:00
|
|
|
fn upload_backup_log(
|
|
|
|
_parts: Parts,
|
|
|
|
req_body: Body,
|
|
|
|
param: Value,
|
2019-11-21 08:36:41 +00:00
|
|
|
_info: &ApiMethod,
|
2020-04-28 08:11:15 +00:00
|
|
|
rpcenv: Box<dyn RpcEnvironment>,
|
2019-12-16 08:59:45 +00:00
|
|
|
) -> ApiResponseFuture {
|
2019-07-25 08:12:45 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
async move {
|
|
|
|
let store = tools::required_string_param(¶m, "store")?;
|
|
|
|
let datastore = DataStore::lookup_datastore(store)?;
|
2019-07-25 08:12:45 +00:00
|
|
|
|
2020-05-30 12:04:15 +00:00
|
|
|
let file_name = CLIENT_LOG_BLOB_NAME;
|
2019-07-25 08:12:45 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
let backup_type = tools::required_string_param(¶m, "backup-type")?;
|
|
|
|
let backup_id = tools::required_string_param(¶m, "backup-id")?;
|
|
|
|
let backup_time = tools::required_integer_param(¶m, "backup-time")?;
|
2019-07-25 08:12:45 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
let backup_dir = BackupDir::new(backup_type, backup_id, backup_time);
|
2019-07-25 08:12:45 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
|
|
|
check_backup_owner(&datastore, backup_dir.group(), &userid)?;
|
2020-04-28 08:11:15 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
let mut path = datastore.base_path();
|
|
|
|
path.push(backup_dir.relative_path());
|
|
|
|
path.push(&file_name);
|
2019-07-25 08:12:45 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
if path.exists() {
|
|
|
|
bail!("backup already contains a log.");
|
|
|
|
}
|
2019-07-25 10:23:33 +00:00
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
println!("Upload backup log to {}/{}/{}/{}/{}", store,
|
|
|
|
backup_type, backup_id, BackupDir::backup_time_to_string(backup_dir.backup_time()), file_name);
|
|
|
|
|
|
|
|
let data = req_body
|
|
|
|
.map_err(Error::from)
|
|
|
|
.try_fold(Vec::new(), |mut acc, chunk| {
|
|
|
|
acc.extend_from_slice(&*chunk);
|
|
|
|
future::ok::<_, Error>(acc)
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
// always verify blob/CRC at server side
|
|
|
|
let blob = DataBlob::load_from_reader(&mut &data[..])?;
|
|
|
|
|
|
|
|
replace_file(&path, blob.raw_data(), CreateOptions::new())?;
|
2019-11-22 12:02:05 +00:00
|
|
|
|
|
|
|
// fixme: use correct formatter
|
|
|
|
Ok(crate::server::formatter::json_response(Ok(Value::Null)))
|
|
|
|
}.boxed()
|
2019-07-25 08:12:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:09:52 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-type": {
|
|
|
|
schema: BACKUP_TYPE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-id": {
|
|
|
|
schema: BACKUP_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-time": {
|
|
|
|
schema: BACKUP_TIME_SCHEMA,
|
|
|
|
},
|
|
|
|
"filepath": {
|
|
|
|
description: "Base64 encoded path.",
|
|
|
|
type: String,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_READ | PRIV_DATASTORE_BACKUP, true),
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Get the entries of the given path of the catalog
|
|
|
|
fn catalog(
|
|
|
|
store: String,
|
|
|
|
backup_type: String,
|
|
|
|
backup_id: String,
|
|
|
|
backup_time: i64,
|
|
|
|
filepath: String,
|
|
|
|
_param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-06-23 10:09:52 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-06-23 10:09:52 +00:00
|
|
|
|
|
|
|
let backup_dir = BackupDir::new(backup_type, backup_id, backup_time);
|
|
|
|
|
|
|
|
let allowed = (user_privs & PRIV_DATASTORE_READ) != 0;
|
2020-08-06 13:46:01 +00:00
|
|
|
if !allowed { check_backup_owner(&datastore, backup_dir.group(), &userid)?; }
|
2020-06-23 10:09:52 +00:00
|
|
|
|
2020-08-10 11:25:05 +00:00
|
|
|
let file_name = CATALOG_NAME;
|
|
|
|
|
2020-08-10 11:25:06 +00:00
|
|
|
let (manifest, files) = read_backup_index(&datastore, &backup_dir)?;
|
2020-08-10 11:25:05 +00:00
|
|
|
for file in files {
|
|
|
|
if file.filename == file_name && file.crypt_mode == Some(CryptMode::Encrypt) {
|
|
|
|
bail!("cannot decode '{}' - is encrypted", file_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 10:09:52 +00:00
|
|
|
let mut path = datastore.base_path();
|
|
|
|
path.push(backup_dir.relative_path());
|
2020-08-10 11:25:05 +00:00
|
|
|
path.push(file_name);
|
2020-06-23 10:09:52 +00:00
|
|
|
|
|
|
|
let index = DynamicIndexReader::open(&path)
|
|
|
|
.map_err(|err| format_err!("unable to read dynamic index '{:?}' - {}", &path, err))?;
|
|
|
|
|
2020-08-10 11:25:06 +00:00
|
|
|
let (csum, size) = index.compute_csum();
|
|
|
|
manifest.verify_file(&file_name, &csum, size)?;
|
|
|
|
|
2020-08-10 11:25:07 +00:00
|
|
|
let chunk_reader = LocalChunkReader::new(datastore, None, CryptMode::None);
|
2020-06-23 10:09:52 +00:00
|
|
|
let reader = BufferedDynamicReader::new(index, chunk_reader);
|
|
|
|
|
|
|
|
let mut catalog_reader = CatalogReader::new(reader);
|
|
|
|
let mut current = catalog_reader.root()?;
|
|
|
|
let mut components = vec![];
|
|
|
|
|
|
|
|
|
|
|
|
if filepath != "root" {
|
|
|
|
components = base64::decode(filepath)?;
|
|
|
|
if components.len() > 0 && components[0] == '/' as u8 {
|
|
|
|
components.remove(0);
|
|
|
|
}
|
|
|
|
for component in components.split(|c| *c == '/' as u8) {
|
|
|
|
if let Some(entry) = catalog_reader.lookup(¤t, component)? {
|
|
|
|
current = entry;
|
|
|
|
} else {
|
|
|
|
bail!("path {:?} not found in catalog", &String::from_utf8_lossy(&components));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut res = Vec::new();
|
|
|
|
|
|
|
|
for direntry in catalog_reader.read_dir(¤t)? {
|
|
|
|
let mut components = components.clone();
|
|
|
|
components.push('/' as u8);
|
|
|
|
components.extend(&direntry.name);
|
|
|
|
let path = base64::encode(components);
|
|
|
|
let text = String::from_utf8_lossy(&direntry.name);
|
|
|
|
let mut entry = json!({
|
|
|
|
"filepath": path,
|
|
|
|
"text": text,
|
|
|
|
"type": CatalogEntryType::from(&direntry.attr).to_string(),
|
|
|
|
"leaf": true,
|
|
|
|
});
|
|
|
|
match direntry.attr {
|
|
|
|
DirEntryAttribute::Directory { start: _ } => {
|
|
|
|
entry["leaf"] = false.into();
|
|
|
|
},
|
|
|
|
DirEntryAttribute::File { size, mtime } => {
|
|
|
|
entry["size"] = size.into();
|
|
|
|
entry["mtime"] = mtime.into();
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
res.push(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res.into())
|
|
|
|
}
|
|
|
|
|
2020-06-23 10:09:53 +00:00
|
|
|
#[sortable]
|
|
|
|
pub const API_METHOD_PXAR_FILE_DOWNLOAD: ApiMethod = ApiMethod::new(
|
|
|
|
&ApiHandler::AsyncHttp(&pxar_file_download),
|
|
|
|
&ObjectSchema::new(
|
2020-08-25 16:52:31 +00:00
|
|
|
"Download single file from pxar file of a backup snapshot. Only works if it's not encrypted.",
|
2020-06-23 10:09:53 +00:00
|
|
|
&sorted!([
|
|
|
|
("store", false, &DATASTORE_SCHEMA),
|
|
|
|
("backup-type", false, &BACKUP_TYPE_SCHEMA),
|
|
|
|
("backup-id", false, &BACKUP_ID_SCHEMA),
|
|
|
|
("backup-time", false, &BACKUP_TIME_SCHEMA),
|
|
|
|
("filepath", false, &StringSchema::new("Base64 encoded path").schema()),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
).access(None, &Permission::Privilege(
|
|
|
|
&["datastore", "{store}"],
|
|
|
|
PRIV_DATASTORE_READ | PRIV_DATASTORE_BACKUP,
|
|
|
|
true)
|
|
|
|
);
|
|
|
|
|
|
|
|
fn pxar_file_download(
|
|
|
|
_parts: Parts,
|
|
|
|
_req_body: Body,
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
rpcenv: Box<dyn RpcEnvironment>,
|
|
|
|
) -> ApiResponseFuture {
|
|
|
|
|
|
|
|
async move {
|
|
|
|
let store = tools::required_string_param(¶m, "store")?;
|
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-06-23 10:09:53 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-06-23 10:09:53 +00:00
|
|
|
|
|
|
|
let filepath = tools::required_string_param(¶m, "filepath")?.to_owned();
|
|
|
|
|
|
|
|
let backup_type = tools::required_string_param(¶m, "backup-type")?;
|
|
|
|
let backup_id = tools::required_string_param(¶m, "backup-id")?;
|
|
|
|
let backup_time = tools::required_integer_param(¶m, "backup-time")?;
|
|
|
|
|
|
|
|
let backup_dir = BackupDir::new(backup_type, backup_id, backup_time);
|
|
|
|
|
|
|
|
let allowed = (user_privs & PRIV_DATASTORE_READ) != 0;
|
2020-08-06 13:46:01 +00:00
|
|
|
if !allowed { check_backup_owner(&datastore, backup_dir.group(), &userid)?; }
|
2020-06-23 10:09:53 +00:00
|
|
|
|
|
|
|
let mut components = base64::decode(&filepath)?;
|
|
|
|
if components.len() > 0 && components[0] == '/' as u8 {
|
|
|
|
components.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut split = components.splitn(2, |c| *c == '/' as u8);
|
2020-08-10 11:25:05 +00:00
|
|
|
let pxar_name = std::str::from_utf8(split.next().unwrap())?;
|
2020-06-23 10:09:53 +00:00
|
|
|
let file_path = split.next().ok_or(format_err!("filepath looks strange '{}'", filepath))?;
|
2020-08-10 11:25:06 +00:00
|
|
|
let (manifest, files) = read_backup_index(&datastore, &backup_dir)?;
|
2020-08-10 11:25:05 +00:00
|
|
|
for file in files {
|
|
|
|
if file.filename == pxar_name && file.crypt_mode == Some(CryptMode::Encrypt) {
|
|
|
|
bail!("cannot decode '{}' - is encrypted", pxar_name);
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 10:09:53 +00:00
|
|
|
|
2020-08-10 11:25:05 +00:00
|
|
|
let mut path = datastore.base_path();
|
|
|
|
path.push(backup_dir.relative_path());
|
|
|
|
path.push(pxar_name);
|
2020-06-23 10:09:53 +00:00
|
|
|
|
|
|
|
let index = DynamicIndexReader::open(&path)
|
|
|
|
.map_err(|err| format_err!("unable to read dynamic index '{:?}' - {}", &path, err))?;
|
|
|
|
|
2020-08-10 11:25:06 +00:00
|
|
|
let (csum, size) = index.compute_csum();
|
|
|
|
manifest.verify_file(&pxar_name, &csum, size)?;
|
|
|
|
|
2020-08-10 11:25:07 +00:00
|
|
|
let chunk_reader = LocalChunkReader::new(datastore, None, CryptMode::None);
|
2020-06-23 10:09:53 +00:00
|
|
|
let reader = BufferedDynamicReader::new(index, chunk_reader);
|
|
|
|
let archive_size = reader.archive_size();
|
|
|
|
let reader = LocalDynamicReadAt::new(reader);
|
|
|
|
|
|
|
|
let decoder = Accessor::new(reader, archive_size).await?;
|
|
|
|
let root = decoder.open_root().await?;
|
|
|
|
let file = root
|
|
|
|
.lookup(OsStr::from_bytes(file_path)).await?
|
|
|
|
.ok_or(format_err!("error opening '{:?}'", file_path))?;
|
|
|
|
|
|
|
|
let file = match file.kind() {
|
|
|
|
EntryKind::File { .. } => file,
|
|
|
|
EntryKind::Hardlink(_) => {
|
|
|
|
decoder.follow_hardlink(&file).await?
|
|
|
|
},
|
|
|
|
// TODO symlink
|
|
|
|
other => bail!("cannot download file of type {:?}", other),
|
|
|
|
};
|
|
|
|
|
|
|
|
let body = Body::wrap_stream(
|
|
|
|
AsyncReaderStream::new(file.contents().await?)
|
|
|
|
.map_err(move |err| {
|
|
|
|
eprintln!("error during streaming of '{:?}' - {}", filepath, err);
|
|
|
|
err
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// fixme: set other headers ?
|
|
|
|
Ok(Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.header(header::CONTENT_TYPE, "application/octet-stream")
|
|
|
|
.body(body)
|
|
|
|
.unwrap())
|
|
|
|
}.boxed()
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:19:04 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
timeframe: {
|
|
|
|
type: RRDTimeFrameResolution,
|
|
|
|
},
|
|
|
|
cf: {
|
|
|
|
type: RRDMode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_BACKUP, true),
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Read datastore stats
|
|
|
|
fn get_rrd_stats(
|
|
|
|
store: String,
|
|
|
|
timeframe: RRDTimeFrameResolution,
|
|
|
|
cf: RRDMode,
|
|
|
|
_param: Value,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2020-06-10 10:02:57 +00:00
|
|
|
create_value_from_rrd(
|
|
|
|
&format!("datastore/{}", store),
|
2020-05-26 10:19:04 +00:00
|
|
|
&[
|
|
|
|
"total", "used",
|
2020-05-28 17:11:37 +00:00
|
|
|
"read_ios", "read_bytes",
|
|
|
|
"write_ios", "write_bytes",
|
|
|
|
"io_ticks",
|
2020-05-26 10:19:04 +00:00
|
|
|
],
|
|
|
|
timeframe,
|
|
|
|
cf,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-31 08:17:35 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-type": {
|
|
|
|
schema: BACKUP_TYPE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-id": {
|
|
|
|
schema: BACKUP_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-time": {
|
|
|
|
schema: BACKUP_TIME_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_READ | PRIV_DATASTORE_BACKUP, true),
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Get "notes" for a specific backup
|
|
|
|
fn get_notes(
|
|
|
|
store: String,
|
|
|
|
backup_type: String,
|
|
|
|
backup_id: String,
|
|
|
|
backup_time: i64,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<String, Error> {
|
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-07-31 08:17:35 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-07-31 08:17:35 +00:00
|
|
|
|
|
|
|
let backup_dir = BackupDir::new(backup_type, backup_id, backup_time);
|
|
|
|
|
|
|
|
let allowed = (user_privs & PRIV_DATASTORE_READ) != 0;
|
2020-08-06 13:46:01 +00:00
|
|
|
if !allowed { check_backup_owner(&datastore, backup_dir.group(), &userid)?; }
|
2020-07-31 08:17:35 +00:00
|
|
|
|
|
|
|
let manifest = datastore.load_manifest_json(&backup_dir)?;
|
|
|
|
|
|
|
|
let notes = manifest["unprotected"]["notes"]
|
|
|
|
.as_str()
|
|
|
|
.unwrap_or("");
|
|
|
|
|
|
|
|
Ok(String::from(notes))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-type": {
|
|
|
|
schema: BACKUP_TYPE_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-id": {
|
|
|
|
schema: BACKUP_ID_SCHEMA,
|
|
|
|
},
|
|
|
|
"backup-time": {
|
|
|
|
schema: BACKUP_TIME_SCHEMA,
|
|
|
|
},
|
|
|
|
notes: {
|
|
|
|
description: "A multiline text.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_MODIFY, true),
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Set "notes" for a specific backup
|
|
|
|
fn set_notes(
|
|
|
|
store: String,
|
|
|
|
backup_type: String,
|
|
|
|
backup_id: String,
|
|
|
|
backup_time: i64,
|
|
|
|
notes: String,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let datastore = DataStore::lookup_datastore(&store)?;
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
2020-07-31 08:17:35 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
let user_privs = user_info.lookup_privs(&userid, &["datastore", &store]);
|
2020-07-31 08:17:35 +00:00
|
|
|
|
|
|
|
let backup_dir = BackupDir::new(backup_type, backup_id, backup_time);
|
|
|
|
|
|
|
|
let allowed = (user_privs & PRIV_DATASTORE_READ) != 0;
|
2020-08-06 13:46:01 +00:00
|
|
|
if !allowed { check_backup_owner(&datastore, backup_dir.group(), &userid)?; }
|
2020-07-31 08:17:35 +00:00
|
|
|
|
|
|
|
let mut manifest = datastore.load_manifest_json(&backup_dir)?;
|
|
|
|
|
|
|
|
manifest["unprotected"]["notes"] = notes.into();
|
|
|
|
|
|
|
|
datastore.store_manifest(&backup_dir, manifest)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:10:49 +00:00
|
|
|
#[sortable]
|
2019-11-21 08:36:41 +00:00
|
|
|
const DATASTORE_INFO_SUBDIRS: SubdirMap = &[
|
2020-06-23 10:09:52 +00:00
|
|
|
(
|
|
|
|
"catalog",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_CATALOG)
|
|
|
|
),
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"download",
|
|
|
|
&Router::new()
|
|
|
|
.download(&API_METHOD_DOWNLOAD_FILE)
|
|
|
|
),
|
2020-06-18 11:55:29 +00:00
|
|
|
(
|
|
|
|
"download-decoded",
|
|
|
|
&Router::new()
|
|
|
|
.download(&API_METHOD_DOWNLOAD_FILE_DECODED)
|
|
|
|
),
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"files",
|
|
|
|
&Router::new()
|
2020-01-23 10:16:57 +00:00
|
|
|
.get(&API_METHOD_LIST_SNAPSHOT_FILES)
|
2019-11-21 08:36:41 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"gc",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_GARBAGE_COLLECTION_STATUS)
|
|
|
|
.post(&API_METHOD_START_GARBAGE_COLLECTION)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"groups",
|
|
|
|
&Router::new()
|
2020-01-17 09:17:18 +00:00
|
|
|
.get(&API_METHOD_LIST_GROUPS)
|
2019-11-21 08:36:41 +00:00
|
|
|
),
|
2020-07-31 08:17:35 +00:00
|
|
|
(
|
|
|
|
"notes",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_GET_NOTES)
|
|
|
|
.put(&API_METHOD_SET_NOTES)
|
|
|
|
),
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"prune",
|
|
|
|
&Router::new()
|
|
|
|
.post(&API_METHOD_PRUNE)
|
|
|
|
),
|
2020-06-23 10:09:53 +00:00
|
|
|
(
|
|
|
|
"pxar-file-download",
|
|
|
|
&Router::new()
|
|
|
|
.download(&API_METHOD_PXAR_FILE_DOWNLOAD)
|
|
|
|
),
|
2020-05-26 10:19:04 +00:00
|
|
|
(
|
|
|
|
"rrd",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_GET_RRD_STATS)
|
|
|
|
),
|
2019-11-21 08:36:41 +00:00
|
|
|
(
|
|
|
|
"snapshots",
|
|
|
|
&Router::new()
|
2020-01-07 11:52:15 +00:00
|
|
|
.get(&API_METHOD_LIST_SNAPSHOTS)
|
2020-01-23 09:16:45 +00:00
|
|
|
.delete(&API_METHOD_DELETE_SNAPSHOT)
|
2019-11-21 08:36:41 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"status",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_STATUS)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"upload-backup-log",
|
|
|
|
&Router::new()
|
|
|
|
.upload(&API_METHOD_UPLOAD_BACKUP_LOG)
|
|
|
|
),
|
2020-06-24 11:11:45 +00:00
|
|
|
(
|
|
|
|
"verify",
|
|
|
|
&Router::new()
|
|
|
|
.post(&API_METHOD_VERIFY)
|
|
|
|
),
|
2019-11-21 08:36:41 +00:00
|
|
|
];
|
|
|
|
|
2019-11-22 12:02:05 +00:00
|
|
|
const DATASTORE_INFO_ROUTER: Router = Router::new()
|
2019-11-21 08:36:41 +00:00
|
|
|
.get(&list_subdirs_api_method!(DATASTORE_INFO_SUBDIRS))
|
|
|
|
.subdirs(DATASTORE_INFO_SUBDIRS);
|
|
|
|
|
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
2020-04-17 12:36:27 +00:00
|
|
|
.get(&API_METHOD_GET_DATASTORE_LIST)
|
2019-11-21 08:36:41 +00:00
|
|
|
.match_all("store", &DATASTORE_INFO_ROUTER);
|