백업에서 1KB보다 작은 파일 교체

백업에서 1KB보다 작은 파일 교체

공유 폴더의 손상된 파일을 교체하려면 백업을 사용해야 합니다. 손상된 모든 파일은 크기 = 1KB로 고정되어 있으며 보관 플래그가 설정되어 있습니다. 기본적으로 대상 파일이 <= 1KB이거나 보관 플래그가 설정된 경우에만 대상 폴더의 파일을 백업의 파일로 바꾸고 싶습니다.

Robocopy는 이를 위한 가능한 도구처럼 보이지만 대상 파일에서 이를 조건화하는 옵션을 볼 수 없습니다. 이 작업을 수행할 수 있을 것으로 보이는 또 다른 도구는 Powershell이지만 저는 이에 익숙하지 않습니다.

두 프로그램 중 하나를 사용하여 이를 어떻게 수행할 수 있습니까?

답변1

Powershell 솔루션은 손상된 파일을 보고 및/또는 복원할 수 있습니다.

# Use full paths!
$Backup = '\\server\backup'
$Corrupted = 'c:\broken_folder'

# Path for log file, can be relative
$LogFile = '.\Restore.log'

# If this variable is set to true, no files will be copied
$ReportOnly = $true

# Remove log file, if exist
if(Test-Path -Path $LogFile -PathType Leaf)
{
    Remove-Item -Path $LogFile -Force
}

# Get all files in directory, recursive
$Corrupted | Get-ChildItem -Recurse |
    # Select files with archive attribute: $_.Mode -like '*a*'
    # And size less or equal to 1KB: ($_.Length / 1KB) -le 1 . Less fancy way: $_.Length -le 1024
    # Ignore folders: -not $_.PsIsContainer
    #
    # In PS 3.0 and higher Get-ChildItem has less cryptic way to get folders and specify attributes:
    # http://www.powershellmagazine.com/2012/08/27/pstip-how-to-get-only-files-the-powershell-3-0-way
    Where-Object {($_.Mode -like '*a*') -and (($_.Length / 1KB) -le 1) -and (-not $_.PsIsContainer)} |
        ForEach-Object {
            # Output log record to pipeline, Tee-Object will catch it later
            "Found corrupted file: $($_.FullName)"

            # Replace current file path with path fo this file in backup folder
            $NewFile =  $_.FullName -replace [regex]::Escape($Corrupted), $Backup

            if(Test-Path -Path $NewFile -PathType Leaf)
            {
                # Output log record to pipeline, Tee-Object will catch it later
                "Found corresponding file from backup: $NewFile"
            }
            else
            {
                # Output log record to pipeline, Tee-Object will catch it later
                "Failed to find corresponding file from backup: $NewFile"
                return
            }

            if(-not $ReportOnly)
            {
                # Output log record to pipeline, Tee-Object will catch it later
                "Restoring file from backup: $NewFile -> $($_.FullName)"

                # Remove corrupted file
                Remove-Item -Path $_.FullName -Force

                # Copy file from backup
                Copy-Item -Path $NewFile -Destination $_.FullName -Force
            }
        } | Tee-Object -FilePath $LogFile -Append # Send log to screen and file

답변2

Robocopy에는 필요한 매개변수가 있습니다. 예를 들어:

/A :: 보관 특성이 설정된 파일만 복사합니다.

/M :: 보관 특성이 있는 파일만 복사하고 재설정합니다.


/MAX:n :: 최대 파일 크기 - n바이트보다 큰 파일을 제외합니다.

/MIN:n :: 최소 파일 크기 – n바이트보다 작은 파일을 제외합니다.

모든 명령 목록은 다음과 같습니다. https://wmoore.wordpress.com/2009/09/01/robocopy-command-line-switches/

관련 정보