Get directory size through SFTP

2

1

A client has a managed websever; on this server is an e-commerce script and part of the script dumps a backup every week. The backup is stored on the web server (not in the HTTP route). The ISP takes a copy, and my clients wants to take a copy too.

What I am trying to do is before downloading the file I want to be able to calculate the backup directory size - but the only access I have is through SFTP. Is it possible to easily get the directory size and then use this in a PowerShell Script.

NOTE: I have written an automatic download script in PowerShell, and I want to extend this.

Forgive me if this sounds vague I can provide further info if you have any specific questions.

developer__c

Posted 2014-06-03T18:09:49.433

Reputation: 232

to clarify, you mean SFTP, which you would access via WinSCP or a like client, not FTPS, which you would access through a web browser/wget/curl or web download manager like FileZilla, right? – Frank Thomas – 2014-06-03T18:38:55.647

@FrankThomas Yes, I will be using SFTP through PuTTY by parsing command line arguments to the .exe from PowerShell – developer__c – 2014-06-03T19:26:07.877

You might be better off using the command line version of WinSCP, since they have an API for tasks like object size. this article is dated, but should provide you some good hints: http://www.codeproject.com/Articles/545586/Retrieve-size-of-a-file-in-a-Unix-server-via-SFTP

– Frank Thomas – 2014-06-03T19:29:33.860

Answers

1

It's easy with WinSCP .NET assembly and its Session.EnumerateRemoteFiles method:

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "host"
    UserName = "user"
    Password = "password"
    SshHostKeyFingerprint = "fingerprint"
}

Write-Host "Connecting..."
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)

Write-Host "Calculating..."
$sum =
    $session.EnumerateRemoteFiles(
        "/remote/path", $Null, [WinSCP.EnumerationOptions]::AllDirectories) |
    Measure-Object -Property Length -Sum
Write-Host "Size is $($sum.Sum)"

(I'm the author of WinSCP)

Martin Prikryl

Posted 2014-06-03T18:09:49.433

Reputation: 13 764

0

I have added a work around, and using PHP I request the directory size on the server - then I can grab the information using a powershell script.

PHP Script to grab file size

<?php
$f = 'PATH FROM LINUX ROOT TO DIRECTORY YOU WANT SIZE OF';
    $io = popen ( '/usr/bin/du -sk ' . $f, 'r' );
    $size = fgets ( $io, 4096);
    $size = substr ( $size, 0, strpos ( $size, "\t" ) );
    pclose ( $io );
    echo 'Backup Size: ' . $f . ' => Size: <h3>' . $size . '</h3>';

?>

Powershell to grab the filesize from the PHP script:

$url = 'PATH TO PHP FILE SIZE SCRIPT'
$pattern =  '(?i)<h3[^>]*>(.*)</h3>'

$webclient = New-Object System.Net.WebClient
$html = $webclient.DownloadString($url)


$result = [Regex]::Matches($html, $pattern)

$result | ForEach-Object {
  [int]$remoteSize =  $_.Groups[1].Value
}

# Correct the disc size (-332)

$remoteSize -=332

developer__c

Posted 2014-06-03T18:09:49.433

Reputation: 232