diff --git a/.gitignore b/.gitignore index d0df364..885f498 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /target +/cloud_backup +/db_backup /result /.vscode - diff --git a/Cargo.lock b/Cargo.lock index b1829c5..bc09f16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -548,12 +557,26 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "chrono" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link", +] + [[package]] name = "cloudbackup" version = "0.1.0" dependencies = [ "aws-config", "aws-sdk-s3", + "chrono", "futures", "serde", "serde_json", @@ -1192,6 +1215,30 @@ dependencies = [ "tracing", ] +[[package]] +name = "iana-time-zone" +version = "0.1.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33e57f83510bb73707521ebaffa789ec8caf86f9657cad665b092b581d40e9fb" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "icu_collections" version = "2.1.1" @@ -2309,12 +2356,65 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index acdc008..7f1652a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ aws-config = "1.1" aws-sdk-s3 = "1.13" tokio = { version = "1", features = ["full"] } futures = "0.3" +chrono = "0.4" diff --git a/src/main.rs b/src/main.rs index 7a98d78..d803733 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod utils; use models::CloudS3; use std::fs; -use utils::{handle_bucket_contents, list_all_buckets}; +use utils::{delete_old_files, handle_bucket_contents, list_all_buckets}; use crate::utils::s3_utils::{create_client, create_config}; @@ -12,14 +12,13 @@ use crate::utils::s3_utils::{create_client, create_config}; async fn main() { // Választható: backup_config vagy clean_and_backup // Jelenleg mindkettőt futtatjuk, ha létezik a fájl + if let Err(e) = run_clean_and_backup().await { + eprintln!("Hiba a clean_and_backup futtatásakor: {}", e); + } if let Err(e) = run_backup_config().await { eprintln!("Hiba a backup_config futtatásakor: {}", e); } - - if let Err(e) = run_clean_and_backup().await { - eprintln!("Hiba a clean_and_backup futtatásakor: {}", e); - } } /// Backup a backup_config.json alapján (eredeti működés) @@ -80,7 +79,7 @@ async fn run_clean_and_backup() -> Result<(), Box> { let bucket_cloud = CloudS3 { name: format!("{}/{}", cloud.name, bucket_name), region: cloud.region.clone(), - bucket: bucket_name, + bucket: bucket_name.clone(), accesskey: cloud.accesskey.clone(), secretekey: cloud.secretekey.clone(), endpoint_url: cloud.endpoint_url.clone(), @@ -91,6 +90,16 @@ async fn run_clean_and_backup() -> Result<(), Box> { Ok(_) => (), Err(e) => eprintln!("Hiba a bucket listázásakor: {}", e), } + + // 14 napnál régebbi fájlok törlése + match delete_old_files(&client, &bucket_cloud, 14).await { + Ok(count) => { + if count > 0 { + println!("{} régi fájl törölve a {} bucket-ből", count, bucket_name); + } + } + Err(e) => eprintln!("Hiba a régi fájlok törlésekor: {}", e), + } } } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e6d26b5..e5132a4 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,4 +1,5 @@ pub mod file_utils; pub mod s3_utils; +pub use s3_utils::delete_old_files; pub use s3_utils::handle_bucket_contents; pub use s3_utils::list_all_buckets; diff --git a/src/utils/s3_utils.rs b/src/utils/s3_utils.rs index e3fb09d..5f96f79 100644 --- a/src/utils/s3_utils.rs +++ b/src/utils/s3_utils.rs @@ -5,6 +5,7 @@ use crate::models::S3Item; use aws_config::BehaviorVersion; use aws_sdk_s3::config::{Credentials, Region}; use aws_sdk_s3::{Client, Error}; +use chrono::{DateTime, Duration, Utc}; use futures::stream::{self, StreamExt}; pub fn create_config(s3: &CloudS3) -> aws_sdk_s3::Config { @@ -174,3 +175,49 @@ pub async fn handle_bucket_contents( Ok(()) } + +/// Törli a 14 napnál régebbi fájlokat a bucket-ből +pub async fn delete_old_files( + client: &Client, + s3: &CloudS3, + days: i64, +) -> Result> { + let items = list_bucket(client, s3).await?; + let cutoff_date = Utc::now() - Duration::days(days); + + let mut deleted_count = 0; + + for item in items.iter().filter(|i| !i.is_folder) { + if let Some(last_modified_str) = &item.last_modified { + // AWS SDK DateTime formátum: "2024-01-15T10:30:00Z" + if let Ok(last_modified) = DateTime::parse_from_rfc3339(last_modified_str) { + let last_modified_utc: DateTime = last_modified.into(); + + if last_modified_utc < cutoff_date { + match client + .delete_object() + .bucket(&s3.bucket) + .key(&item.key) + .send() + .await + { + Ok(_) => { + println!("🗑 Törölve: {} ({})", item.key, last_modified_str); + deleted_count += 1; + } + Err(e) => { + eprintln!("Hiba a törléskor: {} - {}", item.key, e); + } + } + } + } + } + } + + println!( + "Törlés kész: {} fájl törölve ({} napnál régebbiek)", + deleted_count, days + ); + + Ok(deleted_count) +}