Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help automating FTP backups.  WSH or .BAT?

Posted on 2007-04-10
5
Medium Priority
?
263 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

597 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