Removed FTPS backups and added FTP backups

+ Reverted to ftp 3.0.1
+ fmt + clippy
backup_error_fix
Joey Hines 2021-02-12 20:02:57 -06:00
parent 249ad8dad8
commit 0ff76dec0a
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
8 changed files with 145 additions and 51 deletions

133
Cargo.lock generated
View File

@ -21,13 +21,22 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2" checksum = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2"
[[package]]
name = "aho-corasick"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
dependencies = [
"memchr 0.1.11",
]
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.10" version = "0.7.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada" checksum = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada"
dependencies = [ dependencies = [
"memchr", "memchr 2.3.3",
] ]
[[package]] [[package]]
@ -35,13 +44,13 @@ name = "albatross"
version = "0.4.0" version = "0.4.0"
dependencies = [ dependencies = [
"anvil-region", "anvil-region",
"chrono", "chrono 0.4.19",
"config", "config",
"discord-hooks-rs", "discord-hooks-rs",
"flate2", "flate2",
"ftp", "ftp",
"log", "log",
"regex", "regex 1.4.3",
"reqwest", "reqwest",
"serde 1.0.117", "serde 1.0.117",
"ssh2", "ssh2",
@ -159,6 +168,16 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9213f7cd7c27e95c2b57c49f0e69b1ea65b27138da84a170133fd21b07659c00"
dependencies = [
"num",
"time",
]
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.19" version = "0.4.19"
@ -338,13 +357,12 @@ checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]] [[package]]
name = "ftp" name = "ftp"
version = "3.0.1" version = "3.0.1"
source = "git+https://github.com/joeyahines/rust-ftp.git#ab44662b5f27d18116a2721c1ddf76e117ed88be" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "542951aad0071952c27409e3bd7cb62d1a3ad419c4e7314106bf994e0083ad5d"
dependencies = [ dependencies = [
"chrono", "chrono 0.2.25",
"lazy_static 1.4.0", "lazy_static 0.1.16",
"native-tls", "regex 0.1.80",
"openssl",
"regex",
] ]
[[package]] [[package]]
@ -408,7 +426,7 @@ dependencies = [
"futures-core", "futures-core",
"futures-io", "futures-io",
"futures-task", "futures-task",
"memchr", "memchr 2.3.3",
"pin-project", "pin-project",
"pin-utils", "pin-utils",
"slab", "slab",
@ -586,6 +604,12 @@ dependencies = [
"winapi-build", "winapi-build",
] ]
[[package]]
name = "lazy_static"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "0.2.11" version = "0.2.11"
@ -670,6 +694,15 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
[[package]]
name = "memchr"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.3.3" version = "2.3.3"
@ -788,10 +821,21 @@ version = "4.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6" checksum = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
dependencies = [ dependencies = [
"memchr", "memchr 2.3.3",
"version_check 0.1.5", "version_check 0.1.5",
] ]
[[package]]
name = "num"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e"
dependencies = [
"num-integer",
"num-iter",
"num-traits 0.2.11",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.42" version = "0.1.42"
@ -802,6 +846,17 @@ dependencies = [
"num-traits 0.2.11", "num-traits 0.2.11",
] ]
[[package]]
name = "num-iter"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59"
dependencies = [
"autocfg",
"num-integer",
"num-traits 0.2.11",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.1.43" version = "0.1.43"
@ -1044,18 +1099,37 @@ version = "0.1.56"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
[[package]]
name = "regex"
version = "0.1.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
dependencies = [
"aho-corasick 0.5.3",
"memchr 0.1.11",
"regex-syntax 0.3.9",
"thread_local 0.2.7",
"utf8-ranges",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.4.3" version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick 0.7.10",
"memchr", "memchr 2.3.3",
"regex-syntax", "regex-syntax 0.6.22",
"thread_local", "thread_local 1.0.1",
] ]
[[package]]
name = "regex-syntax"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.22" version = "0.6.22"
@ -1187,7 +1261,7 @@ dependencies = [
"lazy_static 0.2.11", "lazy_static 0.2.11",
"linked-hash-map 0.3.0", "linked-hash-map 0.3.0",
"num-traits 0.1.43", "num-traits 0.1.43",
"regex", "regex 1.4.3",
"serde 0.8.23", "serde 0.8.23",
] ]
@ -1358,6 +1432,25 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "thread-id"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
dependencies = [
"kernel32-sys",
"libc",
]
[[package]]
name = "thread_local"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
dependencies = [
"thread-id",
]
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.0.1" version = "1.0.1"
@ -1388,7 +1481,7 @@ dependencies = [
"futures-core", "futures-core",
"iovec", "iovec",
"lazy_static 1.4.0", "lazy_static 1.4.0",
"memchr", "memchr 2.3.3",
"mio", "mio",
"num_cpus", "num_cpus",
"pin-project-lite", "pin-project-lite",
@ -1496,6 +1589,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "utf8-ranges"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.9" version = "0.2.9"

View File

@ -19,4 +19,4 @@ reqwest = { version = "0.10", features = ["blocking", "json"] }
discord-hooks-rs = { git = "https://github.com/joeyahines/discord-hooks-rs" } discord-hooks-rs = { git = "https://github.com/joeyahines/discord-hooks-rs" }
anvil-region = "0.4.0" anvil-region = "0.4.0"
ssh2 = "0.9.1" ssh2 = "0.9.1"
ftp = { git = "https://github.com/joeyahines/rust-ftp.git" , features=["secure", "native-tls"]} ftp = "3.0.1"

View File

@ -4,7 +4,7 @@ use crate::discord::send_webhook;
use crate::error::Result; use crate::error::Result;
use crate::region::Region; use crate::region::Region;
use crate::remote::file::FileBackup; use crate::remote::file::FileBackup;
use crate::remote::ftps::FTPSBackup; use crate::remote::ftp::FTPBackup;
use crate::remote::sftp::SFTPBackup; use crate::remote::sftp::SFTPBackup;
use crate::remote::RemoteBackupSite; use crate::remote::RemoteBackupSite;
use chrono::Utc; use chrono::Utc;
@ -254,13 +254,16 @@ pub fn do_remote_backup(
remote_backup_cfg: &RemoteBackupConfig, remote_backup_cfg: &RemoteBackupConfig,
backup_path: PathBuf, backup_path: PathBuf,
) -> Result<()> { ) -> 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.backup_to_remote(backup_path)?;
sftp_backup.cleanup()?; 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.backup_to_remote(backup_path)?;
ftps_backup.cleanup()?; ftps_backup.cleanup()?;
} }
Ok(()) Ok(())
} }

View File

@ -1,6 +1,6 @@
mod remote; mod remote;
use crate::config::remote::{FTPSConfig, SFTPConfig}; use crate::config::remote::{FTPConfig, SFTPConfig};
use config::{Config, ConfigError, File}; use config::{Config, ConfigError, File};
use serde::Deserialize; use serde::Deserialize;
use std::path::PathBuf; use std::path::PathBuf;
@ -49,7 +49,7 @@ pub struct BackupConfig {
pub struct RemoteBackupConfig { pub struct RemoteBackupConfig {
pub backups_to_keep: u64, pub backups_to_keep: u64,
pub sftp: Option<SFTPConfig>, pub sftp: Option<SFTPConfig>,
pub ftps: Option<FTPSConfig>, pub ftp: Option<FTPConfig>,
} }
/// Configs /// Configs

View File

@ -18,9 +18,9 @@ pub struct SFTPConfig {
pub password: Option<String>, pub password: Option<String>,
} }
/// FTPS Config /// FTP Config
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct FTPSConfig { pub struct FTPConfig {
/// Remote server address /// Remote server address
pub server_addr: String, pub server_addr: String,
/// Remote output directory /// Remote output directory
@ -29,6 +29,4 @@ pub struct FTPSConfig {
pub username: String, pub username: String,
/// Password /// Password
pub password: String, pub password: String,
/// Domain
pub domain: Option<String>,
} }

View File

@ -11,7 +11,7 @@ pub enum AlbatrossError {
ChronoParseError(chrono::ParseError), ChronoParseError(chrono::ParseError),
NoSSHAuth, NoSSHAuth,
RemoteConfigError(String), RemoteConfigError(String),
FTPSError(ftp::FtpError), FTPError(ftp::FtpError),
} }
impl std::error::Error for AlbatrossError {} 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::ChronoParseError(e) => write!(f, "Unable to parse time: {}", e),
AlbatrossError::NoSSHAuth => write!(f, "No SSH auth methods provided in the config"), AlbatrossError::NoSSHAuth => write!(f, "No SSH auth methods provided in the config"),
AlbatrossError::RemoteConfigError(e) => write!(f, "Invalid configuration for {}", e), 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 { impl From<ftp::FtpError> for AlbatrossError {
fn from(e: ftp::FtpError) -> Self { fn from(e: ftp::FtpError) -> Self {
AlbatrossError::FTPSError(e) AlbatrossError::FTPError(e)
} }
} }

View File

@ -1,4 +1,3 @@
use ftp::native_tls::TlsConnector;
use ftp::FtpStream; use ftp::FtpStream;
use std::path::PathBuf; use std::path::PathBuf;
@ -7,8 +6,8 @@ use crate::error;
use crate::error::AlbatrossError; use crate::error::AlbatrossError;
use crate::remote::{PathLocation, RemoteBackupSite}; use crate::remote::{PathLocation, RemoteBackupSite};
/// FTPS Remote Site /// FTP Remote Site
pub struct FTPSBackup { pub struct FTPBackup {
/// FTP command stream /// FTP command stream
stream: FtpStream, stream: FtpStream,
/// Remote target directory /// Remote target directory
@ -17,44 +16,39 @@ pub struct FTPSBackup {
backups_to_keep: usize, backups_to_keep: usize,
} }
impl FTPSBackup { impl FTPBackup {
/// New FTPSBackup /// New FTPBackup
pub fn new(config: &RemoteBackupConfig) -> error::Result<Self> { pub fn new(config: &RemoteBackupConfig) -> error::Result<Self> {
let ftps_config = config let ftp_config = config
.ftps .ftp
.as_ref() .as_ref()
.ok_or_else(|| AlbatrossError::RemoteConfigError("FTPS".to_string()))?; .ok_or_else(|| AlbatrossError::RemoteConfigError("FTP".to_string()))?;
let ctx = TlsConnector::new().unwrap(); let mut ftp_stream = FtpStream::connect(&ftp_config.server_addr)?;
let (ftp_stream, _) = FtpStream::connect(&ftps_config.server_addr)?;
let domain = ftps_config ftp_stream.login(&ftp_config.username, &ftp_config.password)?;
.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)?;
Ok(Self { Ok(Self {
stream: ftp_stream, 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, backups_to_keep: config.backups_to_keep as usize,
}) })
} }
} }
impl Drop for FTPSBackup { impl Drop for FTPBackup {
fn drop(&mut self) { fn drop(&mut self) {
self.stream.quit().ok(); self.stream.quit().ok();
} }
} }
impl RemoteBackupSite for FTPSBackup { impl RemoteBackupSite for FTPBackup {
type FileType = PathLocation; type FileType = PathLocation;
fn backup_to_remote(&mut self, file: PathBuf) -> error::Result<()> { fn backup_to_remote(&mut self, file: PathBuf) -> error::Result<()> {
let mut local_file = std::fs::File::open(&file)?; 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 self.stream
.put(location.to_str().unwrap(), &mut local_file)?; .put(location.to_str().unwrap(), &mut local_file)?;

View File

@ -5,7 +5,7 @@ use chrono::NaiveDateTime;
use crate::error::Result; use crate::error::Result;
pub mod file; pub mod file;
pub mod ftps; pub mod ftp;
pub mod sftp; pub mod sftp;
pub trait RemoteBackupFile { pub trait RemoteBackupFile {