PowerShell MongoDB Backup Script
Automated backup solution for MongoDB on Windows
This PowerShell script automates MongoDB backups on Windows, handling backup creation, verification, retention management, and logging. It runs quietly in the background with comprehensive logging to text files and Windows Event Log.
Download Script Files
Purpose
Ensures reliable MongoDB database backups with these capabilities:
- Support local or remote MongoDB servers
- Automated retention and cleanup
- Detailed activity logging for auditing
- Optional backup verification
- Windows Event Log integration
Key Features
Flexible Target Configuration
- Backup local or remote MongoDB instances
- Default port 27017 with custom port support
- Username/password authentication support
Selective or Full Backups
Backup all databases, specific databases, or selected collections. Oplog support (--oplog) enables point-in-time consistent backups.
Backup Directory and Naming
Backups stored in configurable directory (default: D:\MongoBackups) with timestamped folders:
Logging and Event Recording
All activities logged to daily text files and Windows Application Event Log with timestamps and status.
2025-11-19 07:57:08 AM - INFO - Backup Scope: Entire database 'openeyedb'
2025-11-19 07:57:08 AM - INFO - Retention: Keep 10 latest backups
2025-11-19 07:57:08 AM - INFORMATION - Event Log: Starting MongoDB backup from server: localhost:27017 to D:\MongoBackups\mongodump_20251119_075708am
2025-11-19 07:57:08 AM - INFO - Applying retention policy...
2025-11-19 07:57:08 AM - INFO - Deleted old backup: mongodump_20251109_091920pm
2025-11-19 07:57:08 AM - SUCCESS - Retention cleanup completed. Deleted 1 old backups
2025-11-19 07:57:08 AM - SUCCESS - Created new backup folder: D:\MongoBackups\mongodump_20251119_075708am
2025-11-19 07:57:08 AM - INFO - Using authentication for user: backupuser
2025-11-19 07:57:08 AM - INFO - Starting MongoDB backup process...
2025-11-19 07:57:09 AM - INFORMATION - Event Log: MongoDB backup completed successfully. Backup size: 4.02 MB. Location: D:\MongoBackups\mongodump_20251119_075708am
2025-11-19 07:57:09 AM - SUCCESS - Backup completed successfully. Size: 4.02 MB
2025-11-19 07:57:09 AM - INFO - Verifying backup...
2025-11-19 07:57:09 AM - SUCCESS - Backup verification successful: Found 5 BSON/JSON files
2025-11-19 07:57:09 AM - INFORMATION - Event Log: Backup verification completed successfully
2025-11-19 07:57:09 AM - SUCCESS - === MongoDB Backup Completed Successfully ===
2025-11-19 01:42:44 PM - INFO - === MongoDB Backup Started ===
2025-11-19 01:42:44 PM - INFO - Server: localhost:27017 | Backup Path: D:\MongoBackups\mongodump_20251119_014244pm
Data Retention Policy
Automatic cleanup of old backups based on retention settings:
- RetentionDays: Days of backups to keep (default: 30)
- RetentionCount: Number of recent backups to keep (takes precedence)
Log File Management
- Maximum 10 log files retained
- Each log file limited to 25 MB
- Automatic rotation and deletion of old logs
Optional Backup Verification
When enabled, script verifies backup folders contain valid BSON/JSON files and metadata, with results logged to both text files and Windows Event Log.
Error Handling
All errors recorded in both text logs and Windows Event Log. Common issues include authentication failures, incorrect parameters, and insufficient disk space.
Automation and Scheduling
Can be executed manually or via Windows Task Scheduler for automatic operation. Runs silently when scheduled while maintaining complete activity logging.
Example Execution Scenarios
# === Usage Example ===
#############################################
# Backup everything on a localhost
Backup-MongoDB
# Basic backup without oplog and retention:
Backup-MongoDB -RetentionDays 15
# Keep only 10 latest backups:
Backup-MongoDB -RetentionCount 10
# Remote server with all features:
Backup-MongoDB -Server "SHB55" -Username "admin" -Password "secret" -UseOplog -RetentionCount 7 -VerifyBackup
# Custom backup directory with retention:
Backup-MongoDB -BackupDir "E:\MongoDB\Backups" -RetentionDays 30
# With backup verification:
Backup-MongoDB -Server "localhost" -VerifyBackup
# Backup multiple collections
Backup-MongoDB -Database "mydb" -Collections "users","orders","products"
# Backup single collection
Backup-MongoDB -Database "mydb" -Collections "users"
# Backup single database
Backup-MongoDB -Database testDB -Username "backupuser" -Password "SecureBackupPass123!"
# Backup single database
Backup-MongoDB -Database openeyedb -Username "backupuser" -Password "SecureBackupPass123!" -VerifyBackup -RetentionCount 10
Event Log Entries
| Event ID | Type | Description |
|---|---|---|
| 1000 | Information | Backup started |
| 1001 | Information | Backup completed successfully |
| 1002 | Information | Backup verification passed |
| 1003 | Warning | Backup verification failed |
| 2001 | Error | Backup process failed |
Create a Backup User in MongoDB
mongosh -u admin -p shb12345 --authenticationDatabase admin
use admin
db.createUser({
user: "backupuser",
pwd: "SecureBackupPass123!",
roles: [
{ role: "backup", db: "admin" },
{ role: "restore", db: "admin" },
{ role: "readAnyDatabase", db: "admin" }
]
})
Summary
The MongoDB Backup Script provides automated, consistent backups for local and remote MongoDB servers with built-in retention management, detailed logging, and optional verification for reliable data protection with minimal maintenance.
No comments:
Post a Comment