-- ZFS Replication Tasks Table -- Supports both outbound (sender) and inbound (receiver) replication CREATE TABLE IF NOT EXISTS replication_tasks ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name VARCHAR(255) NOT NULL, direction VARCHAR(20) NOT NULL, -- 'outbound' (sender) or 'inbound' (receiver) -- For outbound replication (sender) source_dataset VARCHAR(512), -- Source dataset on this system (outbound) or remote system (inbound) target_host VARCHAR(255), -- Target host IP or hostname (for outbound) target_port INTEGER DEFAULT 22, -- SSH port (default 22, for outbound) target_user VARCHAR(255) DEFAULT 'root', -- SSH user (for outbound) target_dataset VARCHAR(512), -- Target dataset on remote system (for outbound) target_ssh_key_path TEXT, -- Path to SSH private key (for outbound) -- For inbound replication (receiver) source_host VARCHAR(255), -- Source host IP or hostname (for inbound) source_port INTEGER DEFAULT 22, -- SSH port (for inbound) source_user VARCHAR(255) DEFAULT 'root', -- SSH user (for inbound) local_dataset VARCHAR(512), -- Local dataset to receive snapshots (for inbound) -- Common settings schedule_type VARCHAR(50), -- 'manual', 'hourly', 'daily', 'weekly', 'monthly', 'cron' schedule_config JSONB, -- Schedule configuration (similar to snapshot schedules) compression VARCHAR(50) DEFAULT 'lz4', -- 'off', 'lz4', 'gzip', 'zstd' encryption BOOLEAN DEFAULT false, -- Enable encryption during transfer recursive BOOLEAN DEFAULT false, -- Replicate recursively incremental BOOLEAN DEFAULT true, -- Use incremental replication auto_snapshot BOOLEAN DEFAULT true, -- Auto-create snapshot before replication -- Status and tracking enabled BOOLEAN NOT NULL DEFAULT true, status VARCHAR(50) DEFAULT 'idle', -- 'idle', 'running', 'failed', 'paused' last_run_at TIMESTAMP, last_run_status VARCHAR(50), -- 'success', 'failed', 'partial' last_run_error TEXT, next_run_at TIMESTAMP, last_snapshot_sent VARCHAR(512), -- Last snapshot successfully sent (for outbound) last_snapshot_received VARCHAR(512), -- Last snapshot successfully received (for inbound) -- Statistics total_runs INTEGER DEFAULT 0, successful_runs INTEGER DEFAULT 0, failed_runs INTEGER DEFAULT 0, bytes_sent BIGINT DEFAULT 0, -- Total bytes sent (for outbound) bytes_received BIGINT DEFAULT 0, -- Total bytes received (for inbound) created_by UUID REFERENCES users(id), created_at TIMESTAMP NOT NULL DEFAULT NOW(), updated_at TIMESTAMP NOT NULL DEFAULT NOW(), -- Validation: ensure required fields based on direction CONSTRAINT chk_direction CHECK (direction IN ('outbound', 'inbound')), CONSTRAINT chk_outbound_fields CHECK ( direction != 'outbound' OR ( source_dataset IS NOT NULL AND target_host IS NOT NULL AND target_dataset IS NOT NULL ) ), CONSTRAINT chk_inbound_fields CHECK ( direction != 'inbound' OR ( source_host IS NOT NULL AND source_dataset IS NOT NULL AND local_dataset IS NOT NULL ) ) ); -- Create indexes CREATE INDEX IF NOT EXISTS idx_replication_tasks_direction ON replication_tasks(direction); CREATE INDEX IF NOT EXISTS idx_replication_tasks_enabled ON replication_tasks(enabled); CREATE INDEX IF NOT EXISTS idx_replication_tasks_status ON replication_tasks(status); CREATE INDEX IF NOT EXISTS idx_replication_tasks_next_run ON replication_tasks(next_run_at); CREATE INDEX IF NOT EXISTS idx_replication_tasks_source_dataset ON replication_tasks(source_dataset); CREATE INDEX IF NOT EXISTS idx_replication_tasks_target_host ON replication_tasks(target_host);