141 lines
4.6 KiB
Plaintext
141 lines
4.6 KiB
Plaintext
Set-Location C:
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
# VARIABLES
|
|
|
|
$query = "select SettingValue from cfg.Settings
|
|
where SettingId like 'Values.Modules.Replication.DbInitializationBackupPath%'
|
|
and len(SettingValue) > 1"
|
|
$erroronscript = 0
|
|
$errortext = ''
|
|
$snapshotLocation = ''
|
|
$snapshotHash =''
|
|
|
|
#Get configs from SQL
|
|
$connectionString = "Server='.\apssql';Database='ActiveSystemServer';Integrated Security=True;"
|
|
$connection = New-Object System.Data.SqlClient.SqlConnection
|
|
$connection.ConnectionString = $connectionString
|
|
$connection.Open()
|
|
$command = $connection.CreateCommand()
|
|
$command.CommandText = $query
|
|
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
|
|
$dataset = New-Object System.Data.DataSet
|
|
$adapter.Fill($dataSet) | Out-Null
|
|
$data = $dataset.Tables[0]
|
|
$workFolder=""
|
|
|
|
|
|
#get snapshot location
|
|
$results = Invoke-Sqlcmd -ServerInstance .\apssql -Database master -query "EXEC sp_helpdistributor"
|
|
$snapshotLocation = $results.directory
|
|
$workFolder = "$snapshotLocation\wrk_zip"
|
|
|
|
# Get database version
|
|
$query2 = "SELECT ActivePos_write.upd.DatabaseVersion()"
|
|
$command2 = New-Object System.Data.SqlClient.SqlCommand($query2,$connection)
|
|
|
|
$aposVersion = $command2.ExecuteScalar()
|
|
|
|
#create needed structure in c:\temp\work_repl
|
|
$snapshotLocalDirName = Get-ChildItem -Path $snapshotLocation\unc\*_ACTIVEPOSTRAN | Select-Object -ExpandProperty Name
|
|
New-Item -ItemType Directory -Force $workFolder\unc\$snapshotLocalDirName\$snapshotName
|
|
|
|
#let some time for the FS to settle, to avoid directory not found exceptions
|
|
start-sleep -Seconds 3
|
|
|
|
#find latest snapshot available
|
|
$snapshotName = Get-ChildItem -Path $snapshotLocation\unc\*_ACTIVEPOSTRAN\* | Sort-Object -Descending |Select-Object -First 1 -ExpandProperty Name
|
|
|
|
#copy latest snapshot to work folder
|
|
#copy-Item -Path "$snapshotLocation\unc\*_ACTIVEPOSTRAN\$snapshotName\*.*" -Destination "$workFolder\unc\$snapshotLocalDirName\$snapshotName\"
|
|
(Robocopy.exe $snapshotLocation\unc\*_ACTIVEPOSTRAN\$snapshotName\ $workFolder\unc\$snapshotLocalDirName\$snapshotName\ /MIR /R:3 /W:5)
|
|
|
|
#zip snapshot and folder structure
|
|
$compress = @{
|
|
Path = "$workFolder\unc"
|
|
CompressionLevel = "Fastest"
|
|
DestinationPath = "$workFolder\snapshot_$aposVersion.zip"
|
|
}
|
|
Compress-Archive @compress -Force
|
|
|
|
#compute CRC
|
|
$snapshotHash = Get-FileHash -Path $workFolder\snapshot_$aposVersion.zip -Algorithm SHA512 | Select-Object -ExpandProperty Hash | out-file -FilePath $workFolder\snapshot_$aposVersion.crc -Force
|
|
|
|
#delete work folder
|
|
Remove-Item $workFolder\unc -Force -Recurse
|
|
|
|
# Get backup directory
|
|
$query3 = "SELECT HCIP_value FROM HCITools.dbo.HCI_PARAMS WHERE HCIP_Key = 'BKP_DIR'"
|
|
$command3 = New-Object System.Data.SqlClient.SqlCommand($query3,$connection)
|
|
$source = $command3.ExecuteScalar()
|
|
|
|
$connection.close()
|
|
|
|
#copy every files to relevant shares
|
|
$jobs = foreach($dir in $data){
|
|
$logFile = $dir.SettingValue -replace "[\\]" , ""
|
|
$target = $dir.SettingValue
|
|
$logfile = "/log:`"c:\temp\$logFile.txt`""
|
|
$files = Get-ChildItem -Path $workFolder -Filter "snapshot*.*"
|
|
|
|
if(!(test-path "$Target")){
|
|
try{
|
|
New-Item -ItemType Directory -Force -Path $Target -ErrorAction Stop
|
|
}
|
|
catch{
|
|
$erroronscript = 1
|
|
if($errortext -eq ''){
|
|
$errortext = "Share is not reachable: $Target"
|
|
}
|
|
else{
|
|
$errortext = $errortext + "`r`nShare is not reaechable: $Target"
|
|
}
|
|
}
|
|
}
|
|
|
|
if(test-path "$Target"){
|
|
$files | ForEach-Object {
|
|
$testfile = $_.Directory
|
|
|
|
if(!(test-path $testfile)){
|
|
$erroronscript = 1
|
|
if($errortext -eq ''){
|
|
$errortext = "ActivePos_read file source is not accessible!"
|
|
}
|
|
else{
|
|
$errortext = $errortext + "`r`nActivePos_read file source is not accessible!"
|
|
}
|
|
}
|
|
else{
|
|
start-job -ArgumentList $_.Directory, $target, $_.Name, $logFile -ScriptBlock{
|
|
param($source, $target, $testfile, $logFile )
|
|
$copy = robocopy $source $target $testfile /r:5 /w:120 $logFile
|
|
$LASTEXITCODE
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
$resultsjobs = Get-Job | Wait-Job | Receive-Job
|
|
|
|
foreach($resultjob in $resultsjobs){
|
|
if($resultjob -gt 7){
|
|
$errortext = "Robocopy exit code greater than 7!"
|
|
}
|
|
}
|
|
|
|
Remove-Job *
|
|
|
|
if (Test-Path 'C:\Temp\PScopy.ps1') { Remove-Item 'C:\Temp\PScopy.ps1' -Force }
|
|
|
|
#delete generated local files
|
|
$files | Remove-Item
|
|
|
|
#purge old version snapshots on the share
|
|
Get-ChildItem -Path $target -Filter "snapshot*.*" -Exclude "*$aposVersion*" | Remove-Item
|
|
|
|
IF($errortext -ne ''){ throw($errortext)}
|
|
|