Skip to content

Commit

Permalink
Added scripts to archive log files for issue #182.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellwest committed May 4, 2014
1 parent 6f90deb commit d81fc21
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
----item----
version: 1
id: {22C47B26-223F-4D5A-B760-9BB3C711AF9E}
database: master
path: /sitecore/system/Modules/PowerShell/Script Library/Functions/Compress-Archive
parent: {E22D066A-04D0-4799-9DAD-EDD9EB07C2B2}
name: Compress-Archive
master: {00000000-0000-0000-0000-000000000000}
template: {DD22F1B3-BD87-4DB2-9E7D-F7A496888D43}
templatekey: PowerShell Script

----field----
field: {B1A94FF0-6897-47C0-9C51-AA6ACB80B1F0}
name: Script
key: script
content-length: 1604

<#
.SYNOPSIS
Archives files into a zip archive.

.EXAMPLE
PS C:\> Get-ChildItem -Path . | Compress-Archive

.NOTES
Michael West
#>
function Compress-Archive {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true)]
[System.IO.DirectoryInfo[]]$Directory,

[Parameter(ValueFromPipeline=$true)]
[System.IO.FileInfo[]]$File,

[ValidateNotNullOrEmpty()]
[string]$Name = "archive",

[ValidateNotNullOrEmpty()]
[string]$OutputPath=("$($SitecoreDataFolder)\archived\")
)

begin {
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem

$level = [System.IO.Compression.CompressionLevel]::Optimal

if($Name -notmatch "zip$") {
$Name += ".zip"
}

if(!(Test-Path -Path $OutputPath)) {
New-Item -Path $OutputPath -ItemType directory
}

$zipPath = (Join-Path -Path $OutputPath -ChildPath $Name)
if(Test-Path -Path $zipPath) {
Remove-Item -Path $zipPath
}

$zipFile = [System.IO.Compression.ZipFile]::Open($zipPath, [System.IO.Compression.ZipArchiveMode]::Create)
}

process {
if($File) {
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zipFile, $File.FullName, $File.Name, $level) | Out-Null
}
}

end {
$zipFile.Dispose()
}
}
----version----
language: en
version: 1
revision: 9f194e70-6db5-4980-a1dc-b7dcb77df07d

----field----
field: {25BED78C-4957-4165-998A-CA1B52F67497}
name: __Created
key: __created
content-length: 15

20140428T210908
----field----
field: {8CDC337E-A112-42FB-BBB4-4143751E123F}
name: __Revision
key: __revision
content-length: 36

9f194e70-6db5-4980-a1dc-b7dcb77df07d
----field----
field: {D9CF14B1-FA16-4BA6-9288-E8A174D4D522}
name: __Updated
key: __updated
content-length: 34

20140504T130709:635348056298434237
----field----
field: {BADD9CF9-53E0-4D0C-BCC0-2D784C282F6A}
name: __Updated by
key: __updated by
content-length: 14

sitecore\admin
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
----item----
version: 1
id: {0B28D9EB-31D0-4276-A965-076C6EB2B48E}
database: master
path: /sitecore/system/Modules/PowerShell/Script Library/Tasks/Archive Logs
parent: {8CBF1E18-06EE-49E3-9C30-FB3DE16E96F3}
name: Archive Logs
master: {00000000-0000-0000-0000-000000000000}
template: {DD22F1B3-BD87-4DB2-9E7D-F7A496888D43}
templatekey: PowerShell Script

----field----
field: {B1A94FF0-6897-47C0-9C51-AA6ACB80B1F0}
name: Script
key: script
content-length: 1544

<#
.SYNOPSIS
Archives old log files into zip format to a separate archive directory.

.NOTES
Michael West
#>

<#
Load the function Compress-Archive. The Get-Item command supports a dynamic parameter
called ID whenever the Path parameter is specified. This basically runs the script first
before continuing.
#>
Execute-Script -Item (Get-Item -ID "{22C47B26-223F-4D5A-B760-9BB3C711AF9E}" -Path master:\)

# The archive filename will closely resemble the format of the default logfile names.
$archiveName = "logs.$([datetime]::Now.ToString("yyyy-MM-dd.HHmmss"))"
$archiveDirectory = "$($SitecoreDataFolder)\archived\"
$logDirectory = "$($SitecoreDataFolder)logs\"

# The filter includes log files older than 14 days based on LastWriteTime.
$filter = {
$date = [datetime]::Today.AddDays(-14)
$_.LastWriteTime -lt $date
}

# Get all the log files that match the filter criteria. After zipping up remove the log files.
$logs = Get-ChildItem -Path $logDirectory | Where-Object -Filter $filter
if($logs) {
$logs | Compress-Archive -Name $archiveName
$logs | Remove-Item
}

# The filter includes archive files older than 30 days based on LastWriteTime.
$filter = {
$date = [datetime]::Today.AddDays(-30)
$_.LastWriteTime -lt $date
}

# Get all the archived files that match the filter criteria. Remove the old archives.
$archives = Get-ChildItem -Path $archiveDirectory | Where-Object -Filter $filter
if($archives) {
$archives | Remove-Item
}
----version----
language: en
version: 1
revision: e0c10e7d-0e1b-46ec-8fdf-61489652230d

----field----
field: {25BED78C-4957-4165-998A-CA1B52F67497}
name: __Created
key: __created
content-length: 15

20140428T213145
----field----
field: {8CDC337E-A112-42FB-BBB4-4143751E123F}
name: __Revision
key: __revision
content-length: 36

e0c10e7d-0e1b-46ec-8fdf-61489652230d
----field----
field: {D9CF14B1-FA16-4BA6-9288-E8A174D4D522}
name: __Updated
key: __updated
content-length: 34

20140504T130430:635348054709742831
----field----
field: {BADD9CF9-53E0-4D0C-BCC0-2D784C282F6A}
name: __Updated by
key: __updated by
content-length: 14

sitecore\admin

0 comments on commit d81fc21

Please sign in to comment.