chore: 建立仓库并纳入修改 Cursor 用过的脚本

This commit is contained in:
Nixevol
2026-12-07 02:46:31 +08:00
commit 0d19ca1357
18 changed files with 1529 additions and 0 deletions
+401
View File
@@ -0,0 +1,401 @@
# Restore Cursor chats, MCP, skills, and related settings.
# Default backup is the new machine's user profile copy: G:\Users\Administrator
# Also accepts an older full-disk layout such as F:\Cursor\Users\Administrator.
# Close Cursor completely before running. Login on this machine will be replaced.
# Usage:
# Restore-CursorFromUsb.bat
# powershell -ExecutionPolicy Bypass -File .\Restore-CursorFromUsb.ps1
# powershell -ExecutionPolicy Bypass -File .\Restore-CursorFromUsb.ps1 -Rollback
param(
[string]$BackupRoot = "G:\Users\Administrator",
[switch]$Rollback
)
$ErrorActionPreference = "Stop"
$SnapshotRoot = "C:\CursorMigrationBackup"
$LatestFile = Join-Path $SnapshotRoot "latest.txt"
function Write-Step([string]$Message) {
Write-Host ""
Write-Host "== $Message" -ForegroundColor Cyan
}
function Test-CursorRunning {
$procs = @(Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {
$_.Name -eq "Cursor.exe" -or
($_.ExecutablePath -and $_.ExecutablePath -like "*\Cursor\Cursor.exe")
})
return $procs
}
function Assert-CursorClosed {
$procs = @(Test-CursorRunning)
if ($procs.Count -gt 0) {
Write-Host "Cursor 仍在运行。请先完全退出(含托盘图标),任务管理器里不要留下 Cursor.exe。" -ForegroundColor Yellow
$procs | Select-Object ProcessId, Name, ExecutablePath | Format-Table -AutoSize | Out-Host
exit 1
}
$db = Join-Path $env:APPDATA "Cursor\User\globalStorage\state.vscdb"
if (Test-Path -LiteralPath $db) {
try {
$stream = [System.IO.File]::Open($db, "Open", "ReadWrite", "None")
$stream.Close()
}
catch {
Write-Host "聊天数据库仍被占用。请确认 Cursor 已退出后重新运行。" -ForegroundColor Yellow
Write-Host $_.Exception.Message
exit 1
}
}
}
function Invoke-RoboCopy {
param([Parameter(Mandatory = $true)][string[]]$RoboArgs)
& robocopy.exe @RoboArgs | Out-Host
$code = $LASTEXITCODE
if ($code -ge 8) {
throw "robocopy 失败,退出码 $code : robocopy $($RoboArgs -join ' ')"
}
}
function Copy-Tree {
param(
[Parameter(Mandatory = $true)][string]$Source,
[Parameter(Mandatory = $true)][string]$Destination,
[switch]$Quiet
)
if (-not (Test-Path -LiteralPath $Source)) {
Write-Host "跳过,源不存在: $Source" -ForegroundColor DarkYellow
return
}
New-Item -ItemType Directory -Force -Path $Destination | Out-Null
Write-Host "目录: $Source"
$args = @($Source, $Destination, "/E", "/COPY:DAT", "/DCOPY:DAT", "/R:2", "/W:2", "/XJ", "/FFT")
if ($Quiet) {
$args += @("/NFL", "/NDL", "/NP")
}
Invoke-RoboCopy $args
}
function Copy-FileForce {
param(
[Parameter(Mandatory = $true)][string]$Source,
[Parameter(Mandatory = $true)][string]$Destination
)
if (-not (Test-Path -LiteralPath $Source)) {
Write-Host "跳过,源不存在: $Source" -ForegroundColor DarkYellow
return
}
$parent = Split-Path -Parent $Destination
if ($parent) {
New-Item -ItemType Directory -Force -Path $parent | Out-Null
}
Copy-Item -LiteralPath $Source -Destination $Destination -Force
Write-Host "文件: $Destination"
}
function Get-SqliteSet {
param([Parameter(Mandatory = $true)][string]$Directory, [Parameter(Mandatory = $true)][string]$BaseName)
$files = @()
foreach ($suffix in @("", "-wal", "-shm")) {
$path = Join-Path $Directory ($BaseName + $suffix)
if (Test-Path -LiteralPath $path) {
$files += $path
}
}
return $files
}
function Remove-SqliteSet {
param([Parameter(Mandatory = $true)][string]$Directory, [Parameter(Mandatory = $true)][string]$BaseName)
foreach ($suffix in @("-wal", "-shm", "")) {
$path = Join-Path $Directory ($BaseName + $suffix)
if (Test-Path -LiteralPath $path) {
Remove-Item -LiteralPath $path -Force
}
}
}
function Restore-SqliteDatabase {
param(
[Parameter(Mandatory = $true)][string]$SourceDir,
[Parameter(Mandatory = $true)][string]$DestDir,
[Parameter(Mandatory = $true)][string]$BaseName,
[Parameter(Mandatory = $true)][string]$IncomingRoot
)
$sourceFile = Join-Path $SourceDir $BaseName
if (-not (Test-Path -LiteralPath $sourceFile)) {
throw "找不到数据库: $sourceFile"
}
$stage = Join-Path $IncomingRoot $BaseName
if (Test-Path -LiteralPath $stage) {
Remove-Item -LiteralPath $stage -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $stage | Out-Null
foreach ($suffix in @("", "-wal", "-shm")) {
$name = $BaseName + $suffix
$from = Join-Path $SourceDir $name
if (-not (Test-Path -LiteralPath $from)) { continue }
$sizeMb = "{0:N1}" -f ((Get-Item -LiteralPath $from).Length / 1MB)
Write-Host "复制 $name ($sizeMb MB)"
Invoke-RoboCopy @($SourceDir, $stage, $name, "/J", "/R:2", "/W:3", "/NFL", "/NDL")
$copied = Join-Path $stage $name
$srcLen = (Get-Item -LiteralPath $from).Length
$dstLen = (Get-Item -LiteralPath $copied).Length
if ($srcLen -ne $dstLen) {
throw "$name 大小不一致。源 $srcLen 字节,副本 $dstLen 字节。"
}
}
New-Item -ItemType Directory -Force -Path $DestDir | Out-Null
Remove-SqliteSet -Directory $DestDir -BaseName $BaseName
foreach ($suffix in @("", "-wal", "-shm")) {
$name = $BaseName + $suffix
$from = Join-Path $stage $name
if (Test-Path -LiteralPath $from) {
Move-Item -LiteralPath $from -Destination (Join-Path $DestDir $name) -Force
}
}
Write-Host "已替换 $DestDir\$BaseName"
}
function Get-SourceUser {
param([Parameter(Mandatory = $true)][string]$Root)
$selfMarker = Join-Path $Root "AppData\Roaming\Cursor\User\globalStorage\state.vscdb"
if (Test-Path -LiteralPath $selfMarker) {
return $Root
}
$direct = Join-Path $Root "Users\Administrator"
$marker = Join-Path $direct "AppData\Roaming\Cursor\User\globalStorage\state.vscdb"
if (Test-Path -LiteralPath $marker) {
return $direct
}
$users = Join-Path $Root "Users"
if (Test-Path -LiteralPath $users) {
foreach ($dir in Get-ChildItem -LiteralPath $users -Directory) {
$candidate = Join-Path $dir.FullName "AppData\Roaming\Cursor\User\globalStorage\state.vscdb"
if (Test-Path -LiteralPath $candidate) {
return $dir.FullName
}
}
}
throw "在 $Root 下没有找到 Cursor 的 state.vscdb。请确认 U 盘盘符和备份目录。"
}
function Save-Snapshot {
param(
[Parameter(Mandatory = $true)][string]$Destination,
[Parameter(Mandatory = $true)][string[]]$WorkspaceNames
)
New-Item -ItemType Directory -Force -Path $Destination | Out-Null
$globalStorage = Join-Path $env:APPDATA "Cursor\User\globalStorage"
$snapGlobal = Join-Path $Destination "AppData\Roaming\Cursor\User\globalStorage"
New-Item -ItemType Directory -Force -Path $snapGlobal | Out-Null
foreach ($base in @("state.vscdb", "conversation-search.db")) {
foreach ($path in (Get-SqliteSet -Directory $globalStorage -BaseName $base)) {
Copy-Item -LiteralPath $path -Destination (Join-Path $snapGlobal (Split-Path -Leaf $path)) -Force
}
}
$storage = Join-Path $globalStorage "storage.json"
if (Test-Path -LiteralPath $storage) {
Copy-Item -LiteralPath $storage -Destination (Join-Path $snapGlobal "storage.json") -Force
}
$settings = Join-Path $env:APPDATA "Cursor\User\settings.json"
if (Test-Path -LiteralPath $settings) {
$snapUser = Join-Path $Destination "AppData\Roaming\Cursor\User"
New-Item -ItemType Directory -Force -Path $snapUser | Out-Null
Copy-Item -LiteralPath $settings -Destination (Join-Path $snapUser "settings.json") -Force
}
$ws = Join-Path $env:APPDATA "Cursor\User\workspaceStorage"
if (Test-Path -LiteralPath $ws) {
Copy-Tree -Source $ws -Destination (Join-Path $Destination "AppData\Roaming\Cursor\User\workspaceStorage") -Quiet
}
$manifest = [ordered]@{
created = (Get-Date).ToString("s")
computer = $env:COMPUTERNAME
userProfile = $env:USERPROFILE
appData = $env:APPDATA
localAppData = $env:LOCALAPPDATA
workspaceFolders = $WorkspaceNames
}
$manifestPath = Join-Path $Destination "manifest.json"
$manifest | ConvertTo-Json | Set-Content -LiteralPath $manifestPath -Encoding UTF8
Set-Content -LiteralPath $LatestFile -Value $Destination -Encoding UTF8
Write-Host "快照: $Destination"
}
function Restore-Snapshot {
param([Parameter(Mandatory = $true)][string]$Snapshot)
$manifestPath = Join-Path $Snapshot "manifest.json"
if (-not (Test-Path -LiteralPath $manifestPath)) {
throw "快照缺少 manifest.json: $Snapshot"
}
$manifest = Get-Content -LiteralPath $manifestPath -Raw -Encoding UTF8 | ConvertFrom-Json
$globalStorage = Join-Path $env:APPDATA "Cursor\User\globalStorage"
$snapGlobal = Join-Path $Snapshot "AppData\Roaming\Cursor\User\globalStorage"
foreach ($base in @("state.vscdb", "conversation-search.db")) {
$srcFile = Join-Path $snapGlobal $base
if (-not (Test-Path -LiteralPath $srcFile)) { continue }
Remove-SqliteSet -Directory $globalStorage -BaseName $base
foreach ($path in (Get-SqliteSet -Directory $snapGlobal -BaseName $base)) {
Copy-Item -LiteralPath $path -Destination (Join-Path $globalStorage (Split-Path -Leaf $path)) -Force
}
Write-Host "已回滚 $base"
}
$snapStorage = Join-Path $snapGlobal "storage.json"
if (Test-Path -LiteralPath $snapStorage) {
Copy-Item -LiteralPath $snapStorage -Destination (Join-Path $globalStorage "storage.json") -Force
}
$snapSettings = Join-Path $Snapshot "AppData\Roaming\Cursor\User\settings.json"
if (Test-Path -LiteralPath $snapSettings) {
Copy-Item -LiteralPath $snapSettings -Destination (Join-Path $env:APPDATA "Cursor\User\settings.json") -Force
}
$wsDest = Join-Path $env:APPDATA "Cursor\User\workspaceStorage"
$known = @()
if ($manifest.workspaceFolders) {
$known = @($manifest.workspaceFolders)
}
if (Test-Path -LiteralPath $wsDest) {
foreach ($dir in Get-ChildItem -LiteralPath $wsDest -Directory) {
if ($known -notcontains $dir.Name) {
Write-Host "移除恢复时新增的工作区: $($dir.Name)"
Remove-Item -LiteralPath $dir.FullName -Recurse -Force
}
}
}
$wsSnap = Join-Path $Snapshot "AppData\Roaming\Cursor\User\workspaceStorage"
if (Test-Path -LiteralPath $wsSnap) {
Copy-Tree -Source $wsSnap -Destination $wsDest -Quiet
}
}
function Confirm-Action([string]$Prompt) {
Write-Host ""
Write-Host $Prompt -ForegroundColor Yellow
$answer = Read-Host "输入 YES 继续"
if ($answer -ne "YES") {
Write-Host "已取消。"
exit 1
}
}
Assert-CursorClosed
if ($Rollback) {
if (-not (Test-Path -LiteralPath $LatestFile)) {
Write-Host "这是回滚脚本,还没有可回滚的快照。" -ForegroundColor Yellow
Write-Host "迁移请运行同目录下的 Restore-CursorFromUsb.bat"
exit 1
}
$snapshot = (Get-Content -LiteralPath $LatestFile -Raw).Trim()
if (-not (Test-Path -LiteralPath $snapshot)) {
throw "快照目录不存在: $snapshot"
}
Confirm-Action "将把 Cursor 聊天库和工作区索引恢复到覆盖前的快照:`n$snapshot"
Write-Step "回滚"
Restore-Snapshot -Snapshot $snapshot
Write-Step "回滚完成"
Write-Host "可以重新打开 Cursor。MCP、技能、扩展文件没有从磁盘删除。"
exit 0
}
Write-Step "检查备份"
if (-not (Test-Path -LiteralPath $BackupRoot)) {
throw "备份目录不存在: $BackupRoot"
}
$sourceUser = Get-SourceUser -Root $BackupRoot
$sourceRoaming = Join-Path $sourceUser "AppData\Roaming\Cursor"
$sourceLocal = Join-Path $sourceUser "AppData\Local\Cursor"
$sourceDotCursor = Join-Path $sourceUser ".cursor"
$sourceAgents = Join-Path $sourceUser ".agents"
Write-Host "备份用户目录: $sourceUser"
Write-Host "本机用户目录: $env:USERPROFILE"
$freeC = (Get-PSDrive -Name C).Free
if ($freeC -lt 20GB) {
throw "C 盘剩余空间不足 20 GB(当前 $([math]::Round($freeC/1GB, 1)) GB)。覆盖聊天库前需要足够空间。"
}
Confirm-Action @"
即将用 U 盘备份覆盖本机 Cursor 数据:
- 聊天数据库 state.vscdb(约 7 GB)和搜索索引
- 各项目工作区索引、项目转录、AgentStores
- MCP、Skills、规则、计划、插件、扩展、用户设置
本机当前登录状态和这次新装后的对话会被覆盖。开始前会先做快照到:
$SnapshotRoot
"@
$wsNow = Join-Path $env:APPDATA "Cursor\User\workspaceStorage"
$workspaceNames = @()
if (Test-Path -LiteralPath $wsNow) {
$workspaceNames = @(Get-ChildItem -LiteralPath $wsNow -Directory | ForEach-Object { $_.Name })
}
$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
$snapshot = Join-Path $SnapshotRoot $stamp
$incoming = Join-Path $SnapshotRoot "incoming"
Write-Step "保存当前数据快照"
Save-Snapshot -Destination $snapshot -WorkspaceNames $workspaceNames
Write-Step "替换聊天数据库"
$srcGlobal = Join-Path $sourceRoaming "User\globalStorage"
$dstGlobal = Join-Path $env:APPDATA "Cursor\User\globalStorage"
Restore-SqliteDatabase -SourceDir $srcGlobal -DestDir $dstGlobal -BaseName "state.vscdb" -IncomingRoot $incoming
Restore-SqliteDatabase -SourceDir $srcGlobal -DestDir $dstGlobal -BaseName "conversation-search.db" -IncomingRoot $incoming
Copy-FileForce -Source (Join-Path $srcGlobal "storage.json") -Destination (Join-Path $dstGlobal "storage.json")
Write-Step "复制项目工作区索引"
$srcWorkspace = Join-Path $sourceRoaming "User\workspaceStorage"
$dstWorkspace = Join-Path $env:APPDATA "Cursor\User\workspaceStorage"
Copy-Tree -Source $srcWorkspace -Destination $dstWorkspace -Quiet
if (Test-Path -LiteralPath $srcWorkspace) {
Get-ChildItem -LiteralPath $srcWorkspace -Recurse -File -Force | Where-Object {
$_.Name -in @("state.vscdb", "state.vscdb-wal", "state.vscdb-shm", "workspace.json")
} | ForEach-Object {
$relative = $_.FullName.Substring($srcWorkspace.Length).TrimStart("\")
$target = Join-Path $dstWorkspace $relative
New-Item -ItemType Directory -Force -Path (Split-Path -Parent $target) | Out-Null
Copy-Item -LiteralPath $_.FullName -Destination $target -Force
}
}
Copy-Tree -Source (Join-Path $sourceRoaming "glassMultiRootWorkspaces") -Destination (Join-Path $env:APPDATA "Cursor\glassMultiRootWorkspaces") -Quiet
Copy-FileForce -Source (Join-Path $sourceRoaming "User\settings.json") -Destination (Join-Path $env:APPDATA "Cursor\User\settings.json")
Copy-Tree -Source (Join-Path $sourceRoaming "User\snippets") -Destination (Join-Path $env:APPDATA "Cursor\User\snippets") -Quiet
Write-Step "复制项目转录和 Agent 数据"
Copy-Tree -Source (Join-Path $sourceDotCursor "projects") -Destination (Join-Path $env:USERPROFILE ".cursor\projects") -Quiet
Copy-Tree -Source (Join-Path $sourceDotCursor "plans") -Destination (Join-Path $env:USERPROFILE ".cursor\plans") -Quiet
Copy-Tree -Source (Join-Path $sourceLocal "AgentStores") -Destination (Join-Path $env:LOCALAPPDATA "Cursor\AgentStores") -Quiet
Write-Step "复制 MCP、Skills、规则、插件和扩展"
Copy-FileForce -Source (Join-Path $sourceDotCursor "mcp.json") -Destination (Join-Path $env:USERPROFILE ".cursor\mcp.json")
Copy-FileForce -Source (Join-Path $sourceDotCursor "argv.json") -Destination (Join-Path $env:USERPROFILE ".cursor\argv.json")
Copy-FileForce -Source (Join-Path $sourceDotCursor "cli-config.json") -Destination (Join-Path $env:USERPROFILE ".cursor\cli-config.json")
Copy-Tree -Source (Join-Path $sourceDotCursor "rules") -Destination (Join-Path $env:USERPROFILE ".cursor\rules") -Quiet
Copy-Tree -Source (Join-Path $sourceDotCursor "skills-cursor") -Destination (Join-Path $env:USERPROFILE ".cursor\skills-cursor") -Quiet
Copy-Tree -Source (Join-Path $sourceDotCursor "plugins") -Destination (Join-Path $env:USERPROFILE ".cursor\plugins") -Quiet
Copy-Tree -Source (Join-Path $sourceDotCursor "extensions") -Destination (Join-Path $env:USERPROFILE ".cursor\extensions") -Quiet
Copy-Tree -Source $sourceAgents -Destination (Join-Path $env:USERPROFILE ".agents") -Quiet
if (Test-Path -LiteralPath $incoming) {
Remove-Item -LiteralPath $incoming -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Step "完成"
Write-Host "快照保留在: $snapshot"
Write-Host "现在可以重新打开 Cursor 并登录。"
Write-Host "用原来的路径打开项目后,旧对话会挂回该项目。说明见 Cursor迁移对话记录.md"
Write-Host "若要退回覆盖前的聊天库,运行 Rollback-CursorRestore.bat"