Membuat Backup dan Restore Mysql dengan Lazarus

Pagi – pagi buat catatan di blog ” Membuat Backup dan Restore database Mysql di lazarus “ Cara ini berlaku juga di delphi. LAngsung saja Buatlah 2 button, Button Backup dan Restore. Pada proses ini kita menggunakan mysqldump yang sudah disediakan oleh mysql. Lokasi mysqldump tergantung dari instalasi mysqlnya. Jika menggunakan Xampp, cari lokasi terlebihdahulu.

Pada Button Backup isi event click berikut ;

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;

 

Dan untuk button Restore isi dengan prosedure berikut

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;

 

Semoga bermanfaat

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s