Recipes

Backup & recovery

Back up MySQL to S3

Create a compressed, transaction-consistent MySQL dump and upload it to an S3-compatible bucket.

Supported platforms

LinuxmacOS

Prerequisites

  • mysqldump and gzip
  • An S3-compatible bucket
  • MySQL and S3 credentials exposed through environment-backed Dagu secrets

Workflow YAML

description: Create a compressed MySQL dump and upload it to S3.
schedule: "30 2 * * *"

params:
  - name: host
    default: 127.0.0.1
  - name: port
    type: integer
    default: 3306
    minimum: 1
    maximum: 65535
  - name: user
    default: backup
  - name: database
    required: true
  - name: bucket
    required: true
  - name: prefix
    default: mysql

secrets:
  - name: MYSQL_PASSWORD
    provider: env
    key: MYSQL_PASSWORD
  - name: AWS_ACCESS_KEY_ID
    provider: env
    key: AWS_ACCESS_KEY_ID
  - name: AWS_SECRET_ACCESS_KEY
    provider: env
    key: AWS_SECRET_ACCESS_KEY

defaults:
  retry_policy:
    limit: 2
    interval_sec: 30

steps:
  - id: create_dump
    run: |
      #!/bin/sh
      set -eu
      timestamp="$(date -u +%Y%m%dT%H%M%SZ)"
      file="$DAG_RUN_WORK_DIR/${params.database}-${timestamp}.sql.gz"
      key="${params.prefix}/${params.database}-${timestamp}.sql.gz"
      MYSQL_PWD="$MYSQL_PASSWORD" mysqldump \
        --single-transaction \
        --host="${params.host}" \
        --port="${params.port}" \
        --user="${params.user}" \
        "${params.database}" | gzip > "$file"
      printf 'file=%s\nkey=%s\n' "$file" "$key" >> "$DAGU_OUTPUT_FILE"
    outputs:
      - name: file
      - name: key

  - id: upload
    depends: [create_dump]
    action: s3.upload
    with:
      bucket: ${params.bucket}
      key: ${steps.create_dump.outputs.key}
      source: ${steps.create_dump.outputs.file}
      content_type: application/gzip

Review before running

Replace example values, configure the listed secrets, and review every command for your environment. Syntax validation does not make a community workflow trusted.

Running this workflow with a team?

The Team plan adds SSO, role-based access, audit logs, incident routing, and priority support for three self-hosted Dagu servers. Start a 14-day trial without a credit card.

Start Team trial
View sourceInstall DaguContributed by @dagucloud · validated with Dagu 2.13.0

Related Recipes