jeremylongshore/tons-of-skills-marketplace / plugins/database/database-backup-automator/skills/automating-database-backups
automating-database-backups
FlaggedAutomate database backup processes with scheduling, compression, and encryption. Supports PostgreSQL (pg_dump), MySQL (mysqldump), MongoDB (mongodump), and SQLite. Generates production-ready backup scripts with retention policies and restore procedures. Trigger: "automate database backups", "schedule backups", "create backup script", "disaster recovery". Use when working with automating database backups. Trigger with 'automating', 'database', 'backups'.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 5.8 KB |
| assets/README.md | markdown | 64 B |
| config/settings.yaml | config | 1.3 KB |
| references/README.md | markdown | 606 B |
| references/backup_best_practices.md | markdown | 7.1 KB |
| references/cron_syntax.md | markdown | 5.7 KB |
| references/mongodb_backup_restore.md | markdown | 5.5 KB |
| references/mysql_backup_restore.md | markdown | 5.4 KB |
| references/postgresql_backup_restore.md | markdown | 4.7 KB |
| references/sqlite_backup_restore.md | markdown | 6.8 KB |
| scripts/README.md | markdown | 495 B |
| scripts/backup-monitoring.shexecutable | script | 927 B |
| scripts/backup_scheduler.pyexecutable | script | 10.2 KB |
| scripts/backup_script_generator.pyexecutable | script | 12.1 KB |
| scripts/backup_validator.pyexecutable | script | 14.3 KB |
| scripts/restore_script_generator.pyexecutable | script | 15.4 KB |
references/mongodb_backup_restore.md
View on GitHub# MongoDB Backup and Restore Guide
## Backup Methods
### mongodump (Logical Backup)
**Single Database:**
```bash
mongodump --uri="mongodb://localhost:27017" --db=mydb --out=/backup/
```
**With Authentication:**
```bash
mongodump --uri="mongodb://user:password@localhost:27017/mydb?authSource=admin" \
--out=/backup/
```
**With Compression:**
```bash
mongodump --uri="mongodb://localhost:27017" \
--db=mydb \
--gzip \
--out=/backup/$(date +%Y%m%d_%H%M%S)
```
**Single Collection:**
```bash
mongodump --uri="mongodb://localhost:27017" \
--db=mydb \
--collection=users \
--out=/backup/
```
**Archive Format (Single File):**
```bash
mongodump --uri="mongodb://localhost:27017" \
--db=mydb \
--archive=/backup/mydb.archive \
--gzip
```
### Replica Set Backup
```bash
mongodump --uri="mongodb://rs1.example.com:27017,rs2.example.com:27017/mydb?replicaSet=myrs" \
--readPreference=secondary \
--out=/backup/
```
### Sharded Cluster Backup
```bash
# Connect to mongos
mongodump --uri="mongodb://mongos1.example.com:27017" \
--db=mydb \
--out=/backup/
```
## mongodump Options
| Option | Description |
|--------|-------------|
| `--db` | Database to dump |
| `--collection` | Collection to dump |
| `--query='{"active":true}'` | Filter documents |
| `--gzip` | Compress output files |
| `--archive=file` | Single archive file output |
| `--oplog` | Include oplog for point-in-time |
| `--numParallelCollections=4` | Parallel collection dumps |
| `--excludeCollection=col` | Skip collection |
| `--dumpDbUsersAndRoles` | Include users/roles |
## Restore Methods
### mongorestore
**From Directory:**
```bash
mongorestore --uri="mongodb://localhost:27017" /backup/mydb/
```
**To Different Database:**
```bash
mongorestore --uri="mongodb://localhost:27017" \
--nsFrom="mydb.*" \
--nsTo="mydb_restored.*" \
/backup/mydb/
```
**From Archive:**
```bash
mongorestore --uri="mongodb://localhost:27017" \
--archive=/backup/mydb.archive \
--gzip
```
**Drop Collections First:**
```bash
mongorestore --uri="mongodb://localhost:27017" \
--drop \
/backup/mydb/
```
**Single Collection:**
```bash
mongorestore --uri="mongodb://localhost:27017" \
--db=mydb \
--collection=users \
/backup/mydb/users.bson
```
## mongorestore Options
| Option | Description |
|--------|-------------|
| `--drop` | Drop collections before restore |
| `--noIndexRestore` | Skip index creation |
| `--numParallelCollections=4` | Parallel restore |
| `--numInsertionWorkersPerCollection=4` | Parallel inserts |
| `--stopOnError` | Stop on first error |
| `--maintainInsertionOrder` | Preserve document order |
## Point-in-Time Recovery
### With Oplog
**Backup with oplog:**
```bash
mongodump --uri="mongodb://localhost:27017" \
--oplog \
--out=/backup/full_with_oplog
```
**Restore to specific time:**
```bash
mongorestore --uri="mongodb://localhost:27017" \
--oplogReplay \
--oplogLimit="1642248000:1" \
/backup/full_with_oplog
```
## Production Backup Script
```bash
#!/bin/bash
set -euo pipefail
# Configuration
BACKUP_DIR="/var/backups/mongodb"
RETENTION_DAYS=7
MONGO_URI="${MONGO_URI:-mongodb://localhost:27017}"
DB_NAME="production"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_PATH="${BACKUP_DIR}/${DB_NAME}_${DATE}"
LOG_FILE="${BACKUP_DIR}/backup.log"
mkdir -p "$BACKUP_DIR"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
log "Starting backup of $DB_NAME"
# Perform backup
if mongodump --uri="$MONGO_URI" \
--db="$DB_NAME" \
--gzip \
--out="$BACKUP_PATH" 2>> "$LOG_FILE"; then
SIZE=$(du -sh "$BACKUP_PATH" | cut -f1)
log "Backup completed: $BACKUP_PATH ($SIZE)"
# Verify backup
BSON_COUNT=$(find "$BACKUP_PATH" -name "*.bson.gz" | wc -l)
if [ "$BSON_COUNT" -gt 0 ]; then
log "Backup verification passed ($BSON_COUNT collections)"
else
log "WARNING: No BSON files found in backup"
fi
else
log "ERROR: Backup failed"
rm -rf "$BACKUP_PATH"
exit 1
fi
# Create tar archive
log "Creating archive..."
tar -cf "${BACKUP_PATH}.tar" -C "$BACKUP_DIR" "${DB_NAME}_${DATE}"
rm -rf "$BACKUP_PATH"
log "Archive created: ${BACKUP_PATH}.tar"
# Cleanup old backups
log "Cleaning backups older than $RETENTION_DAYS days"
find "$BACKUP_DIR" -name "*.tar" -mtime "+$RETENTION_DAYS" -delete
log "Backup process completed"
```
## Cloud Backup (Atlas)
For MongoDB Atlas, use the built-in backup or:
```bash
# Backup Atlas cluster
mongodump --uri="mongodb+srv://user:pass@cluster.mongodb.net/mydb" \
--gzip \
--out=/backup/atlas_$(date +%Y%m%d)
```
## Backup User Setup
```javascript
use admin
db.createUser({
user: "backup",
pwd: "secure_password",
roles: [
{ role: "backup", db: "admin" },
{ role: "restore", db: "admin" }
]
})
```
## Troubleshooting
| Issue | Solution |
|-------|----------|
| Authentication failed | Check user credentials and authSource |
| Connection timeout | Increase `--socketTimeoutMS` |
| Oplog overflow | More frequent backups or larger oplog |
| Restore fails on indexes | Use `--noIndexRestore`, create indexes after |
| Memory issues | Reduce `--numParallelCollections` |
| Sharded cluster issues | Run mongodump against mongos, not individual shards |
## Selective Backup/Restore
**Query-based backup:**
```bash
mongodump --uri="mongodb://localhost:27017" \
--db=mydb \
--collection=orders \
--query='{"status":"completed","date":{"$gte":{"$date":"2025-01-01T00:00:00Z"}}}'
```
**Exclude collections:**
```bash
mongodump --uri="mongodb://localhost:27017" \
--db=mydb \
--excludeCollection=logs \
--excludeCollection=sessions
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |
| agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |