Solved

Need help automating FTP backups.  WSH or .BAT?

Posted on 2007-04-10
5
248 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
  • 3
  • 2
5 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now