Solved

Need help automating FTP backups.  WSH or .BAT?

Posted on 2007-04-10
5
259 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 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

726 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