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;