Jump to content

Writing a Log


Nunos
 Share

Go to solution Solved by Subz,

Recommended Posts

I am making a script to backup a SQL DB and I want to write the contents of the results to a file but I think my glasss are fogging up because I am struggling to make it work.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Untitled - 1.ico
#AutoIt3Wrapper_Outfile=Dinerware Backup Utility.exe
#AutoIt3Wrapper_Outfile_x64=Dinerware Bacukup Utilityx64.exe
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_Fileversion=0.0.0.4
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;#RequireAdmin
#include <MsgBoxConstants.au3>
#include <Constants.au3>
#include <Array.au3>
#include <file.au3>
#include <date.au3>
#include <GUIConstants.au3>

Global $Source = '', $Destination = ''


$LogFileName = @AppDataDir & "\IDS\Backup.log"
If Not FileExists("C:\DWBackup\DWBackupLog.log") Then DirCreate("C:\DWBackup\")                     ;===> Checks for base level file structure.
If Not FileExists(@AppDataDir & "\IDS\") Then DirCreate(@AppDataDir & "\IDS\")                      ;===> Checks for base level file structure.
If Not FileExists(@AppDataDir & "\IDS\DWBackup.ini") Then GUI()                                     ;===> Checks for base level file structure.

BackupCreate()                                                                                      ;===> Calls the BackupCreate function to start the backup process.

func BackupCreate()                                                                                 ;===> Creates the Files/Backup

$Server = IniRead(@AppDataDir & "\IDS\DWBackup.ini", "Server Name", "Name", "default")              ;===> Reads the .ini file created above, and sets the associated variables to the information stored. That way it can be piped into the sqlcmd
$DBName = IniRead(@AppDataDir & "\IDS\DWBackup.ini", "Database Name", "DbName", "default")          ;===> Reads the .ini file created above, and sets the associated variables to the information stored. That way it can be piped into the sqlcmd
$Destination = IniRead(@AppDataDir & "\IDS\DWBackup.ini", "Backup Destination", "Path", "default")  ;===> Reads the .ini file created above, and sets the associated variables to the information stored. That way it can be piped into the sqlcmd
$LogFile = IniRead(@AppDataDir & "\IDS\DWBackup.ini", "Log File Destination", "Path", "default")    ;===> Reads the .ini file created above, and sets the associated variables to the information stored. That way it can be piped into the sqlcmd

_log('========== Starting Dinerware Backup APP ==========')

Run(@ComSpec & ' /c sqlcmd -S ' & $Server & ' -E -Q "BACKUP DATABASE ' & $DBName & ' TO DISK=''' & $Destination & ''' WITH INIT" -o ' & $LogFile,@ScriptDir,@SW_HIDE)

;===> This is the cmd command that is ran through AutoIt. The variales (e.g. $Server) are read from the .ini file and pipes that information into the command where needed.


;===> Just a simple sleep timer for everything to get caught up
Sleep(5000)

;===> This is where the information that is wrapped in the "_log" functions comes from
FileOpen($LogFile,0)
$LogCopy = FileRead($LogFile)
FileClose($LogFile)
_log('Backup - ' & $LogCopy)        ;===> The information denoted above is inserted here

_exit()

endfunc

func _exit()                                                        ;===> This function appends the ending part of the wrap for the Logfile
    _log('********** Ending Dinerware Backup APP **********')       ;===> What gets appended to the logfile
    exit
endfunc

func _log($str)                                                     ;===> This is the function that creates the Backup Log for techs to see when backups were being ran
    local $hfl = fileopen($LogFileName,1)                           ;===> Creates the Logfile located in \AppData\Roaming\IDS\Backup.log and assigns the variable
    if $hfl = -1 Then                                               ;===> If an Error occurs, display this message
        msgbox(0,'','Log file failed to open.' & @crlf & 'File name = ' & $LogFileName)     ;===> Let's user know that the log didn't open
        Exit                                                        ;===> Exits script on error.
    EndIf
    filewrite($hfl,_now() & ' ' & $str & @CRLF)                     ;===> Writes the date, time, and the string that should be the name of the files.
    fileclose($hfl)                                                 ;===> Closes the file
endfunc

Func GUI()                                                                          ;===> This function handles the GUI aspect of this script in its current state.

   Local $msg
   Local $btnOkay
   Local $btnClose
   Local $btnBrowse

   $GUI = GUICreate("Dinerware Backup", 310, 280)                                   ;===> Will create a dialog box that when displayed is centered

   GUICtrlCreateLabel("Please input your server name starting with '.\'",15,25)     ;===> Label for the input box below to denote what your server name is.
    $inpBoxSrvName = GUICtrlCreateInput(".\DINERWARE", 15, 45, 150, 20)             ;===> Input Box for the server name, will be changed to a Combo Box once arrays are figured out. Currently prepopulates with standard server name.
   GUICtrlCreateLabel("Please input your database name", 15, 75)                    ;===> Label for the input box below to denote what your database name is.
    $inpBoxDbName = GUICtrlCreateInput("dinerware", 15, 95, 150, 20)                ;===> Input Box for for the database name, will be changed to a Combo Box once arrays are figured out. Currently prepopulates with standard database name.

   GUICtrlCreateLabel("Please select a destination for your backup", 15, 125)       ;===> Label for backup destination selection
    $btnBrowseDest = GUICtrlCreateButton("Browse", 235, 125, 65, 25)                ;===> Creation of browse button, further logic below
    $inpBoxDest = GUICtrlCreateInput("", 15, 145, 150, 20)                          ;===> Creates the input box where the information selected from "Browse" will be piped in. User can also type in the path if known to them.


   GUICtrlCreateLabel("Please select a destination of the log file", 15, 175)       ;===> Label for logfile destination selection
    $btnBrowseLog = GUICtrlCreateButton("Browse", 235, 175, 65, 25)                 ;===> Creation of browse button, further logic below
    $inpBoxLog = GUICtrlCreateInput("", 15, 195, 150, 20)                           ;===> Creates the input box where the information selected from "Browse" will be piped in. User can also type in the path if known to them.

    $btnOkay = GUICtrlCreateButton("Okay", 65, 245, 85, 25)                         ;===> Creation of "Okay" button. This will fire the rest of the script when clicked.
    $btnClose = GUICtrlCreateButton("Close", 165, 245, 85, 25)                      ;===> Creation of "Close" Button. This will exit out of the application entirely.

   GUISetState()                                                                    ;===> Enables GUI's to be shown

      While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE                                            ;===> When the "X" button is clicked, this exits the application
                Exit
            Case $msg = $btnBrowseDest
                ;===> Logic for when the Backup Destination Browse button is clicked. This will enable the user to select the folder they would like to designate to house their DW backups
               $Path = FileSelectFolder('Select destination for your backup.', 'C:\', 2, $Source, $GUI)
                  If $Path Then
                     GUICtrlSetData($inpBoxDest, $Path)                             ;===> This is what pipes the information from the file selection into the input box earlier.
                     $Source = $Path
                  EndIf
            Case $msg = $btnBrowseLog
                ;===> Same as above for the other browse button, but this is for the Logfile selection
               $Path = FileSelectFolder('Select destination for your log file.', 'C:\', 2, $Source, $GUI)
                  If $Path Then
                     GUICtrlSetData($inpBoxLog, $Path)                              ;===> Again as above, this is what will pipe the relevant information into the input box.
                     $Source = $Path
                  EndIf
            Case $msg = $btnOkay

                 ;===> This is where we get into the creation of the .ini file. This file will store all the pertinant information that will later be needed by the sqlcmd. This can also be used to store information for scheduling tasks in task scheduler.
                 IniWrite(@AppDataDir & "\IDS\DWBackup.ini", "Server Name", "Name", GUICtrlRead($inpBoxSrvName))
                 IniWrite(@AppDataDir & "\IDS\DWBackup.ini", "Database Name", "DbName", GUICtrlRead($inpBoxDbName))
                 IniWrite(@AppDataDir & "\IDS\DWBackup.ini", "Backup Destination", "Path", GUICtrlRead($inpBoxDest) & "\DWBackup.bak")      ;===> This is just appending "\DWBackup.bak" to the file selection selected above. If this wasn't here, it would look like this, "C:\DWBackup\". This is why we need the last bit, to direct the sqlcmd to output to the proper files
                 IniWrite(@AppDataDir & "\IDS\DWBackup.ini", "Log File Destination", "Path", GUICtrlRead($inpBoxLog) & "\DWBackupLog.log")  ;===> Same as previous line



                ExitLoop                ;===> This is an ExitLoop, in order to carry on with the next function (e.g. BackupCreate() ).
            Case $msg = $btnClose       ;===> Kills script
                Exit
        EndSelect
      WEnd

EndFunc

The section I am struggling with is...

;===> This is where the information that is wrapped in the "_log" functions comes from
FileOpen($LogFile,0)
$LogCopy = FileRead($LogFile)
FileClose($LogFile)
_log('Backup - ' & $LogCopy)

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...