SS64 Discussion Forum

You are not logged in.

#1 2008-08-29 13:01:44

Simon Sheppard
Super Administrator
Registered: 2005-08-27
Posts: 506
Website

Restoring zero byte files from a backup

Here's an example of using Powershell to restore a group of files from a backup.

This was used to patch up one of our file servers after some lovely 'enterprise' software decided to start randomly truncating files.
We suddenly found ourselves with thousands of empty (zero byte) files split across different directories with unpredictable file dates.
Simply restoring a backup of everything was not an option, as 90% of the files were OK and we don't want to overwrite the non-zero files with old copies.

First we restored a tape backup of everything to a separate folder.
Then the script below was used to find each zero byte file, retrieve the matching file from the backup folder and copy it to a third, temporary destination.
(recreating directories in the new destination as needed.)

With this done we could check the right files had been copied and then robocopy them all back onto the live server.

$usersRoot = '\\server1\t$\Users\'
$backupRoot = '\\server1\t$\backup\'
$destinationRoot = '\\server1\t$\new\'

Get-ChildItem -LiteralPath $usersRoot -Force -Recurse `
    | Where-Object {$_.length -eq 0 -and (-not $_.PSIsContainer)} `
    | ForEach-Object {
        # Identify the destination folder
        $destinationFolder = $_.PSParentPath.Replace("$usersRoot","$destinationRoot")
        #
        # Identify the backup file to copy
        $FileToCopy = $_.PSPath.Replace("$usersRoot","$backupRoot")
        #
        # If the backup file is also 0 bytes then skip it
        Get-Item -LiteralPath $FileToCopy | Where-Object {$_.length -ne 0} | ForEach-Object {
        #
        # If the destination folder does not exist, create it
        if (-not (Test-Path -LiteralPath $destinationFolder)) {
            New-Item -ItemType Directory -Path $destinationFolder | Out-Null
        }
        Copy-Item -LiteralPath $FileToCopy -Destination $destinationFolder
        }
    }

# This script will pass through the directory structure once, copying files as it goes.
# Each fully qualified file name must be less than 260 characters,
# Each directory name must be less than 248 characters.

note
My first attempt at this was to try Robocopy with /max:0 /L to list all the zero byte files, unfortunately /max:0 did not return any files, so it was powershell to the rescue.

Offline

#2 2009-08-15 20:54:33

audiogalaxy
New Shoes
Registered: 2009-02-11
Posts: 4

Re: Restoring zero byte files from a backup

very useful example, thanks!

Offline

#3 2009-11-02 16:11:10

arenas
8086
Registered: 2009-05-01
Posts: 10

Re: Restoring zero byte files from a backup

hello many thnx for this script !

how can i creat en batch with posh?

- timer
- run the script
- ftp upload

i have to use IE?
many tnx

Offline

#4 2009-11-02 16:28:47

Simon Sheppard
Super Administrator
Registered: 2005-08-27
Posts: 506
Website

Re: Restoring zero byte files from a backup

- timer
- run the script

start-sleep (or just setup a scheduled task to run powershell.exe)


- ftp upload

You can run FTP right from the powershell command line just as in CMD
http://ss64.com/nt/ftp.html

Offline

#5 2009-11-03 16:12:39

arenas
8086
Registered: 2009-05-01
Posts: 10

Re: Restoring zero byte files from a backup

hello

this script was aceptet but his not run...

schtasks /create /sc once /st 21:50 /sd 2009/11/03 /tn "test" /TR "C:\Program Files (x86)\PowerGUI\ScriptEditor.exe -command `"&{C:\Users\Administrateur\Documents\PS\myfile.ps1}`""

thnx for you help !

ad:

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
  <Provider Name="Microsoft-Windows-TaskScheduler" Guid="{de7b24ea-73c8-4a09-985d-5bdadcfa9017}" />
  <EventID>140</EventID>
  <Version>0</Version>
  <Level>4</Level>
  <Task>140</Task>
  <Opcode>0</Opcode>
  <Keywords>0x8000000000000000</Keywords>
  <TimeCreated SystemTime="2009-11-03T21:13:44.729Z" />
  <EventRecordID>771344</EventRecordID>
  <Correlation />
  <Execution ProcessID="600" ThreadID="8704" />
  <Channel>Microsoft-Windows-TaskScheduler/Operational</Channel>
  <Computer>XXXXXXX</Computer>
  <Security UserID="S-1-5-18" />
  </System>
- <EventData Name="TaskUpdated">
  <Data Name="TaskName">test</Data>
  <Data Name="UserName">XXXXXXX</Data>
  </EventData>
  </Event>

Last edited by arenas (2009-11-03 16:16:38)

Offline

Board footer

Powered by FluxBB