-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.ps1
74 lines (59 loc) · 2.56 KB
/
migrate.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 脚本以 UTF8-BOM 编码格式保存,以兼容 Powershell7 和旧版本的 Powershell
# 设置数据库连接参数
$sourceHost = 'localhost'
$sourcePort = '3306'
$sourceUser = 'root'
$sourcePass = ''
$targetHost = 'localhost'
$targetPort = '3307'
$targetUser = 'root'
$targetPass = ''
# $DBList = @('3dviewserver-ecityos', 'hc7.0', 'hw-platform', 'license_agent', 'nacos', 'xxl_job', 'test_agent')
$DBList = @('hc7.0')
function Save-MySQLDB {
[CmdletBinding()]
param(
[Parameter(Mandatory)] [string]$DBHost,
[Parameter(Mandatory)] [int]$DBPort,
[Parameter(Mandatory)] [string]$DBUser,
[Parameter(Mandatory)] [string]$DBPassword,
[Parameter(Mandatory)] [string]$DBName,
[Parameter(Mandatory)] [string]$BackupPath
)
try {
Write-Host "开始备份数据库:$DBName..."
& '.\mysqldump.exe' --default-character-set=utf8mb4 --skip-triggers -h $DBHost -P $DBPort -u $DBUser --password=$DBPassword -B $DBName -r $BackupPath\$DBName.sql
Write-Host "数据库备份完成,备份文件位于: $BackupPath\$DBName.sql"
} catch {
Write-Error "备份过程中发生错误: $_"
}
}
function Restore-MySQLDB {
[CmdletBinding()]
param(
[Parameter(Mandatory)] [string]$BackupPath,
[Parameter(Mandatory)] [string]$DBHost,
[Parameter(Mandatory)] [int]$DBPort,
[Parameter(Mandatory)] [string]$DBUser,
[Parameter(Mandatory)] [string]$DBPassword,
[Parameter(Mandatory)] [string]$DBName
)
# 使用Start-Process执行恢复命令,通过管道将备份文件内容传递给mysql命令
try {
Write-Host "开始恢复数据库:$DBName..."
Get-Content "${BackupPath}\${DBName}.sql" -Encoding utf8 |
& '.\mysql.exe' --default-character-set=utf8mb4 -h $DBHost -P $DBPort -u $DBUser --password=$DBPassword
Write-Host "数据库${DBName}恢复完成。"
} catch {
Write-Error "恢复过程中发生错误: $_"
}
}
New-Item -Path 'dump' -ItemType Directory -Force
foreach ($sourceDB in $DBList) {
Save-MySQLDB -DBHost $sourceHost -DBPort $sourcePort -DBUser $sourceUser -DBPassword $sourcePass -DBName $sourceDB -BackupPath 'dump'
}
foreach ($sourceDB in $DBList) {
# Restore-MySQLDB -BackupPath 'dump' -DBHost $targetHost -DBPort $targetPort -DBUser $targetUser -DBPassword $targetPass -DBName $sourceDB
}
Write-Host -NoNewLine "迁移完成,按任意键退出..."
[void][System.Console]::ReadKey($true)