Advertisement
filhotecmail

BakupRestore

Sep 4th, 2017
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.83 KB | None | 0 0
  1. {*******************************************************}
  2. {                                                       }
  3. {       FrentePDV                                       }
  4. {                                                       }
  5. {       Copyright (C) 2017 Leopard report               }
  6. {   C:\Leopard\Pdv\  - FrentePDV  1.0.0.0               }
  7. {                    04/09/2017 00:34:23                }
  8. { Devenvolvedor: Carlos Alberto Dias da Silva Filho     }
  9. {*******************************************************}
  10.  
  11. unit BkpRestoreClass;
  12.  
  13. interface
  14.  
  15.  uses Classes,FireDAC.Stan.StorageBin, FireDAC.Stan.Def, FireDAC.VCLUI.Wait, FireDAC.Phys.IBWrapper, FireDAC.Stan.Intf,
  16.   FireDAC.Phys, FireDAC.Phys.IBBase, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  17.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.StdCtrls, Vcl.ExtCtrls,
  18.   Data.SqlExpr, TypInfo,
  19.   FireDAC.Stan.Option,
  20.   FireDAC.Stan.Error,
  21.   FireDAC.UI.Intf,
  22.   FireDAC.Phys.Intf,
  23.   FireDAC.Stan.Pool,
  24.   FireDAC.Stan.Async,
  25.   FireDAC.Comp.Client,
  26.   FireDAC.Phys.FB;
  27.  
  28.  type IRestoreInterface = interface
  29.  ['{162CC8A4-69C3-435A-8A61-77D1268EFBCE}']
  30.  function RealizaRestore(aRestore: TFDIBRestore): Boolean;
  31.   end;
  32.  
  33.  type IBkpInterface = interface
  34.  ['{C4B18E84-29B9-4A49-BA24-BCFEE51D4714}']
  35.   Function RealizaBkp(aBkp: TFDIBBackup):Boolean;
  36.  
  37.  end;
  38.  
  39.  type TParametros = class
  40.   private
  41.   {Private Declaration}
  42.     FLocalBkp: string;
  43.     FProtocol: TIBProtocol;
  44.     FDatabase: string;
  45.     FPassWord: string;
  46.     FHost: string;
  47.     FUserName: string;
  48.     FPorta: integer;
  49.     procedure setFDataBase(const Value: string);
  50.     procedure setFLocalBkp(const Value: string);
  51.     procedure setFPassWord(const Value: string);
  52.     procedure setFUserName(const Value: string);
  53.     procedure setFPorta(const Value: Integer);
  54.  
  55.  end;
  56.  
  57.  type TBackupFB = class(TInterfacedPersistent,IBkpInterface)
  58.  
  59.   protected
  60.   procedure OnAfterBackup; dynamic;
  61.  
  62.   private
  63.   {Private declaration}
  64.   FFDIBBackup: TFDIBBackup;
  65.   FFDPhysFBDriverLink1: TFDPhysFBDriverLink;
  66.   FTParametros: TParametros;
  67.   FAfterBackup: TNotifyEvent;
  68.   Function RealizaBkp(aBkp: TFDIBBackup):Boolean;
  69.   property Parametros: TParametros read FTParametros write FTParametros;
  70.   procedure DepoisdoBackup(Sender: TObject);
  71.   procedure ProgressBackup(ASender: TFDPhysDriverService; const AMessage: String);
  72.   public
  73.   {Public declaration declaration}
  74.    property AfterBackup: TNotifyEvent read FAfterBackup write FAfterBackup;
  75.      (*Metodos do Constructor*)
  76.    /// <summary>
  77.    /// Inserir os Dados para que se inicie o Processo de Backup ao criar o Objeto
  78.    /// </summary>
  79.    /// <param name="aDatabase"></param>
  80.    /// <param name="aLocalBkp"></param>
  81.    /// <param name="aUserName"></param>
  82.    /// <param name="aPassWord"></param>
  83.    /// <param name="aHost"></param>
  84.    /// <param name="aPorta"></param>
  85.    constructor Create(aDatabase,aLocalBkp,aUserName,aPassWord,aHost: string;aPorta: integer);
  86.    destructor Destroy; override;
  87.  
  88.   end;
  89.  
  90.   type TRestoreFB = class(TInterfacedPersistent,IRestoreInterface)
  91.  
  92.   private
  93.    var FFDPhysFBDriverLink1: TFDPhysFBDriverLink;
  94.    var FDIBRestore : TFDIBRestore;
  95.    function RealizaRestore(aRestore: TFDIBRestore): Boolean;
  96.    public
  97.    constructor Create(aUserName,aPassword,aHost,aDatabase,aBackupFiles:String);
  98.    destructor Destroy; override;
  99.  
  100.  end;
  101.  
  102. implementation
  103.  
  104. uses
  105.   FuncoesLibraryClass;
  106.  
  107. { TBackupFB }
  108.  
  109. procedure TBackupFB.OnAfterBackup;
  110. begin
  111.  if Assigned(FAfterBackup) then
  112.        FAfterBackup(Self);
  113.  
  114. end;
  115.  
  116. procedure TBackupFB.ProgressBackup(ASender: TFDPhysDriverService; const AMessage: String);
  117. begin
  118.    // Caso queira adicionar um TGauge ou ProgressBar
  119. end;
  120.  
  121. constructor TBackupFB.Create(aDatabase,aLocalBkp,aUserName,aPassWord,aHost: string;aPorta: integer);
  122.  begin
  123.  inherited Create;
  124.  FFDPhysFBDriverLink1:= TFDPhysFBDriverLink.Create(Nil);
  125.   try
  126.   FFDIBBackup:= TFDIBBackup.Create(Nil);
  127.   Self.AfterBackup := DepoisdoBackup;
  128.   FTParametros := TParametros.Create;
  129.   FFDIBBackup.OnProgress:= ProgressBackup;
  130.   finally
  131.    CarregaWait('Iniciando processo de backup',500);
  132.    FTParametros.FDatabase:= aDatabase;
  133.    FTParametros.fLocalBkp:=  aLocalBkp;
  134.    FTParametros.fUserName:=  aUserName;
  135.    FTParametros.fPassWord:=  aPassWord;
  136.    FTParametros.fHost   := aHost;
  137.    FTParametros.fProtocol:= TIBProtocol(ipLocal);
  138.    FTParametros.fPorta:= aPorta;
  139.    CarregaWait('Carregando dados para Backup',500);
  140.    FFDIBBackup.DriverLink:= FFDPhysFBDriverLink1;
  141.    FFDIBBackup.Protocol:= FTParametros.fProtocol;
  142.    FFDIBBackup.Database:= FTParametros.fDatabase;
  143.    FFDIBBackup.Host:=     FTParametros.fHost;
  144.    FFDIBBackup.UserName:= FTParametros.fUserName;
  145.    FFDIBBackup.Password:= FTParametros.fPassWord;
  146.    FFDIBBackup.BackupFiles.Add(FTParametros.fLocalBkp);
  147.    FFDIBBackup.Port:= FTParametros.fPorta;
  148.    FFDIBBackup.Verbose:= True;
  149.   end;
  150.  
  151.   try
  152.     try
  153.      CarregaWait('Realizando BAckup da base de dados',500);
  154.      RealizaBkp(FFDIBBackup);
  155.     except
  156.      On E: Exception do begin
  157.       Raise
  158.      end;
  159.     end;
  160.   finally
  161.     CarregaWait('Backup realizado com sucesso',500);
  162.   end;
  163. end;
  164.  
  165. procedure TBackupFB.DepoisdoBackup(Sender: TObject);
  166.   var Restore: TRestoreFB;
  167. begin
  168.    FFDPhysFBDriverLink1.DisposeOf;
  169.    FFDIBBackup.DisposeOf;
  170.    Parametros.DisposeOf;
  171.    WinExec('C:\Program Files (x86)\Firebird\Firebird_2_5\bin\instsvc.exe stop',SW_SHOW);
  172.    CarregaWait('Iniciando Restore da base de dados',500);
  173.    Restore := TRestoreFB.Create('Sysdba','1234','127.0.0.1','C:\Leopard\PDV\PDV.FDB','c:\Leopard\PDV\PDV.Fbk');
  174. end;
  175.  
  176. destructor TBackupFB.Destroy;
  177. begin
  178.  
  179.   inherited Destroy;
  180. end;
  181.  
  182. function TBackupFB.RealizaBkp(aBkp: TFDIBBackup): Boolean;
  183. begin
  184.   try
  185.    Result:= False;
  186.    aBkp.Backup;
  187.    Result:= True;
  188.    OnAfterBackup;
  189.   except
  190.    on E: Exception do begin
  191.    raise Exception.Create('Erro ao gerar o Backup, verifique as informações!');
  192.    end;
  193.   end;
  194. end;
  195.  
  196. { TParametros }
  197.  
  198. procedure TParametros.setFDataBase(const Value: string);
  199. begin
  200.    { TODO : Validar o Local database, se for inválido abrir uma tela para poder apontar onde está a base de dados. }
  201.   FDatabase := Value;
  202. end;
  203.  
  204. procedure TParametros.setFLocalBkp(const Value: string);
  205. begin
  206.    { TODO : Validar o Local onde será gravado o Bkp se não existir criar }
  207.   FLocalBkp := Value;
  208. end;
  209.  
  210. procedure TParametros.setFPassWord(const Value: string);
  211. begin
  212.    { TODO : Validar a senha do Banco de dados }
  213.   FPassWord := Value;
  214. end;
  215.  
  216. procedure TParametros.setFPorta(const Value: Integer);
  217. begin
  218.   FPorta := Value;
  219. end;
  220.  
  221. procedure TParametros.setFUserName(const Value: string);
  222. begin
  223.    { TODO : Validar o Nome de usuário no banco de dados }
  224.   FUserName := Value;
  225. end;
  226.  
  227. { TRestoreFB }
  228.  
  229. constructor TRestoreFB.Create(aUserName,aPassword,aHost,aDatabase,aBackupFiles:String);
  230. begin
  231.  
  232.    WinExec('C:\Program Files (x86)\Firebird\Firebird_2_5\bin\instsvc.exe start',SW_SHOW);
  233.    FFDPhysFBDriverLink1:= TFDPhysFBDriverLink.Create(Nil);
  234.    FDIBRestore := TFDIBRestore.Create(Nil);
  235.    FDIBRestore.UserName := aUserName;
  236.    FDIBRestore.DriverLink:= FFDPhysFBDriverLink1;
  237.    FDIBRestore.Password := aPassword;
  238.    FDIBRestore.Protocol:= TIBProtocol(ipLocal);
  239.    FDIBRestore.Host:= aHost;
  240.    FDIBRestore.Database:= aDatabase;
  241.    FDIBRestore.BackupFiles.Add(aBackupFiles);
  242.    try
  243.      RealizaRestore(FDIBRestore);
  244.    finally
  245.      FDIBRestore.DisposeOf;
  246.      FFDPhysFBDriverLink1.DisposeOf;
  247.    end;
  248. end;
  249.  
  250. destructor TRestoreFB.Destroy;
  251. begin
  252.  
  253.   inherited;
  254. end;
  255.  
  256. function TRestoreFB.RealizaRestore(aRestore: TFDIBRestore): Boolean;
  257. begin
  258.    try
  259.    Result:= False;
  260.    aRestore.Options:= [roReplace,roValidate];
  261.    aRestore.Restore;
  262.    Result:= True;
  263.   except
  264.    on E: Exception do begin
  265.    raise Exception.Create('Erro ao Restaurar a base de dados, verifique as informações!');
  266.    end;
  267.   end;
  268. end;
  269.  
  270. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement