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 crate::backend::backup;
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Error, Result};
use log::{error, info}; use log::{error, info};
use sqlx::SqlitePool; use sqlx::SqlitePool;
use std::fs::File; use std::fs::{create_dir_all, remove_dir_all, File};
use std::io::{copy, BufReader}; use std::io::{copy, BufReader};
use std::path::Path; use std::path::Path;
use std::process::Command;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use walkdir::WalkDir; use walkdir::WalkDir;
use zip::{write::FileOptions, CompressionMethod, ZipWriter}; use zip::{write::FileOptions, CompressionMethod, ZipWriter};
@ -12,6 +13,7 @@ use zip::{write::FileOptions, CompressionMethod, ZipWriter};
fn add_dir_to_archive( fn add_dir_to_archive(
archive: &mut ZipWriter<File>, archive: &mut ZipWriter<File>,
dir: &Path, dir: &Path,
base: &str,
options: FileOptions, options: FileOptions,
) -> Result<()> { ) -> Result<()> {
for entry in WalkDir::new(dir) for entry in WalkDir::new(dir)
@ -19,15 +21,19 @@ fn add_dir_to_archive(
.into_iter() .into_iter()
.filter_map(|e| e.ok()) .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)?;
let mut reader = BufReader::new(File::open(entry.path())?);
copy(&mut reader, archive)?;
} }
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(()) Ok(())
} }
@ -47,13 +53,31 @@ fn perform_backup(backup: backup::Backup) -> Result<()> {
add_dir_to_archive( add_dir_to_archive(
&mut archive, &mut archive,
&Path::new(&format!("{host}/etc/nginx")), &Path::new(&format!("{host}/etc/nginx")),
"nginx",
options, options,
)?; )?;
} }
if backup.config.mail_server { if backup.config.mail_server {
info!("Starting mail server backup..."); 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 { if let Some(_cfg) = &backup.config.docker {

View File

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