{"slug":"umami-migration","title":"Umami Cloud迁移到本地踩坑记录","description":"早期的我很有先见之明，在使用Umami的时候是自建的，但是不久后因为想要全上云，又用回了Umami Cloud，但是现在访问记录激增，我又不得不迁移到本地...","coverImage":"https://b.acofork.com/img/umami-migration-umami-migration.webp","coverWidth":1315,"coverHeight":1023,"date":"2025-12-05","tags":["服务器"],"pinned":false,"wordCount":821,"html":"<h2 id=\"正式开始\">正式开始</h2><p>首先，我们想要迁移，首先肯定是要拿到原来的Umami Cloud存放的数据</p>\n<p>在 <a href=\"https://cloud.umami.is/settings/data\" target=\"_blank\" rel=\"noopener noreferrer\">Umami Cloud Data | Settings</a> 中，我们可以选择，导出数据（Export）\n<img src=\"https://b.acofork.com/img/umami-migration-umami-migration-1.webp\" alt=\"\" width=\"2072\" height=\"741\" loading=\"eager\" decoding=\"async\" /></p>\n<p>当数据准备好后（几分钟左右），你的邮箱会收到一封Umami Cloud发来的邮件\n<img src=\"https://b.acofork.com/img/umami-migration-umami-migration-2.webp\" alt=\"\" width=\"1533\" height=\"826\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>下载下来的文件是一个以UUID命名的ZIP压缩包，将其解压可以得到3个CSV文件\n<img src=\"https://b.acofork.com/img/umami-migration-umami-migration-3.webp\" alt=\"\" width=\"814\" height=\"297\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>其中，只有 <code>website_event.csv</code> 有用，其他两个文件仅有表头，无数据</p>\n<p>这样，我们就成功拿到了旧Umami数据</p>\n<p>接下来，我们需要在本地部署Umami PostgreSQL版本的 <strong>最新版</strong> （目前版本为 3.x）（部署教程略）</p>\n<p>然后我们还需要安装一个CSV编辑软件： <a href=\"https://smoothcsv.com/\" target=\"_blank\" rel=\"noopener noreferrer\">SmoothCSV - The ultimate CSV editor for macOS &amp; Windows</a></p>\n<p>在本地Umami中新建一个网站，Umami会随机为你生成一个UUID</p>\n<p>接下来我们需要在你的终端安装一个图形化管理PostgreSQL的软件： <a href=\"https://www.pgadmin.org/\" target=\"_blank\" rel=\"noopener noreferrer\">pgAdmin - PostgreSQL Tools</a></p>\n<p>然后连上数据库，你将可以看到这些表\n<img src=\"https://b.acofork.com/img/umami-migration-umami-migration-4.webp\" alt=\"\" width=\"619\" height=\"1096\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>接下来我们查看 <code>website</code> 这张表，将 <code>website_event.csv</code> 打开，查看旧的 <code>website_id</code> 将新的ID改为旧的，这样才能数据匹配\n<img src=\"https://b.acofork.com/img/umami-migration-umami-migration-5.webp\" alt=\"\" width=\"1280\" height=\"1392\" loading=\"lazy\" decoding=\"async\" /></p>\n<p><img src=\"https://b.acofork.com/img/umami-migration-umami-migration-6.webp\" alt=\"\" width=\"2482\" height=\"1096\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>接下来，我们正式开始数据导入，由于导入后的数据有两个 <code>session_id</code> 字段，而在Umami自我管理的过程中这个 <code>session_id</code> 字段是 <strong>唯一约束的</strong> ，我们需要先解除这个限制，在正式上线前还要再加回这个限制（后面会说）</p>\n<p>让我们删除主键约束和索引</p>\n<div class=\"markdown-code\" data-language=\"sql\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>sql</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER TABLE</span> session <span class=\"hljs-keyword\">DROP</span> <span class=\"hljs-keyword\">CONSTRAINT</span> session_pkey;\n<span class=\"hljs-keyword\">DROP</span> INDEX session_session_id_key;</code></pre></div><p>接下来，我们需要将数据配对，我们先查看 <code>website_event.csv</code> 这张表。这里应该只有表头，没有数据（我有是因为我已经导入完成了，教程是后写的）</p>\n<p><strong>注意顺序</strong> 如： <code>event_id</code> <code>website_id</code> <code>session_id</code> ...</p>\n<p><img src=\"https://b.acofork.com/img/umami-migration-umami-migration-7.webp\" alt=\"\" width=\"2482\" height=\"1096\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>编辑CSV文件，你需要将顺序配对，并且删除表中没有的列，如： <code>browser</code> <code>os</code> ...\n<img src=\"https://b.acofork.com/img/umami-migration-umami-migration-8.webp\" alt=\"\" width=\"1898\" height=\"1323\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>确保数据库中的表头顺序和CSV文件中的表头顺序一一对应，顺序一致，不多不少后，<strong>Ctrl + S 保存</strong> CSV文件，我们开始导入并在选项中勾选 <strong>标题</strong></p>\n<p><img src=\"https://b.acofork.com/img/umami-migration-umami-migration-9.webp\" alt=\"\" width=\"481\" height=\"507\" loading=\"lazy\" decoding=\"async\" /></p>\n<p><img src=\"https://b.acofork.com/img/umami-migration-umami-migration-10.webp\" alt=\"\" width=\"704\" height=\"548\" loading=\"lazy\" decoding=\"async\" /></p>\n<p><img src=\"https://b.acofork.com/img/umami-migration-umami-migration-11.webp\" alt=\"\" width=\"702\" height=\"552\" loading=\"lazy\" decoding=\"async\" />\n导入成功后如图\n<img src=\"https://b.acofork.com/img/umami-migration-umami-migration-12.webp\" alt=\"\" width=\"1855\" height=\"783\" loading=\"lazy\" decoding=\"async\" /></p>\n<p><img src=\"https://b.acofork.com/img/umami-migration-umami-migration-13.webp\" alt=\"\" width=\"2482\" height=\"1096\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>再编辑 <code>session</code> 这张表，手法同上，导入成功后如图\n<img src=\"https://b.acofork.com/img/umami-migration-umami-migration-14.webp\" alt=\"\" width=\"2482\" height=\"1096\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>接下来打开本地Umami的WebUI，查看数据是否有异常\n<img src=\"https://b.acofork.com/img/umami-migration-umami-migration-15.webp\" alt=\"\" width=\"2559\" height=\"1356\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>确保导入后数据无异常后，我们开始删除另一条 <code>session_id</code> 记录并回锁主键。否则上线后Umami将无法再向表中插入数据</p>\n<div class=\"markdown-code\" data-language=\"sql\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>sql</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT</span> session_id, <span class=\"hljs-built_in\">COUNT</span>(<span class=\"hljs-operator\">*</span>) \n<span class=\"hljs-keyword\">FROM</span> public.session\n<span class=\"hljs-keyword\">GROUP</span> <span class=\"hljs-keyword\">BY</span> session_id\n<span class=\"hljs-keyword\">HAVING</span> <span class=\"hljs-built_in\">COUNT</span>(<span class=\"hljs-operator\">*</span>) <span class=\"hljs-operator\">&gt;</span> <span class=\"hljs-number\">1</span>;\n\n<span class=\"hljs-comment\">-- 保留最早的 created_at</span>\n<span class=\"hljs-keyword\">DELETE</span> <span class=\"hljs-keyword\">FROM</span> public.session\n<span class=\"hljs-keyword\">WHERE</span> ctid <span class=\"hljs-keyword\">NOT</span> <span class=\"hljs-keyword\">IN</span> (\n    <span class=\"hljs-keyword\">SELECT</span> <span class=\"hljs-built_in\">MIN</span>(ctid)\n    <span class=\"hljs-keyword\">FROM</span> public.session\n    <span class=\"hljs-keyword\">GROUP</span> <span class=\"hljs-keyword\">BY</span> session_id\n);\n\n<span class=\"hljs-keyword\">ALTER TABLE</span> public.session\n<span class=\"hljs-keyword\">ADD CONSTRAINT</span> session_id_unique <span class=\"hljs-keyword\">UNIQUE</span> (session_id);</code></pre></div><p>至此，迁移工作结束</p>\n","toc":[{"id":"正式开始","text":"正式开始","depth":2}]}