Backup Report

Automate database backups with Slack notifications. Get instant alerts on success or failure.

What it does

  1. Runs a database backup command
  2. Captures the backup result (success/failure, size, duration)
  3. Posts a formatted status message to Slack
  4. Tags oncall if the backup fails

Prerequisites

  • cli4ai add postgres - Install the Postgres package (or your DB package)
  • cli4ai add slack - Install the Slack package
  • Database and Slack credentials configured

Usage

# Run backup and notify ops channel
cli4ai routines run backup-report

# Notify a different channel
cli4ai routines run backup-report --var channel=infrastructure

# Add to crontab for daily backups at 2 AM
# 0 2 * * * /usr/local/bin/cli4ai routines run backup-report

Variables

Variable Default Description
channel ops Slack channel for notifications

Bash Version

Save as ~/.cli4ai/routines/backup-report.routine.sh

#!/usr/bin/env bash
set -euo pipefail

# backup-report.routine.sh
# Run database backups and post status to Slack
# Usage: cli4ai routines run backup-report --var channel=ops

CHANNEL="${CLI4AI_VAR_CHANNEL:-ops}"
DATE=$(date +%Y-%m-%d)
TIMESTAMP=$(date +%H:%M:%S)

echo "🗄️ Starting backup at $TIMESTAMP..." >&2

# Run postgres backup (adjust command for your setup)
BACKUP_RESULT=$(cli4ai run postgres backup --format json 2>&1) || true

# Parse result
if echo "$BACKUP_RESULT" | jq -e '.success' > /dev/null 2>&1; then
  SIZE=$(echo "$BACKUP_RESULT" | jq -r '.size // "unknown"')
  DURATION=$(echo "$BACKUP_RESULT" | jq -r '.duration // "unknown"')

  cli4ai run slack post "$CHANNEL" "✅ *Backup Complete* ($DATE $TIMESTAMP)
• Size: $SIZE
• Duration: $DURATION
\`\`\`json
$BACKUP_RESULT
\`\`\`"
  echo "✅ Backup successful, notified #$CHANNEL" >&2
else
  cli4ai run slack post "$CHANNEL" "❌ *Backup Failed* ($DATE $TIMESTAMP)
\`\`\`
$BACKUP_RESULT
\`\`\`
<@oncall> please investigate"
  echo "❌ Backup failed, alerted #$CHANNEL" >&2
  exit 1
fi

JSON Version

Save as ~/.cli4ai/routines/backup-report.routine.json

{
  "version": 1,
  "name": "backup-report",
  "description": "Run database backup and post status to Slack",
  "vars": {
    "channel": { "default": "ops" }
  },
  "steps": [
    {
      "id": "backup",
      "type": "cli4ai",
      "package": "postgres",
      "command": "backup",
      "capture": "json",
      "continueOnError": true
    },
    {
      "id": "get-date",
      "type": "exec",
      "cmd": "date",
      "args": ["+%Y-%m-%d %H:%M:%S"],
      "capture": "text"
    },
    {
      "id": "notify",
      "type": "cli4ai",
      "package": "slack",
      "command": "post",
      "args": [
        "{{vars.channel}}",
        "{{steps.backup.exitCode == 0 ? '✅' : '❌'}} *Backup {{steps.backup.exitCode == 0 ? 'Complete' : 'Failed'}}* ({{steps.get-date.stdout}})"
      ],
      "capture": "json"
    }
  ],
  "result": {
    "backup": "{{steps.backup.json}}",
    "notification": "{{steps.notify.json}}"
  }
}

Tips

  • Run via cron for scheduled daily/hourly backups
  • Modify to backup multiple databases in sequence
  • Add S3 upload step for offsite backup storage
  • Include disk space check before running backup

← Back to Examples