Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7Zip4PowerShell and backup filesize/duration #65

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ By [@BananaAcid](https://github.com/BananaAcid/)
- [Java JDK](https://aws.amazon.com/corretto/?filtered-posts.sort-by=item.additionalFields.createdDate&filtered-posts.sort-order=desc)
- [Microsoft XNA Redistributable](https://www.microsoft.com/en-ca/download/details.aspx?id=20914)

# Optional Additional Packages

- [7Zip4PowerShell](https://www.powershellgallery.com/packages/7Zip4Powershell/)

```ps
Install-Module -Name 7Zip4Powershell
```

# Expanding the Code

You can create pull requests for changes to this project.
Expand Down
10 changes: 8 additions & 2 deletions functions/server/Backup-Server.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function Backup-Server {
Write-ServerMsg "Creating Backup."
#Create backup name from date and time
$BackupName = Get-TimeStamp
$BackupStart = Get-Date

#Check if it's friday (Sunday is 0)
if ((Get-Date -UFormat %u) -eq 5) {
Expand Down Expand Up @@ -89,7 +90,11 @@ try {
}

# Compress the temporary directory into a zip archive using the specified compression options
Compress-Archive -Path $TempDirectory -DestinationPath "$($Backups.Path)\$Type\$BackupName.zip" -CompressionLevel 'Fastest'
if (Get-Module -ListAvailable -Name 7Zip4PowerShell) {
Compress-7Zip -Path $TempDirectory -ArchiveFileName "$($Backups.Path)\$Type\$BackupName.zip" -CompressionLevel 'Fast' -Format Zip
} else {
Compress-Archive -Path $TempDirectory -DestinationPath "$($Backups.Path)\$Type\$BackupName.zip" -CompressionLevel 'Fastest'
}

# Remove the temporary directory
Remove-Item -Path $TempDirectory -Force -Recurse
Expand All @@ -98,7 +103,8 @@ catch {
Exit-WithError -ErrorMsg "Unable to backup server."
}

Write-ServerMsg "Backup Created : $BackupName.zip"
$zipFile = Get-Item "$($Backups.Path)\$Type\$BackupName.zip"
Write-ServerMsg "Backup Created : $BackupName.zip [$(Format-FileSize $zipFile.Length)][$((New-TimeSpan -Start $BackupStart).toString("g"))]"

#Delete old backups
Write-ServerMsg "Deleting old backups."
Expand Down
12 changes: 12 additions & 0 deletions functions/util/Format-FileSize.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function Format-FileSize {
param([long]$size)
if ($size -gt 1GB) {
"{0:N2} GB" -f ($size / 1GB)
} elseif ($size -gt 1MB) {
"{0:N2} MB" -f ($size / 1MB)
} elseif ($size -gt 1KB) {
"{0:N2} KB" -f ($size / 1KB)
} else {
"$size bytes"
}
}