Created
October 30, 2016 12:17
-
-
Save haamond/ad3cf3e9f9cb2c9a7b1c6f69395d8257 to your computer and use it in GitHub Desktop.
A Power Shell that backups mongoDB all collections and compress it into a zip file with help of .NET framework
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
function ZipFiles( $zipfilename, $sourcedir ) | |
{ | |
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | |
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal | |
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, $zipfilename, $compressionLevel, $False) | |
} | |
$date = Get-Date -UFormat %Y-%m-%d; | |
$backupFolder = $date; | |
$basePath = "C:\MongoBackup"; | |
$destinationPath = Join-Path $basePath $backupFolder; | |
if(!(Test-Path -Path $destinationPath)) { | |
New-Item -ItemType directory -Path $destinationPath; | |
(C:\mongodb\bin\mongodump.exe --out $destinationPath); | |
Add-Type -Path 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.IO.Compression.FileSystem.dll' | |
ZipFiles "$destinationPath.zip" $destinationPath | Out-Null | |
Remove-Item $destinationPath -Recurse -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment