?
Solved

Need help automating FTP backups.  WSH or .BAT?

Posted on 2007-04-10
5
Medium Priority
?
261 Views
Last Modified: 2010-04-16
I'd like to automate the process of copying some data off-site.  I am currently manually using ftp to copy the newest sql .bak and .trn files from the SQL backup folder on disk.  First, I rar the latest backup and all trn files into one .rar file.  I then transfer this rar file to a remote ftp server and delete the previous one.

It would be great to come up with a batch or script that can handle this for me.  The part I am having trouble with is writing a script that finds the newest .bak file and any .trn files that are newer than that in a specific directory.  I then want to take all of those filenames and build a rar package.  I am not sure is a batch file or wsh is the best approach, or necessarily how to do what I need to do in either of those environments.

Windows 2000 server.

Any ideas?
0
Comment
Question by:abenage
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 18882172
http://support.microsoft.com/kb/96269 provides the details on how to do this...

I would do something like this to get the latest file:

for /f %%a in ('dir *.trn /od /b') do set fname=%%a

now, %fname% will be the 'newest' file, and you can continue with compressing it.

Not sure how to use command-line rar functions though...
0
 
LVL 1

Author Comment

by:abenage
ID: 18883699
' ******************************************************************
call main
' ******************************************************************

Sub Main
call Doit

end Sub


'******************************************************************
sub Doit
set WshShell = WScript.CreateObject("WScript.Shell")

Dim sFileDateStamp          
Dim sUsername
dim sPassword
dim sFTPServer
dim sData
Dim sAutoFtpFilename
Dim sDirToZip


strYear = Year(Now)
strMonth = Month(Now)
strDay = Day(Now)

If strMonth < 9 Then
strMonth = "0" & strMonth
End If
If strDay < 9 Then
strDay = "0" & strDay
End If

MyDate = strYear & strMonth & strDay


 
sFileDateStamp  = mydate & "0800"    '<-- Used to determine which files to backup
sAutoFtpFilename = "c:\temp\autoftp.txt" 'Where the ftp input file is found
sMyZipFile = "FILE"
sDirToZip = "FILEPATH"
sUsername = "USERNAME"
sPassword = "PASSWORD"
sFTPServer= "HOST"




sData = _
    "open " & sFTPServer & vbcrlf & _
    "" & susername  & vbcrlf & _
    "" & spassword  & vbcrlf & _
    "bin" & vbcrlf & _
    "hash" & vbcrlf & _
    "cd database" & vbcrlf & _
     "put " & sMyZipFile & vbcrlf & _
    "bye"


    call RunCMD("cmd /k rar.exe a -hp$gdtrfb! " & sMyZipFile & " -rr50 -ta" & sFileDateStamp & " -m5 -o+")

    do while CheckProcess("Rar.exe") <> "RUNNING"
         WScript.Sleep 1000
    loop

    WScript.Sleep 1000                    '<-- Added

    call CreateAfile(sAutoFtpFilename,sData)
    call RunCMD("ftp -s:" & sAutoFtpFilename )

    WScript.Sleep 1000

    do while CheckProcess("FTP.EXE") <> "RUNNING"
         WScript.Sleep 1000
    loop
   
    call DeleteAfile(sMyZipFile)

end sub

' ******************************************************************
Sub CreateAfile(filename,filedata)
  Dim fso, MyFile
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set MyFile = fso.CreateTextFile(filename, True)
  MyFile.WriteLine(filedata)
  MyFile.Close
End Sub
' ******************************************************************
sub RunCMD(cmdl)
    Dim oShell
    Set oShell = WScript.CreateObject ("WSCript.shell")
    oShell.run cmdl
    Set oShell = Nothing
end sub
' ******************************************************************
function CheckProcess(sAppName)
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery _
        ("Select * from Win32_Process Where Name = '"& sAppName & "'")
    If colProcesses.Count = 0 Then
        CheckProcess="RUNNING"
    Else
        CheckProcess="NOTRUNNING"
    End If    
end function
' ******************************************************************

sub DeleteAFile(FileToBeDeleted)

Set fso = CreateObject("Scripting.FileSystemObject")
Set aFile = fso.GetFile(FileToBeDeleted)
aFile.Delete
end sub
'*******************************************************************
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18884310
Looks like you got this?
0
 
LVL 1

Author Comment

by:abenage
ID: 18884576
Yeah, But I'll give you points.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18884589
Thanx!  Very appreciated.
 
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question