Index of every page on this site: llms.txt
All skills
jeremylongshore avatar

/automating-database-backups

@820dfc8
Run it yourself

Automate 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'.

Use this Skill: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/automating-database-backups

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesmongodb_backup_restore.md

≈1.4k tokens on demand. Your agent reads this file only when SKILL.md points to it.

MongoDB Backup and Restore Guide

Backup Methods

mongodump (Logical Backup)

Single Database:

mongodump --uri="mongodb://localhost:27017" --db=mydb --out=/backup/

With Authentication:

mongodump --uri="mongodb://user:password@localhost:27017/mydb?authSource=admin" \
  --out=/backup/

With Compression:

mongodump --uri="mongodb://localhost:27017" \
  --db=mydb \
  --gzip \
  --out=/backup/$(date +%Y%m%d_%H%M%S)

Single Collection:

mongodump --uri="mongodb://localhost:27017" \
  --db=mydb \
  --collection=users \
  --out=/backup/

Archive Format (Single File):

mongodump --uri="mongodb://localhost:27017" \
  --db=mydb \
  --archive=/backup/mydb.archive \
  --gzip

Replica Set Backup

mongodump --uri="mongodb://rs1.example.com:27017,rs2.example.com:27017/mydb?replicaSet=myrs" \
  --readPreference=secondary \
  --out=/backup/

Sharded Cluster Backup

# 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:

mongorestore --uri="mongodb://localhost:27017" /backup/mydb/

To Different Database:

mongorestore --uri="mongodb://localhost:27017" \
  --nsFrom="mydb.*" \
  --nsTo="mydb_restored.*" \
  /backup/mydb/

From Archive:

mongorestore --uri="mongodb://localhost:27017" \
  --archive=/backup/mydb.archive \
  --gzip

Drop Collections First:

mongorestore --uri="mongodb://localhost:27017" \
  --drop \
  /backup/mydb/

Single Collection:

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:

mongodump --uri="mongodb://localhost:27017" \
  --oplog \
  --out=/backup/full_with_oplog

Restore to specific time:

mongorestore --uri="mongodb://localhost:27017" \
  --oplogReplay \
  --oplogLimit="1642248000:1" \
  /backup/full_with_oplog

Production Backup Script

#!/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:

# Backup Atlas cluster
mongodump --uri="mongodb+srv://user:pass@cluster.mongodb.net/mydb" \
  --gzip \
  --out=/backup/atlas_$(date +%Y%m%d)

Backup User Setup

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:

mongodump --uri="mongodb://localhost:27017" \
  --db=mydb \
  --collection=orders \
  --query='{"status":"completed","date":{"$gte":{"$date":"2025-01-01T00:00:00Z"}}}'

Exclude collections:

mongodump --uri="mongodb://localhost:27017" \
  --db=mydb \
  --excludeCollection=logs \
  --excludeCollection=sessions

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

2 warnings2mo3 checks · Risk MEDIUM
  • Gen Agent Trust Hub2mo

    The skill automates database backups by generating and scheduling shell scripts. While performing its intended function, it handles sensitive credentials, modifies system persistence via cron, and utilizes sudo for administrative tasks. The script generation logic is vulnerable to command injection if malicious database parameters are provided, and the documentation suggests insecure credential handling practices.

  • Socket2mo

    2 alerts: gptAnomaly

  • Snyk2mo

    Risk: LOW · No issues

Signed by skilld at 820dfc8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated last month
What it can do
Reads files Edits files Runs commands
version
1.27.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
tags
[
  "database",
  "postgresql",
  "mysql",
  "mongodb"
]
All 10 allowed tools
ReadWriteEditGrepGlobBash(pg_dump:*)Bash(mysqldump:*)Bash(mongodump:*)Bash(cron:*)Bash(gpg:*)
Other metadata
compatibility
Designed for Claude Code

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/automating-database-backups