Powershell 7을 사용하여 Samba 서버로 파일을 전송하는 방법.

MSSQL 덤프를 복사하여 콜드 스토리지 솔루션으로 원격 Linux Samba 서버에 저장하도록 설계한 twoScripts를 공유하고 싶습니다. 모든 서버가 Windows 서버였으며 내가 이것을 만들기로 결정한 이유는 Powershell의 작은 자동화 스크립트.

파일을 냉장 보관 파일 서버로 전송합니다.



1부: 런처

$str_smb_mapp=(Get-SmbMapping | select LocalPath,RemotePath)

if($str_smb_mapp -like '*db-backups*') {
      Write-Host 'Close Stucked SMB Share on the \\<hostname/ip>\db-backups'
      Remove-SmbMapping -RemotePath "\\<hostname/ip>\db-backups" -Force
}


#- Verify Last 16 files
$global:SourceDir = "D:\MovedBackups\"
$files=(Get-ChildItem -Filter "*.bak" -Path $global:SourceDir  | sort LastWriteTime | select -last 16 | select -first 16 name, @{Name="Gigabytes";Expression={[Math]::round($_.length / 1GB, 2)}},@{Name="LastWriteTime";Expression={$_.LastWriteTime}})

pwsh.exe -File C:\Scripts\SERVER\<launch-script1>.ps1


파트 2: *본체 *

$global:Logfile = "C:\Scripts\SERVER\MovedBackupLog\" + $(Get-Date).ToString('yyyy-MM-dd-hh-mm-ss-tt') + ".log"  
$global:DestDir = "W:"
Function LogWrite
{
   Param ([string]$logstring)
   Add-content -path $global:Logfile -value ((Get-Date).ToString('yyyy-MM-dd-hh-mm-ss-tt') + " - " + $logstring)
}
LogWrite "=================" 
LogWrite "Open Share Folder Before Starting Copy Op"
LogWrite "=================" 
# Open Share  Folder
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive($global:DestDir, "\\<hostname/ip>\db-backups", $false, "backup-user", "<password>")
sleep 1s
ls $global:DestDir 

LogWrite "=================" 
LogWrite "================="
LogWrite "REMOVING OLDEST BACKUP JOB STARTED"
LogWrite  "=================" 
$limit = (Get-Date).AddDays(-3)
$theOldestFiles = $(dir $global:DestDir | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | select -First 16 ) 

foreach  ($OldestFile in $theOldestFiles) {
    LogWrite "Started: $OldestFile"
    Remove-Item -Path $OldestFile -Force
    LogWrite "Completed: $OldestFile" 
}

LogWrite "=================" 
LogWrite "================="
LogWrite "REMOVING OLDEST BACKUP JOB FINISHED"
LogWrite  "=================" 
sleep 1s
LogWrite "=================" 
LogWrite "COPY JOB Started"
LogWrite "=================" 

powershell -windowstyle hidden C:\Scripts\SERVER\<scriptname>.ps1 | Add-content -path $global:Logfile

LogWrite "=================" 
LogWrite "================="
LogWrite "COPY JOB FINISHED"
LogWrite  "=================" 
LogWrite "DESTINATION IS:$global:DestDir" 
#$filesCount= $(Get-ChildItem -Filter "*.bak"  -Path $global:DestDir  | sort LastWriteTime  | Measure-Object -property length -sum)
#LogWrite "Files Count: $($filesCount.Count) "

# Close Share 
LogWrite "=================" 
LogWrite " Closing Share Connection"
$net.RemoveNetworkDrive($global:DestDir)
#Log the end
LogWrite "Script ended successfully"


3부: 실제 기능


$global:Logfile = "C:\Scripts\SERVER\MovedBackupLog\" + $(Get-Date).ToString('yyyy-MM-dd-hh-mm-ss-tt') + ".log"   
$global:SourceDir= "D:\MovedBackups\"
$global:DestDir = "W:\"
Function LogWrite
{
   Param ([string]$logstring)
   Add-content -path $global:Logfile -value ((Get-Date).ToString('yyyy-MM-dd-hh-mm-ss-tt') + " - " + $logstring)
}
workflow Copy-Files {
    param($files)
    echo "files: $files"
    $SourceDir = "D:\MovedBackups\"
    $DestDir = "W:\"

    foreach -parallel -throttlelimit 6 ($file in $files) {
         echo "Started: $SourceDir\$file"
         #Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::D:\TestBackup_temp\$file"  -Destination "Microsoft.PowerShell.Core\FileSystem::Z:\" -Force
         robocopy  $SourceDir $DestDir /ZB /B  /R:3 /W:5 /V /NP /copy:DT  /J /log:"C:\Scripts\SERVER\MovedBackupLog\$file.log"  $file
         echo "Completed: $SourceDir\$file" 
    }

}

#Execute Workflow
#$files=(Get-ChildItem -Filter "*.bak" -Path "D:\MovedBackups\"  | sort LastWriteTime | select -last 1)
$files=(Get-ChildItem -Filter "*.bak" -Path $global:SourceDir  | sort LastWriteTime | select -last 16 | select -first 16 name, @{Name="Gigabytes";Expression={[Math]::round($_.length / 1GB, 2)}},@{Name="LastWriteTime";Expression={$_.LastWriteTime}})
echo $files
Copy-Files $files.Name



콜드 스토리지 서버에서 이전 백업을 제거합니다.




$global:Logfile = "C:\tools\tvs-scripts\PROD\SERVER-MovedBackupLog\" + $(Get-Date).ToString('yyyy-MM-dd-hh-mm-ss-tt') + "-clean.log"  
$global:DestDir = "z:"
Function LogWrite
{
   Param ([string]$logstring)
   Add-content -path $global:Logfile -value ((Get-Date).ToString('yyyy-MM-dd-hh-mm-ss-tt') + " - " + $logstring)
}

LogWrite "=================" 
LogWrite "Open Share Folder Before Starting CleanUp Op"
LogWrite "=================" 
# Open Share  Folder
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive($global:DestDir, "\\<server/ip>\db-backups", $false, "backup-user", "backup-user!")
sleep 1s
ls z:\

LogWrite "=================" 
LogWrite "================="
LogWrite "REMOVING OLDEST BACKUP JOB STARTED"
LogWrite  "=================" 
$theOldestFiles = $(dir $global:DestDir | sort lastwritetime | select -First 16)

foreach  ($OldestFile in $theOldestFiles) {
    LogWrite "Started: $OldestFile"
    Remove-Item -Path $OldestFile -Force
    LogWrite "Completed: $OldestFile" 
}

LogWrite "=================" 
LogWrite "================="
LogWrite "REMOVING OLDEST BACKUP JOB FINISHED"
LogWrite  "=================" 

# Close Share 
LogWrite "=================" 
LogWrite " Closing Share Connection"
$net.RemoveNetworkDrive($global:DestDir)
#Log the end
LogWrite "Script ended successfully"


엄지 척을 포기한다면 튜토리얼이 마음에 드시기 바랍니다! 에서 나를 팔로우하고 내Newletter를 구독하여 다가오는 자습서를 놓치지 않도록 할 수도 있습니다.

미디어 속성



내 게시물에 사용하고 있는 멋진Clark Tibbs을 디자인해 준 것에 대해photo 에 감사드립니다.

좋은 웹페이지 즐겨찾기