Skip to content

Commit 3daefe6

Browse files
Merge pull request #1 from alirzasahb/main
added built-in compress for mongodump command
2 parents 97b2331 + 2aa3580 commit 3daefe6

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

backupMongoDatabase.ps1

Lines changed: 23 additions & 10 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,19 +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-
mongodump -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+
4251

4352
Write-Host "Creating the backup for $databaseName..."
4453

4554
$watch.Stop();
4655
Write-Host "MongoDB backup completed in "$watch.Elapsed.ToString()
4756

48-
# Zip sfolder
49-
Write-Host "Zipping the folder backup folder..."
50-
Compress-Archive -Path "$directoryPath" -DestinationPath $zippedFileDestinationPath
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
5167

52-
# Delete the backup folter created
53-
Write-Host "Delete backup's folder"
54-
Remove-Item "$directoryPath" -Recurse
5568

5669
#endregion

0 commit comments

Comments
 (0)