Added webhook for backup removal

backup_error_fix
Joey Hines 2020-06-09 09:38:57 -05:00
parent f8caf62101
commit 6406984e10
1 changed files with 16 additions and 5 deletions

View File

@ -221,8 +221,9 @@ fn get_time_from_file_name(
/// # Params /// # Params
/// * `output_dir` - output directory containing /// * `output_dir` - output directory containing
/// * `keep` - number of backups to keep /// * `keep` - number of backups to keep
fn remove_old_backups(output_dir: &PathBuf, keep: u64) -> Result<(), std::io::Error> { fn remove_old_backups(output_dir: &PathBuf, keep: u64) -> Result<usize, std::io::Error> {
let mut backups = vec![]; let mut backups = vec![];
let mut num_of_removed_backups: usize = 0;
for entry in output_dir.read_dir()? { for entry in output_dir.read_dir()? {
let entry = entry?; let entry = entry?;
@ -242,15 +243,15 @@ fn remove_old_backups(output_dir: &PathBuf, keep: u64) -> Result<(), std::io::Er
b_time.cmp(&a_time) b_time.cmp(&a_time)
}); });
let number_to_remove = backups.len() - keep as usize; num_of_removed_backups = backups.len() - keep as usize;
for _i in 0..number_to_remove { for _i in 0..num_of_removed_backups {
let oldest = backups.pop().unwrap(); let oldest = backups.pop().unwrap();
remove_file(oldest.path())?; remove_file(oldest.path())?;
} }
} }
Ok(()) Ok(num_of_removed_backups)
} }
/// Sends a webhook to Discord if its configured /// Sends a webhook to Discord if its configured
@ -341,7 +342,15 @@ fn do_backup(cfg: AlbatrossConfig) -> Result<(), std::io::Error> {
remove_dir_all(&tmp_dir)?; remove_dir_all(&tmp_dir)?;
remove_old_backups(&cfg.backup.output_dir, cfg.backup.backups_to_keep)?; let backups_removed = remove_old_backups(&cfg.backup.output_dir, cfg.backup.backups_to_keep)?;
if backups_removed > 0 {
let msg = format!(
"Albatross mistook {} of your old backup for a french fries and ate it!! SKRAWWWW",
backups_removed
);
send_webhook(msg.as_str(), &cfg);
}
let secs = timer.elapsed().as_secs(); let secs = timer.elapsed().as_secs();
send_webhook( send_webhook(
@ -375,6 +384,8 @@ fn main() {
Err(e) => println!("Error doing backup: {}", e), Err(e) => println!("Error doing backup: {}", e),
_ => {} _ => {}
} }
println!("Backup complete");
} else { } else {
println!("No worlds specified to backed up!") println!("No worlds specified to backed up!")
} }