100 lines
2.5 KiB
PowerShell
100 lines
2.5 KiB
PowerShell
param(
|
|
[string]$DB_HOST = '43.138.168.68',
|
|
[int]$DB_PORT = 3306,
|
|
[string]$DB_NAME = 'frameworkdb2023',
|
|
[string]$DB_USER = 'root',
|
|
[string]$DB_PASSWORD = 'ylfw20230626@'
|
|
)
|
|
|
|
# 确保输出目录存在
|
|
$outputDir = Join-Path $PSScriptRoot '..\sql'
|
|
if (!(Test-Path $outputDir)) {
|
|
New-Item -ItemType Directory -Path $outputDir | Out-Null
|
|
}
|
|
|
|
# 构建输出文件路径
|
|
$schemaFile = Join-Path $outputDir "base-schema.sql"
|
|
$dataFile = Join-Path $outputDir "base-data.sql"
|
|
|
|
# 构建 mysqldump 命令参数
|
|
$commonArgs = @(
|
|
"--host=$hostName",
|
|
"--port=$port",
|
|
"--user=$user",
|
|
"--routines",
|
|
"--triggers",
|
|
"--single-transaction",
|
|
"--set-charset",
|
|
"--default-character-set=utf8mb4"
|
|
)
|
|
|
|
if ($password) {
|
|
$commonArgs += "--password=$password"
|
|
}
|
|
|
|
Write-Host "开始导出数据库结构和数据..."
|
|
Write-Host "数据库: $database"
|
|
Write-Host "主机: $hostName:$port"
|
|
Write-Host "用户: $user"
|
|
Write-Host "输出目录: $outputDir"
|
|
Write-Host ""
|
|
|
|
# 导出数据库结构(仅结构,不包含数据)
|
|
Write-Host "正在导出数据库结构到: $schemaFile"
|
|
try {
|
|
$schemaArgs = $commonArgs + @(
|
|
"--no-data",
|
|
"--result-file=$schemaFile",
|
|
$database
|
|
)
|
|
|
|
& mysqldump @schemaArgs
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host "✓ 数据库结构导出成功" -ForegroundColor Green
|
|
} else {
|
|
throw "mysqldump 返回错误代码: $LASTEXITCODE"
|
|
}
|
|
} catch {
|
|
Write-Error "导出数据库结构失败: $_"
|
|
exit 1
|
|
}
|
|
|
|
# 导出数据(仅数据,不包含结构)
|
|
Write-Host ""
|
|
Write-Host "正在导出数据到: $dataFile"
|
|
try {
|
|
$dataArgs = $commonArgs + @(
|
|
"--no-create-info",
|
|
"--skip-triggers",
|
|
"--result-file=$dataFile",
|
|
$database
|
|
)
|
|
|
|
& mysqldump @dataArgs
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host "✓ 数据导出成功" -ForegroundColor Green
|
|
} else {
|
|
throw "mysqldump 返回错误代码: $LASTEXITCODE"
|
|
}
|
|
} catch {
|
|
Write-Error "导出数据失败: $_"
|
|
exit 1
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "数据库导出完成!" -ForegroundColor Green
|
|
Write-Host "结构文件: $schemaFile"
|
|
Write-Host "数据文件: $dataFile"
|
|
|
|
# 显示文件大小
|
|
if (Test-Path $schemaFile) {
|
|
$schemaSize = (Get-Item $schemaFile).Length
|
|
Write-Host "结构文件大小: $([math]::Round($schemaSize/1KB, 2)) KB"
|
|
}
|
|
|
|
if (Test-Path $dataFile) {
|
|
$dataSize = (Get-Item $dataFile).Length
|
|
Write-Host "数据文件大小: $([math]::Round($dataSize/1KB, 2)) KB"
|
|
} |