feat: added support for the 'MailServer' backup config
Build and Deploy / build-docker (push) Successful in 4m30s Details

This commit is contained in:
antifallobst 2023-11-23 16:30:05 +01:00
parent 57d8271c5d
commit 1f26e74e38
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
2 changed files with 36 additions and 12 deletions

View File

@ -1,10 +1,11 @@
use crate::backend::backup;
use anyhow::{bail, Context, Result};
use anyhow::{bail, Context, Error, Result};
use log::{error, info};
use sqlx::SqlitePool;
use std::fs::File;
use std::fs::{create_dir_all, remove_dir_all, File};
use std::io::{copy, BufReader};
use std::path::Path;
use std::process::Command;
use tokio::sync::mpsc;
use walkdir::WalkDir;
use zip::{write::FileOptions, CompressionMethod, ZipWriter};
@ -12,6 +13,7 @@ use zip::{write::FileOptions, CompressionMethod, ZipWriter};
fn add_dir_to_archive(
archive: &mut ZipWriter<File>,
dir: &Path,
base: &str,
options: FileOptions,
) -> Result<()> {
for entry in WalkDir::new(dir)
@ -19,16 +21,20 @@ fn add_dir_to_archive(
.into_iter()
.filter_map(|e| e.ok())
{
let file_type = entry.file_type();
if !entry.file_type().is_file() {
continue;
}
if file_type.is_dir() {
archive.add_directory(entry.path().to_string_lossy(), options)?;
} else if file_type.is_file() {
archive.start_file(entry.path().to_string_lossy(), options)?;
archive.start_file(
format!(
"{base}/{}",
entry.path().strip_prefix(dir)?.to_string_lossy()
),
options,
)?;
let mut reader = BufReader::new(File::open(entry.path())?);
copy(&mut reader, archive)?;
}
}
Ok(())
}
@ -47,13 +53,31 @@ fn perform_backup(backup: backup::Backup) -> Result<()> {
add_dir_to_archive(
&mut archive,
&Path::new(&format!("{host}/etc/nginx")),
"nginx",
options,
)?;
}
if backup.config.mail_server {
info!("Starting mail server backup...");
bail!("The config option 'mail_server' is not implemented yet!");
let dir = format!("{host}/opt/nerdcult/mailcow-dockerized/");
let tmp = format!("{}/tmp", env!("NC_AW_BACKUP_PATH"));
create_dir_all(&tmp)?;
let status = Command::new("bash")
.arg(format!("{dir}/helper-scripts/backup_and_restore.sh"))
.arg("backup")
.arg("all")
.env("MAILCOW_BACKUP_LOCATION", &tmp)
.status()?;
if !status.success() {
return Err(Error::msg("Failed to create mailcow backup}"));
}
add_dir_to_archive(&mut archive, &Path::new(&tmp), "mailcow", options)?;
remove_dir_all(&tmp)?;
}
if let Some(_cfg) = &backup.config.docker {

View File

@ -32,7 +32,7 @@ pub async fn prepare(pool: &SqlitePool) -> Result<()> {
.await?;
if !tokio::fs::try_exists(env!("NC_AW_BACKUP_PATH")).await? {
tokio::fs::create_dir(env!("NC_AW_BACKUP_PATH")).await?;
tokio::fs::create_dir_all(env!("NC_AW_BACKUP_PATH")).await?;
}
Ok(())