Skip to content

Instantly share code, notes, and snippets.

@haamond
Created October 30, 2016 12:17
Show Gist options
  • Save haamond/ad3cf3e9f9cb2c9a7b1c6f69395d8257 to your computer and use it in GitHub Desktop.
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
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