Einzelnen Beitrag anzeigen

Hobbycoder

Registriert seit: 22. Feb 2017
930 Beiträge
 
#9

AW: Datenbank duplizieren

  Alt 24. Jun 2020, 17:30
Über einen Dump - Restore wie hier dargestellt.

Dump:
Delphi-Quellcode:
procedure TMain.BackupClick(Sender: TObject);
const
   READ_BYTES = 2048;

var
  command: TProcess;
  s: string;
  output: TStringList;
  BytesRead, n: LongInt;
  m: TMemoryStream;

begin
  if not (ZConnection1.User = 'root') then begin
    ShowMessage('Only root could access.');
    Exit
  end;
  SaveDialog1.Free;
  SaveDialog1 := TSaveDialog.Create(Self);
  SaveDialog1.InitialDir:= GetUserDir + 'My Documents\';
  SaveDialog1.Filter:='Backup Files|*.bfl';
  if SaveDialog1.Execute then begin
    s := SaveDialog1.FileName;
    if not (copy(s,Length(s)-3,Length(s)-1) = '.bfl') then
      s := s + '.bfl';
    command := TProcess.Create(Nil);
    output := TStringList.Create;
    M := TMemoryStream.Create;
    BytesRead := 0;
    command.CommandLine := 'c:\mysqldump -u root -p'+ZConnection1.Password+' -h '+ZConnection1.HostName+' database';
    command.Options := command.Options + [poUsePipes];
    command.Execute;
    while command.Running do begin
      // ensure the space
      M.SetSize(BytesRead + READ_BYTES);
      // we try to read
      n := command.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
      if n > 0 then
        Inc(BytesRead, n)
      else
        // whitout data, wait 100 ms
        Sleep(100);
    end;
    // we read the last part
    repeat
      // ensure the space
      M.SetSize(BytesRead + READ_BYTES);
      // we try to read
      n := command.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
      if n > 0 then
       Inc(BytesRead, n);
    until n <= 0;
    M.SetSize(BytesRead);
    output.LoadFromStream(M);
    output.SaveToFile(s);
    output.Free;
    command.Free;
    M.Free;
    ShowMessage('Backup ready.')
  end else
    ShowMessage('Backup canceled.');
end;
Restore:
Delphi-Quellcode:
procedure TFMain.restoreClick(Sender: TObject);
var
  s: string;
  tfFile: TextFile;
begin
  if not (ZConnection1.User = 'root') then begin
    ShowMessage('Only root could access.');
    Exit
  end;
  OpenDialog1.Free;
  OpenDialog1 := tOpenDialog.Create(Self);
  OpenDialog1.InitialDir:=GetUserDir+'My Documents\';
  OpenDialog1.Filter:='Backup Files|*.bfl';
  if OpenDialog1.Execute then begin
    AssignFile(tfFile, OpenDialog1.FileName);
    if not FileExists(OpenDialog1.FileName) then
      exit;
    reset(tfFile);
    ZQuery1.SQL.Clear;
    readln(tfFile,s);
    While not eof(tfFile) do begin
      Readln(tfFile,s);
      if length(s) > 0 then begin
        s := StringReplace(s, #10, '', [rfReplaceAll]);
        s := StringReplace(s, #13, '', [rfReplaceAll]);
        s := StringReplace(s, #9, ' ', [rfReplaceAll]);
        if not ((s[1]+s[2]) = '--') then begin
          ZQuery1.SQL.Add(s);
          if s[length(s)] = ';then begin
            ZQuery1.ExecSQL;
            ZQuery1.SQL.Clear
          end
        end
      end
    end;
    CloseFile(tfFile);
    ShowMessage('Restauration done.');
  end else
    ShowMessage('Restauration canceled.');
end;
Hab's nicht getestet. Aber mit ein paar kleinen Anpassungen könnte man den Umweg über eine Datei eliminieren.
Gruß Hobbycoder
Alle sagten: "Das geht nicht.". Dann kam einer, der wusste das nicht, und hat's einfach gemacht.
  Mit Zitat antworten Zitat