Skip to content

Commit 2aa3580

Browse files
authored
seperate gzip & zip backup methods
1 parent 0960dc8 commit 2aa3580

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

backupMongoDatabase.ps1

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ $currentDate = get-date -format yyyyMMddHHmm
1414
$directoryName = "$databaseName-$currentDate"
1515
$directoryPath = Join-Path $backupPath $directoryName
1616

17-
<# Zip #>
18-
$zippedFileDestinationPath = "$backupPath\$directoryName.zip";
19-
2017
#endregion
2118

2219
#region Backup Process
@@ -38,12 +35,35 @@ Write-Host "Backing up the Database: '$databaseName' to local directory: $backup
3835

3936
# ATTENTION ⚠: Use the absolute path if you haven't added mongo to your System Path
4037
# Ex: C:\mongodb\bin\mongodump.exe -h $mongoDbHost -d $databaseName -o "$directoryPath"
41-
# Version 3.2 introduced gzip & archive
42-
mongodump --gzip -h "$mongoDbHost" -d "$databaseName" -o "$directoryPath"
38+
39+
<# ZIP #>
40+
# zip backup method for mongodbtools version 3.1 or lower
41+
42+
# $zippedFileDestinationPath = "$backupPath\$directoryName.zip";
43+
# mongodump -h "$mongoDbHost" -d "$databaseName" -o "$directoryPath"
44+
45+
<# gzip #>
46+
# gzip & archive for mongodbtools version 3.2 or above
47+
48+
$archiveFileDestinationPath = "$backupPath\$directoryName.gz";
49+
mongodump --gzip -h "$mongoDbHost" -d "$databaseName" --archive="$archiveFileDestinationPath"
50+
4351

4452
Write-Host "Creating the backup for $databaseName..."
4553

4654
$watch.Stop();
4755
Write-Host "MongoDB backup completed in "$watch.Elapsed.ToString()
4856

57+
# ATTENTION ⚠: UnComment Lines Below If You Wish To Use ZIP Method
58+
# Zip BackUp Folder (Powershell Built-in Command | Alternate Way)
59+
60+
# Write-Host "Zipping the folder backup folder..."
61+
# Compress-Archive -Path "$directoryPath" -DestinationPath $zippedFileDestinationPath
62+
63+
# Delete the uncompressed backup folter created
64+
65+
# Write-Host "Delete backup's folder"
66+
# Remove-Item "$directoryPath" -Recurse
67+
68+
4969
#endregion

0 commit comments

Comments
 (0)