Files
dpl/scripts/fuc-xb-launch/export-final-snapshot.php
T

48 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
// Exports the verified restore of the frozen legacy backup.
ini_set('display_errors', '0');
try {
$database = $argv[1] ?? '';
if (PHP_SAPI !== 'cli' || !preg_match('/\Afuc_legacy_restore_[a-f0-9]{12}\z/D', $database)) {
throw new RuntimeException('Expected an isolated legacy restore');
}
$sourceRoot = '/www/wwwroot/fuuuu.cloud';
chdir($sourceRoot);
require $sourceRoot . '/config/.config.php';
$connection = new PDO(
'mysql:unix_socket=/run/mysqld/mysqld.sock;dbname=' . $database . ';charset=utf8mb4',
'root', '',
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]
);
$tables = ['user', 'order', 'plan_queue', 'product', 'invoice', 'paylist', 'user_money_log',
'payback', 'invite_up', 'link', 'user_coupon', 'vip_map', 'renewal_quote', 'announcement'];
$omitUser = ['ga_token', 'api_token', 'usdt_addr', 'reg_ip', 'remark', 'banned_reason'];
$write = static function (array $record): void {
echo json_encode($record, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR), "\n";
};
$counts = $columns = [];
foreach ($tables as $table) {
$columns[$table] = $connection->query('SHOW COLUMNS FROM `' . $table . '`')->fetchAll(PDO::FETCH_COLUMN);
if ($table === 'user') $columns[$table] = array_values(array_diff($columns[$table], $omitUser));
$counts[$table] = (int) $connection->query('SELECT COUNT(*) FROM `' . $table . '`')->fetchColumn();
}
$write([
'kind' => 'header', 'format' => 'fuc-research-snapshot-v1', 'snapshot_utc' => gmdate('c'),
'timezone' => $_ENV['timeZone'], 'database_identity_sha256' => hash('sha256', $_ENV['db_database']),
'counts' => $counts,
]);
foreach ($tables as $table) {
$fields = implode(',', array_map(static fn ($field) => '`' . $field . '`', $columns[$table]));
$order = $table === 'announcement' ? ' ORDER BY sort DESC,date DESC,id DESC'
: (in_array('id', $columns[$table], true) ? ' ORDER BY id' : '');
$rows = $connection->query('SELECT ' . $fields . ' FROM `' . $table . '`' . $order);
foreach ($rows as $row) $write(['kind' => 'row', 'table' => $table, 'row' => $row]);
}
$write(['kind' => 'footer', 'counts' => $counts, 'source_business_writes' => 0]);
} catch (Throwable $failure) {
fwrite(STDERR, get_class($failure) . ' on line ' . $failure->getLine() . "\n");
exit(1);
}