Added File remote backups

+ Copies the backup to a target directory
+ Useful for backing up to another local drive or mounted network drive
+ Various improvements
+ Fixed .drone.yml
This commit is contained in:
zerohd committed 2021-09-19 13:56:30 -06:00
1 parent d207c6df19
commit 8378f2ead3
12 files changed
+70 -66

No files matched your search

+4 -4
View File
@@ -1,4 +1,4 @@
use crate::config::BackupConfig;
use crate::config::remote::FileConfig;
use crate::error::Result;
use crate::remote::{PathLocation, RemoteBackupSite};
use std::path::PathBuf;
@@ -12,10 +12,10 @@ pub struct FileBackup {
impl FileBackup {
/// New FileBackup
pub fn new(config: &BackupConfig) -> Result<Self> {
pub fn new(config: &FileConfig, backups_to_keep: usize) -> Result<Self> {
Ok(Self {
target_dir: config.output_dir.clone(),
backups_to_keep: config.backups_to_keep as usize,
target_dir: config.path.clone(),
backups_to_keep,
})
}
}
+6 -12
View File
@@ -1,10 +1,9 @@
use ftp::FtpStream;
use std::path::PathBuf;
use crate::config::RemoteBackupConfig;
use crate::error;
use crate::error::AlbatrossError;
use crate::remote::{PathLocation, RemoteBackupSite};
use crate::config::remote::FTPConfig;
/// FTP Remote Site
pub struct FTPBackup {
@@ -18,20 +17,15 @@ pub struct FTPBackup {
impl FTPBackup {
/// New FTPBackup
pub fn new(config: &RemoteBackupConfig) -> error::Result<Self> {
let ftp_config = config
.ftp
.as_ref()
.ok_or_else(|| AlbatrossError::RemoteConfigError("FTP".to_string()))?;
pub fn new(config: &FTPConfig, backups_to_keep: usize) -> error::Result<Self> {
let mut ftp_stream = FtpStream::connect(&config.server_addr)?;
let mut ftp_stream = FtpStream::connect(&ftp_config.server_addr)?;
ftp_stream.login(&ftp_config.username, &ftp_config.password)?;
ftp_stream.login(&config.username, &config.password)?;
Ok(Self {
stream: ftp_stream,
target_dir: ftp_config.remote_dir.clone(),
backups_to_keep: config.backups_to_keep as usize,
target_dir: config.remote_dir.clone(),
backups_to_keep,
})
}
}
+5 -5
View File
@@ -9,13 +9,13 @@ pub mod ftp;
pub mod sftp;
pub trait RemoteBackupFile {
/// Type containing the location of the remote backup
/// Type containing the location of the remote_backup backup
type LocationType;
/// Get the underlying location type
fn location(&self) -> Self::LocationType;
/// Get the time the remote file was created
/// Get the time the remote_backup file was created
fn time_created(&self) -> chrono::NaiveDateTime;
/// Parse the time created from the file name
@@ -34,10 +34,10 @@ pub trait RemoteBackupSite {
/// Struct representing the location of a backup on the site
type FileType: RemoteBackupFile;
/// Backup a file to the the remote site
/// Backup a file to the the remote_backup site
fn backup_to_remote(&mut self, file: PathBuf) -> Result<()>;
/// Get the locations backups contained on the remote site
/// Get the locations backups contained on the remote_backup site
fn get_backups(&mut self) -> Result<Vec<Self::FileType>>;
/// Remove a backup from the side
@@ -46,7 +46,7 @@ pub trait RemoteBackupSite {
/// Number of backups to keep on the site
fn backups_to_keep(&self) -> usize;
/// Cleanup old backups on the remote site
/// Cleanup old backups on the remote_backup site
fn cleanup(&mut self) -> Result<usize> {
let mut backups = self.get_backups()?;
+10 -15
View File
@@ -3,10 +3,10 @@ use std::path::PathBuf;
use ssh2::Session;
use crate::config::RemoteBackupConfig;
use crate::error;
use crate::error::AlbatrossError;
use crate::remote::{PathLocation, RemoteBackupSite};
use crate::config::remote::SFTPConfig;
/// SFTP Remote Site
pub struct SFTPBackup {
@@ -20,30 +20,25 @@ pub struct SFTPBackup {
impl SFTPBackup {
/// New SFTPBackup
pub fn new(config: &RemoteBackupConfig) -> error::Result<Self> {
let sftp_config = config
.sftp
.as_ref()
.ok_or_else(|| AlbatrossError::RemoteConfigError("SFTP".to_string()))?;
let tcp = TcpStream::connect(&sftp_config.server_addr)?;
pub fn new(config: &SFTPConfig, backups_to_keep: usize) -> error::Result<Self> {
let tcp = TcpStream::connect(&config.server_addr)?;
let mut sess = Session::new()?;
sess.set_tcp_stream(tcp);
sess.handshake().unwrap();
if let Some(password) = &sftp_config.password {
sess.userauth_password(&sftp_config.username, password)?;
} else if let Some(key) = &sftp_config.private_key {
let public_key = sftp_config.public_key.as_deref();
sess.userauth_pubkey_file(&sftp_config.username, public_key, key, None)?;
if let Some(password) = &config.password {
sess.userauth_password(&config.username, password)?;
} else if let Some(key) = &config.private_key {
let public_key = config.public_key.as_deref();
sess.userauth_pubkey_file(&config.username, public_key, key, None)?;
} else {
return Err(AlbatrossError::NoSSHAuth);
}
Ok(Self {
session: sess,
target_dir: sftp_config.remote_dir.clone(),
backups_to_keep: config.backups_to_keep as usize,
target_dir: config.remote_dir.clone(),
backups_to_keep,
})
}
}