#!PowerShell # Function: Import data from script files or RAR script files into MySQL specific libraries # Author: JiangJun # Create-Time: 2025-8-10 15:57:26 # Version: 0.3 # Set-ExecutionPolicy RemoteSigned -Scope Process -Force $logFile = "ScriptExecutionLog_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt" function Write-Log { param ( [string]$message ) $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $logMessage = "[$timestamp] $message" # [debug] # Write-Output $logMessage | Out-File -FilePath $logFile -Append Write-Host $logMessage } function randomText { $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.ToCharArray() $length = 12 $random = New-Object System.Random $string = -join (1..$length | ForEach-Object { $chars[$random.Next(0, $chars.Length)] }) return $string } function Import-Script { param ( [string]$dbServer, [string]$dbPort, [string]$dbUser, [string]$dbPassword ) if (Test-Path -Path ".\script" -PathType Container) { Write-Log "Found script folder, starting to process .." $subFolders = Get-ChildItem -Path ".\script" -Directory if ($subFolders.Count -eq 0) { Write-Log "There are no subfolders in the script folder, skipping processing." exit } foreach ($folder in $subFolders) { Write-Log "Database connection parameters - Please modify according to the actual situation $($folder.Name)" $rarFiles = Get-ChildItem -Path $folder.FullName -Filter "*.rar" if ($rarFiles.Count -eq 0) { Write-Log "There are no SQL files in the folder $($folder.Name), skipping." continue } foreach ($rarFile in $rarFiles) { Write-Log "Processing file: $($rarFile.Name) (located in $($folder.Name))" $rarName = $rarFile.BaseName $rarDirectoryName = (Get-Item $rarFile.FullName).DirectoryName try { Start-Process -FilePath "rar" -ArgumentList "x -pshzyh!234 `"$($rarFile.FullName)`" `"$rarDirectoryName`"" -NoNewWindow -Wait if (Test-Path $rarFile.FullName) { Remove-Item $rarFile.FullName -Force } } catch { Write-Log "Error unrar $($rarFile.Name):$_" Write-Log "=> $($rarFile.FullName)" } } } foreach ($folder in $subFolders) { Write-Log "Database connection parameters - Please modify according to the actual situation $($folder.Name)" $sqlFiles = Get-ChildItem -Path $folder.FullName -Filter "*.sql" if ($sqlFiles.Count -eq 0) { Write-Log "There are no SQL files in the folder $($folder.Name), skipping." continue } foreach ($sqlFile in $sqlFiles) { Write-Log "Processing file: $($sqlFile.Name) (located in $($folder.Name))" $dbName = $sqlFile.BaseName try { Write-Log "Creating database $dbName and importing $($sqlFile.Name)" Start-Process -FilePath "mysql" -ArgumentList "-h$dbServer -u$dbUser -p$dbPassword -P$dbPort $($folder.Name)" -RedirectStandardInput $sqlFile.FullName -NoNewWindow -Wait Write-Log "Successfully imported $($sqlFile.Name) into database $($folder.Name)" $date = $(Get-Date -Format "yyyy-MM-dd") $random = randomText $target = ".\patch\$($date)" New-Item -Path "$target" -ItemType Directory -Force Start-Process -FilePath "rar" -ArgumentList "a -pPassword#$date! -hp $target\$($random).rar `"$($sqlFile.FullName)`"" -NoNewWindow -Wait if (Test-Path $sqlFile.FullName) { Remove-Item $sqlFile.FullName -Force } } catch { Write-Log "Error importing $($sqlFile.Name):$_" Write-Log "=> $($sqlFile.FullName)" } } } Write-Log "All script processing has been completed." } } # Param 1: MySQL IP # Param 2: MySQL Port # Param 3: MySQL root # Param 4: MySQL root_password Import-Script "127.0.0.1" "33306" "root" "Root@2025"