Last active
August 29, 2017 12:21
-
-
Save MVKozlov/6d545443437209c72f7172922c985012 to your computer and use it in GitHub Desktop.
Backup log files into date-named archives with folder structure saving.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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