src/backup/datastore.rs: use timestamp with zone offset

This commit is contained in:
Dietmar Maurer 2019-03-01 17:36:40 +01:00
parent dc4c09faae
commit e909522f06
3 changed files with 31 additions and 20 deletions

View File

@ -7,7 +7,7 @@ use crate::backup::*;
use crate::api_schema::*; use crate::api_schema::*;
use crate::api_schema::router::*; use crate::api_schema::router::*;
use chrono::{Utc, TimeZone}; use chrono::{Local, TimeZone};
use serde_json::Value; use serde_json::Value;
use std::io::Write; use std::io::Write;
@ -82,7 +82,7 @@ fn upload_catar(
let datastore = DataStore::lookup_datastore(store)?; let datastore = DataStore::lookup_datastore(store)?;
let (mut path, _new) = datastore.create_backup_dir( let (mut path, _new) = datastore.create_backup_dir(
backup_type, backup_id, Utc.timestamp(backup_time, 0))?; backup_type, backup_id, Local.timestamp(backup_time, 0))?;
path.push(archive_name); path.push(archive_name);
@ -138,7 +138,7 @@ fn download_catar(
let backup_type = tools::required_string_param(&param, "type")?; let backup_type = tools::required_string_param(&param, "type")?;
let backup_id = tools::required_string_param(&param, "id")?; let backup_id = tools::required_string_param(&param, "id")?;
let backup_time = tools::required_integer_param(&param, "time")?; let backup_time = tools::required_integer_param(&param, "time")?;
let backup_time = Utc.timestamp(backup_time, 0); let backup_time = Local.timestamp(backup_time, 0);
println!("Download {}.catar from {} ({}/{}/{}/{}.didx)", archive_name, store, println!("Download {}.catar from {} ({}/{}/{}/{}.didx)", archive_name, store,
backup_type, backup_id, backup_time, archive_name); backup_type, backup_id, backup_time, archive_name);

View File

@ -15,7 +15,7 @@ use super::fixed_index::*;
use super::dynamic_index::*; use super::dynamic_index::*;
use super::index::*; use super::index::*;
use chrono::{Utc, TimeZone}; use chrono::Local;
/// Datastore Management /// Datastore Management
/// ///
@ -55,18 +55,20 @@ pub struct BackupDir {
/// Backup group /// Backup group
pub group: BackupGroup, pub group: BackupGroup,
/// Backup timestamp /// Backup timestamp
pub backup_time: DateTime<Utc>, pub backup_time: DateTime<Local>,
} }
impl BackupDir { impl BackupDir {
fn backup_time_to_file_name(backup_time: DateTime<Local>) -> String {
backup_time.to_rfc3339().to_string()
}
pub fn relative_path(&self) -> PathBuf { pub fn relative_path(&self) -> PathBuf {
let mut relative_path = self.group.group_path(); let mut relative_path = self.group.group_path();
let date_str = self.backup_time.format("%Y-%m-%dT%H:%M:%S").to_string(); relative_path.push(&Self::backup_time_to_file_name(self.backup_time));
relative_path.push(&date_str);
relative_path relative_path
} }
@ -207,7 +209,7 @@ impl DataStore {
&self, &self,
backup_type: &str, backup_type: &str,
backup_id: &str, backup_id: &str,
backup_time: DateTime<Utc>, backup_time: DateTime<Local>,
) -> Result<(PathBuf, bool), io::Error> { ) -> Result<(PathBuf, bool), io::Error> {
let mut relative_path = PathBuf::new(); let mut relative_path = PathBuf::new();
@ -220,7 +222,7 @@ impl DataStore {
full_path.push(&relative_path); full_path.push(&relative_path);
std::fs::create_dir_all(&full_path)?; std::fs::create_dir_all(&full_path)?;
let date_str = backup_time.format("%Y-%m-%dT%H:%M:%S").to_string(); let date_str = BackupDir::backup_time_to_file_name(backup_time);
println!("date: {}", date_str); println!("date: {}", date_str);
relative_path.push(&date_str); relative_path.push(&date_str);
@ -244,7 +246,7 @@ impl DataStore {
static ref BACKUP_TYPE_REGEX: regex::Regex = regex::Regex::new(r"^(host|vm|ct)$").unwrap(); static ref BACKUP_TYPE_REGEX: regex::Regex = regex::Regex::new(r"^(host|vm|ct)$").unwrap();
static ref BACKUP_ID_REGEX: regex::Regex = regex::Regex::new(r"^[A-Za-z][A-Za-z0-9_-]+$").unwrap(); static ref BACKUP_ID_REGEX: regex::Regex = regex::Regex::new(r"^[A-Za-z][A-Za-z0-9_-]+$").unwrap();
static ref BACKUP_DATE_REGEX: regex::Regex = regex::Regex::new( static ref BACKUP_DATE_REGEX: regex::Regex = regex::Regex::new(
r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$").unwrap(); r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\+[0-9]{2}:[0-9]{2}$").unwrap();
} }
tools::scandir(libc::AT_FDCWD, &path, &BACKUP_TYPE_REGEX, |l0_fd, backup_type, file_type| { tools::scandir(libc::AT_FDCWD, &path, &BACKUP_TYPE_REGEX, |l0_fd, backup_type, file_type| {
@ -254,7 +256,7 @@ impl DataStore {
tools::scandir(l1_fd, backup_id, &BACKUP_DATE_REGEX, |l2_fd, backup_time, file_type| { tools::scandir(l1_fd, backup_id, &BACKUP_DATE_REGEX, |l2_fd, backup_time, file_type| {
if file_type != nix::dir::Type::Directory { return Ok(()); } if file_type != nix::dir::Type::Directory { return Ok(()); }
let dt = Utc.datetime_from_str(backup_time, "%Y-%m-%dT%H:%M:%S")?; let dt = backup_time.parse::<DateTime<Local>>()?;
let mut files = vec![]; let mut files = vec![];

View File

@ -108,17 +108,26 @@ fn list_backups(
for item in list { for item in list {
let id = item["backup_id"].as_str(). unwrap(); let id = item["backup_id"].as_str().unwrap();
let btype = item["backup_type"].as_str(). unwrap(); let btype = item["backup_type"].as_str().unwrap();
let epoch = item["backup_time"].as_i64(). unwrap(); let epoch = item["backup_time"].as_i64().unwrap();
let backup_time = Local.timestamp(epoch, 0);
let time_str = Local.timestamp(epoch, 0).format("%c"); let backup_dir = BackupDir {
group: BackupGroup {
backup_type: btype.to_string(),
backup_id: id.to_string(),
},
backup_time
};
let files = item["files"].as_array().unwrap(); let files = item["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect();
for file in files { let info = BackupInfo { backup_dir, files };
let filename = file.as_str().unwrap();
println!("| {} | {} | {} | {}", btype, id, time_str, filename); for filename in info.files {
let path = info.backup_dir.relative_path().to_str().unwrap().to_owned();
println!("{} | {}/{}", backup_time.format("%c"), path, filename);
} }
} }