Skip to content
← Back to Functions
Code

Database Backup Manifest

Creates a manifest of tables, row counts, checksums, and dump metadata.

Function signature

ogBuildBackupManifest(tables = array(), options = array())

Categories

  • Database Integrity

Parameters

tablesTable metadata rows.optionsOptional database_name, generated_at, and include_checksum flags. Recognized keys: `database_name`, `generated_at`.

Return value

Public-safe status string returned by the function.

  • success
  • message
  • data

Compatibility

Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.

Minimum PHP version: 7.4

Security notes

Use caller-owned allowlists and procedural mysqli prepared execution where SQL plans are returned; validate file paths, MIME policies, and permissions before file or download workflows.

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Creates a database backup manifest from table metadata.
 *
 * This does not create a dump. It records the planned backup scope so the
 * backup process can be audited before and after execution.
 *
 * @param array $tables Table metadata rows.
 * @param array $options Optional database_name, generated_at, and include_checksum flags.
 * @return array Backup manifest result.
 */
function ogBuildBackupManifest($tables = array(), $options = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	if (!is_array($tables)) {
		$result['message'] = 'Tables must be supplied as an array.';
		return $result;
	}

	if (!is_array($options)) {
		$options = array();
	}

	$database_name = '';
	if (!empty($options['database_name'])) {
		$database_name = trim((string)$options['database_name']);
	}

	$generated_at = date('c');
	if (!empty($options['generated_at'])) {
		$generated_at = trim((string)$options['generated_at']);
	}

	$manifest_tables = array();
	$total_rows = 0;
	foreach ($tables as $table) {
		if (!is_array($table)) {
			continue;
		}

		$name = '';
		if (!empty($table['name'])) {
			$name = trim((string)$table['name']);
		}
		if (empty($name) || !preg_match('/^[a-zA-Z0-9_]+$/', $name)) {
			continue;
		}

		$row_count = 0;
		if (isset($table['row_count'])) {
			$row_count = (int)$table['row_count'];
		}
		if ($row_count < 0) {
			$row_count = 0;
		}

		$engine = '';
		if (!empty($table['engine'])) {
			$engine = trim((string)$table['engine']);
		}

		$checksum_source = $name . ':' . $row_count . ':' . $engine;
		$checksum = hash('sha256', $checksum_source);

		$manifest_tables[] = array(
			'name' => $name,
			'row_count' => $row_count,
			'engine' => $engine,
			'checksum' => $checksum
		);
		$total_rows += $row_count;
	}

	$result['success'] = true;
	$result['message'] = 'Backup manifest built.';
	$result['data'] = array(
		'database_name' => $database_name,
		'generated_at' => $generated_at,
		'table_count' => count($manifest_tables),
		'total_rows' => $total_rows,
		'tables' => $manifest_tables
	);

	return $result;
}