2020-01-10 10:09:55 +00:00
|
|
|
//! Sync datastore from remote server
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2020-01-08 12:53:19 +00:00
|
|
|
use serde_json::json;
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::{Seek, SeekFrom};
|
|
|
|
use chrono::{Utc, TimeZone};
|
|
|
|
|
|
|
|
use proxmox::api::api;
|
2020-04-17 13:27:04 +00:00
|
|
|
use proxmox::api::{ApiMethod, Router, RpcEnvironment, Permission};
|
2020-01-08 12:53:19 +00:00
|
|
|
|
|
|
|
use crate::server::{WorkerTask};
|
|
|
|
use crate::backup::*;
|
|
|
|
use crate::client::*;
|
2020-01-16 13:32:06 +00:00
|
|
|
use crate::config::remote;
|
2020-01-08 12:53:19 +00:00
|
|
|
use crate::api2::types::*;
|
2020-04-28 08:11:15 +00:00
|
|
|
use crate::config::acl::{PRIV_DATASTORE_BACKUP, PRIV_DATASTORE_READ};
|
2020-04-18 06:23:04 +00:00
|
|
|
use crate::config::cached_user_info::CachedUserInfo;
|
2020-01-08 12:53:19 +00:00
|
|
|
|
|
|
|
// fixme: implement filters
|
2020-01-10 10:09:55 +00:00
|
|
|
// fixme: delete vanished groups
|
2020-01-08 12:53:19 +00:00
|
|
|
// Todo: correctly lock backup groups
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
async fn pull_index_chunks<I: IndexFile>(
|
2020-01-08 12:53:19 +00:00
|
|
|
_worker: &WorkerTask,
|
|
|
|
chunk_reader: &mut RemoteChunkReader,
|
|
|
|
target: Arc<DataStore>,
|
|
|
|
index: I,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
|
|
|
|
for pos in 0..index.index_count() {
|
|
|
|
let digest = index.index_digest(pos).unwrap();
|
|
|
|
let chunk_exists = target.cond_touch_chunk(digest, false)?;
|
|
|
|
if chunk_exists {
|
|
|
|
//worker.log(format!("chunk {} exists {}", pos, proxmox::tools::digest_to_hex(digest)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//worker.log(format!("sync {} chunk {}", pos, proxmox::tools::digest_to_hex(digest)));
|
|
|
|
let chunk = chunk_reader.read_raw_chunk(&digest)?;
|
|
|
|
|
|
|
|
target.insert_chunk(&chunk, &digest)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn download_manifest(
|
|
|
|
reader: &BackupReader,
|
|
|
|
filename: &std::path::Path,
|
|
|
|
) -> Result<std::fs::File, Error> {
|
|
|
|
|
|
|
|
let tmp_manifest_file = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.read(true)
|
|
|
|
.open(&filename)?;
|
|
|
|
|
|
|
|
let mut tmp_manifest_file = reader.download(MANIFEST_BLOB_NAME, tmp_manifest_file).await?;
|
|
|
|
|
|
|
|
tmp_manifest_file.seek(SeekFrom::Start(0))?;
|
|
|
|
|
|
|
|
Ok(tmp_manifest_file)
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
async fn pull_single_archive(
|
2020-01-08 12:53:19 +00:00
|
|
|
worker: &WorkerTask,
|
|
|
|
reader: &BackupReader,
|
|
|
|
chunk_reader: &mut RemoteChunkReader,
|
|
|
|
tgt_store: Arc<DataStore>,
|
|
|
|
snapshot: &BackupDir,
|
|
|
|
archive_name: &str,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut path = tgt_store.base_path();
|
|
|
|
path.push(snapshot.relative_path());
|
|
|
|
path.push(archive_name);
|
|
|
|
|
|
|
|
let mut tmp_path = path.clone();
|
|
|
|
tmp_path.set_extension("tmp");
|
|
|
|
|
|
|
|
worker.log(format!("sync archive {}", archive_name));
|
|
|
|
let tmpfile = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.read(true)
|
|
|
|
.open(&tmp_path)?;
|
|
|
|
|
|
|
|
let tmpfile = reader.download(archive_name, tmpfile).await?;
|
|
|
|
|
|
|
|
match archive_type(archive_name)? {
|
|
|
|
ArchiveType::DynamicIndex => {
|
|
|
|
let index = DynamicIndexReader::new(tmpfile)
|
|
|
|
.map_err(|err| format_err!("unable to read dynamic index {:?} - {}", tmp_path, err))?;
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
pull_index_chunks(worker, chunk_reader, tgt_store.clone(), index).await?;
|
2020-01-08 12:53:19 +00:00
|
|
|
}
|
|
|
|
ArchiveType::FixedIndex => {
|
|
|
|
let index = FixedIndexReader::new(tmpfile)
|
|
|
|
.map_err(|err| format_err!("unable to read fixed index '{:?}' - {}", tmp_path, err))?;
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
pull_index_chunks(worker, chunk_reader, tgt_store.clone(), index).await?;
|
2020-01-08 12:53:19 +00:00
|
|
|
}
|
|
|
|
ArchiveType::Blob => { /* nothing to do */ }
|
|
|
|
}
|
|
|
|
if let Err(err) = std::fs::rename(&tmp_path, &path) {
|
|
|
|
bail!("Atomic rename file {:?} failed - {}", path, err);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
async fn pull_snapshot(
|
2020-01-08 12:53:19 +00:00
|
|
|
worker: &WorkerTask,
|
|
|
|
reader: Arc<BackupReader>,
|
|
|
|
tgt_store: Arc<DataStore>,
|
|
|
|
snapshot: &BackupDir,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut manifest_name = tgt_store.base_path();
|
|
|
|
manifest_name.push(snapshot.relative_path());
|
|
|
|
manifest_name.push(MANIFEST_BLOB_NAME);
|
|
|
|
|
|
|
|
let mut tmp_manifest_name = manifest_name.clone();
|
|
|
|
tmp_manifest_name.set_extension("tmp");
|
|
|
|
|
|
|
|
let mut tmp_manifest_file = download_manifest(&reader, &tmp_manifest_name).await?;
|
|
|
|
let tmp_manifest_blob = DataBlob::load(&mut tmp_manifest_file)?;
|
|
|
|
tmp_manifest_blob.verify_crc()?;
|
|
|
|
|
|
|
|
if manifest_name.exists() {
|
2020-01-21 11:28:01 +00:00
|
|
|
let manifest_blob = proxmox::try_block!({
|
2020-01-08 12:53:19 +00:00
|
|
|
let mut manifest_file = std::fs::File::open(&manifest_name)
|
|
|
|
.map_err(|err| format_err!("unable to open local manifest {:?} - {}", manifest_name, err))?;
|
|
|
|
|
|
|
|
let manifest_blob = DataBlob::load(&mut manifest_file)?;
|
|
|
|
manifest_blob.verify_crc()?;
|
|
|
|
Ok(manifest_blob)
|
|
|
|
}).map_err(|err: Error| {
|
|
|
|
format_err!("unable to read local manifest {:?} - {}", manifest_name, err)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if manifest_blob.raw_data() == tmp_manifest_blob.raw_data() {
|
|
|
|
return Ok(()); // nothing changed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let manifest = BackupManifest::try_from(tmp_manifest_blob)?;
|
|
|
|
|
|
|
|
let mut chunk_reader = RemoteChunkReader::new(reader.clone(), None, HashMap::new());
|
|
|
|
|
|
|
|
for item in manifest.files() {
|
|
|
|
let mut path = tgt_store.base_path();
|
|
|
|
path.push(snapshot.relative_path());
|
|
|
|
path.push(&item.filename);
|
|
|
|
|
|
|
|
if path.exists() {
|
|
|
|
match archive_type(&item.filename)? {
|
|
|
|
ArchiveType::DynamicIndex => {
|
|
|
|
let index = DynamicIndexReader::open(&path)?;
|
|
|
|
let (csum, size) = index.compute_csum();
|
|
|
|
match manifest.verify_file(&item.filename, &csum, size) {
|
|
|
|
Ok(_) => continue,
|
|
|
|
Err(err) => {
|
|
|
|
worker.log(format!("detected changed file {:?} - {}", path, err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArchiveType::FixedIndex => {
|
|
|
|
let index = FixedIndexReader::open(&path)?;
|
|
|
|
let (csum, size) = index.compute_csum();
|
|
|
|
match manifest.verify_file(&item.filename, &csum, size) {
|
|
|
|
Ok(_) => continue,
|
|
|
|
Err(err) => {
|
|
|
|
worker.log(format!("detected changed file {:?} - {}", path, err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArchiveType::Blob => {
|
|
|
|
let mut tmpfile = std::fs::File::open(&path)?;
|
|
|
|
let (csum, size) = compute_file_csum(&mut tmpfile)?;
|
|
|
|
match manifest.verify_file(&item.filename, &csum, size) {
|
|
|
|
Ok(_) => continue,
|
|
|
|
Err(err) => {
|
|
|
|
worker.log(format!("detected changed file {:?} - {}", path, err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
pull_single_archive(
|
2020-01-08 12:53:19 +00:00
|
|
|
worker,
|
|
|
|
&reader,
|
|
|
|
&mut chunk_reader,
|
|
|
|
tgt_store.clone(),
|
|
|
|
snapshot,
|
|
|
|
&item.filename,
|
|
|
|
).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(err) = std::fs::rename(&tmp_manifest_name, &manifest_name) {
|
|
|
|
bail!("Atomic rename file {:?} failed - {}", manifest_name, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup - remove stale files
|
|
|
|
tgt_store.cleanup_backup_dir(snapshot, &manifest)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
pub async fn pull_snapshot_from(
|
2020-01-08 12:53:19 +00:00
|
|
|
worker: &WorkerTask,
|
|
|
|
reader: Arc<BackupReader>,
|
|
|
|
tgt_store: Arc<DataStore>,
|
|
|
|
snapshot: &BackupDir,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let (_path, is_new) = tgt_store.create_backup_dir(&snapshot)?;
|
|
|
|
|
|
|
|
if is_new {
|
|
|
|
worker.log(format!("sync snapshot {:?}", snapshot.relative_path()));
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
if let Err(err) = pull_snapshot(worker, reader, tgt_store.clone(), &snapshot).await {
|
2020-01-08 12:53:19 +00:00
|
|
|
if let Err(cleanup_err) = tgt_store.remove_backup_dir(&snapshot) {
|
|
|
|
worker.log(format!("cleanup error - {}", cleanup_err));
|
|
|
|
}
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
worker.log(format!("re-sync snapshot {:?}", snapshot.relative_path()));
|
2020-01-10 10:09:55 +00:00
|
|
|
pull_snapshot(worker, reader, tgt_store.clone(), &snapshot).await?
|
2020-01-08 12:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
pub async fn pull_group(
|
2020-01-08 12:53:19 +00:00
|
|
|
worker: &WorkerTask,
|
|
|
|
client: &HttpClient,
|
|
|
|
src_repo: &BackupRepository,
|
|
|
|
tgt_store: Arc<DataStore>,
|
|
|
|
group: &BackupGroup,
|
2020-01-17 10:24:55 +00:00
|
|
|
delete: bool,
|
2020-01-08 12:53:19 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/snapshots", src_repo.store());
|
|
|
|
|
|
|
|
let args = json!({
|
|
|
|
"backup-type": group.backup_type(),
|
|
|
|
"backup-id": group.backup_id(),
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut result = client.get(&path, Some(args)).await?;
|
|
|
|
let mut list: Vec<SnapshotListItem> = serde_json::from_value(result["data"].take())?;
|
|
|
|
|
|
|
|
list.sort_unstable_by(|a, b| a.backup_time.cmp(&b.backup_time));
|
|
|
|
|
|
|
|
let auth_info = client.login().await?;
|
2020-01-25 11:18:00 +00:00
|
|
|
let fingerprint = client.fingerprint();
|
2020-01-08 12:53:19 +00:00
|
|
|
|
2020-01-22 14:06:28 +00:00
|
|
|
let last_sync = tgt_store.last_successful_backup(group)?;
|
2020-01-08 12:53:19 +00:00
|
|
|
|
2020-01-23 08:58:57 +00:00
|
|
|
let mut remote_snapshots = std::collections::HashSet::new();
|
|
|
|
|
2020-01-08 12:53:19 +00:00
|
|
|
for item in list {
|
|
|
|
let backup_time = Utc.timestamp(item.backup_time, 0);
|
2020-01-23 08:58:57 +00:00
|
|
|
remote_snapshots.insert(backup_time);
|
|
|
|
|
2020-01-08 12:53:19 +00:00
|
|
|
if let Some(last_sync_time) = last_sync {
|
|
|
|
if last_sync_time > backup_time { continue; }
|
|
|
|
}
|
|
|
|
|
2020-01-25 11:18:00 +00:00
|
|
|
let options = HttpClientOptions::new()
|
|
|
|
.password(Some(auth_info.ticket.clone()))
|
|
|
|
.fingerprint(fingerprint.clone());
|
|
|
|
|
|
|
|
let new_client = HttpClient::new(src_repo.host(), src_repo.user(), options)?;
|
2020-01-08 12:53:19 +00:00
|
|
|
|
|
|
|
let reader = BackupReader::start(
|
|
|
|
new_client,
|
|
|
|
None,
|
|
|
|
src_repo.store(),
|
|
|
|
&item.backup_type,
|
|
|
|
&item.backup_id,
|
|
|
|
backup_time,
|
|
|
|
true,
|
|
|
|
).await?;
|
|
|
|
|
|
|
|
let snapshot = BackupDir::new(item.backup_type, item.backup_id, item.backup_time);
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
pull_snapshot_from(worker, reader, tgt_store.clone(), &snapshot).await?;
|
2020-01-08 12:53:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 10:24:55 +00:00
|
|
|
if delete {
|
2020-01-23 08:58:57 +00:00
|
|
|
let local_list = group.list_backups(&tgt_store.base_path())?;
|
|
|
|
for info in local_list {
|
|
|
|
let backup_time = info.backup_dir.backup_time();
|
|
|
|
if remote_snapshots.contains(&backup_time) { continue; }
|
|
|
|
worker.log(format!("delete vanished snapshot {:?}", info.backup_dir.relative_path()));
|
|
|
|
tgt_store.remove_backup_dir(&info.backup_dir)?;
|
|
|
|
}
|
2020-01-17 10:24:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:53:19 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:09:55 +00:00
|
|
|
pub async fn pull_store(
|
2020-01-08 12:53:19 +00:00
|
|
|
worker: &WorkerTask,
|
|
|
|
client: &HttpClient,
|
|
|
|
src_repo: &BackupRepository,
|
|
|
|
tgt_store: Arc<DataStore>,
|
2020-01-17 10:24:55 +00:00
|
|
|
delete: bool,
|
2020-04-28 08:11:15 +00:00
|
|
|
username: String,
|
2020-01-08 12:53:19 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let path = format!("api2/json/admin/datastore/{}/groups", src_repo.store());
|
|
|
|
|
|
|
|
let mut result = client.get(&path, None).await?;
|
|
|
|
|
2020-01-17 09:17:18 +00:00
|
|
|
let mut list: Vec<GroupListItem> = serde_json::from_value(result["data"].take())?;
|
2020-01-08 12:53:19 +00:00
|
|
|
|
|
|
|
list.sort_unstable_by(|a, b| {
|
2020-01-17 09:17:18 +00:00
|
|
|
let type_order = a.backup_type.cmp(&b.backup_type);
|
2020-01-08 12:53:19 +00:00
|
|
|
if type_order == std::cmp::Ordering::Equal {
|
2020-01-17 09:17:18 +00:00
|
|
|
a.backup_id.cmp(&b.backup_id)
|
2020-01-08 12:53:19 +00:00
|
|
|
} else {
|
|
|
|
type_order
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut errors = false;
|
|
|
|
|
2020-01-17 10:24:55 +00:00
|
|
|
let mut new_groups = std::collections::HashSet::new();
|
2020-04-28 08:11:15 +00:00
|
|
|
for item in list.iter() {
|
|
|
|
new_groups.insert(BackupGroup::new(&item.backup_type, &item.backup_id));
|
|
|
|
}
|
2020-01-17 10:24:55 +00:00
|
|
|
|
2020-01-08 12:53:19 +00:00
|
|
|
for item in list {
|
2020-01-17 09:17:18 +00:00
|
|
|
let group = BackupGroup::new(&item.backup_type, &item.backup_id);
|
2020-04-28 08:11:15 +00:00
|
|
|
|
|
|
|
let owner = tgt_store.create_backup_group(&group, &username)?;
|
|
|
|
// permission check
|
|
|
|
if owner != username { // only the owner is allowed to create additional snapshots
|
|
|
|
worker.log(format!("sync group {}/{} failed - owner check failed ({} != {})",
|
|
|
|
item.backup_type, item.backup_id, username, owner));
|
|
|
|
errors = true;
|
|
|
|
continue; // do not stop here, instead continue
|
|
|
|
}
|
|
|
|
|
2020-01-17 10:24:55 +00:00
|
|
|
if let Err(err) = pull_group(worker, client, src_repo, tgt_store.clone(), &group, delete).await {
|
2020-01-17 09:17:18 +00:00
|
|
|
worker.log(format!("sync group {}/{} failed - {}", item.backup_type, item.backup_id, err));
|
2020-01-08 12:53:19 +00:00
|
|
|
errors = true;
|
2020-04-28 08:11:15 +00:00
|
|
|
continue; // do not stop here, instead continue
|
2020-01-08 12:53:19 +00:00
|
|
|
}
|
2020-01-17 10:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if delete {
|
2020-01-21 11:28:01 +00:00
|
|
|
let result: Result<(), Error> = proxmox::try_block!({
|
2020-01-17 10:24:55 +00:00
|
|
|
let local_groups = BackupGroup::list_groups(&tgt_store.base_path())?;
|
|
|
|
for local_group in local_groups {
|
|
|
|
if new_groups.contains(&local_group) { continue; }
|
|
|
|
worker.log(format!("delete vanished group '{}/{}'", local_group.backup_type(), local_group.backup_id()));
|
|
|
|
if let Err(err) = tgt_store.remove_backup_group(&local_group) {
|
2020-01-23 08:58:57 +00:00
|
|
|
worker.log(err.to_string());
|
2020-01-17 10:24:55 +00:00
|
|
|
errors = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
if let Err(err) = result {
|
|
|
|
worker.log(format!("error during cleanup: {}", err));
|
|
|
|
errors = true;
|
|
|
|
};
|
2020-01-08 12:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if errors {
|
|
|
|
bail!("sync failed with some errors.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
store: {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
2020-01-16 12:54:29 +00:00
|
|
|
remote: {
|
|
|
|
schema: REMOTE_ID_SCHEMA,
|
2020-01-08 12:53:19 +00:00
|
|
|
},
|
|
|
|
"remote-store": {
|
|
|
|
schema: DATASTORE_SCHEMA,
|
|
|
|
},
|
2020-01-17 10:24:55 +00:00
|
|
|
delete: {
|
|
|
|
description: "Delete vanished backups. This remove the local copy if the remote backup was deleted.",
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
default: true,
|
|
|
|
},
|
2020-01-08 12:53:19 +00:00
|
|
|
},
|
|
|
|
},
|
2020-04-17 13:27:04 +00:00
|
|
|
access: {
|
2020-04-18 06:23:04 +00:00
|
|
|
// Note: used parameters are no uri parameters, so we need to test inside function body
|
2020-04-28 08:11:15 +00:00
|
|
|
description: r###"The user needs Datastore.Backup privilege on '/datastore/{store}',
|
|
|
|
and needs to own the backup group. Datastore.Read is required on '/remote/{remote}/{remote-store}'.
|
|
|
|
"###,
|
2020-04-18 06:23:04 +00:00
|
|
|
permission: &Permission::Anybody,
|
2020-04-17 13:27:04 +00:00
|
|
|
},
|
2020-01-08 12:53:19 +00:00
|
|
|
)]
|
2020-01-10 10:09:55 +00:00
|
|
|
/// Sync store from other repository
|
|
|
|
async fn pull (
|
2020-01-08 12:53:19 +00:00
|
|
|
store: String,
|
2020-01-16 12:54:29 +00:00
|
|
|
remote: String,
|
2020-01-08 12:53:19 +00:00
|
|
|
remote_store: String,
|
2020-01-17 10:24:55 +00:00
|
|
|
delete: Option<bool>,
|
2020-01-08 12:53:19 +00:00
|
|
|
_info: &ApiMethod,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<String, Error> {
|
|
|
|
|
2020-04-18 06:23:04 +00:00
|
|
|
let user_info = CachedUserInfo::new()?;
|
|
|
|
|
2020-01-08 12:53:19 +00:00
|
|
|
let username = rpcenv.get_user().unwrap();
|
2020-04-28 08:11:15 +00:00
|
|
|
user_info.check_privs(&username, &["datastore", &store], PRIV_DATASTORE_BACKUP, false)?;
|
2020-04-27 05:13:06 +00:00
|
|
|
user_info.check_privs(&username, &["remote", &remote, &remote_store], PRIV_DATASTORE_READ, false)?;
|
2020-01-08 12:53:19 +00:00
|
|
|
|
2020-01-17 10:24:55 +00:00
|
|
|
let delete = delete.unwrap_or(true);
|
|
|
|
|
2020-01-08 12:53:19 +00:00
|
|
|
let tgt_store = DataStore::lookup_datastore(&store)?;
|
|
|
|
|
2020-01-16 13:32:06 +00:00
|
|
|
let (remote_config, _digest) = remote::config()?;
|
|
|
|
let remote: remote::Remote = remote_config.lookup("remote", &remote)?;
|
2020-01-16 12:54:29 +00:00
|
|
|
|
2020-01-25 11:18:00 +00:00
|
|
|
let options = HttpClientOptions::new()
|
|
|
|
.password(Some(remote.password.clone()))
|
|
|
|
.fingerprint(remote.fingerprint.clone());
|
|
|
|
|
|
|
|
let client = HttpClient::new(&remote.host, &remote.userid, options)?;
|
2020-01-08 12:53:19 +00:00
|
|
|
let _auth_info = client.login() // make sure we can auth
|
|
|
|
.await
|
2020-01-16 12:54:29 +00:00
|
|
|
.map_err(|err| format_err!("remote connection to '{}' failed - {}", remote.host, err))?;
|
2020-01-08 12:53:19 +00:00
|
|
|
|
2020-01-16 12:54:29 +00:00
|
|
|
let src_repo = BackupRepository::new(Some(remote.userid), Some(remote.host), remote_store);
|
2020-01-08 12:53:19 +00:00
|
|
|
|
|
|
|
// fixme: set to_stdout to false?
|
|
|
|
let upid_str = WorkerTask::spawn("sync", Some(store.clone()), &username.clone(), true, move |worker| async move {
|
|
|
|
|
|
|
|
worker.log(format!("sync datastore '{}' start", store));
|
|
|
|
|
|
|
|
// explicit create shared lock to prevent GC on newly created chunks
|
|
|
|
let _shared_store_lock = tgt_store.try_shared_chunk_store_lock()?;
|
|
|
|
|
2020-04-28 08:11:15 +00:00
|
|
|
pull_store(&worker, &client, &src_repo, tgt_store.clone(), delete, username).await?;
|
2020-01-08 12:53:19 +00:00
|
|
|
|
|
|
|
worker.log(format!("sync datastore '{}' end", store));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(upid_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
2020-01-10 10:09:55 +00:00
|
|
|
.post(&API_METHOD_PULL);
|