2022-05-09 13:39:29 +00:00
|
|
|
use std::convert::TryFrom;
|
2022-04-20 13:30:04 +00:00
|
|
|
use std::fmt;
|
2019-05-11 08:19:34 +00:00
|
|
|
use std::os::unix::io::RawFd;
|
2022-04-20 13:30:04 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
2020-11-03 05:36:50 +00:00
|
|
|
|
2022-04-24 16:37:15 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2021-07-06 11:26:35 +00:00
|
|
|
|
2022-04-24 16:49:09 +00:00
|
|
|
use proxmox_sys::fs::lock_dir_noblock;
|
|
|
|
|
2022-04-25 08:42:23 +00:00
|
|
|
use pbs_api_types::{
|
2022-05-09 13:39:29 +00:00
|
|
|
Authid, BackupNamespace, BackupType, GroupFilter, BACKUP_DATE_REGEX, BACKUP_FILE_REGEX,
|
2022-04-25 08:42:23 +00:00
|
|
|
};
|
2022-04-24 16:37:15 +00:00
|
|
|
use pbs_config::{open_backup_lockfile, BackupLockGuard};
|
2019-03-05 06:18:12 +00:00
|
|
|
|
2022-05-09 13:39:29 +00:00
|
|
|
use crate::manifest::{BackupManifest, MANIFEST_BLOB_NAME, MANIFEST_LOCK_NAME};
|
2022-04-28 08:26:00 +00:00
|
|
|
use crate::{DataBlob, DataStore};
|
2020-01-06 10:35:22 +00:00
|
|
|
|
2019-03-05 06:28:13 +00:00
|
|
|
/// BackupGroup is a directory containing a list of BackupDir
|
2022-04-20 13:30:04 +00:00
|
|
|
#[derive(Clone)]
|
2019-03-05 06:18:12 +00:00
|
|
|
pub struct BackupGroup {
|
2022-04-20 13:30:04 +00:00
|
|
|
store: Arc<DataStore>,
|
|
|
|
|
2022-05-09 13:39:29 +00:00
|
|
|
ns: BackupNamespace,
|
2022-04-19 08:38:46 +00:00
|
|
|
group: pbs_api_types::BackupGroup,
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 13:30:04 +00:00
|
|
|
impl fmt::Debug for BackupGroup {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("BackupGroup")
|
|
|
|
.field("store", &self.store.name())
|
|
|
|
.field("group", &self.group)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:18:12 +00:00
|
|
|
impl BackupGroup {
|
2022-05-09 13:39:29 +00:00
|
|
|
pub(crate) fn new(
|
|
|
|
store: Arc<DataStore>,
|
|
|
|
ns: BackupNamespace,
|
|
|
|
group: pbs_api_types::BackupGroup,
|
|
|
|
) -> Self {
|
|
|
|
Self { store, ns, group }
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 10:20:28 +00:00
|
|
|
/// Access the underlying [`BackupGroup`](pbs_api_types::BackupGroup).
|
|
|
|
#[inline]
|
|
|
|
pub fn group(&self) -> &pbs_api_types::BackupGroup {
|
|
|
|
&self.group
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:42:23 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn backup_ns(&self) -> &BackupNamespace {
|
2022-05-09 13:39:29 +00:00
|
|
|
&self.ns
|
2022-04-25 08:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-04-14 13:05:58 +00:00
|
|
|
pub fn backup_type(&self) -> BackupType {
|
2022-04-19 08:38:46 +00:00
|
|
|
self.group.ty
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 08:42:23 +00:00
|
|
|
#[inline]
|
2019-03-05 06:18:12 +00:00
|
|
|
pub fn backup_id(&self) -> &str {
|
2022-04-19 08:38:46 +00:00
|
|
|
&self.group.id
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 13:30:04 +00:00
|
|
|
pub fn full_group_path(&self) -> PathBuf {
|
2022-05-09 13:39:29 +00:00
|
|
|
self.store.group_path(&self.ns, &self.group)
|
2022-04-20 13:30:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 07:45:22 +00:00
|
|
|
pub fn relative_group_path(&self) -> PathBuf {
|
2022-05-09 13:39:29 +00:00
|
|
|
let mut path = self.store.namespace_path(&self.ns);
|
|
|
|
path.push(self.group.ty.as_str());
|
|
|
|
path.push(&self.group.id);
|
|
|
|
path
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
2019-05-11 08:19:34 +00:00
|
|
|
|
2022-04-20 13:30:04 +00:00
|
|
|
pub fn list_backups(&self) -> Result<Vec<BackupInfo>, Error> {
|
2019-05-11 08:19:34 +00:00
|
|
|
let mut list = vec![];
|
|
|
|
|
2022-04-24 16:03:27 +00:00
|
|
|
let path = self.full_group_path();
|
2019-05-11 08:19:34 +00:00
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
proxmox_sys::fs::scandir(
|
2021-03-14 18:18:35 +00:00
|
|
|
libc::AT_FDCWD,
|
|
|
|
&path,
|
|
|
|
&BACKUP_DATE_REGEX,
|
|
|
|
|l2_fd, backup_time, file_type| {
|
|
|
|
if file_type != nix::dir::Type::Directory {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-05-11 08:19:34 +00:00
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
let backup_dir = self.backup_dir_with_rfc3339(backup_time)?;
|
2021-03-14 18:18:35 +00:00
|
|
|
let files = list_backup_files(l2_fd, backup_time)?;
|
2019-05-11 08:19:34 +00:00
|
|
|
|
2022-04-20 13:30:04 +00:00
|
|
|
let protected = backup_dir.is_protected();
|
2021-10-27 11:22:26 +00:00
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
list.push(BackupInfo {
|
|
|
|
backup_dir,
|
|
|
|
files,
|
|
|
|
protected,
|
|
|
|
});
|
2019-05-11 08:19:34 +00:00
|
|
|
|
2021-03-14 18:18:35 +00:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
)?;
|
2019-05-11 08:19:34 +00:00
|
|
|
Ok(list)
|
|
|
|
}
|
2019-12-04 14:49:11 +00:00
|
|
|
|
2022-04-20 07:55:48 +00:00
|
|
|
/// Finds the latest backup inside a backup group
|
2022-04-20 13:30:04 +00:00
|
|
|
pub fn last_backup(&self, only_finished: bool) -> Result<Option<BackupInfo>, Error> {
|
|
|
|
let backups = self.list_backups()?;
|
2022-04-20 07:55:48 +00:00
|
|
|
Ok(backups
|
|
|
|
.into_iter()
|
|
|
|
.filter(|item| !only_finished || item.is_finished())
|
|
|
|
.max_by_key(|item| item.backup_dir.backup_time()))
|
|
|
|
}
|
|
|
|
|
2022-04-20 13:30:04 +00:00
|
|
|
pub fn last_successful_backup(&self) -> Result<Option<i64>, Error> {
|
2020-01-06 10:35:22 +00:00
|
|
|
let mut last = None;
|
|
|
|
|
2022-04-24 16:03:27 +00:00
|
|
|
let path = self.full_group_path();
|
2020-01-06 10:35:22 +00:00
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
proxmox_sys::fs::scandir(
|
2021-03-14 18:18:35 +00:00
|
|
|
libc::AT_FDCWD,
|
|
|
|
&path,
|
|
|
|
&BACKUP_DATE_REGEX,
|
|
|
|
|l2_fd, backup_time, file_type| {
|
|
|
|
if file_type != nix::dir::Type::Directory {
|
|
|
|
return Ok(());
|
2020-01-06 10:35:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 18:18:35 +00:00
|
|
|
let mut manifest_path = PathBuf::from(backup_time);
|
|
|
|
manifest_path.push(MANIFEST_BLOB_NAME);
|
|
|
|
|
|
|
|
use nix::fcntl::{openat, OFlag};
|
|
|
|
match openat(
|
|
|
|
l2_fd,
|
|
|
|
&manifest_path,
|
|
|
|
OFlag::O_RDONLY,
|
|
|
|
nix::sys::stat::Mode::empty(),
|
|
|
|
) {
|
|
|
|
Ok(rawfd) => {
|
|
|
|
/* manifest exists --> assume backup was successful */
|
|
|
|
/* close else this leaks! */
|
|
|
|
nix::unistd::close(rawfd)?;
|
|
|
|
}
|
|
|
|
Err(nix::Error::Sys(nix::errno::Errno::ENOENT)) => {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
bail!("last_successful_backup: unexpected error - {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
let timestamp = proxmox_time::parse_rfc3339(backup_time)?;
|
2021-03-14 18:18:35 +00:00
|
|
|
if let Some(last_timestamp) = last {
|
|
|
|
if timestamp > last_timestamp {
|
|
|
|
last = Some(timestamp);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
last = Some(timestamp);
|
|
|
|
}
|
2020-01-06 10:35:22 +00:00
|
|
|
|
2021-03-14 18:18:35 +00:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
)?;
|
2020-01-06 10:35:22 +00:00
|
|
|
|
|
|
|
Ok(last)
|
|
|
|
}
|
2021-10-28 13:00:50 +00:00
|
|
|
|
|
|
|
pub fn matches(&self, filter: &GroupFilter) -> bool {
|
2022-04-19 08:38:46 +00:00
|
|
|
self.group.matches(filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn backup_dir(&self, time: i64) -> Result<BackupDir, Error> {
|
|
|
|
BackupDir::with_group(self.clone(), time)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn backup_dir_with_rfc3339<T: Into<String>>(
|
|
|
|
&self,
|
|
|
|
time_string: T,
|
|
|
|
) -> Result<BackupDir, Error> {
|
|
|
|
BackupDir::with_rfc3339(self.clone(), time_string.into())
|
|
|
|
}
|
2022-04-20 13:30:04 +00:00
|
|
|
|
|
|
|
pub fn iter_snapshots(&self) -> Result<crate::ListSnapshots, Error> {
|
|
|
|
crate::ListSnapshots::new(self.clone())
|
|
|
|
}
|
2022-04-24 16:49:09 +00:00
|
|
|
|
|
|
|
/// Destroy the group inclusive all its backup snapshots (BackupDir's)
|
|
|
|
///
|
|
|
|
/// Returns true if all snapshots were removed, and false if some were protected
|
|
|
|
pub fn destroy(&self) -> Result<bool, Error> {
|
|
|
|
let path = self.full_group_path();
|
|
|
|
let _guard =
|
|
|
|
proxmox_sys::fs::lock_dir_noblock(&path, "backup group", "possible running backup")?;
|
|
|
|
|
|
|
|
log::info!("removing backup group {:?}", path);
|
|
|
|
let mut removed_all_snaps = true;
|
|
|
|
for snap in self.iter_snapshots()? {
|
|
|
|
let snap = snap?;
|
|
|
|
if snap.is_protected() {
|
|
|
|
removed_all_snaps = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
snap.destroy(false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if removed_all_snaps {
|
|
|
|
std::fs::remove_dir_all(&path).map_err(|err| {
|
|
|
|
format_err!("removing group directory {:?} failed - {}", path, err)
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(removed_all_snaps)
|
|
|
|
}
|
2022-05-09 13:39:29 +00:00
|
|
|
|
|
|
|
/// Returns the backup owner.
|
|
|
|
///
|
|
|
|
/// The backup owner is the entity who first created the backup group.
|
|
|
|
pub fn get_owner(&self) -> Result<Authid, Error> {
|
|
|
|
self.store.get_owner(&self.ns, self.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the backup owner.
|
|
|
|
pub fn set_owner(&self, auth_id: &Authid, force: bool) -> Result<(), Error> {
|
|
|
|
self.store
|
|
|
|
.set_owner(&self.ns, &self.as_ref(), auth_id, force)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<pbs_api_types::BackupNamespace> for BackupGroup {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &pbs_api_types::BackupNamespace {
|
|
|
|
&self.ns
|
|
|
|
}
|
2022-04-19 08:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<pbs_api_types::BackupGroup> for BackupGroup {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &pbs_api_types::BackupGroup {
|
|
|
|
&self.group
|
2021-10-28 13:00:50 +00:00
|
|
|
}
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 13:05:58 +00:00
|
|
|
impl From<&BackupGroup> for pbs_api_types::BackupGroup {
|
|
|
|
fn from(group: &BackupGroup) -> pbs_api_types::BackupGroup {
|
2022-04-19 08:38:46 +00:00
|
|
|
group.group.clone()
|
2022-04-14 13:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BackupGroup> for pbs_api_types::BackupGroup {
|
|
|
|
fn from(group: BackupGroup) -> pbs_api_types::BackupGroup {
|
2022-04-19 08:38:46 +00:00
|
|
|
group.group
|
2022-04-14 13:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:04:59 +00:00
|
|
|
impl fmt::Display for BackupGroup {
|
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-05-09 13:39:29 +00:00
|
|
|
if self.ns.is_root() {
|
|
|
|
fmt::Display::fmt(&self.group, f)
|
|
|
|
} else {
|
|
|
|
write!(f, "[{}]:{}", self.ns, self.group)
|
|
|
|
}
|
2020-06-24 10:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
impl From<BackupDir> for BackupGroup {
|
|
|
|
fn from(dir: BackupDir) -> BackupGroup {
|
|
|
|
BackupGroup {
|
2022-04-20 13:30:04 +00:00
|
|
|
store: dir.store,
|
2022-05-09 13:39:29 +00:00
|
|
|
ns: dir.ns,
|
2022-04-19 08:38:46 +00:00
|
|
|
group: dir.dir.group,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 06:16:56 +00:00
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
impl From<&BackupDir> for BackupGroup {
|
|
|
|
fn from(dir: &BackupDir) -> BackupGroup {
|
|
|
|
BackupGroup {
|
2022-04-20 13:30:04 +00:00
|
|
|
store: Arc::clone(&dir.store),
|
2022-05-09 13:39:29 +00:00
|
|
|
ns: dir.ns.clone(),
|
2022-04-19 08:38:46 +00:00
|
|
|
group: dir.dir.group.clone(),
|
|
|
|
}
|
2020-06-23 06:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:18:12 +00:00
|
|
|
/// Uniquely identify a Backup (relative to data store)
|
2019-03-05 06:28:13 +00:00
|
|
|
///
|
|
|
|
/// We also call this a backup snaphost.
|
2022-04-20 13:30:04 +00:00
|
|
|
#[derive(Clone)]
|
2019-03-05 06:18:12 +00:00
|
|
|
pub struct BackupDir {
|
2022-04-20 13:30:04 +00:00
|
|
|
store: Arc<DataStore>,
|
2022-05-09 13:39:29 +00:00
|
|
|
ns: BackupNamespace,
|
2022-04-19 08:38:46 +00:00
|
|
|
dir: pbs_api_types::BackupDir,
|
2020-09-12 13:10:47 +00:00
|
|
|
// backup_time as rfc3339
|
2021-03-14 18:18:35 +00:00
|
|
|
backup_time_string: String,
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 13:30:04 +00:00
|
|
|
impl fmt::Debug for BackupDir {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("BackupDir")
|
|
|
|
.field("store", &self.store.name())
|
|
|
|
.field("dir", &self.dir)
|
|
|
|
.field("backup_time_string", &self.backup_time_string)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:18:12 +00:00
|
|
|
impl BackupDir {
|
2022-04-19 08:38:46 +00:00
|
|
|
/// Temporarily used for tests.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn new_test(dir: pbs_api_types::BackupDir) -> Self {
|
|
|
|
Self {
|
2022-04-20 13:30:04 +00:00
|
|
|
store: unsafe { DataStore::new_test() },
|
2022-04-19 08:38:46 +00:00
|
|
|
backup_time_string: Self::backup_time_to_string(dir.time).unwrap(),
|
2022-05-09 13:39:29 +00:00
|
|
|
ns: BackupNamespace::root(),
|
2022-04-19 08:38:46 +00:00
|
|
|
dir,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn with_group(group: BackupGroup, backup_time: i64) -> Result<Self, Error> {
|
|
|
|
let backup_time_string = Self::backup_time_to_string(backup_time)?;
|
2021-03-14 18:18:35 +00:00
|
|
|
Ok(Self {
|
2022-04-20 13:30:04 +00:00
|
|
|
store: group.store,
|
2022-05-09 13:39:29 +00:00
|
|
|
ns: group.ns,
|
2022-04-19 08:38:46 +00:00
|
|
|
dir: (group.group, backup_time).into(),
|
2021-03-14 18:18:35 +00:00
|
|
|
backup_time_string,
|
|
|
|
})
|
2020-09-15 07:34:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
pub(crate) fn with_rfc3339(
|
|
|
|
group: BackupGroup,
|
|
|
|
backup_time_string: String,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
let backup_time = proxmox_time::parse_rfc3339(&backup_time_string)?;
|
2021-03-14 18:18:35 +00:00
|
|
|
Ok(Self {
|
2022-04-20 13:30:04 +00:00
|
|
|
store: group.store,
|
2022-05-09 13:39:29 +00:00
|
|
|
ns: group.ns,
|
2022-04-19 08:38:46 +00:00
|
|
|
dir: (group.group, backup_time).into(),
|
2021-03-14 18:18:35 +00:00
|
|
|
backup_time_string,
|
|
|
|
})
|
2019-05-11 10:07:09 +00:00
|
|
|
}
|
2019-03-05 06:18:12 +00:00
|
|
|
|
2022-04-25 08:42:23 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn backup_ns(&self) -> &BackupNamespace {
|
2022-05-09 13:39:29 +00:00
|
|
|
&self.ns
|
2022-04-25 08:42:23 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn backup_type(&self) -> BackupType {
|
|
|
|
self.dir.group.ty
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn backup_id(&self) -> &str {
|
|
|
|
&self.dir.group.id
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-09-12 13:10:47 +00:00
|
|
|
pub fn backup_time(&self) -> i64 {
|
2022-04-19 08:38:46 +00:00
|
|
|
self.dir.time
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 13:10:47 +00:00
|
|
|
pub fn backup_time_string(&self) -> &str {
|
|
|
|
&self.backup_time_string
|
|
|
|
}
|
|
|
|
|
2021-03-14 18:18:35 +00:00
|
|
|
pub fn relative_path(&self) -> PathBuf {
|
2022-05-11 10:17:45 +00:00
|
|
|
let mut path = self.ns.path();
|
2022-05-09 13:39:29 +00:00
|
|
|
path.push(self.dir.group.ty.as_str());
|
|
|
|
path.push(&self.dir.group.id);
|
|
|
|
path.push(&self.backup_time_string);
|
|
|
|
path
|
2022-04-19 08:38:46 +00:00
|
|
|
}
|
2019-03-05 06:18:12 +00:00
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
/// Returns the absolute path for backup_dir, using the cached formatted time string.
|
2022-04-20 13:30:04 +00:00
|
|
|
pub fn full_path(&self) -> PathBuf {
|
2022-05-09 13:39:29 +00:00
|
|
|
self.store.snapshot_path(&self.ns, &self.dir)
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
2019-07-22 08:12:51 +00:00
|
|
|
|
2022-04-20 13:30:04 +00:00
|
|
|
pub fn protected_file(&self) -> PathBuf {
|
|
|
|
let mut path = self.store.base_path();
|
2021-10-27 11:22:26 +00:00
|
|
|
path.push(self.relative_path());
|
|
|
|
path.push(".protected");
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2022-04-20 13:30:04 +00:00
|
|
|
pub fn is_protected(&self) -> bool {
|
|
|
|
let path = self.protected_file();
|
2021-10-27 11:22:26 +00:00
|
|
|
path.exists()
|
|
|
|
}
|
|
|
|
|
2020-09-12 13:10:47 +00:00
|
|
|
pub fn backup_time_to_string(backup_time: i64) -> Result<String, Error> {
|
|
|
|
// fixme: can this fail? (avoid unwrap)
|
2022-02-08 13:57:16 +00:00
|
|
|
proxmox_time::epoch_to_rfc3339_utc(backup_time)
|
2019-07-22 08:12:51 +00:00
|
|
|
}
|
2022-04-24 16:37:15 +00:00
|
|
|
|
2022-04-24 17:09:38 +00:00
|
|
|
/// load a `DataBlob` from this snapshot's backup dir.
|
|
|
|
pub fn load_blob(&self, filename: &str) -> Result<DataBlob, Error> {
|
|
|
|
let mut path = self.full_path();
|
|
|
|
path.push(filename);
|
|
|
|
|
|
|
|
proxmox_lang::try_block!({
|
|
|
|
let mut file = std::fs::File::open(&path)?;
|
|
|
|
DataBlob::load_from_reader(&mut file)
|
|
|
|
})
|
|
|
|
.map_err(|err| format_err!("unable to load blob '{:?}' - {}", path, err))
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:37:15 +00:00
|
|
|
/// Returns the filename to lock a manifest
|
|
|
|
///
|
|
|
|
/// Also creates the basedir. The lockfile is located in
|
|
|
|
/// '/run/proxmox-backup/locks/{datastore}/{type}/{id}/{timestamp}.index.json.lck'
|
2022-04-24 16:49:09 +00:00
|
|
|
fn manifest_lock_path(&self) -> Result<String, Error> {
|
2022-04-24 16:37:15 +00:00
|
|
|
let mut path = format!("/run/proxmox-backup/locks/{}/{self}", self.store.name());
|
|
|
|
std::fs::create_dir_all(&path)?;
|
|
|
|
use std::fmt::Write;
|
|
|
|
let ts = self.backup_time_string();
|
|
|
|
write!(path, "/{ts}{}", &MANIFEST_LOCK_NAME)?;
|
|
|
|
|
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Locks the manifest of a snapshot, for example, to update or delete it.
|
|
|
|
pub(crate) fn lock_manifest(&self) -> Result<BackupLockGuard, Error> {
|
|
|
|
let path = self.manifest_lock_path()?;
|
|
|
|
|
|
|
|
// actions locking the manifest should be relatively short, only wait a few seconds
|
|
|
|
open_backup_lockfile(&path, Some(std::time::Duration::from_secs(5)), true)
|
|
|
|
.map_err(|err| format_err!("unable to acquire manifest lock {:?} - {}", &path, err))
|
|
|
|
}
|
2022-04-24 16:49:09 +00:00
|
|
|
|
|
|
|
/// Destroy the whole snapshot, bails if it's protected
|
|
|
|
///
|
|
|
|
/// Setting `force` to true skips locking and thus ignores if the backup is currently in use.
|
|
|
|
pub fn destroy(&self, force: bool) -> Result<(), Error> {
|
|
|
|
let full_path = self.full_path();
|
|
|
|
|
|
|
|
let (_guard, _manifest_guard);
|
|
|
|
if !force {
|
|
|
|
_guard = lock_dir_noblock(&full_path, "snapshot", "possibly running or in use")?;
|
|
|
|
_manifest_guard = self.lock_manifest()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_protected() {
|
|
|
|
bail!("cannot remove protected snapshot"); // use special error type?
|
|
|
|
}
|
|
|
|
|
|
|
|
log::info!("removing backup snapshot {:?}", full_path);
|
|
|
|
std::fs::remove_dir_all(&full_path).map_err(|err| {
|
|
|
|
format_err!("removing backup snapshot {:?} failed - {}", full_path, err,)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// the manifest doesn't exist anymore, no need to keep the lock (already done by guard?)
|
|
|
|
if let Ok(path) = self.manifest_lock_path() {
|
|
|
|
let _ = std::fs::remove_file(path); // ignore errors
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-05-09 13:39:29 +00:00
|
|
|
|
|
|
|
/// Get the datastore.
|
|
|
|
pub fn datastore(&self) -> &Arc<DataStore> {
|
|
|
|
&self.store
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the backup owner.
|
|
|
|
///
|
|
|
|
/// The backup owner is the entity who first created the backup group.
|
|
|
|
pub fn get_owner(&self) -> Result<Authid, Error> {
|
|
|
|
self.store.get_owner(&self.ns, self.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Lock the snapshot and open a reader.
|
|
|
|
pub fn locked_reader(&self) -> Result<crate::SnapshotReader, Error> {
|
|
|
|
crate::SnapshotReader::new_do(self.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load the manifest without a lock. Must not be written back.
|
|
|
|
pub fn load_manifest(&self) -> Result<(BackupManifest, u64), Error> {
|
|
|
|
let blob = self.load_blob(MANIFEST_BLOB_NAME)?;
|
|
|
|
let raw_size = blob.raw_size();
|
|
|
|
let manifest = BackupManifest::try_from(blob)?;
|
|
|
|
Ok((manifest, raw_size))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the manifest of the specified snapshot. Never write a manifest directly,
|
|
|
|
/// only use this method - anything else may break locking guarantees.
|
|
|
|
pub fn update_manifest(
|
|
|
|
&self,
|
|
|
|
update_fn: impl FnOnce(&mut BackupManifest),
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
self.store.update_manifest(self, update_fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<pbs_api_types::BackupNamespace> for BackupDir {
|
|
|
|
fn as_ref(&self) -> &pbs_api_types::BackupNamespace {
|
|
|
|
&self.ns
|
|
|
|
}
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
2020-06-23 06:16:56 +00:00
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
impl AsRef<pbs_api_types::BackupDir> for BackupDir {
|
|
|
|
fn as_ref(&self) -> &pbs_api_types::BackupDir {
|
|
|
|
&self.dir
|
2022-04-14 13:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
impl AsRef<pbs_api_types::BackupGroup> for BackupDir {
|
|
|
|
fn as_ref(&self) -> &pbs_api_types::BackupGroup {
|
|
|
|
&self.dir.group
|
2022-04-14 13:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
impl From<&BackupDir> for pbs_api_types::BackupGroup {
|
|
|
|
fn from(dir: &BackupDir) -> pbs_api_types::BackupGroup {
|
|
|
|
dir.dir.group.clone()
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 06:09:52 +00:00
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
impl From<BackupDir> for pbs_api_types::BackupGroup {
|
|
|
|
fn from(dir: BackupDir) -> pbs_api_types::BackupGroup {
|
|
|
|
dir.dir.group.into()
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 06:09:52 +00:00
|
|
|
|
2022-04-19 08:38:46 +00:00
|
|
|
impl From<&BackupDir> for pbs_api_types::BackupDir {
|
|
|
|
fn from(dir: &BackupDir) -> pbs_api_types::BackupDir {
|
|
|
|
dir.dir.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BackupDir> for pbs_api_types::BackupDir {
|
|
|
|
fn from(dir: BackupDir) -> pbs_api_types::BackupDir {
|
|
|
|
dir.dir
|
2020-06-23 06:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 06:18:12 +00:00
|
|
|
|
2022-04-21 13:04:59 +00:00
|
|
|
impl fmt::Display for BackupDir {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-05-09 13:39:29 +00:00
|
|
|
if self.ns.is_root() {
|
|
|
|
write!(f, "{}/{}", self.dir.group, self.backup_time_string)
|
|
|
|
} else {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"[{}]:{}/{}",
|
|
|
|
self.ns, self.dir.group, self.backup_time_string
|
|
|
|
)
|
|
|
|
}
|
2019-03-05 08:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:28:13 +00:00
|
|
|
/// Detailed Backup Information, lists files inside a BackupDir
|
2022-04-20 13:30:04 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2019-03-05 06:18:12 +00:00
|
|
|
pub struct BackupInfo {
|
|
|
|
/// the backup directory
|
|
|
|
pub backup_dir: BackupDir,
|
|
|
|
/// List of data files
|
|
|
|
pub files: Vec<String>,
|
2021-10-27 11:22:26 +00:00
|
|
|
/// Protection Status
|
|
|
|
pub protected: bool,
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BackupInfo {
|
2022-04-20 13:30:04 +00:00
|
|
|
pub fn new(backup_dir: BackupDir) -> Result<BackupInfo, Error> {
|
2022-04-24 16:03:27 +00:00
|
|
|
let path = backup_dir.full_path();
|
2019-08-07 06:27:52 +00:00
|
|
|
|
|
|
|
let files = list_backup_files(libc::AT_FDCWD, &path)?;
|
2022-04-20 13:30:04 +00:00
|
|
|
let protected = backup_dir.is_protected();
|
2019-08-07 06:27:52 +00:00
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
Ok(BackupInfo {
|
|
|
|
backup_dir,
|
|
|
|
files,
|
|
|
|
protected,
|
|
|
|
})
|
2019-08-07 06:27:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 06:18:12 +00:00
|
|
|
pub fn sort_list(list: &mut Vec<BackupInfo>, ascendending: bool) {
|
2021-03-14 18:18:35 +00:00
|
|
|
if ascendending {
|
|
|
|
// oldest first
|
2022-04-19 08:38:46 +00:00
|
|
|
list.sort_unstable_by(|a, b| a.backup_dir.dir.time.cmp(&b.backup_dir.dir.time));
|
2021-03-14 18:18:35 +00:00
|
|
|
} else {
|
|
|
|
// newest first
|
2022-04-19 08:38:46 +00:00
|
|
|
list.sort_unstable_by(|a, b| b.backup_dir.dir.time.cmp(&a.backup_dir.dir.time));
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 12:33:11 +00:00
|
|
|
pub fn is_finished(&self) -> bool {
|
|
|
|
// backup is considered unfinished if there is no manifest
|
2022-04-14 11:27:53 +00:00
|
|
|
self.files.iter().any(|name| name == MANIFEST_BLOB_NAME)
|
2020-07-29 12:33:11 +00:00
|
|
|
}
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
2019-05-11 08:19:34 +00:00
|
|
|
|
2021-03-14 18:18:35 +00:00
|
|
|
fn list_backup_files<P: ?Sized + nix::NixPath>(
|
|
|
|
dirfd: RawFd,
|
|
|
|
path: &P,
|
|
|
|
) -> Result<Vec<String>, Error> {
|
2019-05-11 08:19:34 +00:00
|
|
|
let mut files = vec![];
|
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
proxmox_sys::fs::scandir(dirfd, path, &BACKUP_FILE_REGEX, |_, filename, file_type| {
|
2021-03-14 18:18:35 +00:00
|
|
|
if file_type != nix::dir::Type::File {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-05-11 08:19:34 +00:00
|
|
|
files.push(filename.to_owned());
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(files)
|
|
|
|
}
|