Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/services/api/models/agent/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct DatabaseStorage {
#[serde(deserialize_with = "deserialize_snake_case")]
pub config: Value,
pub provider: String,
#[serde(default)]
pub prefix: Option<String>,
}

#[derive(Debug, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/services/storage/providers/azure_blob/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl StorageProvider for AzureBlobProvider {
};

let file_name = full_file_name(encrypt);
let remote_file_path = full_file_path(&file_name);
let remote_file_path = full_file_path(&file_name, storage.prefix.as_deref());
info!(
"Starting block upload to azure blob {}/{}",
config.container_name, remote_file_path
Expand Down
2 changes: 1 addition & 1 deletion src/services/storage/providers/google_cloud_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl StorageProvider for GoogleCloudStorageProvider {

let file_name = full_file_name(encrypt);
info!("Uploading file {}", file_name);
let remote_file_path = full_file_path(&file_name);
let remote_file_path = full_file_path(&file_name, storage.prefix.as_deref());

let client = match build_client(&config).await {
Ok(c) => c,
Expand Down
2 changes: 1 addition & 1 deletion src/services/storage/providers/google_drive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl StorageProvider for GoogleDriveProvider {

info!("Uploading file {}", file_name);

let remote_file_path = full_file_path(&file_name);
let remote_file_path = full_file_path(&file_name, storage.prefix.as_deref());

match upload_stream_to_google_drive(
&config,
Expand Down
2 changes: 1 addition & 1 deletion src/services/storage/providers/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl StorageProvider for LocalProvider {
let encrypt = encrypt.unwrap_or(false);

let file_name = full_file_name(encrypt);
let remote_file_path = full_file_path(&file_name);
let remote_file_path = full_file_path(&file_name, storage.prefix.as_deref());

let total_size = match fs::metadata(&file_path).await {
Ok(meta) => meta.len(),
Expand Down
2 changes: 1 addition & 1 deletion src/services/storage/providers/s3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl StorageProvider for S3Provider {
info!("Uploading file {}", file_name);

let bucket = &config.bucket_name;
let remote_file_path = full_file_path(&file_name);
let remote_file_path = full_file_path(&file_name, storage.prefix.as_deref());
info!("S3 key {:}", remote_file_path);
info!(
"Starting multipart upload to s3://{}/{}",
Expand Down
12 changes: 8 additions & 4 deletions src/tests/utils/file_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ fn full_file_name_matches_expected_suffix() {
}

#[test]
fn full_file_path_prefixes_backups_directory_and_date() {
fn full_file_path_uses_default_or_configured_prefix() {
let file_name = "backup.tar.gz".to_string();
let full_path = full_file_path(&file_name);

assert!(full_path.starts_with("backups/"));
assert!(full_path.ends_with("/backup.tar.gz"));
let default_path = full_file_path(&file_name, None);
assert!(default_path.starts_with("backups/"));
assert!(default_path.ends_with("/backup.tar.gz"));

let configured_path = full_file_path(&file_name, Some("/portabase/"));
assert!(configured_path.starts_with("portabase/"));
assert!(configured_path.ends_with("/backup.tar.gz"));
}

#[tokio::test]
Expand Down
10 changes: 8 additions & 2 deletions src/utils/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ pub fn full_file_name(encrypt: bool) -> String {
}
}

pub fn full_file_path(file_name: &String) -> String {
format!("backups/{}/{}", Utc::now().format("%Y-%m-%d"), file_name)
pub fn full_file_path(file_name: &String, prefix: Option<&str>) -> String {
let prefix = prefix
.map(str::trim)
.map(|prefix| prefix.trim_matches(char::from(47)))
.filter(|prefix| !prefix.is_empty())
.unwrap_or("backups");

format!("{}/{}/{}", prefix, Utc::now().format("%Y-%m-%d"), file_name)
}

const CHUNK_SIZE: usize = 16 * 1024 * 1024;
Expand Down