Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {*******************************************************}
- { }
- { FrentePDV }
- { }
- { Copyright (C) 2017 Leopard report }
- { C:\Leopard\Pdv\ - FrentePDV 1.0.0.0 }
- { 04/09/2017 00:34:23 }
- { Devenvolvedor: Carlos Alberto Dias da Silva Filho }
- {*******************************************************}
- unit BkpRestoreClass;
- interface
- uses Classes,FireDAC.Stan.StorageBin, FireDAC.Stan.Def, FireDAC.VCLUI.Wait, FireDAC.Phys.IBWrapper, FireDAC.Stan.Intf,
- FireDAC.Phys, FireDAC.Phys.IBBase, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.StdCtrls, Vcl.ExtCtrls,
- Data.SqlExpr, TypInfo,
- FireDAC.Stan.Option,
- FireDAC.Stan.Error,
- FireDAC.UI.Intf,
- FireDAC.Phys.Intf,
- FireDAC.Stan.Pool,
- FireDAC.Stan.Async,
- FireDAC.Comp.Client,
- FireDAC.Phys.FB;
- type IRestoreInterface = interface
- ['{162CC8A4-69C3-435A-8A61-77D1268EFBCE}']
- function RealizaRestore(aRestore: TFDIBRestore): Boolean;
- end;
- type IBkpInterface = interface
- ['{C4B18E84-29B9-4A49-BA24-BCFEE51D4714}']
- Function RealizaBkp(aBkp: TFDIBBackup):Boolean;
- end;
- type TParametros = class
- private
- {Private Declaration}
- FLocalBkp: string;
- FProtocol: TIBProtocol;
- FDatabase: string;
- FPassWord: string;
- FHost: string;
- FUserName: string;
- FPorta: integer;
- procedure setFDataBase(const Value: string);
- procedure setFLocalBkp(const Value: string);
- procedure setFPassWord(const Value: string);
- procedure setFUserName(const Value: string);
- procedure setFPorta(const Value: Integer);
- end;
- type TBackupFB = class(TInterfacedPersistent,IBkpInterface)
- protected
- procedure OnAfterBackup; dynamic;
- private
- {Private declaration}
- FFDIBBackup: TFDIBBackup;
- FFDPhysFBDriverLink1: TFDPhysFBDriverLink;
- FTParametros: TParametros;
- FAfterBackup: TNotifyEvent;
- Function RealizaBkp(aBkp: TFDIBBackup):Boolean;
- property Parametros: TParametros read FTParametros write FTParametros;
- procedure DepoisdoBackup(Sender: TObject);
- procedure ProgressBackup(ASender: TFDPhysDriverService; const AMessage: String);
- public
- {Public declaration declaration}
- property AfterBackup: TNotifyEvent read FAfterBackup write FAfterBackup;
- (*Metodos do Constructor*)
- /// <summary>
- /// Inserir os Dados para que se inicie o Processo de Backup ao criar o Objeto
- /// </summary>
- /// <param name="aDatabase"></param>
- /// <param name="aLocalBkp"></param>
- /// <param name="aUserName"></param>
- /// <param name="aPassWord"></param>
- /// <param name="aHost"></param>
- /// <param name="aPorta"></param>
- constructor Create(aDatabase,aLocalBkp,aUserName,aPassWord,aHost: string;aPorta: integer);
- destructor Destroy; override;
- end;
- type TRestoreFB = class(TInterfacedPersistent,IRestoreInterface)
- private
- var FFDPhysFBDriverLink1: TFDPhysFBDriverLink;
- var FDIBRestore : TFDIBRestore;
- function RealizaRestore(aRestore: TFDIBRestore): Boolean;
- public
- constructor Create(aUserName,aPassword,aHost,aDatabase,aBackupFiles:String);
- destructor Destroy; override;
- end;
- implementation
- uses
- FuncoesLibraryClass;
- { TBackupFB }
- procedure TBackupFB.OnAfterBackup;
- begin
- if Assigned(FAfterBackup) then
- FAfterBackup(Self);
- end;
- procedure TBackupFB.ProgressBackup(ASender: TFDPhysDriverService; const AMessage: String);
- begin
- // Caso queira adicionar um TGauge ou ProgressBar
- end;
- constructor TBackupFB.Create(aDatabase,aLocalBkp,aUserName,aPassWord,aHost: string;aPorta: integer);
- begin
- inherited Create;
- FFDPhysFBDriverLink1:= TFDPhysFBDriverLink.Create(Nil);
- try
- FFDIBBackup:= TFDIBBackup.Create(Nil);
- Self.AfterBackup := DepoisdoBackup;
- FTParametros := TParametros.Create;
- FFDIBBackup.OnProgress:= ProgressBackup;
- finally
- CarregaWait('Iniciando processo de backup',500);
- FTParametros.FDatabase:= aDatabase;
- FTParametros.fLocalBkp:= aLocalBkp;
- FTParametros.fUserName:= aUserName;
- FTParametros.fPassWord:= aPassWord;
- FTParametros.fHost := aHost;
- FTParametros.fProtocol:= TIBProtocol(ipLocal);
- FTParametros.fPorta:= aPorta;
- CarregaWait('Carregando dados para Backup',500);
- FFDIBBackup.DriverLink:= FFDPhysFBDriverLink1;
- FFDIBBackup.Protocol:= FTParametros.fProtocol;
- FFDIBBackup.Database:= FTParametros.fDatabase;
- FFDIBBackup.Host:= FTParametros.fHost;
- FFDIBBackup.UserName:= FTParametros.fUserName;
- FFDIBBackup.Password:= FTParametros.fPassWord;
- FFDIBBackup.BackupFiles.Add(FTParametros.fLocalBkp);
- FFDIBBackup.Port:= FTParametros.fPorta;
- FFDIBBackup.Verbose:= True;
- end;
- try
- try
- CarregaWait('Realizando BAckup da base de dados',500);
- RealizaBkp(FFDIBBackup);
- except
- On E: Exception do begin
- Raise
- end;
- end;
- finally
- CarregaWait('Backup realizado com sucesso',500);
- end;
- end;
- procedure TBackupFB.DepoisdoBackup(Sender: TObject);
- var Restore: TRestoreFB;
- begin
- FFDPhysFBDriverLink1.DisposeOf;
- FFDIBBackup.DisposeOf;
- Parametros.DisposeOf;
- WinExec('C:\Program Files (x86)\Firebird\Firebird_2_5\bin\instsvc.exe stop',SW_SHOW);
- CarregaWait('Iniciando Restore da base de dados',500);
- Restore := TRestoreFB.Create('Sysdba','1234','127.0.0.1','C:\Leopard\PDV\PDV.FDB','c:\Leopard\PDV\PDV.Fbk');
- end;
- destructor TBackupFB.Destroy;
- begin
- inherited Destroy;
- end;
- function TBackupFB.RealizaBkp(aBkp: TFDIBBackup): Boolean;
- begin
- try
- Result:= False;
- aBkp.Backup;
- Result:= True;
- OnAfterBackup;
- except
- on E: Exception do begin
- raise Exception.Create('Erro ao gerar o Backup, verifique as informações!');
- end;
- end;
- end;
- { TParametros }
- procedure TParametros.setFDataBase(const Value: string);
- begin
- { TODO : Validar o Local database, se for inválido abrir uma tela para poder apontar onde está a base de dados. }
- FDatabase := Value;
- end;
- procedure TParametros.setFLocalBkp(const Value: string);
- begin
- { TODO : Validar o Local onde será gravado o Bkp se não existir criar }
- FLocalBkp := Value;
- end;
- procedure TParametros.setFPassWord(const Value: string);
- begin
- { TODO : Validar a senha do Banco de dados }
- FPassWord := Value;
- end;
- procedure TParametros.setFPorta(const Value: Integer);
- begin
- FPorta := Value;
- end;
- procedure TParametros.setFUserName(const Value: string);
- begin
- { TODO : Validar o Nome de usuário no banco de dados }
- FUserName := Value;
- end;
- { TRestoreFB }
- constructor TRestoreFB.Create(aUserName,aPassword,aHost,aDatabase,aBackupFiles:String);
- begin
- WinExec('C:\Program Files (x86)\Firebird\Firebird_2_5\bin\instsvc.exe start',SW_SHOW);
- FFDPhysFBDriverLink1:= TFDPhysFBDriverLink.Create(Nil);
- FDIBRestore := TFDIBRestore.Create(Nil);
- FDIBRestore.UserName := aUserName;
- FDIBRestore.DriverLink:= FFDPhysFBDriverLink1;
- FDIBRestore.Password := aPassword;
- FDIBRestore.Protocol:= TIBProtocol(ipLocal);
- FDIBRestore.Host:= aHost;
- FDIBRestore.Database:= aDatabase;
- FDIBRestore.BackupFiles.Add(aBackupFiles);
- try
- RealizaRestore(FDIBRestore);
- finally
- FDIBRestore.DisposeOf;
- FFDPhysFBDriverLink1.DisposeOf;
- end;
- end;
- destructor TRestoreFB.Destroy;
- begin
- inherited;
- end;
- function TRestoreFB.RealizaRestore(aRestore: TFDIBRestore): Boolean;
- begin
- try
- Result:= False;
- aRestore.Options:= [roReplace,roValidate];
- aRestore.Restore;
- Result:= True;
- except
- on E: Exception do begin
- raise Exception.Create('Erro ao Restaurar a base de dados, verifique as informações!');
- end;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement