Backup & Restore

Comprehensive guide to backing up and restoring your Query-2jz data using the CLI.

Backup Overview

Learn about Query-2jz's backup system and how to create, manage, and restore backups.

Backup Types

  • Full Backup: Complete database backup including all data and schema
  • Incremental Backup: Only changes since the last backup
  • Schema Backup: Database structure only (no data)
  • Data Backup: Data only (no schema)
  • Model-specific Backup: Backup specific models only

Backup Features

  • • Automatic compression and encryption
  • • Schema-aware backups with relationships
  • • Multiple export formats (JSON, SQL, CSV)
  • • Incremental backup support
  • • Backup validation and integrity checks
  • • Automated retention policies
  • • Cloud storage integration

Creating Backups

Learn how to create different types of backups with various options.

Backup Creation

Commands for creating backups

Basic Backup Commands

# Create full backup
query-2jz backup create

# Create backup with custom name
query-2jz backup create --name "backup-$(date +%Y-%m-%d)"

# Create backup to specific directory
query-2jz backup create --output ./backups/

# Create backup with timestamp
query-2jz backup create --timestamp

# Create backup with description
query-2jz backup create --description "Pre-deployment backup"

Incremental Backups

# Create incremental backup
query-2jz backup create --incremental

# Create incremental backup since specific date
query-2jz backup create --incremental --since 2024-01-01

# Create incremental backup since last backup
query-2jz backup create --incremental --since-last

# Create incremental backup for specific models
query-2jz backup create --incremental --models User,Post

Model-Specific Backups

# Backup specific models
query-2jz backup create --models User,Post,Comment

# Backup models with filters
query-2jz backup create --models User --where '{"status":"active"}'

# Backup models with relationships
query-2jz backup create --models User --include-relations

# Backup models with specific fields
query-2jz backup create --models User --fields id,name,email

Advanced Backup Options

# Compressed backup
query-2jz backup create --compress

# Encrypted backup
query-2jz backup create --encrypt --key-file ./backup.key

# Backup with compression and encryption
query-2jz backup create --compress --encrypt --key-file ./backup.key

# Backup with custom format
query-2jz backup create --format json
query-2jz backup create --format sql
query-2jz backup create --format csv

# Backup with metadata
query-2jz backup create --include-metadata

# Backup with validation
query-2jz backup create --validate

Backup Management

Manage your backups with listing, validation, and cleanup commands.

Backup Management

Commands for managing backups

Listing Backups

# List all backups
query-2jz backup list

# List backups with detailed information
query-2jz backup list --detailed

# List backups by date range
query-2jz backup list --since 2024-01-01 --until 2024-12-31

# List backups by type
query-2jz backup list --type full
query-2jz backup list --type incremental

# List backups by size
query-2jz backup list --sort-by size

# List backups with specific pattern
query-2jz backup list --pattern "backup-*"

Backup Validation

# Validate backup integrity
query-2jz backup validate --backup ./backup-2024-01-01

# Validate with checksum verification
query-2jz backup validate --checksum --backup ./backup-2024-01-01

# Validate specific models in backup
query-2jz backup validate --models User,Post --backup ./backup-2024-01-01

# Validate backup without restoring
query-2jz backup validate --dry-run --backup ./backup-2024-01-01

# Validate multiple backups
query-2jz backup validate --backup ./backups/ --all

Backup Cleanup

# Clean up old backups
query-2jz backup cleanup --retention-days 30

# Clean up with dry run
query-2jz backup cleanup --dry-run --retention-days 30

# Clean up by backup type
query-2jz backup cleanup --type daily --retention-days 7
query-2jz backup cleanup --type weekly --retention-days 4
query-2jz backup cleanup --type monthly --retention-days 12

# Clean up with size limit
query-2jz backup cleanup --max-size 1GB

# Clean up with confirmation
query-2jz backup cleanup --retention-days 30 --confirm

Restoring Backups

Learn how to restore your data from backups with various options.

Basic Restore Commands

# Restore from backup
query-2jz backup restore --backup ./backup-2024-01-01

# Restore with confirmation
query-2jz backup restore --backup ./backup-2024-01-01 --confirm

# Restore to specific database
query-2jz backup restore --backup ./backup-2024-01-01 --database my-db

# Restore with validation
query-2jz backup restore --validate --backup ./backup-2024-01-01

# Restore with rollback option
query-2jz backup restore --rollback --backup ./backup-2024-01-01

Selective Restore

# Restore specific models
query-2jz backup restore --models User,Post --backup ./backup-2024-01-01

# Restore models with filters
query-2jz backup restore --models User --where '{"status":"active"}' --backup ./backup-2024-01-01

# Restore with date range
query-2jz backup restore --since 2024-01-01 --until 2024-12-31 --backup ./backup-2024-01-01

# Restore without overwriting existing data
query-2jz backup restore --skip-existing --backup ./backup-2024-01-01

# Restore with data transformation
query-2jz backup restore --transform ./transform.js --backup ./backup-2024-01-01

Restore Options

# Restore with batch processing
query-2jz backup restore --batch-size 1000 --backup ./backup-2024-01-01

# Restore with progress tracking
query-2jz backup restore --progress --backup ./backup-2024-01-01

# Restore with error handling
query-2jz backup restore --continue-on-error --backup ./backup-2024-01-01

# Restore with logging
query-2jz backup restore --log-file ./restore.log --backup ./backup-2024-01-01

# Restore with dry run
query-2jz backup restore --dry-run --backup ./backup-2024-01-01

Automated Backups

Set up automated backup schedules and policies.

Backup Scheduling

# Schedule daily backup
query-2jz backup schedule --cron "0 2 * * *" --type full

# Schedule incremental backup every 6 hours
query-2jz backup schedule --cron "0 */6 * * *" --type incremental

# Schedule weekly backup
query-2jz backup schedule --cron "0 3 * * 0" --type full --name "weekly-backup"

# Schedule monthly backup
query-2jz backup schedule --cron "0 4 1 * *" --type full --name "monthly-backup"

# List scheduled backups
query-2jz backup schedule --list

# Remove scheduled backup
query-2jz backup schedule --remove --id "backup-schedule-1"

Backup Policies

# Set backup retention policy
query-2jz backup policy --retention-days 30

# Set backup retention by type
query-2jz backup policy --daily 7 --weekly 4 --monthly 12

# Set backup size limit
query-2jz backup policy --max-size 10GB

# Set backup compression policy
query-2jz backup policy --compress --compression-level 6

# Set backup encryption policy
query-2jz backup policy --encrypt --key-file ./backup.key

# Show current backup policy
query-2jz backup policy --show

Backup Notifications

# Enable backup notifications
query-2jz backup notify --enable

# Configure email notifications
query-2jz backup notify --email admin@example.com

# Configure Slack notifications
query-2jz backup notify --slack --webhook-url https://hooks.slack.com/...

# Configure Discord notifications
query-2jz backup notify --discord --webhook-url https://discord.com/api/webhooks/...

# Test notifications
query-2jz backup notify --test

# Disable notifications
query-2jz backup notify --disable

Cloud Storage Integration

Configure cloud storage for backup storage and retrieval.

AWS S3 Configuration

# Configure S3 storage
query-2jz backup storage --provider s3 --bucket my-backups --region us-east-1

# Configure S3 with credentials
query-2jz backup storage --provider s3 --bucket my-backups --access-key AKIA... --secret-key ...

# Configure S3 with IAM role
query-2jz backup storage --provider s3 --bucket my-backups --use-iam

# Test S3 connection
query-2jz backup storage --test

# List S3 backups
query-2jz backup list --storage s3

Google Cloud Storage

# Configure GCS storage
query-2jz backup storage --provider gcs --bucket my-backups --project my-project

# Configure GCS with service account
query-2jz backup storage --provider gcs --bucket my-backups --service-account ./service-account.json

# Configure GCS with application default credentials
query-2jz backup storage --provider gcs --bucket my-backups --use-adc

Azure Blob Storage

# Configure Azure storage
query-2jz backup storage --provider azure --container my-backups --account myaccount

# Configure Azure with connection string
query-2jz backup storage --provider azure --container my-backups --connection-string "DefaultEndpointsProtocol=https;..."

Backup Configuration

Configure backup settings in your Query-2jz configuration file.

// query-2jz.config.js
module.exports = {
  backup: {
    // Backup storage configuration
    storage: {
      local: {
        path: './backups',
        enabled: true
      },
      s3: {
        bucket: 'my-backups',
        region: 'us-east-1',
        enabled: false
      },
      gcs: {
        bucket: 'my-backups',
        project: 'my-project',
        enabled: false
      }
    },

    // Backup retention policy
    retention: {
      daily: 7,
      weekly: 4,
      monthly: 12,
      yearly: 5
    },

    // Backup compression
    compression: {
      enabled: true,
      algorithm: 'gzip',
      level: 6
    },

    // Backup encryption
    encryption: {
      enabled: false,
      algorithm: 'aes-256-gcm',
      keyFile: './backup.key'
    },

    // Backup scheduling
    schedule: {
      enabled: true,
      cron: '0 2 * * *', // Daily at 2 AM
      type: 'full'
    },

    // Backup notifications
    notifications: {
      enabled: true,
      email: 'admin@example.com',
      slack: {
        webhook: 'https://hooks.slack.com/...',
        channel: '#backups'
      }
    }
  }
};

Best Practices

Do

  • • Test backup restoration regularly
  • • Use automated backup schedules
  • • Store backups in multiple locations
  • • Encrypt sensitive backup data
  • • Monitor backup success and failures
  • • Implement backup retention policies
  • • Document backup and restore procedures

Don't

  • • Skip backup validation
  • • Store backups only locally
  • • Ignore backup failure notifications
  • • Use weak encryption for sensitive data
  • • Skip testing restore procedures
  • • Ignore backup retention policies
  • • Skip documenting backup procedures