Substitua arquivos menores que 1 KB do backup

Substitua arquivos menores que 1 KB do backup

Preciso usar backups para substituir arquivos corrompidos em uma pasta compartilhada. Todos os arquivos corrompidos têm tamanho fixo = 1 KB e têm o sinalizador de arquivo definido. Basicamente, gostaria de substituir os arquivos na pasta de destino por arquivos de backups somente se o arquivo de destino tiver <= 1 KB e/ou tiver o sinalizador de arquivo definido.

Robocopy parece uma ferramenta possível para isso, mas não consigo ver uma opção para condicioná-lo no arquivo de destino. Outra ferramenta que parece fazer isso é o Powershell, mas não estou familiarizado com ele.

Como posso conseguir isso com qualquer um dos programas?

Responder1

Solução Powershell, pode relatar e/ou restaurar arquivos corrompidos:

# 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

Responder2

Robocopy tem os parâmetros que você precisa. Por exemplo:

/A :: copia apenas arquivos com o atributo Archive definido.

/M :: copie apenas os arquivos com o atributo Archive e redefina-o.


/MAX:n :: Tamanho máximo do arquivo – exclui arquivos maiores que n bytes.

/MIN:n :: Tamanho MÍNIMO do arquivo – exclui arquivos menores que n bytes.

Aqui está uma lista de todos os comandos: https://wmoore.wordpress.com/2009/09/01/robocopy-command-line-switches/

informação relacionada