---
name: automating-database-backups
description: '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''.'
allowed-tools: Read, Write, Edit, Grep, Glob, Bash(pg_dump:*), Bash(mysqldump:*), Bash(mongodump:*), Bash(cron:*), Bash(gpg:*)
version: 1.27.0
author: Jeremy Longshore <jeremy@intentsolutions.io>
license: MIT
tags:
  - database
  - postgresql
  - mysql
  - mongodb
compatibility: Designed for Claude Code
title: automating-database-backups
canonical_url: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/automating-database-backups
last_updated: 2026-10-23T05:08:30.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [assets/README.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/assets/README.md), [config/settings.yaml](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/config/settings.yaml), [references/backup_best_practices.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/backup_best_practices.md), [references/cron_syntax.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/cron_syntax.md), [references/mongodb_backup_restore.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/mongodb_backup_restore.md), [references/mysql_backup_restore.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/mysql_backup_restore.md), [references/postgresql_backup_restore.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/postgresql_backup_restore.md), [references/README.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/README.md), [references/sqlite_backup_restore.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/sqlite_backup_restore.md), [scripts/backup_scheduler.py](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/scripts/backup_scheduler.py), [scripts/backup_script_generator.py](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/scripts/backup_script_generator.py), [scripts/backup_validator.py](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/scripts/backup_validator.py), [scripts/backup-monitoring.sh](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/scripts/backup-monitoring.sh), [scripts/README.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/scripts/README.md), [scripts/restore_script_generator.py](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/scripts/restore_script_generator.py).
>
> If the user asked to install this Skill, run `npx skilld install jeremylongshore/tons-of-skills-marketplace/automating-database-backups`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/automating-database-backups`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/jeremylongshore/tons-of-skills-marketplace/automating-database-backups) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/automating-database-backups`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/automating-database-backups --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/automating-database-backups --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Database Backup Automation

Generate production-ready backup scripts for PostgreSQL, MySQL, MongoDB, and SQLite with compression, encryption, scheduling, and retention policies.

## Quick Start

### PostgreSQL Backup

```bash
#!/bin/bash
set -euo pipefail
BACKUP_DIR="/var/backups/postgresql"
DB_NAME="mydb"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="${BACKUP_DIR}/${DB_NAME}_${DATE}.sql.gz"

pg_dump -h localhost -U postgres -d "$DB_NAME" \
  --format=custom \
  --compress=9 \
  --file="$BACKUP_FILE"

# Encrypt with GPG (optional)
gpg --symmetric --cipher-algo AES256 --batch --passphrase-file /etc/backup.key "$BACKUP_FILE"
rm "$BACKUP_FILE"
```

### MySQL Backup

```bash
#!/bin/bash
BACKUP_DIR="/var/backups/mysql"
DB_NAME="mydb"
DATE=$(date +%Y%m%d_%H%M%S)

mysqldump -h localhost -u root -p"${MYSQL_PASSWORD}" \
  --single-transaction \
  --routines \
  --triggers \
  "$DB_NAME" | gzip > "${BACKUP_DIR}/${DB_NAME}_${DATE}.sql.gz"
```

### MongoDB Backup

```bash
#!/bin/bash
mongodump --uri="mongodb://localhost:27017" \  # 27017: MongoDB port
  --db=mydb \
  --out=/var/backups/mongodb/$(date +%Y%m%d_%H%M%S) \
  --gzip
```

## Instructions

### Step 1: Gather Requirements

Ask the user for:

- Database type (PostgreSQL, MySQL, MongoDB, SQLite)
- Database connection details (host, port, database name)
- Backup schedule (cron expression or frequency)
- Retention policy (days to keep)
- Encryption requirement (yes/no)
- Backup destination (local path, S3, GCS)

### Step 2: Generate Backup Script

Use `scripts/backup_script_generator.py` to create a customized backup script:

```bash
python3 ${CLAUDE_SKILL_DIR}/scripts/backup_script_generator.py \
  --db-type postgresql \
  --database mydb \
  --output /opt/backup-scripts/mydb-backup.sh \
  --compression gzip \
  --encryption gpg
```

### Step 3: Schedule with Cron

Use `scripts/backup_scheduler.py` to create cron entries:

```bash
python3 ${CLAUDE_SKILL_DIR}/scripts/backup_scheduler.py \
  --script /opt/backup-scripts/mydb-backup.sh \
  --schedule "0 2 * * *" \
  --user postgres
```

### Step 4: Validate Backup

After backup completes, validate integrity:

```bash
python3 ${CLAUDE_SKILL_DIR}/scripts/backup_validator.py \
  --backup-file /var/backups/postgresql/mydb_20250115.sql.gz \
  --db-type postgresql
```

### Step 5: Generate Restore Procedure

Create matching restore script:

```bash
python3 ${CLAUDE_SKILL_DIR}/scripts/restore_script_generator.py \
  --db-type postgresql \
  --database mydb \
  --output /opt/backup-scripts/mydb-restore.sh
```

## Cron Schedule Reference

| Schedule | Cron Expression | Description |
|----------|-----------------|-------------|
| Daily 2 AM | `0 2 * * *` | Low-traffic window |
| Every 6 hours | `0 */6 * * *` | Frequent backups |
| Weekly Sunday | `0 2 * * 0` | Weekly full backup |
| Monthly 1st | `0 2 1 * *` | Monthly archive |

## Retention Policy Example

```bash
# Keep daily backups for 7 days
# Keep weekly backups for 4 weeks
# Keep monthly backups for 12 months

find /var/backups -name "*.gz" -mtime +7 -delete  # Daily cleanup
find /var/backups/weekly -mtime +28 -delete       # Weekly cleanup
find /var/backups/monthly -mtime +365 -delete     # 365: Monthly cleanup
```

## Output

- **Backup Scripts**: Database-specific shell scripts with compression and encryption
- **Cron Entries**: Ready-to-install crontab configurations
- **Restore Scripts**: Matching restore procedures for each backup type
- **Validation Reports**: Integrity check results for backup files

## Error Handling

| Error | Cause | Solution |
|-------|-------|----------|
| Connection refused | DB not running | Check service status: `systemctl status postgresql` |
| Permission denied | Wrong credentials | Verify user has backup privileges |
| Disk full | No space | Check space: `df -h`, clean old backups |
| Lock timeout | Active transactions | Use `--single-transaction` for MySQL |

## Resources

- [`references/postgresql_backup_restore.md`](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/postgresql_backup_restore.md) - PostgreSQL backup guide
- [`references/mysql_backup_restore.md`](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/mysql_backup_restore.md) - MySQL backup guide
- [`references/mongodb_backup_restore.md`](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/mongodb_backup_restore.md) - MongoDB backup guide
- [`references/sqlite_backup_restore.md`](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/sqlite_backup_restore.md) - SQLite backup guide
- [`references/backup_best_practices.md`](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/backup_best_practices.md) - Security and storage best practices
- [`references/cron_syntax.md`](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/automating-database-backups/references/cron_syntax.md) - Cron scheduling reference

## Overview

Automate database backup processes with scheduling, compression, and encryption.

## Prerequisites

- Access to the PostgreSQL environment or API
- Required CLI tools installed and authenticated
- Familiarity with PostgreSQL concepts and terminology

## Examples

**Basic usage**: Apply automating database backups to a standard project setup with default configuration options.

**Advanced scenario**: Customize automating database backups for production environments with multiple constraints and team-specific requirements.
