mirror of
https://git.ahines.net/joeyahines/Albatross.git
synced 2026-11-08 09:13:57 +00:00
Removed FTPS backups and added FTP backups
+ Reverted to ftp 3.0.1 + fmt + clippy
This commit is contained in:
8 files changed
+145
-51
No files matched your search
+6
-3
@@ -4,7 +4,7 @@ use crate::discord::send_webhook;
|
||||
use crate::error::Result;
|
||||
use crate::region::Region;
|
||||
use crate::remote::file::FileBackup;
|
||||
use crate::remote::ftps::FTPSBackup;
|
||||
use crate::remote::ftp::FTPBackup;
|
||||
use crate::remote::sftp::SFTPBackup;
|
||||
use crate::remote::RemoteBackupSite;
|
||||
use chrono::Utc;
|
||||
@@ -254,13 +254,16 @@ pub fn do_remote_backup(
|
||||
remote_backup_cfg: &RemoteBackupConfig,
|
||||
backup_path: PathBuf,
|
||||
) -> Result<()> {
|
||||
if let Ok(mut sftp_backup) = SFTPBackup::new(remote_backup_cfg) {
|
||||
if remote_backup_cfg.sftp.is_some() {
|
||||
let mut sftp_backup = SFTPBackup::new(&remote_backup_cfg)?;
|
||||
sftp_backup.backup_to_remote(backup_path)?;
|
||||
sftp_backup.cleanup()?;
|
||||
} else if let Ok(mut ftps_backup) = FTPSBackup::new(remote_backup_cfg) {
|
||||
} else if remote_backup_cfg.ftp.is_some() {
|
||||
let mut ftps_backup = FTPBackup::new(&remote_backup_cfg)?;
|
||||
ftps_backup.backup_to_remote(backup_path)?;
|
||||
ftps_backup.cleanup()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
mod remote;
|
||||
|
||||
use crate::config::remote::{FTPSConfig, SFTPConfig};
|
||||
use crate::config::remote::{FTPConfig, SFTPConfig};
|
||||
use config::{Config, ConfigError, File};
|
||||
use serde::Deserialize;
|
||||
use std::path::PathBuf;
|
||||
@@ -49,7 +49,7 @@ pub struct BackupConfig {
|
||||
pub struct RemoteBackupConfig {
|
||||
pub backups_to_keep: u64,
|
||||
pub sftp: Option<SFTPConfig>,
|
||||
pub ftps: Option<FTPSConfig>,
|
||||
pub ftp: Option<FTPConfig>,
|
||||
}
|
||||
|
||||
/// Configs
|
||||
|
||||
@@ -18,9 +18,9 @@ pub struct SFTPConfig {
|
||||
pub password: Option<String>,
|
||||
}
|
||||
|
||||
/// FTPS Config
|
||||
/// FTP Config
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct FTPSConfig {
|
||||
pub struct FTPConfig {
|
||||
/// Remote server address
|
||||
pub server_addr: String,
|
||||
/// Remote output directory
|
||||
@@ -29,6 +29,4 @@ pub struct FTPSConfig {
|
||||
pub username: String,
|
||||
/// Password
|
||||
pub password: String,
|
||||
/// Domain
|
||||
pub domain: Option<String>,
|
||||
}
|
||||
+3
-3
@@ -11,7 +11,7 @@ pub enum AlbatrossError {
|
||||
ChronoParseError(chrono::ParseError),
|
||||
NoSSHAuth,
|
||||
RemoteConfigError(String),
|
||||
FTPSError(ftp::FtpError),
|
||||
FTPError(ftp::FtpError),
|
||||
}
|
||||
|
||||
impl std::error::Error for AlbatrossError {}
|
||||
@@ -28,7 +28,7 @@ impl std::fmt::Display for AlbatrossError {
|
||||
AlbatrossError::ChronoParseError(e) => write!(f, "Unable to parse time: {}", e),
|
||||
AlbatrossError::NoSSHAuth => write!(f, "No SSH auth methods provided in the config"),
|
||||
AlbatrossError::RemoteConfigError(e) => write!(f, "Invalid configuration for {}", e),
|
||||
AlbatrossError::FTPSError(e) => write!(f, "FTPS error: {}", e),
|
||||
AlbatrossError::FTPError(e) => write!(f, "FTP error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,6 @@ impl From<chrono::ParseError> for AlbatrossError {
|
||||
|
||||
impl From<ftp::FtpError> for AlbatrossError {
|
||||
fn from(e: ftp::FtpError) -> Self {
|
||||
AlbatrossError::FTPSError(e)
|
||||
AlbatrossError::FTPError(e)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
use ftp::native_tls::TlsConnector;
|
||||
use ftp::FtpStream;
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -7,8 +6,8 @@ use crate::error;
|
||||
use crate::error::AlbatrossError;
|
||||
use crate::remote::{PathLocation, RemoteBackupSite};
|
||||
|
||||
/// FTPS Remote Site
|
||||
pub struct FTPSBackup {
|
||||
/// FTP Remote Site
|
||||
pub struct FTPBackup {
|
||||
/// FTP command stream
|
||||
stream: FtpStream,
|
||||
/// Remote target directory
|
||||
@@ -17,44 +16,39 @@ pub struct FTPSBackup {
|
||||
backups_to_keep: usize,
|
||||
}
|
||||
|
||||
impl FTPSBackup {
|
||||
/// New FTPSBackup
|
||||
impl FTPBackup {
|
||||
/// New FTPBackup
|
||||
pub fn new(config: &RemoteBackupConfig) -> error::Result<Self> {
|
||||
let ftps_config = config
|
||||
.ftps
|
||||
let ftp_config = config
|
||||
.ftp
|
||||
.as_ref()
|
||||
.ok_or_else(|| AlbatrossError::RemoteConfigError("FTPS".to_string()))?;
|
||||
.ok_or_else(|| AlbatrossError::RemoteConfigError("FTP".to_string()))?;
|
||||
|
||||
let ctx = TlsConnector::new().unwrap();
|
||||
let (ftp_stream, _) = FtpStream::connect(&ftps_config.server_addr)?;
|
||||
let mut ftp_stream = FtpStream::connect(&ftp_config.server_addr)?;
|
||||
|
||||
let domain = ftps_config
|
||||
.domain
|
||||
.as_ref()
|
||||
.map_or_else(|| "".to_string(), |s| s.clone());
|
||||
let mut ftp_stream = ftp_stream.into_secure(ctx, domain.as_str())?;
|
||||
ftp_stream.login(&ftps_config.username, &ftps_config.password)?;
|
||||
ftp_stream.login(&ftp_config.username, &ftp_config.password)?;
|
||||
|
||||
Ok(Self {
|
||||
stream: ftp_stream,
|
||||
target_dir: ftps_config.remote_dir.clone(),
|
||||
target_dir: ftp_config.remote_dir.clone(),
|
||||
backups_to_keep: config.backups_to_keep as usize,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for FTPSBackup {
|
||||
impl Drop for FTPBackup {
|
||||
fn drop(&mut self) {
|
||||
self.stream.quit().ok();
|
||||
}
|
||||
}
|
||||
|
||||
impl RemoteBackupSite for FTPSBackup {
|
||||
impl RemoteBackupSite for FTPBackup {
|
||||
type FileType = PathLocation;
|
||||
|
||||
fn backup_to_remote(&mut self, file: PathBuf) -> error::Result<()> {
|
||||
let mut local_file = std::fs::File::open(&file)?;
|
||||
let location = self.target_dir.join(file);
|
||||
let location = self.target_dir.join(file.file_name().unwrap());
|
||||
|
||||
self.stream
|
||||
.put(location.to_str().unwrap(), &mut local_file)?;
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ use chrono::NaiveDateTime;
|
||||
use crate::error::Result;
|
||||
|
||||
pub mod file;
|
||||
pub mod ftps;
|
||||
pub mod ftp;
|
||||
pub mod sftp;
|
||||
|
||||
pub trait RemoteBackupFile {
|
||||
|
||||
Reference in new issue
Block a user