Powershell 7을 사용하여 Samba 서버로 파일을 전송하는 방법.
6962 단어 tutorialsysadminpowershelldevops
파일을 냉장 보관 파일 서버로 전송합니다.
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 에 감사드립니다.
Reference
이 문제에 관하여(Powershell 7을 사용하여 Samba 서버로 파일을 전송하는 방법.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/tvelmachos/how-to-transfer-files-to-samba-server-with-powershell-7-1nn5텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)