forked from laysense/laysense.cn
Migrate Laysense to upstream LaySite 1.4.3 with external site data
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
# laysense.cn 部署与升级
|
||||
|
||||
网站使用 LaySite 官方 1.4.3。站点扩展在 `plugin/laysense/`,通过 Webman 保留品牌外观、版权服务页样式、首页动效、原静态文件地址和旧文章跳转。核心代码与官方发布包一致。
|
||||
|
||||
## 从当前站点迁移一次
|
||||
|
||||
将新版本部署到新的代码目录。旧站的运行目录是首选数据源,`backup` 分支的单独检出也可作为快照来源。使用运行 Webman 的系统账户执行迁移。先暂停旧站的写入,再运行下列命令。
|
||||
|
||||
```sh
|
||||
cd /srv/laysense-code
|
||||
composer install --no-dev --prefer-dist
|
||||
php site/migrate.php /srv/laysense-old /srv/laysense-data
|
||||
ln -s /srv/laysense-data/.env .env
|
||||
php bin/install.php
|
||||
php start.php start
|
||||
```
|
||||
|
||||
迁移会读取旧库和 WAL 中的已提交数据,生成完整的独立 SQLite 文件。它同时复制 SQLite 密钥(如有)、上传文件、旧 URL 对应的静态素材及原站点环境配置。脚本会拒绝覆盖已有目标数据目录。管理员账号也在迁移后的数据库里,可继续使用原账号登录。
|
||||
|
||||
产品与服务及支持项目分类分别采用 LaySite 的标准标识 `catalog` 和 `projects`。分类名称、文章关系和旧 URL 都会保留。页脚地址与三个旧区块样式也会转换为官方备份工具可导入的格式。
|
||||
|
||||
反向代理将新代码目录的 `public/` 作为静态目录,把其余请求转给 Webman。旧站素材路径需要保留这一回退规则,才能由站点扩展从独立数据目录提供文件。沿用 CDN 时,同步或刷新对应素材的缓存即可。
|
||||
|
||||
## 后续更新
|
||||
|
||||
进入新站代码目录,先检查可用版本。带目标标签执行更新前暂停 Webman。
|
||||
|
||||
```sh
|
||||
php bin/update.php check
|
||||
php bin/update.php
|
||||
```
|
||||
|
||||
可直接指定版本和备份路径,例如 `php bin/update.php vX.Y.Z /srv/laysense-data/backups/pre-update.tar`。更新器会替换官方发布包中的系统文件及运行数据库迁移。站点扩展和独立数据目录会保留。
|
||||
|
||||
日常备份应包含整个 `/srv/laysense-data` 目录。LaySite 的站点备份另可用于移植数据库内容和媒体库上传文件。
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
function sourcePath(string $root, array $env, string $key, string $default): string
|
||||
{
|
||||
$path = $env[$key] ?? $default;
|
||||
|
||||
return str_starts_with($path, '/') ? $path : $root . '/' . $path;
|
||||
}
|
||||
|
||||
function copyFile(string $source, string $target): void
|
||||
{
|
||||
$directory = dirname($target);
|
||||
if (!is_file($source) || is_link($source)
|
||||
|| (!is_dir($directory) && !mkdir($directory, 0700, true))
|
||||
|| !copy($source, $target)) {
|
||||
throw new RuntimeException('无法迁移文件 ' . $source);
|
||||
}
|
||||
}
|
||||
|
||||
function openSqlite(string $path, bool $readOnly = false): PDO
|
||||
{
|
||||
$options = $readOnly ? (class_exists(Pdo\Sqlite::class)
|
||||
? [Pdo\Sqlite::ATTR_OPEN_FLAGS => Pdo\Sqlite::OPEN_READONLY]
|
||||
: [PDO::SQLITE_ATTR_OPEN_FLAGS => PDO::SQLITE_OPEN_READONLY]) : [];
|
||||
$pdo = new PDO('sqlite:' . $path, options: $options + [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);
|
||||
$pdo->exec('PRAGMA busy_timeout = 5000');
|
||||
|
||||
return $pdo;
|
||||
}
|
||||
|
||||
function alignSiteData(PDO $db): void
|
||||
{
|
||||
$categories = ['services' => 'catalog', 'supported-projects' => 'projects'];
|
||||
$db->exec('PRAGMA foreign_keys = ON');
|
||||
$db->beginTransaction();
|
||||
foreach ($categories as $legacy => $standard) {
|
||||
$db->prepare('INSERT INTO article_categories (slug, name) SELECT ?, name FROM article_categories WHERE slug = ?
|
||||
ON CONFLICT(slug) DO UPDATE SET name = excluded.name')->execute([$standard, $legacy]);
|
||||
$db->prepare('INSERT OR IGNORE INTO article_category_links (article_id, category_slug)
|
||||
SELECT article_id, ? FROM article_category_links WHERE category_slug = ?')->execute([$standard, $legacy]);
|
||||
$db->prepare('DELETE FROM article_category_links WHERE category_slug = ?')->execute([$legacy]);
|
||||
$db->prepare('DELETE FROM article_categories WHERE slug = ?')->execute([$legacy]);
|
||||
}
|
||||
$replace = $db->prepare('UPDATE sections SET feed = ? WHERE page_slug = ? AND key = ?');
|
||||
foreach ($db->query("SELECT page_slug, key, feed FROM sections WHERE feed IS NOT NULL AND feed <> ''")->fetchAll() as $row) {
|
||||
$feed = json_decode($row['feed'], true, flags: JSON_THROW_ON_ERROR);
|
||||
$mapped = array_values(array_unique(array_map(static fn (string $slug): string => $categories[$slug] ?? $slug, $feed['categories'])));
|
||||
if ($mapped !== $feed['categories']) {
|
||||
$feed['categories'] = $mapped;
|
||||
$replace->execute([json_encode($feed, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE), $row['page_slug'], $row['key']]);
|
||||
}
|
||||
}
|
||||
$db->exec("UPDATE sections SET style = 'plain' WHERE style IN ('about', 'projects', 'demos')");
|
||||
$db->exec("UPDATE sections SET body = replace(replace(body, '<address>', '<div class=\"site-address\">'), '</address>', '</div>')
|
||||
WHERE body LIKE '%<address>%'");
|
||||
if ($db->query('PRAGMA foreign_key_check')->fetch() !== false) {
|
||||
throw new RuntimeException('迁移数据包含无效的关联记录。');
|
||||
}
|
||||
$db->commit();
|
||||
}
|
||||
|
||||
function removeStage(string $directory): void
|
||||
{
|
||||
$entries = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
|
||||
RecursiveIteratorIterator::CHILD_FIRST);
|
||||
foreach ($entries as $entry) {
|
||||
if ($entry->isDir() && !$entry->isLink()) {
|
||||
rmdir($entry->getPathname());
|
||||
} else {
|
||||
unlink($entry->getPathname());
|
||||
}
|
||||
}
|
||||
rmdir($directory);
|
||||
}
|
||||
|
||||
umask(0077);
|
||||
if ($argc !== 3) {
|
||||
fwrite(STDERR, "用法:php site/migrate.php 旧站根目录 新站数据目录\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
$stage = null;
|
||||
$exitCode = 0;
|
||||
try {
|
||||
$source = realpath($argv[1]);
|
||||
$parent = realpath(dirname($argv[2]));
|
||||
$target = $parent . '/' . basename($argv[2]);
|
||||
if ($source === false || $parent === false || !is_dir($parent) || file_exists($target) || is_link($target)) {
|
||||
throw new RuntimeException('请选择现有的旧站根目录和一个尚未使用的新数据目录。');
|
||||
}
|
||||
|
||||
$env = Dotenv::createArrayBacked($source)->safeLoad();
|
||||
$original = sourcePath($source, $env, 'LAYSITE_DB', 'storage/laysite.sqlite');
|
||||
$uploads = sourcePath($source, $env, 'LAYSITE_MEDIA_DIR', 'storage/media');
|
||||
if (!is_file($original)) {
|
||||
throw new RuntimeException('旧站目录缺少 SQLite 数据库。');
|
||||
}
|
||||
$stage = $parent . '/.laysense-migration-' . bin2hex(random_bytes(8));
|
||||
if (!mkdir($stage, 0700)) {
|
||||
throw new RuntimeException('无法创建迁移目录。');
|
||||
}
|
||||
$oldDb = openSqlite($original, true);
|
||||
if ($oldDb->query('PRAGMA quick_check')->fetchColumn() !== 'ok') {
|
||||
throw new RuntimeException('旧站 SQLite 未通过完整性检查。');
|
||||
}
|
||||
// SQLite reads the live WAL when taking the consistent snapshot.
|
||||
$oldDb->exec('VACUUM INTO ' . $oldDb->quote($stage . '/laysite.sqlite'));
|
||||
$oldDb = null;
|
||||
if (is_file($original . '.key')) {
|
||||
copyFile($original . '.key', $stage . '/laysite.sqlite.key');
|
||||
}
|
||||
|
||||
$db = openSqlite($stage . '/laysite.sqlite');
|
||||
mkdir($stage . '/media', 0700);
|
||||
if (is_dir($uploads)) {
|
||||
foreach (new FilesystemIterator($uploads, FilesystemIterator::SKIP_DOTS) as $entry) {
|
||||
if ($entry->isFile()) {
|
||||
copyFile($entry->getPathname(), $stage . '/media/' . $entry->getFilename());
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($db->query('SELECT name, size FROM media') as $media) {
|
||||
if (!preg_match('/^[a-f0-9]{32}\.[a-z0-9]+$/D', $media['name'])
|
||||
|| !is_file($stage . '/media/' . $media['name']) || filesize($stage . '/media/' . $media['name']) !== (int) $media['size']) {
|
||||
throw new RuntimeException('旧站媒体清单中的文件缺失或大小不一致。');
|
||||
}
|
||||
}
|
||||
alignSiteData($db);
|
||||
$db = null;
|
||||
|
||||
$assets = json_decode(file_get_contents(dirname(__DIR__) . '/plugin/laysense/assets.json'), true, flags: JSON_THROW_ON_ERROR);
|
||||
foreach ($assets as $asset) {
|
||||
copyFile($source . '/public/' . $asset, $stage . '/' . $asset);
|
||||
}
|
||||
if (is_file($source . '/backup.tar')) {
|
||||
copyFile($source . '/backup.tar', $stage . '/backups/legacy-backup.tar');
|
||||
}
|
||||
$dotenv = is_file($source . '/.env') ? file_get_contents($source . '/.env') : "LAYSITE_SITE_URL=https://laysense.cn\n";
|
||||
$dotenv = preg_replace('/^\h*(?:export\h+)?(?:LAYSITE_DB|LAYSITE_MEDIA_DIR|LAYSENSE_SITE_DIR)\h*=.*(?:\R|$)/m', '', $dotenv);
|
||||
foreach (['LAYSITE_DB' => $target . '/laysite.sqlite', 'LAYSITE_MEDIA_DIR' => $target . '/media', 'LAYSENSE_SITE_DIR' => $target] as $key => $value) {
|
||||
if (preg_match('/[\r\n\x00]/', $value)) {
|
||||
throw new RuntimeException('数据目录包含无效字符。');
|
||||
}
|
||||
$dotenv = rtrim($dotenv) . "\n" . $key . '="' . addcslashes($value, "\\\"$") . "\"\n";
|
||||
}
|
||||
if (file_put_contents($stage . '/.env', $dotenv) === false || file_exists($target) || !rename($stage, $target)) {
|
||||
throw new RuntimeException('无法写入站点数据目录。');
|
||||
}
|
||||
$stage = null;
|
||||
printf("站点数据已迁至 %s\n", $target);
|
||||
} catch (Throwable $error) {
|
||||
fwrite(STDERR, $error->getMessage() . PHP_EOL);
|
||||
$exitCode = 1;
|
||||
} finally {
|
||||
if ($stage !== null && is_dir($stage)) {
|
||||
removeStage($stage);
|
||||
}
|
||||
}
|
||||
|
||||
exit($exitCode);
|
||||
Reference in New Issue
Block a user