Sign in to follow this  
Followers 0
aharrold

Beginner Requires Pointers...

4 posts in this topic

I'm not after someone to complete my script but would prefer pointers as to what commands/functions to use to meet my objectives. Never written a script before and am quite pleased that this one actually works and does what I want in principle but I'd like to refine and ensure it runs properly on my server in a live environment.

The script basically runs NTBackup on the server and backs up the systemstate to local disk, file is renamed to include date format at the end, file is zipped using 7-zip, original *.bkf file is deleted, zipped file is copied to created share on Q: drive, script checks I only have 5 dated copies on both local and central server and deletes any others, updates local txt file with result.

Any help pointers would be much appreciated.

Cheers

; Create Share to Central Server -

;

;Run System State Backup

Run("ntbackup backup systemstate /J sys-state /F d:\sysstatebackups\backup1.bkf")

; Now wait for Backup to close before continuing

ProcessWaitClose("ntbackup.exe")

;Ideally need a rename file entry here to append date format onto file (backup02062008.bkf)

;Zip file here using 7-ZIP and delete .bkf file

;Map Q drive to Central Server

DriveMapAdd ( "Q:", "\\192.168.0.100\systemstate\")

;Copy System State to Central Server

FileCopy("D:\sysstatebackups\backup*.bkf", "Q:\")

;Check number of instances of *.zip files in folder and delete oldest leaving 5 copies

;Same as line above but on Central Server

;Write results to a text file if successful or a failure, appended in date format saved on server being backed up.

; Disconnect Share

;DriveMapDel("Q:")

Share this post


Link to post
Share on other sites



You can use FileMove() to rename the file, just move it to the same directory with the original name with the date added at the end of the string (before the file extension).

FileMove("d:\sysstatebackups\backup1.bkf", "d:\sysstatebackups\backup" & @MON & @MDAY & @YEAR & ".bkf")

For your *.zip file search, look in the help file for FileFindFirstFile() and FileFindNextFile() functions.

$search = FileFindFirstFile("*.zip")
While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    MsgBox(4096, "File:", $file)
WEnd
FileClose($search)

FileOpen(), FileWrite() and FileWriteLine() can be used to create your log file.

$logfile = FileOpen("log.txt", 1)
FileWriteLine($logfile, "Successful backup at " & @HOUR & ":" & @MIN & " on " & @MON & "/" & @MDAY & "/" & @YEAR)
FileClose($logfile)

Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

Share this post


Link to post
Share on other sites

Thanks for the pointer, managed to add the following code that tested and done the job...... :)

;Renames File to Date Format

$fName = "backup.bkf"

If FileExists ($fName) Then FileMove ($fName, @MDay & @Mon & @Year & ".bkf", 1)

You can use FileMove() to rename the file, just move it to the same directory with the original name with the date added at the end of the string (before the file extension).

FileMove("d:\sysstatebackups\backup1.bkf", "d:\sysstatebackups\backup" & @MON & @MDAY & @YEAR & ".bkf")

For your *.zip file search, look in the help file for FileFindFirstFile() and FileFindNextFile() functions.

$search = FileFindFirstFile("*.zip")
While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    MsgBox(4096, "File:", $file)
WEnd
FileClose($search)

FileOpen(), FileWrite() and FileWriteLine() can be used to create your log file.

$logfile = FileOpen("log.txt", 1)
FileWriteLine($logfile, "Successful backup at " & @HOUR & ":" & @MIN & " on " & @MON & "/" & @MDAY & "/" & @YEAR)
FileClose($logfile)

Share this post


Link to post
Share on other sites

Here's what I have so far... In principle it creates the backup, zip's the file and copies to my shared drive, although some time's it will fail for some unknown reason at any of the tasks hence why I was trying to get each task to log. The log file is created and entries are being written to it but both Failure and Success are logged per task. I cant seem to find much on the use of the Return Codes provided by each task apart from looking through example and others codes.... Any pointers/help much appreciated, also any tutorial sites/books/ebooks on Autoit scripting would be great too...

; Script Function: Take Systemstate Backup and copy to remote server

;Create Logfile for task
$file = FileOpen("d:\sysstatebackups\" & @MDay & @Mon & @Year &".txt", 1)

    ; Check if file opened for writing OK
     If $file = -1 Then
         MsgBox(0, "Error", "Unable to open file.")
         Exit
     EndIf

;Run System State Backup
$backup = Run("ntbackup backup systemstate /J sys-state /F d:\sysstatebackups\backup.bkf")
    if $backup = 0 Then
        FileWriteLine($file, "Backup Failed - " & @HOUR & ":" & @MIN & " on " & @MON & "/" & @MDAY & "/" & @YEAR)
        Exit
    EndIf
; Now wait for Backup to close before continuing
    ProcessWaitClose("ntbackup.exe")

;Log Successful backup to systemstate.txt file
    FileWriteLine($file, "Backup Successful - " & @HOUR & ":" & @MIN & " on " & @MON & "/" & @MDAY & "/" & @YEAR)


;Renames File to Date Format
;   $fName = "d:\sysstatebackups\backup.bkf"
;       If FileExists ($fName) Then FileMove ($fName, @MDay & @Mon & @Year & ".bkf", 1)
    
;Zip file here using 7-ZIP and delete .bkf file
$zip = Run("7z a d:\sysstatebackups\ d:\sysstatebackups\*.bkf")
    if $zip = 0 Then
        FileWriteLine($file, "Compression Failed - " & @HOUR & ":" & @MIN & " on " & @MON & "/" & @MDAY & "/" & @YEAR)
    EndIf
    FileWriteLine($file, "Compression Successful - " & @HOUR & ":" & @MIN & " on " & @MON & "/" & @MDAY & "/" & @YEAR)

;Map Q drive to Central Server 
    DriveMapAdd ( "Q:", "\\192.168.0.11\systemstate\")

;Copy System State to Central Server
$CopyQ = FileCopy("D:\sysstatebackups\@MDay & @Mon & @Year &.txt", "Q:\")
    if $CopyQ = 0 Then
        FileWriteLine($file, "Transfer to Central Failed - " & @HOUR & ":" & @MIN & " on " & @MON & "/" & @MDAY & "/" & @YEAR)
    EndIf
    FileWriteLine($file, "Transfer to Central Successful - " & @HOUR & ":" & @MIN & " on " & @MON & "/" & @MDAY & "/" & @YEAR)

;Check number of instances of *.zip files in folder and delete oldest leaving 5 copies
;Same as line above but on Central Server
;Write results to a text file if successful or a failure, appended in date format saved on server being backed up.

; Disconnect Share
;DriveMapDel("Q:")

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