?
Solved

Copy/Backup Script FreeBSD 4.10

Posted on 2005-03-09
11
Medium Priority
?
340 Views
Last Modified: 2012-08-13
Hi. I need a script for BSD to do the following:
I have a monitoring program that stores graphs (.png) in a certain directory and updates them daily. I need a script that can copy those files so I have a record of daily occurences e.g todays files will be saved in a folder 09032005 & tomorrows 10032005. Then at the end of the month all the folders for the days are moved into a folder called March2005 and April2005 is started. Any ideas? Thanks.
0
Comment
Question by:KevoZam
[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
  • 8
11 Comments
 
LVL 4

Expert Comment

by:HackLife
ID: 13500178
I'd create a vbscript to do it. But that's me.
0
 

Author Comment

by:KevoZam
ID: 13503863
I dont know how to code but a sample script will do. Something i can add as a cron job every night
0
 
LVL 4

Expert Comment

by:HackLife
ID: 13507908
Ok. Here's my scripts. There are 3 files, so copy a paste in the designated files. Run config.vbs first.



' Config.vbs - Joseph B. Nguyen (HackLife at gmail.com)
Dim D(1), App, Fdr, Dir, Ini
Set App = CreateObject("Shell.Application")
Set Fso = CreateObject("Scripting.FileSystemObject")

For i = 0 To 1
    Set Dir = App.BrowseForFolder(&H0, "Please select the first directory:", BrowseInfo, root)
    If InStr(Dir.Title,"Desktop") Then
        For Each Item In Dir.Items
            If InStr(Item.Path, "Desktop") Then
                D(i) = Left(Item.Path,InStrRev(Item.Path,"\")-1)
            End If
        Next
    Else
        If (Not Dir Is Nothing) Then
            D(i) = Dir.Items.Item.Path
        End If
    End If
Next

Pause = InputBox("How many seconds do you want the script to wait between each sync?" _
                    & vbCrLf & "The shorter the time, the more processor intensive it is.",,60)

UserReply = MsgBox("Do you want the script to run continuously?" & vbCrLf _
                    & "To stop the script, you must run 'StopScript.vbs'", vbYesNo)
If UserReply = vbYes Then
    RunForever = "True"
Else
    RunForever = "False"
End If

Ini = "' The following configuration have been saved in 'DirectorySync.ini':" & vbNewLine
Ini = Ini & "' ----------------------------------------------------------------" & vbNewLine
Ini = Ini & "Dir1 = " & D(0) & vbNewLine
Ini = Ini & "Dir2 = " & D(1) & vbNewLine
Ini = Ini & "Pause = " & CStr(Pause) & vbNewLine
Ini = Ini & "RunForever = " & RunForever & vbNewLine

Fso.OpenTextFile("DirectorySync.ini", 2, True).Write Ini
Msgbox Ini

Set Fso = Nothing
Set Dir = Nothing
Set App = Nothing

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 4

Expert Comment

by:HackLife
ID: 13507917
' StopScript.vbs - Joseph B. Nguyen (HackLife at gmail.com)
Dim Re
Dim Fso

Set Re = New RegExp
Set Fso = CreateObject("Scripting.FileSystemObject")
Re.Global = True
Re.IgnoreCase = True

Txt = Fso.OpenTextFile("DirectorySync.ini", 1, False).ReadAll
Re.Pattern = "RunForever\s*=\s*True[^\w\s]*"
NewTxt = Re.Replace(Txt, "RunForever = False")
Fso.OpenTextFile("DirectorySync.ini", 2, False).Write NewTxt
Re.Pattern = "Pause\s*=\s*[\w]*[^\n]*"
Set Match = Re.Execute(Txt)
Sec = Trim(Right(Match(0), Len(Match(0)) - InstrRev(Match(0),"=") ))
Msgbox "The script is set to stop in " & CInt(Sec) & " seconds." & vbNewLine & vbNewLine & NewTxt

Set Match = Nothing
Set Fso = Nothing
Set Re = Nothing
0
 
LVL 4

Expert Comment

by:HackLife
ID: 13507923
' Directory Sync - Joseph B. Nguyen (HackLife at gmail.com)
Dim DS
Set DS = New DirSync
Do
      DS.CopyAll Dir1, Dir2
      DS.CopyAll Dir2, Dir1
      DS.ReInit
      WScript.Sleep Pause * 1000
Loop Until RunForever <> True
Msgbox "Script is finished."
Set DS = Nothing

' ***********************************************************************
Class DirSync
      Private Start
    Private Fso, Lg
      Public Arc1, Arc2
      Private FilesCompared
      Private DirSearched
    Private Sub Class_Initialize()
            Start = Timer
        Set Fso = CreateObject("Scripting.FileSystemObject")
            Set Lg = Fso.OpenTextFile(Fso.GetBaseName(WScript.ScriptFullName) & ".log", 8, True)
            EventLog "Script started."
            If Fso.FileExists("DirectorySync.ini") Then _
                  ExecuteGlobal Fso.OpenTextFile("DirectorySync.ini", 1, False).ReadAll
            If IsNull(Dir1) Or IsNull(Dir2) Then WScript.Quit
            Arc1 = AddSlash(Dir1) & "_Archive"
            Arc2 = AddSlash(Dir2) & "_Archive"
            CreateFolders(Arc1)
            CreateFolders(Arc2)
      End Sub

      Public Sub ReInit
            If Fso.FileExists("DirectorySync.ini") Then _
                  ExecuteGlobal Fso.OpenTextFile("DirectorySync.ini", 1, False).ReadAll
      End Sub

    Public Sub CopyFiles(D1, D2)
            On Error Resume Next
            If Not Fso.FolderExists(D1) Then CreateFolders(D1)
            If Not Fso.FolderExists(D2) Then CreateFolders(D2)
        If Fso.FolderExists(D1) Then
            If Fso.FolderExists(D2) Then
                  Set Files = Fso.GetFolder(D1).Files
                For Each F1 In Files
                    F2 = AddSlash(D2) & F1.Name
                              FilesCompared = FilesCompared + 1
                    If Fso.FileExists(F2) Then
                        If Fso.GetFile(F1).DateLastModified > Fso.GetFile(F2).DateLastModified Then
                                          EventLog "Move file: " & F2 & " >> " & AddSlash(Arc2) & Serialize(F1.Path)
                                          Fso.MoveFile F2, AddSlash(Arc2) & Serialize(F1.Path)
                                          EventLog "Copy file: " & F1 & " > " & F2
                                          Fso.CopyFile F1, F2, False
                        ElseIf Fso.GetFile(F1).DateLastModified < Fso.GetFile(F2).DateLastModified Then
                                          EventLog "Move file: " & F1 & " >> " & AddSlash(Arc1) & Serialize(F1.Path)
                                          Fso.MoveFile F1, AddSlash(Arc1) & Serialize(F1.Path)
                                          EventLog "Copy file: " & F2 & " > " & F1
                                          Fso.CopyFile F2, F1, False
                        End If
                    Else
                                    EventLog F1 & " > " & F2
                        Fso.CopyFile F1, F2
                    End If
                Next
            End If
        End If
            If Err Then EventLog "Error: " & Err.Description
            On Error Goto 0
    End Sub

      Public Function Serialize(F)
            If Fso.FileExists(F) Then
                  Dim Yr, Mo, Dy, Hr, Mi, Se, Ex, Base
                  ModDate = Now 'Fso.GetFile(F).DateLastModified
                  Yr = Year(ModDate)
                  Mo = Month(ModDate)
                  Dy = Day(ModDate)
                  Hr = Hour(ModDate)
                  Mi = Minute(ModDate)
                  Se = Second(ModDate)
                  Ex = Right(F,Len(F)-InStrRev(F,"."))
                  Base = Fso.GetBaseName(F)'Left(F,InStrRev(F,"."))
                  Serialize = Base & "_" & Yr & Mo & Dy & Hr & Mi & Se & "." & Ex
            Else
                  Serialize = ""
            End If
      End Function

    Public Sub CopyAll(D1, D2)
            DirSearched = DirSearched + 1
            Dim S1, S2
        If Fso.FolderExists(D1) Then
            CopyFiles D1, D2
            Set SubDirs = Fso.GetFolder(D1).SubFolders
            For Each S1 in SubDirs
                S2 = AddSlash(D2) & S1.Name
                If Fso.FolderExists(S2) Then
                      CopyAll S1, S2
                        Else
                              EventLog "Copy folder: " & S1 & " > " & S2
                              Fso.CopyFolder S1, S2
                        End If
            Next
            ElseIf Fso.FolderExists(D2) Then
                  EventLog "Copy folder: " & D2 & " > " & D1
                  Fso.CopyFolder D2, D1
        End If
    End Sub

      Public Function AddSlash(D)
            AddSlash = D
            If Right(D,1) <> "\" Then AddSlash = D & "\"
      End Function

    Public Sub CreateFolders(Path)
            Dim SN, Dirs, Dir, F
            Dirs = Split(Trim(Path),"\")
            If Left(Path,2) = "\\" Then
                  F = "\\"
            ElseIf InStr(Path,":") = 0 Then
                  SN = WScript.ScriptFullName
                  F = Left(SN,InStrRev(SN,"\"))
            End If
            On Error Resume Next
            For Each Dir In Dirs
                  If Trim(Dir) <> "" Then
                        F = F & Dir
                        If Not Fso.FolderExists(F) Then
                              Fso.CreateFolder(F)
                              EventLog "Created folder: " & F
                        End If
                        F = F & "\"
                  End If
            Next
            On Error Goto 0
      End Sub

      Public Sub EventLog(Msg)
            Lg.WriteLine(Date & ", " & Time & ", " & Msg)
      End Sub

    Private Sub Class_Terminate()
            EventLog FilesCompared & " file comparisons performed."
            EventLog DirSearched & " directories searched."
            EventLog "Script ran for " & Timer - Start & " seconds."
            Set Lg = Nothing
        Set Fso = Nothing
    End Sub
End Class
' ***********************************************************************
0
 
LVL 4

Expert Comment

by:HackLife
ID: 13507973
It doesn't move to "monthYear" folder but it does archive them into archive folders. Point it to a folder that is not it's own subdirectory. Set the Pause to 86400 <-- = 24 hours. and It will archive every 24 hours. YOu have to set it to run continuously.
0
 
LVL 4

Expert Comment

by:HackLife
ID: 13508048
Oops... change this in the config.vbs file....

Ini = Ini & "Dir1 = " & D(0) & vbNewLine
Ini = Ini & "Dir2 = " & D(1) & vbNewLine

To

Ini = Ini & "Dir1 = """ & D(0) & "" & vbNewLine
Ini = Ini & "Dir2 = """ & D(1) & "" & vbNewLine
0
 
LVL 4

Expert Comment

by:HackLife
ID: 13508058
And this

Ini = Ini & "RunForever = " & RunForever & vbNewLine

To

Ini = Ini & "RunForever = """ & RunForever & "" & vbNewLine
0
 
LVL 4

Accepted Solution

by:
HackLife earned 1400 total points
ID: 13508124
I'm sorry. Let me post the darn thing again:

Change this in Config.vbs

Ini = Ini & "Dir1 = " & D(0) & vbNewLine
Ini = Ini & "Dir2 = " & D(1) & vbNewLine
Ini = Ini & "Pause = " & CStr(Pause) & vbNewLine
Ini = Ini & "RunForever = " & RunForever & vbNewLine

To this

Ini = Ini & "Dir1 = " & Chr(34) & D(0) & Chr(34) & vbNewLine
Ini = Ini & "Dir2 = " & Chr(34) & D(1) & Chr(34) & vbNewLine
Ini = Ini & "Pause = " & CStr(Pause) & vbNewLine
Ini = Ini & "RunForever = " & Chr(34) & RunForever & Chr(34) & vbNewLine
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

752 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