<# .SYNOPSIS Used in automatic p2fServer backup processing #> param( ## Kind of call: OK, ERROR [Parameter(Mandatory = $true, Position = 0)] [string]$Kind, ## Last p2fRoot change time: 2016-08-10 12:43:17 [Parameter(Mandatory = $true, Position = 1)] [string]$LastChange, ## Last backup time: 2016-08-10 12:43:17 [Parameter(Mandatory = $true, Position = 2)] [string]$LastBackup, ## Path to backup folder: "C:\tmp\p2f\Backup\p2fRoot-16-08-24T14-41" [Parameter(Position = 3)] [string]$BackupPath ) Set-StrictMode -Version 3 Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType Information -EventId 5000 -Message "Backup script called" # V5 Compress-Archive -Path C:\Stuff -DestinationPath archive.zip function ZipFiles($zipfilename, $sourcedir ) { Add-Type -Assembly System.IO.Compression.FileSystem $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, $zipfilename, $compressionLevel, $false) } # continue on ok only If ($Kind -ne "OK") { exit 1 } $ZipFileName = $BackupPath + ".zip" if ((Test-Path -Path $ZipFileName -PathType Leaf)) { Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType error -EventId 5000 -Message "Zip file already exist" exit 2 } $p2fError = $false If (Test-Path -Path $BackupPath) { Try { ZipFiles -zipfilename $ZipFileName -sourcedir $BackupPath } Catch { Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType error -EventId 5000 -Message "Error on zip backup folder" $p2fError = $true } } # remove backup folder If (! $p2fError) { Try { Remove-Item -Recurse -ErrorAction stop $BackupPath } Catch { Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType error -EventId 5000 -Message "Error on removing " + $BackupPath } } exit 0