Sign in to follow this  
Followers 0
JohnV

Newbie Help with Backup Script

4 posts in this topic

Hello, this is my first attempt at scripting AutoIT and trying to utilize AutoIT's Gui.

I am stuck with a routine in the backup script that I'm writing. I got the initial backup buttons to work, but my restore buttons do not run to completion and does not give an error or copy any files. I get the initial prompts but the restore function does not run.

My only clue is the RestoreData function?

Please help a newbie.

P.S. If there is a way to detect the usb drive automatically or detect and grab the drive where the autoit app is run from, I would appreciate it. If there is a way to optimize or make the script run more efficiently then please give suggestions. thank you

#cs
    Migration TOOL
    Author: JohnV
    Date: 09/22/2010
    Description: Used to backup users common data.
    Version: 0.1 - Initial Programming of Code to do the Backup Process
#ce

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <Process.au3> ; Required for the _RunDOS command

;..............VARIABLE DECLARATION...........................
Global $strComputer = "."
Global $userStore; Location of users profile.
Global $pstStore
Global $pstCfg
Global $backupStore
Global $pstSavStr = $backupStore & "\OutlookStore" ; Outlook store location
Global $pstSavCfg = $backupStore & "\OutlookCFG" ; Outlook config location
Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2") ; Creates a connection to WMI
Global $OS = @OSVersion ; OS Version Detection
Global $myDocs
Global $myPics

;........................OPERATING SYSTEM DETECTION................................
Switch $OS ; A Switch statement. We are going to make the value returned by @OSVersion more readable.

    Case "WIN_VISTA"
        ;   $OS = "Windows Vista"
        #RequireAdmin ;Requires Admin Authentication for writing to registry
        $userStore = "C:\Users\" & @UserName
        $pstStore = $userStore & "\AppData\Local\Microsoft\Outlook"
        $pstCfg = $userStore & "\AppData\Roaming\Microsoft\Outlook"
        $myDocs = "\Documents"
        $myPics = "\Pictures"
    Case "WIN_XP"
        ;   $OS = "Windows XP"
        $userStore = "C:\Documents and Settings\" & @UserName
        $pstStore = $userStore & "\Application Data\Microsoft\Outlook"
        $pstCfg = $userStore & "\Local Settings\Application Data\Microsoft\Outlook"
        $myDocs = "\My Documents"
    Case "WIN_2000"
        ;   $OS = "Windows 2000"
        $userStore = "E:\User Profiles\" & @UserName
        $pstStore = $userStore & "\Application Data\Microsoft\Outlook"
        $pstCfg = $userStore & "\Local Settings\Application Data\Microsoft\Outlook"
        $myDocs = "\My Documents"
    Case Else
        ;   $OS = "Unknown!"
        MsgBox(0, "Error", "Unknown Operating System")
        Exit
EndSwitch

;.....................LOCATION OF BACKUP FOLDERS............................
Global $Data[2] ; ADD/DELETE FOLDERS Based on needs.........................
$Data[0] = "\Favorites"
$Data[1] = "\Desktop"
;$Data[2] = $myDocs
;$Data[3] = $myPics

#Region ### START Koda GUI section ### Form=e:\repairtools\general tools\autoit\scite\koda\forms\databackupform.kxf
$frmMigration = GUICreate("Migration Tool", 205, 125, 192, 124)
$mainTab = GUICtrlCreateTab(8, 8, 188, 112)
$tbBackup = GUICtrlCreateTabItem("Backup")
$Label4 = GUICtrlCreateLabel("Data Backup", 48, 40, 95, 22)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")
$btnBusb = GUICtrlCreateButton("USB Drive", 16, 88, 83, 25)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$btnBnetwork = GUICtrlCreateButton("Network", 104, 88, 83, 25)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$Label2 = GUICtrlCreateLabel("Where to backup the DATA?", 16, 64, 165, 17)
GUICtrlSetFont(-1, 8, 400, 0, "Verdana")
GUICtrlSetColor(-1, 0xFF0000)
$tbRestore = GUICtrlCreateTabItem("Restore")
$Label1 = GUICtrlCreateLabel("Data Restore", 48, 40, 95, 22)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")
$Label3 = GUICtrlCreateLabel("Restore DATA from Where?", 16, 64, 162, 17)
GUICtrlSetFont(-1, 8, 400, 0, "Verdana")
GUICtrlSetColor(-1, 0xFF0000)
$btnRusb = GUICtrlCreateButton("USB Drive", 16, 88, 83, 25)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$btnRnetwork = GUICtrlCreateButton("Network", 104, 88, 83, 25)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
GUICtrlCreateTabItem("")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1 ; GUI Interface
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE ; Closes the App
            Exit
        Case $btnBnetwork ; Use the Backup - Network Button
            GUISetState(@SW_HIDE) ;Hides the GUI and starts the process
            $backupStore = "\\server\sharedrive\" & @UserName ; Destination of backup.
            Call("Backup")
        Case $btnBusb ; Use the Backup - USB Button
            GUISetState(@SW_HIDE) ;Hides the GUI and starts the process
            $usbDrive = InputBox("USB Drive Location", "Please enter drive letter of USB" & @CRLF & _
            'Drive! EX:"e"', "e","",190, 135 )
            $backupStore = $usbDrive & ":\UserBackup\" & @UserName
            Call("Backup")
        Case $btnRnetwork ; Use the Restore - Network Button
            GUISetState(@SW_HIDE)
            $backupStore = "\\mspj8100959\storage$\" & @UserName ; Destination of backup.
            Call("Restore")
        Case $btnRusb ; Use the Restore - USB Button
            GUISetState(@SW_HIDE) ;Hides the GUI and starts the process
            $usbDrive = InputBox("USB Drive Location", "Please enter drive letter of USB" & @CRLF & _
            'Drive! EX:"e"', "e","",190, 135 )
            $backupStore = $usbDrive & ":\UserBackup\" & @UserName
            Call("Restore")
    EndSwitch
WEnd

;..........................START OF FUNCTIONS....................................
Func Backup() ; Backup Instructions
;......................START PROCESS OF HANDLING BACKUP PROCEDURES................
   $AnswerMain = MsgBox(36, "User Backup Tool Ver 0.1", "Please SHUTDOWN all open" & @CRLF & _
   "applications and files before continuing!" & @CRLF & _
    "While the backup is running," & @CRLF & _
    "DO NOT Shutdown or Disconnect the PC." & @CRLF & _
    "If you agree to this, Please Click 'YES'.")

    If $AnswerMain = 6 Then
        Call("CloseOutlook") ; Closes Outlook if it is open.
        Call("BackupData") ; Run the Data Copy Routine
        Call("Printers") ; Grabs the Network Printers Settings.
        Call("NetworkDrive") ; Grabs the Network Drive Settings.
        _RunDOS("regedit.exe /e " & $backupStore & '\outlook.reg "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem"') ;Backup Outlook Settings
        _RunDOS("regedit.exe /e " & $backupStore & '\ODBC_Machine.reg "HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI"') ;Backup Machine ODBC Settings
        _RunDOS("regedit.exe /e " & $backupStore & '\ODBC_User.reg "HKEY_CURRENT_USER\Software\ODBC\ODBC.INI" ') ;Backup User ODBC Settings
        MsgBox(16, "BACKUP STATUS", "Backup Completed Successfully!")
        Exit
    Else
        Exit
    EndIf
EndFunc

Func Restore() ; Restore Instructions
    $AnswerRestore = MsgBox(36, "User Retore Tool Ver 0.1", "Please don't run any applications" & @CRLF & _
    "while the data is being restored.")

    If $AnswerRestore = 6 Then
        Call("RestoreData") ; Run the Data Copy Restore Routine
        _RunDOS("regedit.exe /s " & $backupStore & "\ODBC_Machine.reg") ;Backup Machine ODBC Settings
        _RunDOS("regedit.exe /s " & $backupStore & "\ODBC_User.reg") ;Backup User ODBC Settings
        MsgBox(16, "RESTORE STATUS", "Restore Completed Successfully!")
        Exit
    Else
        Exit
    EndIf
EndFunc

Func BackupData() ;Routine to run the migration process
    Local $answer
    Local $i

    If FileExists($backupStore) Then
        $answer = MsgBox(36, "Old Backup Data", "Old Data Exists, Do you want to delete?")
        If $answer = 6 Then
            DirRemove($backupStore, 1)
            DirCreate($backupStore)
            For $i = 0 To UBound($Data) - 1
                DirCopy($userStore & $Data[$i], $backupStore & $Data[$i], 1)
            Next
            DirCopy($pstStore, $pstSavStr, 1) ; Copy Outlook location
            DirCopy($pstCfg, $pstSavCfg, 1)
        Else
            Exit
        EndIf
    Else
        DirCreate($backupStore)

        For $i = 0 To UBound($Data) - 1
            DirCopy($userStore & $Data[$i], $backupStore & $Data[$i], 1)
        Next
        DirCopy($pstStore, $pstSavStr, 1) ; Copy Outlook location
        DirCopy($pstCfg, $pstSavCfg, 1)
    EndIf
EndFunc   ;==>Migrate

Func RestoreData() ; Routine to run the restore process
    Local $j

    If FileExists($backupStore) Then
        For $j = 0 To UBound($Data) - 1
            DirCopy($backupStore & $Data($j), $userStore & $Data($j), 1)
        Next
            DirCopy($pstSavStr, $pstStore, 1)
            DirCopy($pstSavCfg, $pstCfg, 1)
    Else
        MsgBox(0,"No Migration Data Exits", "USER DATA DOES NOT EXISTS AT THIS LOCATION!!" & @CRLF & _
        "Please check if its located at another SOURCE" & _
        @CRLF & "OR Backup Users Data before continuing!")
        Exit
    EndIf
EndFunc

Func CloseOutlook() ; Routine to Close Outlook Apps
    ProcessClose("OUTLOOK.EXE")
    $PID = ProcessExists("OUTLOOK.EXE") ; Will return the PID or 0 if the process isn't found.
    If $PID Then ProcessClose($PID)
EndFunc   ;==>CloseOutlook

Func Printers() ; Querys network Printers and puts in text file

    Const $DEFAULT = 4 ; Sets Default Printer
    ;Local $objWMIService = ObjGet ("winmgmts:\\" & $strComputer & "\root\cimv2") ;Disabled as using Global Variable
    Local $colPrinters = $objWMIService.ExecQuery("Select * From Win32_Printer")
    Local $FilePrinter = FileOpen($backupStore & "\Printers.txt", 1)

    If $FilePrinter = -1 Then
        MsgBox(0, "Error", "Unable to Open File.")
        Exit
    EndIf
    FileOpen($FilePrinter)

    For $objPrinter In $colPrinters
        If $objPrinter.Attributes And $DEFAULT Then
            If $objPrinter.ShareName <> "" Then
                FileWriteLine($FilePrinter, $objPrinter.Name & ",")
            EndIf
        EndIf
    Next

    FileClose($FilePrinter)

EndFunc   ;==>Printers

Func NetworkDrive() ; Querys network Drive and Puts the locations in a txt file

    Local $colDrives = $objWMIService.ExecQuery("Select * From Win32_MappedLogicalDisk")
    Local $FileNetwork = FileOpen($backupStore & "\NetworkDrives.txt", 1)

    If $FileNetwork = -1 Then
        MsgBox(0, "Error", "Unable to Open File.")
        Exit
    EndIf

    FileOpen($FileNetwork)

    For $objDrive In $colDrives
        If $objDrive.Name <> "" Then
            FileWrite($FileNetwork, $objDrive.Name)
            FileWriteLine($FileNetwork, $objDrive.ProviderName & ",")
        EndIf
    Next
    FileClose($FileNetwork)

EndFunc   ;==>NetworkDrive

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Couple things I noticed, you will want to use $Data[$j] not $Data($j)

and I don't know if this will change anything but I've always done

For $j=1 to Ubound($Data)-1

instead of $j=0

*edit*

I've seen a number of posts on the forum about detecting a USB drive, but never really looked at them as I have had no need for it (yet).

*another edit*

I see why you use 0 now, you start the data in $Data[0]... you can ignore that part.

Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

Couple things I noticed, you will want to use $Data[$j] not $Data($j)

and I don't know if this will change anything but I've always done

For $j=1 to Ubound($Data)-1

instead of $j=0

*edit*

I've seen a number of posts on the forum about detecting a USB drive, but never really looked at them as I have had no need for it (yet).

*another edit*

I see why you use 0 now, you start the data in $Data[0]... you can ignore that part.

Thank you for your input. That 2nd set of eyes really helped. It seems to be working now, I did not even see the ()

Share this post


Link to post
Share on other sites

I added this bit to get it working on Windows 7, so i could see it.

Win7 and Vista been very similar folder structure

Case "WIN_7"
        ;   $OS = "Windows 7"
        #RequireAdmin ;Requires Admin Authentication for writing to registry
        $userStore = "C:\Users\" & @UserName
        $pstStore = $userStore & "\AppData\Local\Microsoft\Outlook"
        $pstCfg = $userStore & "\AppData\Roaming\Microsoft\Outlook"
        $myDocs = "\Documents"
        $myPics = "\Pictures"

One suggestion i thought off was have a button to backup to desktop in addition to the other two.

Nice piece of work

Chimaera

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0