Skip to content

Commit

Permalink
Added zstd compression
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew121410 committed Sep 29, 2023
1 parent 467c6c1 commit da162af
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The backup function will back up the folders you specify, and compress them and
By default, it will use tar.gz, unless specified otherwise.

### Optional Backup Arguments
1. --format `The format to use (tar.gz, zip)`
1. --format `The format to use (tar.gz, tar.zst, zip)`
2. --exclude `Excludes files from the backup`
3. --sftp `Uploads the backup to a SFTP server. Example 1: --sftp user@host:22 /remote/path Example 2: --sftp "user@host:22 path/to/key /remote/path"` (**Password Authentication is not supported.**)
4. --delete-after-upload `Deletes the backup after uploading it to the SFTP server.`
Expand Down
34 changes: 32 additions & 2 deletions src/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use indicatif::{ProgressBar, ProgressStyle};
use openssh::Stdio;
use openssh_sftp_client::Sftp;

#[derive(PartialEq)]
pub enum BackupFormat {
TarGz,
TarZst,
Zip,
}

Expand Down Expand Up @@ -44,6 +46,7 @@ impl Backup {

let extension = match self.backup_format {
BackupFormat::TarGz => "tar.gz",
BackupFormat::TarZst => "tar.zst",
BackupFormat::Zip => "zip",
};

Expand All @@ -53,6 +56,11 @@ impl Backup {
return Err(Error::new(ErrorKind::Other, "The tar command does not exist. Please install it and try again."));
}
}
BackupFormat::TarZst => {
if !self.does_zstd_command_exist() {
return Err(Error::new(ErrorKind::Other, "The zstd command does not exist. Please install it and try again."));
}
}
BackupFormat::Zip => {
if !self.does_zip_command_exist() {
return Err(Error::new(ErrorKind::Other, "The zip command does not exist. Please install it and try again."));
Expand All @@ -74,7 +82,7 @@ impl Backup {
// Create compressed tar or zip archive of the Minecraft server files
let mut cmd: Command = Command::new("tar");
match self.backup_format {
BackupFormat::TarGz => {
BackupFormat::TarGz | BackupFormat::TarZst => {
// You may exclude files and folders by splitting them with a : (colon)
// Example: "logs:plugins/dynmap"
if let Some(exclude) = &self.exclude {
Expand All @@ -85,7 +93,12 @@ impl Backup {
}
}

cmd.arg("-czf").arg(&backup_path);
if self.backup_format == BackupFormat::TarZst {
cmd.arg("--zstd").arg("-cf"); // c = create, f = file
} else {
cmd.arg("-czf"); // c = create, z = gzip, f = file
}
cmd.arg(&backup_path);

// You may backup multiple folders by splitting them with a : (colon)
// Example: "world:world_nether:world_the_end"
Expand Down Expand Up @@ -148,6 +161,12 @@ impl Backup {
cmd.arg(&backup_path.file_name().unwrap());
cmd.arg(&hash_path.file_name().unwrap());
}
BackupFormat::TarZst => {
cmd.arg("--zstd").arg("-cf").arg(&combined_backup_path);
cmd.arg("-C").arg(&self.backup_directory);
cmd.arg(&backup_path.file_name().unwrap());
cmd.arg(&hash_path.file_name().unwrap());
}
BackupFormat::Zip => {
cmd = Command::new("zip");
cmd.current_dir(&self.backup_directory);
Expand Down Expand Up @@ -334,6 +353,17 @@ impl Backup {
the_output.status.success()
}

fn does_zstd_command_exist(&self) -> bool {
let output = Command::new("zstd").arg("--version").output();

if output.is_err() {
return false;
}

let the_output = output.unwrap();
the_output.status.success()
}

fn does_zip_command_exist(&self) -> bool {
let output = Command::new("zip").arg("--version").output();

Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async fn main() {
.action(ArgAction::Set)
.required(false))
.arg(clap::Arg::new("format")
.help("The format to use (tar.gz, zip)")
.help("The format to use (tar.gz, tar.zst, zip)")
.long("format")
.action(ArgAction::Set)
.required(false)
Expand Down Expand Up @@ -340,7 +340,9 @@ async fn handle_backup(backup_matches: &ArgMatches) {
}

let mut backup_format: BackupFormat = BackupFormat::TarGz;
if format.eq("zip") {
if format.eq("tar.zst") {
backup_format = BackupFormat::TarZst;
} else if format.eq("zip") {
backup_format = BackupFormat::Zip;
}

Expand Down

0 comments on commit da162af

Please sign in to comment.