Created
June 11, 2018 22:51
-
-
Save Hexalon/3259f28b053f7f0c483fc89c0b700a45 to your computer and use it in GitHub Desktop.
Backup script to work around vendor's design limitations.
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
<# | |
.NOTES | |
=========================================================================== | |
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.82 | |
Created on: 3/6/2015 12:58 | |
Created by: Colin Squier <[email protected]> | |
Filename: DBBackup.ps1 | |
=========================================================================== | |
.DESCRIPTION | |
Backup script to work around vendor's design limitations. | |
#> | |
<# | |
.SYNOPSIS | |
Creates a .zip archive from a file or directory. | |
.DESCRIPTION | |
This function makes use of the System.IO.Compression.Zipfile class that is included with .NET 4.5. | |
Using the PowerShell cmdlet Add-Type to add a Microsoft .NET Framework type to the PowerShell session, | |
by leveraging .NET classes to natively create .zip archives. | |
.PARAMETER DestinationFileName | |
The zip file that you intend to create. | |
.PARAMETER SourceDirectory | |
Source data to include in the archive. | |
.PARAMETER CompressionLevel | |
The type of compression to use. Optimal (default option), Fastest, NoCompression. | |
.PARAMETER IncludeParentDir | |
This option will include the parent directory. | |
.EXAMPLE | |
PS C:\> Zip-Directory -DestinationFileName 'Value1' -SourceDirectory 'Value2' | |
.NOTES | |
Use of this function requires .NET 4.5 and PowerShell 3+. | |
#> | |
function Zip-Directory | |
{ | |
Param ( | |
[Parameter(Mandatory = $True)][string]$DestinationFileName, | |
[Parameter(Mandatory = $True)][string]$SourceDirectory, | |
[Parameter(Mandatory = $False)][string]$CompressionLevel = "Optimal", | |
[Parameter(Mandatory = $False)][switch]$IncludeParentDir | |
) | |
Add-Type -AssemblyName System.IO.Compression.FileSystem | |
$CompressionLevel = [System.IO.Compression.CompressionLevel]::$CompressionLevel | |
[System.IO.Compression.ZipFile]::CreateFromDirectory($SourceDirectory, $DestinationFileName, $CompressionLevel, $IncludeParentDir) | |
} | |
Copy-Item -Path "C:\DBCDRdump.sql" -Destination "E:\DB backup\DBSQLBackup\DBCDRdump.sql" -Force | |
Copy-Item -Path "C:\DBConfigDump.sql" -Destination "E:\DB backup\DBSQLBackup\DBConfigDump.sql" -Force | |
$SourceFolder = "E:\DB backup\" | |
$DestinationFileName = "E:\DBBackup-" + (Get-Date).ToString("yyyyMMdd-HHmmss") + ".zip" | |
$Compression = "Optimal" | |
Zip-Directory -DestinationFileName $DestinationFileName ` | |
-SourceDirectory $SourceFolder ` | |
-CompressionLevel $Compression ` | |
$Path = "E:\" | |
$Keep = 3 | |
$Dirs = Get-ChildItem -Path $Path -Recurse | Where-Object { $_.PsIsContainer } | |
foreach ($Dir in $Dirs) | |
{ | |
$Files = Get-ChildItem -Path $Dir.FullName | Where-Object { -not $_.PsIsContainer -and $_.Name -match "DBBackup-\d{8}-\d{6}.zip" } | |
if ($Files.Count -gt $Keep) | |
{ | |
$Files | Sort-Object CreationTime | Select-Object -First ($Files.Count - $Keep) | Remove-Item -Force | |
} | |
} | |
$Files = Get-ChildItem -Path $Path | Where-Object { -not $_.PsIsContainer -and $_.Name -match "DBBackup-\d{8}-\d{6}.zip" } | |
foreach ($File in $Files) | |
{ | |
$Path = Join-Path -Path E:\ -ChildPath $File | |
Copy-Item -Path $Path -Destination \\10.20.48.81\E$\DBBackup\$File | |
Clear-Variable -Name Path | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment