2020-05-28 08:06:44 +00:00
|
|
|
use anyhow::{Error};
|
2019-08-21 12:09:27 +00:00
|
|
|
use lazy_static::lazy_static;
|
2020-01-11 08:18:42 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use serde::{Serialize, Deserialize};
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2020-03-02 11:52:11 +00:00
|
|
|
use proxmox::api::{
|
|
|
|
api,
|
|
|
|
schema::*,
|
|
|
|
section_config::{
|
|
|
|
SectionConfig,
|
|
|
|
SectionConfigData,
|
|
|
|
SectionConfigPlugin,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-11 08:18:42 +00:00
|
|
|
use proxmox::tools::{fs::replace_file, fs::CreateOptions};
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2020-01-11 09:42:09 +00:00
|
|
|
use crate::api2::types::*;
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2019-08-21 12:11:07 +00:00
|
|
|
lazy_static! {
|
2018-12-08 12:58:45 +00:00
|
|
|
static ref CONFIG: SectionConfig = init();
|
|
|
|
}
|
|
|
|
|
2020-01-11 08:18:42 +00:00
|
|
|
// fixme: define better schemas
|
|
|
|
pub const DIR_NAME_SCHEMA: Schema = StringSchema::new("Directory name").schema();
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
properties: {
|
2020-05-20 09:29:59 +00:00
|
|
|
name: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
|
|
|
path: {
|
|
|
|
schema: DIR_NAME_SCHEMA,
|
|
|
|
},
|
2020-01-11 08:18:42 +00:00
|
|
|
comment: {
|
|
|
|
optional: true,
|
2020-01-13 11:02:13 +00:00
|
|
|
schema: SINGLE_LINE_COMMENT_SCHEMA,
|
2020-01-11 08:18:42 +00:00
|
|
|
},
|
2020-05-20 06:38:10 +00:00
|
|
|
"gc-schedule": {
|
2020-05-20 09:29:59 +00:00
|
|
|
optional: true,
|
2020-05-20 06:38:10 +00:00
|
|
|
schema: GC_SCHEDULE_SCHEMA,
|
2020-05-20 09:29:59 +00:00
|
|
|
},
|
|
|
|
"prune-schedule": {
|
2020-05-20 06:38:10 +00:00
|
|
|
optional: true,
|
2020-05-20 09:29:59 +00:00
|
|
|
schema: PRUNE_SCHEDULE_SCHEMA,
|
2020-05-20 06:38:10 +00:00
|
|
|
},
|
2020-05-20 09:29:59 +00:00
|
|
|
"keep-last": {
|
|
|
|
optional: true,
|
|
|
|
schema: PRUNE_SCHEMA_KEEP_LAST,
|
|
|
|
},
|
|
|
|
"keep-hourly": {
|
|
|
|
optional: true,
|
|
|
|
schema: PRUNE_SCHEMA_KEEP_HOURLY,
|
|
|
|
},
|
|
|
|
"keep-daily": {
|
|
|
|
optional: true,
|
|
|
|
schema: PRUNE_SCHEMA_KEEP_DAILY,
|
|
|
|
},
|
|
|
|
"keep-weekly": {
|
|
|
|
optional: true,
|
|
|
|
schema: PRUNE_SCHEMA_KEEP_WEEKLY,
|
|
|
|
},
|
|
|
|
"keep-monthly": {
|
|
|
|
optional: true,
|
|
|
|
schema: PRUNE_SCHEMA_KEEP_MONTHLY,
|
|
|
|
},
|
|
|
|
"keep-yearly": {
|
|
|
|
optional: true,
|
|
|
|
schema: PRUNE_SCHEMA_KEEP_YEARLY,
|
2020-01-11 08:18:42 +00:00
|
|
|
},
|
2020-10-20 08:08:25 +00:00
|
|
|
"verify-new": {
|
|
|
|
optional: true,
|
|
|
|
type: bool,
|
|
|
|
},
|
2020-01-11 08:18:42 +00:00
|
|
|
}
|
|
|
|
)]
|
2020-05-20 06:38:10 +00:00
|
|
|
#[serde(rename_all="kebab-case")]
|
2020-01-11 08:18:42 +00:00
|
|
|
#[derive(Serialize,Deserialize)]
|
|
|
|
/// Datastore configuration properties.
|
|
|
|
pub struct DataStoreConfig {
|
2020-05-20 09:29:59 +00:00
|
|
|
pub name: String,
|
2020-01-13 11:14:14 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-01-11 08:18:42 +00:00
|
|
|
pub comment: Option<String>,
|
|
|
|
pub path: String,
|
2020-05-20 06:38:10 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
|
|
|
pub gc_schedule: Option<String>,
|
2020-05-20 09:29:59 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
|
|
|
pub prune_schedule: Option<String>,
|
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-05-20 11:00:13 +00:00
|
|
|
pub keep_last: Option<u64>,
|
2020-05-20 09:29:59 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-05-20 11:00:13 +00:00
|
|
|
pub keep_hourly: Option<u64>,
|
2020-05-20 09:29:59 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-05-20 11:00:13 +00:00
|
|
|
pub keep_daily: Option<u64>,
|
2020-05-20 09:29:59 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-05-20 11:00:13 +00:00
|
|
|
pub keep_weekly: Option<u64>,
|
2020-05-20 09:29:59 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-05-20 11:00:13 +00:00
|
|
|
pub keep_monthly: Option<u64>,
|
2020-05-20 09:29:59 +00:00
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
2020-05-20 11:00:13 +00:00
|
|
|
pub keep_yearly: Option<u64>,
|
2020-10-20 08:08:25 +00:00
|
|
|
/// If enabled, all backups will be verified right after completion.
|
|
|
|
#[serde(skip_serializing_if="Option::is_none")]
|
|
|
|
pub verify_new: Option<bool>,
|
2020-05-20 09:29:59 +00:00
|
|
|
}
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
fn init() -> SectionConfig {
|
2020-01-11 08:18:42 +00:00
|
|
|
let obj_schema = match DataStoreConfig::API_SCHEMA {
|
|
|
|
Schema::Object(ref obj_schema) => obj_schema,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2020-05-20 09:29:59 +00:00
|
|
|
let plugin = SectionConfigPlugin::new("datastore".to_string(), Some(String::from("name")), obj_schema);
|
2020-01-11 09:42:09 +00:00
|
|
|
let mut config = SectionConfig::new(&DATASTORE_SCHEMA);
|
2018-12-08 12:58:45 +00:00
|
|
|
config.register_plugin(plugin);
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
2020-01-15 10:57:12 +00:00
|
|
|
pub const DATASTORE_CFG_FILENAME: &str = "/etc/proxmox-backup/datastore.cfg";
|
|
|
|
pub const DATASTORE_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.datastore.lck";
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2020-01-14 11:57:03 +00:00
|
|
|
pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
|
2020-05-28 08:06:44 +00:00
|
|
|
|
|
|
|
let content = proxmox::tools::fs::file_read_optional_string(DATASTORE_CFG_FILENAME)?;
|
|
|
|
let content = content.unwrap_or(String::from(""));
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2020-01-14 11:57:03 +00:00
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
|
|
|
let data = CONFIG.parse(DATASTORE_CFG_FILENAME, &content)?;
|
|
|
|
Ok((data, digest))
|
2018-12-08 12:58:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 11:51:31 +00:00
|
|
|
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
|
|
|
|
let raw = CONFIG.write(DATASTORE_CFG_FILENAME, &config)?;
|
|
|
|
|
2019-12-19 09:20:13 +00:00
|
|
|
let backup_user = crate::backup::backup_user()?;
|
2019-12-18 09:41:58 +00:00
|
|
|
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0640);
|
|
|
|
// set the correct owner/group/permissions while saving file
|
|
|
|
// owner(rw) = root, group(r)= backup
|
|
|
|
let options = CreateOptions::new()
|
|
|
|
.perm(mode)
|
|
|
|
.owner(nix::unistd::ROOT)
|
2019-12-19 09:20:13 +00:00
|
|
|
.group(backup_user.gid);
|
2019-12-18 09:41:58 +00:00
|
|
|
|
|
|
|
replace_file(DATASTORE_CFG_FILENAME, raw.as_bytes(), options)?;
|
2018-12-09 11:51:31 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-12 11:18:28 +00:00
|
|
|
|
|
|
|
// shell completion helper
|
2019-03-12 13:39:51 +00:00
|
|
|
pub fn complete_datastore_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
2018-12-12 11:41:59 +00:00
|
|
|
match config() {
|
2020-01-14 11:57:03 +00:00
|
|
|
Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
|
2018-12-12 11:18:28 +00:00
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 06:40:53 +00:00
|
|
|
|
|
|
|
pub fn complete_acl_path(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
list.push(String::from("/"));
|
2020-10-19 07:54:29 +00:00
|
|
|
list.push(String::from("/datastore"));
|
|
|
|
list.push(String::from("/datastore/"));
|
2020-04-14 06:40:53 +00:00
|
|
|
|
|
|
|
if let Ok((data, _digest)) = config() {
|
|
|
|
for id in data.sections.keys() {
|
2020-10-19 07:54:29 +00:00
|
|
|
list.push(format!("/datastore/{}", id));
|
2020-04-14 06:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list
|
|
|
|
}
|
2020-05-20 07:41:58 +00:00
|
|
|
|
|
|
|
pub fn complete_calendar_event(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
// just give some hints about possible values
|
|
|
|
["minutely", "hourly", "daily", "mon..fri", "0:0"]
|
|
|
|
.iter().map(|s| String::from(*s)).collect()
|
|
|
|
}
|