views:

45

answers:

3

I have clients out there running an SQL Server Express 2005 and each of these needs a backup each month and that backup needs to be moved to our server in case they lose their backup. Our software automatically backs up the database each month but we have to manually go in and copy it across. Is there any way to automate the copying of files up to 800 megs from their machine to ours each month perhaps using FTP? Also, if using FTP it has to support resume in case we lose the connection three querters through which happens quite often. I would like to write this functionality into our VB.net application that only requires the .net framework and not use any third party controls.

+2  A: 

I think this is where you'd be better off if you used something like RSync rather than a home-grown solution.

Anton Gogolev
A: 

It's certainly doable with either the WebClient classes, or (Ftp)WebRequest/WebResponse series of classes - and I can give you some sample code if required - but unless you have some specific business case for rolling your own something like RSync may be the better choice.

EDIT;

The WebClient route is the simplest, but it doesn't give you much control;

Imports System.Net
...
Dim Client As New WebClient
Client.DownloadFile("ftp://ftp.example.com/Database.bak", "D:\Backups\Database.bak")

If you want a bit more control, and to manage FTP resumes then something like this will do the trick;

Public Sub TransferFile(ByVal SourcePath As String, ByVal DestinationPath As String)

    Dim SourceRequest As FtpWebRequest

    Dim Buffer(4095) As Byte
    Dim BytesRead As Integer

    ' Assumes source is on FTP server...
    SourceRequest = DirectCast(WebRequest.Create(SourcePath), FtpWebRequest)
    SourceRequest.Method = WebRequestMethods.Ftp.DownloadFile

    ' If we already have a local file, then resume from the end of it...
    SourceRequest.ContentOffset = If(File.Exists(DestinationPath), New FileInfo(DestinationPath).Length, 0)

    ' Assume destination file is local/UNC file. FileMode.Append will create a new file if one doesn't exist.
    Using DestinationFile As New FileStream(DestinationPath, FileMode.Append, FileAccess.Write, FileShare.None)
        Using SourceResponse As WebResponse = SourceRequest.GetResponse()
            Using SourceStream As Stream = SourceResponse.GetResponseStream()

                Do

                    BytesRead = SourceStream.Read(Buffer, 0, Buffer.Length)
                    DestinationFile.Write(Buffer, 0, BytesRead)

                    ' Calculate speed, progress, show to user/log, etc...

                Loop While BytesRead > 0

            End Using
        End Using
    End Using

End Sub

This assumes you're transferring from FTP -> local. Username/password can be supplied as in the SourcePath as so; ftp://username:[email protected]

Hope this helps.

Gareth Wilson
Hi Gareth, thanks. Would you be able to send me some of that sample code to look through? I would prefer to include it in my project and not be dependent on any other software. Thanks so much. Henry.
henry
A: 

Another alternative would be to use a program like SyncBack, that supports syncronization across FTP, can be scheduled to run daily/weekly/monthly and has worked for me to do what you're talking about, for years. I'm not sure about Resuming an FTP transfer - but it does a great job of backing things up across FTP.

Logan