Skip to content

Instantly share code, notes, and snippets.

@MVKozlov
Last active August 29, 2017 12:21
Show Gist options
  • Save MVKozlov/6d545443437209c72f7172922c985012 to your computer and use it in GitHub Desktop.
Save MVKozlov/6d545443437209c72f7172922c985012 to your computer and use it in GitHub Desktop.
Backup log files into date-named archives with folder structure saving.
# Needed DotNetZip.dll from https://github.com/haf/DotNetZip.Semverd
# in script directory
param(
$inputPath,
$outputPath,
$ArchivePrefix = 'Arch',
$days = 7
)
$inputpath = $inputpath -replace '\\$' # remove trailing \
$outputPath = $outputPath -replace '\\$' # remove trailing \
$lastwrite = (Get-Date).Date.AddDays(-$days) # always use 00:00 as start time
# Load dll
$ionic = Join-Path $PSScriptRoot 'DotNetZip.dll' #Ionic.Zip.dll
if (-not (Test-Path $ionic)) {
throw "Can''t find $ionic"
}
$dll = [System.Reflection.Assembly]::LoadFrom($ionic)
if (-not $dll) {
throw "Can''t load $ionic"
}
$ArchiveName = '{0}\{1}{2:yyyy-MM-dd}.zip' -f $outputPath, $ArchivePrefix, $lastwrite
"Creating new $ArchiveName "
$zip = New-Object Ionic.Zip.ZipFile $ArchiveName
$zip.UseUnicodeAsNecessary = $true
$zip.UseZip64WhenSaving = [Ionic.Zip.Zip64Option]::AsNecessary # Always
Get-ChildItem $inputPath -Recurse |
Where-Object { $_.Attributes -notmatch 'Directory' -and $_.LastWriteTime -lt $lastwrite } |
Foreach-Object {
$file = $_
$dir = $file.DirectoryName.Substring($inputPath.Length)
" Add $file to $dir"
[void]$zip.AddFile($file.FullName, $dir)
}
"Saving $ArchiveName with $($zip.Count) files"
$zip.Save()
$zip.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment