Solved

Need help automating FTP backups.  WSH or .BAT?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
regex to extract ip:john 17 69
How to batch remove spreadsheet password 19 143
Windows Batch File - Read CSV 9 35
Powershell pass array of objects to another script 8 45
It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

912 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

18 Experts available now in Live!

Get 1:1 Help Now