Jump to content
Sign in to follow this  

Automated backup script questions

Recommended Posts


OK, I've tried out a few free backup options, and none of them do what I want. So........ I made my own with autoit! Anyways, I am running into a slight problem when using dircopy and filecopy. I myself will really only use this script to backup entire directories, but I want to make it possible to back up individual files as well. The problem is, when you use dircopy on a file, or filecopy on a dir, it gives back an error. I've included my script below, and you can see that I made a _Copy function to try doing a dircopy, then filecopy if there was an error, but this is not working correctly. Is there a function or something that will copy something, no matter if it is a file or a dir??

Other than that question, if anyone has any suggestions for optimizing/prettifying my code, please let me know. I'm working on a gui for making the .ini file now, and as soon as that is done I'll post this over in example scripts for further review. Just wanted to get some first impressions first :-)

;MsgBox(0, "start", "starting!")

    Backs up directorys fine... does includes and excludes perfectly for directories.
    I can do the same for individual files.... but is it really worth it?  I'd kind of rather move onto the gui for creating the ini file.  Maybe once I do that I'll come back and do files
    Files should be pretty much the same as directories, just change dircopy to filecopy
#include <File.au3>
#include <Array.au3>
#include <Date.au3>

While 1

;~ MsgBox(0, '', "The wday is: " & @WDAY)
    $ini = @ScriptDir & "\backup.ini"
    $wday = IniRead($ini, "Schedule", "wday", 1)
    $hour = IniRead($ini, "Schedule", "hour", 17)
    $runflag = IniRead($ini, "Schedule", "RunFlag", 1)
;~ MsgBox(0, "starting", $wday)
    If $hour == @HOUR Then ;need to change this to @wday or @mday depending on how often i want it to run...
        If $runflag == 1 Then
;~ MsgBox(0, "starting", "should call backup() now...")

            $backup = Backup($ini)
            If $backup == 0 Then ;if an error with the backup, then try again
                $backup = Backup($ini)
                If $backup == 1 Then ;if the second time is succesful, set the flag to 0 so it wont run again
                    $runflag = 0
                Else ;but if it fails again, set the flag to 1, so it will try to run again in the next period of time
                    $runflag = 1

                $runflag = 0
            IniWrite($ini, "Schedule", "RunFlag", $runflag)
;~      MsgBox(0, "not running backup", "not right day, or already run today!", 1)
        IniWrite($ini, "Schedule", "RunFlag", 1)


Func Backup($ini)
    $BackupDest = IniRead($ini, "Destination", "Dest", "ERROR")
    $BackupSource = IniRead($ini, "Files", "Source", "ERROR")
    ;MsgBox(0, "location", $BackupDest)
    $file = _FileCreate($BackupDest & "\btest.txt")
;~ MsgBox(0, "fileopen", $file)
    $del = FileDelete($BackupDest & "\btest.txt")
;~ MsgBox(0, "fileopen", $del)
    $BackupDest = $BackupDest & "\" & @YEAR & "-" & @MON & "-" & @MDAY
    ;$drive = DriveStatus("j:\")
    ;MsgBox(0, "J", $drive)
    If $file > 0 Then
        $FileList = _FileListToArray($BackupSource, "*", 2)
        If @error = 1 Then
            MsgBox(0, "ERROR", "No Files\Folders Found to backup.  Check .ini file")
        ;_ArrayDisplay($FileList, "$FileList")
        ;Do specific includes here
        $includeFile = IniReadSection($ini, "Include")
        _ArrayDisplay($includeFile, "$FileList")
        If IsArray($includeFile) Then

            $i = 1
            While $i < $includeFile[0][0] + 1
                ;now need to add in includes and excludes
                $copy = DirCopy($includeFile[$i][1], $BackupDest & "\IncludeFiles\", 1)
;~      $copy = FileCopy("C:\Documents and Settings\Rick\My Documents\My Music\iTunes\iTunes Music\Podcasts\Escape Pod\EP97_ Cinderella Suicide.mp3", "J:\external\backups\autoit\test\IncludeFiles\", 9)
;~      MsgBox(0, "Copied", $copy & $includeFile[$i][1])
                $i = $i + 1

        ;Process regular source to dest copying here, excluding excludes
        $ExcludeFile = IniReadSection($ini, "Exclude")
;~  _ArrayDisplay($ExcludeFile, "$FileList")
        $i = 1
        While $i < $FileList[0] + 1
            ;now need to add in includes and excludes
            $source = $BackupSource & "\" & $FileList[$i]
;~      MsgBox(0, "Preparing to test source", $source)
            $ii = 1
            $exit = 1
            If IsArray($ExcludeFile) Then
                While $ii < $ExcludeFile[0][0] + 1
;~          MsgBox(0, "testing against", $ExcludeFile[$ii][1])
                    If $source == $ExcludeFile[$ii][1] Then
;~              MsgBox(0, "Source should be excluded!", $source)
                        $exit = 0
                    $ii = $ii + 1
            ;if source is not in exclude array, then
            If $exit == 1 Then
                $copy = DirCopy($source, $BackupDest & "\" & $FileList[$i], 1)
;~          MsgBox(0, "Copied", $copy & $FileList[$i])
            $i = $i + 1
        IniWrite($ini, "Time", "LastSuccessfulBackup", _Now())
        MsgBox(0, "Complete", "Sucessfuly backed up to: " & $BackupDest, 3)
        Return 1
        IniWrite($ini, "Time", "LastFailedTry", _Now())
        MsgBox(0, "Backup ERROR", "Destination not ready! Please connect and try again.  Destination set to: " & $BackupDest)
        Return 0

EndFunc   ;==>Backup

Func _Copy($fsource, $fdest)
    $copy = 0
    $r = "ERROR"
    $copy = DirCopy($fsource, $fdest, 0)
    If $copy == 0 Then
        $copy = FileCopy($fsource, $fdest, 8)
        If $copy == 0 Then
            $r = "error"
            $r = "FileCopy"
        $r = "DirCopy"
    Return $r
EndFunc   ;==>_CopyoÝ÷ ٩ݶ¬jjez)â~)^¶­!©®²)àjëh×6[Destination]
Source=C:\Documents and Settings\Rick\My Documents

1=C:\Documents and Settings\Rick\My Documents\My Music
2=C:\Documents and Settings\Rick\My Documents\My Pictures

C:\Documents and Settings\Rick\My Documents\My Music\Podcasts\Escape Pod\EP97_ Cinderella Suicide.mp3

LastFailedTry=8/7/2008 3:29:21 PM
LastSuccessfulBackup=8/7/2008 5:36:42 PM

wday = 5
hour = 17

Share this post

Link to post
Share on other sites

You could use FileGetAttrib() to check if it's a directory or not, or copy with xcopy.exe that follows Windows, it's really powerful.

Edited by AdmiralAlkex

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.