Jump to content
Sign in to follow this  
Belini

Backup personalizado

Recommended Posts

Precisei fazer um programa de backup que atendesse as minhas necessidades e quero compartilhar com vocês.

#RequireAdmin
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=.\Bk.ico
#AutoIt3Wrapper_Compression=0
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Res_Comment=Função: Aplicativo para fazer backup
#AutoIt3Wrapper_Res_Description=Autoit3 3.3.9.4
#AutoIt3Wrapper_Res_Fileversion=1.1.10.18
#AutoIt3Wrapper_Res_LegalCopyright=Feito por Belini
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****

#cs -------------------------------------------------
    Title:          Backup Personal
    Objective:      Fazer backup de arquivos e pastas
    Author:         Belini (Brazil)
    Autoit Version: 3.3.9.4
    Version:        1.0.10.18
    Forum:          http://www.autoitbrasil.com/forum
#ce -------------------------------------------------

#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include "_UskinLibrary.au3"
#include <GuiListView.au3>
#include "Skin_03.au3"
#include <File.au3>

Global $iFix_Col = 1, $Dir_backup, $Input, $Listv, $Listv_Files, $Check, $lab[3]
Global $nMsg, $proceed = 1, $item_num, $But[8], $item_text[4], $mantidos = 0, $atualizados = 0

_Arq_ini()
_Uskin_LoadDLL()
_USkin_Init(_Skin_03(True)); Skin usada
_Gui_backup()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $But[1]
            _Add_Line()
        Case $But[2]
            _Del_line()
        Case $But[3]
            _Opt_Dir()
        Case $But[4]
            _Opt_Backup()
        Case $But[5]
            _Dir_Backup()
        Case $But[6]
            $proceed = 0
        Case $But[7]
            _Start_backup()
        Case $Check
            _Opt_Finalize()
    EndSwitch
WEnd

Func _Add_Line()
    Local $dir = FileSelectFolder("Procurando pasta.", "")
    If IniRead(@ScriptDir & "\Config.ini", "PASTAS", $dir, "") = "" Then
        GUICtrlCreateListViewItem($dir & "|Incluir Subpastas" & "|Sim", $Listv)
        IniWrite(@ScriptDir & "\Config.ini", "PASTAS", $dir, "|Incluir Subpastas" & "|Sim")
    Else
        MsgBox(4096, "", "Este diretório já foi escolhido!", 3)
    EndIf
EndFunc   ;==>_Add_Line

Func _Arq_ini()
    If Not FileExists(@ScriptDir & "\Config.ini") Then _
            FileWrite(@ScriptDir & "\Config.ini", "[BACKUP]" & @CRLF & "endereço= " & @CRLF & @CRLF & "[DESLIGAR]" & @CRLF & "opção=0" & @CRLF & @CRLF & "[PASTAS]")
EndFunc   ;==>_Arq_ini

Func _Backup_Files($path = "")
    Local $searh_files, $i
    $searh_files = _FileListToArray($path, "*.*", 1)
    If Not @error Then
        For $i = 1 To $searh_files[0]
            If GUIGetMsg() = $But[6] Then $proceed = 0
            If $proceed = 0 Then ExitLoop
            _Compare_files($path & "\" & $searh_files[$i])
        Next
    EndIf
EndFunc   ;==>_Backup_Files

Func _Backup_Full($path = "", $counter = 0)
    $counter = 0
    $path &= '\'
    Local $file, $searh_files
    $searh_files = FileFindFirstFile($path & '*')
    If $searh_files = -1 Then Return ''
    While 1
        If GUIGetMsg() = $But[6] Then $proceed = 0
        $file = FileFindNextFile($searh_files)
        If @error Or $proceed = 0 Then ExitLoop
        If @extended Then
            If $counter >= 10 Then ContinueLoop
            _Backup_Full($path & $file, $counter + 1)
        Else
            _Compare_files($path & $file)
        EndIf
    WEnd
    FileClose($searh_files)
EndFunc   ;==>_Backup_Full

Func _Compare_files($file = "")
    Local $File_backup, $item, $folder
    $item = StringTrimLeft($file, StringInStr($file, "\", 1, -1))
    $folder = StringTrimLeft($file, StringInStr($file, "\", 1, -2))
    $folder = StringMid($folder, 1, StringInStr($folder, "\", 1, -1) -1 )
    $File_backup = StringReplace($file, StringMid($file, 1, 2), $Dir_backup, 0, 2)
    $file = FileGetShortName($file)
    $File_backup = FileGetShortName($File_backup)
    _GUICtrlListView_SetItemText($Listv, 0, $folder, 2)
    _GUICtrlListView_SetItemText($Listv, 0, $item, 1)
    If FileGetTime($file, 0, 1) > FileGetTime($File_backup, 0, 1) Then
        $atualizados += 1
        FileCopy($file, $File_backup, 9)
        GUICtrlSetData($lab[2], "Atualizados: " & $atualizados)
    Else
        $mantidos += 1
        GUICtrlSetData($lab[1], "Mantidos: " & $mantidos)
    EndIf
EndFunc   ;==>_Compare_files

Func _Del_line()
    $item_num = _GUICtrlListView_GetSelectedIndices(GUICtrlGetHandle($Listv))
    $item_text[1] = _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), Number($item_num))
    _GUICtrlListView_DeleteItemsSelected(GUICtrlGetHandle($Listv))
    IniDelete(@ScriptDir & "\Config.ini", "PASTAS", $item_text[1])
EndFunc   ;==>_Del_line

Func _Dir_Backup()
    $Dir_backup = FileSelectFolder("Procurando pasta.", "", 1)
    ;GUICtrlSetState($Input, $GUI_FOCUS)
    GUICtrlSetData($Input, $Dir_backup)
    IniWrite(@ScriptDir & "\Config.ini", "BACKUP", "endereço", $Dir_backup)
EndFunc   ;==>_Dir_Backup

Func _Gui_backup()
    GUICreate("Backup Personal   -   Feito  Por  Belini", 660, 679, -1, -1)
    GUICtrlCreateGroup("Diretório para backup ", 352, 11, 290, 69)
    $But[1] = GUICtrlCreateButton("Incluir endereço", 15, 16, 140, 25)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $But[2] = GUICtrlCreateButton("Excluir endereço", 15, 56, 140, 25)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $But[3] = GUICtrlCreateButton("Arquivos / Subpastas", 170, 16, 170, 25)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $But[4] = GUICtrlCreateButton("Marcar / Desmarcar", 170, 56, 170, 25)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $But[5] = GUICtrlCreateButton("Procurar", 553, 36, 75, 25)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    $But[6] = GUICtrlCreateButton("Cancelar", 514, 631, 130, 33)
    GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
    $But[7] = GUICtrlCreateButton("Iniciar", 390, 631, 110, 33)
    GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
    $Input = GUICtrlCreateInput(IniRead(@ScriptDir & "\Config.ini", "BACKUP", "endereço", ""), 368, 38, 183, 21)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    $Check = GUICtrlCreateCheckbox("Desligar o computador quando finalizar a tarefa.", 15, 647, 352, 25)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")

    $lab[1] = GUICtrlCreateLabel("Mantidos: 0", 32, 623, 175, 25)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $lab[2] = GUICtrlCreateLabel("Atualizados: 0", 210, 623, 175, 25)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")

    $Listv = GUICtrlCreateListView("Endereço  das  Pastas|Listar  Arquivos|Fazer  Backup", 16, 96, 627, 520, _
            BitOR($LVS_SHOWSELALWAYS, $LVS_SINGLESEL), BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    GUICtrlSetColor(-1, 0x000000)
    _GUICtrlListView_SetColumnWidth($Listv, 0, 395)
    _GUICtrlListView_SetColumnWidth($Listv, 1, 120)
    _GUICtrlListView_SetColumnWidth($Listv, 2, 105)
    _GUICtrlListView_JustifyColumn($Listv, 1, 2)
    _GUICtrlListView_JustifyColumn($Listv, 2, 2)
    GUISetState(@SW_SHOW)
    Local $aArray = IniReadSection(@ScriptDir & "\Config.ini", "PASTAS")
    _ArraySort($aArray)
    If Not @error Then
        For $i = 1 To $aArray[0][0]
            If FileExists($aArray[$i][0]) Then
                GUICtrlCreateListViewItem(StringReplace($aArray[$i][0] & $aArray[$i][1], "=|", "|"), $Listv)
            Else
                GUICtrlCreateListViewItem($aArray[$i][0] & "|Pasta Inexistente|Não", $Listv)
            EndIf
        Next
    EndIf
    If IniRead(@ScriptDir & "\Config.ini", "DESLIGAR", "opção", "1") = "1" Then GUICtrlSetState($Check, $GUI_CHECKED)
EndFunc   ;==>_Gui_backup

Func _Opt_Dir()
    $item_num = _GUICtrlListView_GetSelectedIndices(GUICtrlGetHandle($Listv))
    $item_text[1] = _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), Number($item_num), 1)
    $item_text[2] = _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), Number($item_num), 2)
    Switch $item_text[1]
        Case "Apenas Diretório"
            _GUICtrlListView_SetItemText($Listv, $item_num, "Incluir Subpastas", 1)
            IniWrite(@ScriptDir & "\Config.ini", "PASTAS", _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), Number($item_num)), "|Incluir Subpastas|" & $item_text[2])
        Case "Incluir Subpastas"
            _GUICtrlListView_SetItemText($Listv, $item_num, "Apenas Diretório", 1)
            IniWrite(@ScriptDir & "\Config.ini", "PASTAS", _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), Number($item_num)), "|Apenas Diretório|" & $item_text[2])
        Case "Pasta Inexistente"
            MsgBox(4096, "", "Diretório inexistente!", 3)
    EndSwitch
EndFunc   ;==>_Opt_Dir

Func _Opt_Backup()
    $item_num = _GUICtrlListView_GetSelectedIndices(GUICtrlGetHandle($Listv))
    $item_text[1] = _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), Number($item_num), 1)
    $item_text[2] = _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), Number($item_num), 2)
    If $item_text[1] <> "Pasta Inexistente" Then
        Switch $item_text[2]
            Case "Sim"
                _GUICtrlListView_SetItemText($Listv, $item_num, "Não", 2)
                IniWrite(@ScriptDir & "\Config.ini", "PASTAS", _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), Number($item_num)), "|" & $item_text[1] & "|Não")
            Case "Não"
                _GUICtrlListView_SetItemText($Listv, $item_num, "Sim", 2)
                IniWrite(@ScriptDir & "\Config.ini", "PASTAS", _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), Number($item_num)), "|" & $item_text[1] & "|Sim")
        EndSwitch
    Else
        MsgBox(4096, "", "Diretório inexistente!", 3)
    EndIf
EndFunc   ;==>_Opt_Backup

Func _Opt_Finalize()
    If GUICtrlRead($Check) = 1 Then
        IniWrite(@ScriptDir & "\Config.ini", "DESLIGAR", "opção", "1")
    Else
        IniWrite(@ScriptDir & "\Config.ini", "DESLIGAR", "opção", "0")
    EndIf
EndFunc   ;==>_Opt_Finalize

Func _Start_backup()
    Local $itens = _GUICtrlListView_GetItemCount($Listv)
    If $itens > 0 Then
        $Dir_backup = GUICtrlRead($Input)
        If FileExists($Dir_backup) Then
            For $i = 1 To $itens
                If $proceed = 0 Then ExitLoop
                $item_text[1] = _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), 0, 0)
                $item_text[2] = _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), 0, 1)
                $item_text[3] = _GUICtrlListView_GetItemText(GUICtrlGetHandle($Listv), 0, 2)
                If $item_text[2] <> "Pasta Inexistente" And $item_text[3] = "Sim" Then
                    If $item_text[2] = "Incluir Subpastas" Then
                        _Backup_Full($item_text[1], 0)
                    Else
                        _Backup_Files($item_text[1])
                    EndIf
                EndIf
                If $proceed = 1 Then _GUICtrlListView_DeleteItem($Listv, 0)
            Next
            If $proceed = 1 Then
                If GUICtrlRead($Check) = 1 Then
                    Shutdown(1)
                Else
                    GUICtrlSetState($Listv_Files, $GUI_HIDE)
                    MsgBox(4096, "", "Tarefa Finalizada!")
                    Exit
                EndIf
            Else
                $proceed = 1
                _GUICtrlListView_SetItemText($Listv, 0, $item_text[2], 1)
                _GUICtrlListView_SetItemText($Listv, 0, $item_text[3], 2)
            EndIf
        Else
            MsgBox(4096, "", "Diretório de backup não existe!", 3)
        EndIf
    EndIf
EndFunc   ;==>_Start_backup
Spoiler

Backup_personal.png

Baixar arquivos:  https://mega.nz/#!cdNw0CaQ!q4r5KYyOXGb-a9dVJeZLz8Le9Z29UobQuhDtYeVZl5g

 

  • Thanks 1

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  

×