Topic: Backup

As far as I know, backup and restore DB file feature is not implemented in MVD yet.


I added Backup Database menu item on File menu. It would act as a save as... command. Instead of user locating application folder and copies .db file to another location for db backup purposes, they would do it within app via file menu item.

What would be the script for starting windows save as dialog and save db file on selected loaction?


procedure MenuClick1 (Sender: string);
begin
     ......
end;


If the above is doable, can user choose a different db file name or it always has to be sqlite?

Adam
God... please help me become the person my dog thinks I am.

2 (edited by derek 2017-01-08 09:52:00)

Re: Backup

Hi Adam,
I always use a simple copy command that runs automatically whenever an application closes (seemed safer than rely on Users remembering to do it).  It's just 1 line in a script.
I'd be the first to admit that it's pretty simple (but just because it's simple, doesn't mean it's no good!) and it doesn't attempt to automate a 'restore' - that would need to be done manually.  But for the projects that I write, anything more complex would be overkill.
Maybe it gives you a couple of ideas to get you started.
Derek.

Post's attachments

Attachment icon adam backup.zip 343.77 kb, 491 downloads since 2017-01-08 

Re: Backup

Hi Derek,


Thanks a lot for the sample...


Yes, it's a very simple solution for private - own projects. However, I'm not sure it can be used for public projects as we wouldn't know which drives users would have in their computer. Copy file could be used in public project if we can have appropriate settings on options window such as boolean to use backup or not, ability to specify backup folder and back up timer which I don't know how to add these settings to options window and make them work.


My thinking was to do it via menu command as it may be simpler approach - even though not proper one but still better than manually locating db file and copying.


Imho, proper way may be is doing via settings on options window as explained above paragraph. Plus created backup file extension would be .backup and ability to open - save backup file within application when needed.

Adam
God... please help me become the person my dog thinks I am.

Re: Backup

I prefer the solution suggested on the post 1, using menu item and windows save as dialog where user can choose a location and name the copied db file but the following cut-down version may do the job.

I have tried:

procedure MenuClick1 (Sender: string);
var
    buttonSelected : Integer;
begin
    buttonSelected :=MessageDlg('This will Copy Database file in to App Backup folder',mtWarning,mbOK+mbCancel,0);
    if buttonSelected = mrOK then
        begin
            CopyFile('sqlite.db', 'backup/backup '+ FormatDateTime('dd-mm-yyyy hh-nn-ss', now)+'.db');
        end;
end;

However, it didn't create backup folder and copied the db file.


With this version additional script needed to specify maximum backups. For instance if max backup count specified as 5 on the sixth backup oldest backup file gets deleted before copying.


Please see the attached sample project:

Post's attachments

Attachment icon MenuBackupTest.zip 7.38 kb, 466 downloads since 2017-01-09 

Adam
God... please help me become the person my dog thinks I am.

Re: Backup

Example for save dialog

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
var
    SaveDialog: TSaveDialog;
begin
    SaveDialog := TSaveDialog.Create(Form1);

    if SaveDialog.Execute then
    begin
        ShowMessage( SaveDialog.FileName );
    end;

    SaveDialog.Free;
end;


for select folder:

procedure Form1_Button2_OnClick (Sender: string; var Cancel: boolean);
var
    s: string;
begin
    s := '';
    SelectDirectory('Caption', 'c:\', s); // default dialog
    SelectDirectory('Caption', 'c:\', s, True); // with button create a directory
    SelectDirectory('Caption', 'c:\', s, True, True); // with button create a directory and shows files
    if s<>'' then ShowMessage(s);
end;
Dmitry.

Re: Backup

Thanks a lot Dmitry..........


Save as dialog is showing fine with your script.
However, save/copy function needed.
Also save as file type needed on windows save as dialog. Currently it's empty.


I have tried the following but didn't show save as file type and didn't save/copy db file into selected folder. In any case CopyFile('sqlite.db'); returns error.:

procedure MenuClick1 (Sender: string);

var
    SaveDialog: TSaveDialog;
begin
    SaveDialog := TSaveDialog.Create(Form1);

    if SaveDialog.Execute then
    begin
        CopyFile('sqlite.db');
        ShowMessage( SaveDialog.FileName );
    end;

    SaveDialog.Free;
end;
Adam
God... please help me become the person my dog thinks I am.

Re: Backup

Sorry Adam, I don't known how to solve your problem, but if you want to combine :


1- Select directory
2 - Save as in the selected directory, you can do :

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
var
    SaveDialog: TSaveDialog;
    s : String;
begin
    s := '';
    SelectDirectory('Caption', 'c:\', s, True);
    if s<>'' then
        begin
            SaveDialog := TSaveDialog.Create(Form1);
                with SaveDialog do
                    begin
                         //SaveDialog.DefaultExt := 'db'; --SELECT EXTENSION, NOT WORKING
                         //saveDialog.FilterIndex := 1;  --DEFAULT EXTENSION IN COMBOBOX, NOT WORKING
                         saveDialog.InitialDir := s;
                    end;

            if SaveDialog.Execute then
            begin
                ShowMessage( SaveDialog.FileName );
            end;
        end;

    SaveDialog.Free;
end;

Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Backup

Hi Mathias,


Thanks for the effort....
Hopefully Dmitry will provide the solution.

Adam
God... please help me become the person my dog thinks I am.

Re: Backup

Check it out

procedure MenuClick1 (Sender: string);
var
    SaveDialog: TSaveDialog;
    sFileName: string;
begin
    sFileName := FormatDateTime('dd-mm-yyyy hh-nn-ss', now)+'.db';

    SaveDialog := TSaveDialog.Create(Form1);
    SaveDialog.FileName := sFileName;
    SaveDialog.Filter := 'Database|*.db|Any file|*.*';

    if SaveDialog.Execute then
    begin
        CopyFile(ExtractFilePath(Application.ExeName)+'sqlite.db', SaveDialog.FileName);
    end;

    SaveDialog.Free;
end;
Dmitry.

Re: Backup

Adam, If I may add another possible solution to your backup process. Typically once you define a backup folder you would want to backup to that same folder without re-selecting the folder each time you do a backup. I usually have a settings/control table which stores definitions for my applications. One of these definitions can be a backup folder. In the attached project, the first time you run a backup it will ask for a folder to store your backups. Once you select a folder, then each time you run a backup it will copy the database, with a time stamp, and store it in the selected folder without asking for the backup folder over and over. If in the future you want to change the backup folder just go into the Options | Define Backup Folder and select another folder to store your backups. I also added the feature to remove the oldest backup when the threshold of backups has reached five.

Post's attachments

Attachment icon MenuBackup Another Solution.zip 587.28 kb, 465 downloads since 2017-01-10 

Re: Backup

DriveSoft wrote:

Check it out

procedure MenuClick1 (Sender: string);
var
    SaveDialog: TSaveDialog;
    sFileName: string;
begin
    sFileName := FormatDateTime('dd-mm-yyyy hh-nn-ss', now)+'.db';

    SaveDialog := TSaveDialog.Create(Form1);
    SaveDialog.FileName := sFileName;
    SaveDialog.Filter := 'Database|*.db|Any file|*.*';

    if SaveDialog.Execute then
    begin
        CopyFile(ExtractFilePath(Application.ExeName)+'sqlite.db', SaveDialog.FileName);
    end;

    SaveDialog.Free;
end;

Hi Dmitry,


Thank you so much.........


The above provides the solution I was looking for.
One thing is missing tho... When overwriting on existing saved file there is no warning dialog such as "Are you sure you want to overwrite xyz.file"
(I thought windows would provide the warning dialog but it doesn't)

Adam
God... please help me become the person my dog thinks I am.

12 (edited by AD1408 2017-01-10 23:16:28)

Re: Backup

Hi EHW,


Thank you so much for the alternative solution. It's great...............
It's in the direction of proper solution to backup imho.


This is the backup solution I'd love to have:
https://s24.postimg.org/qiw552c2d/zzz_Test4.png


Maximum backup count: When specified backups count reached the oldest backup removed.


Attached MVD employees project for clean start. I used a button for accessing backup form but it can also be a menu item.
I have used image for radio buttons on backup form as a placeholders since there is no radio button component in MVD yet.


If user clicks on "Backup the database file" button on form1, backup folder within app folder automatically and copies the file there, if user clicks on "Set backup location" button on form1 then frmBDFSettings launched. If user changes backup location thereafter backups stored in newly defined location.


Perhaps, it's too much to ask you but hopefully Dmitry codes it and offers as a proper backup solution. Even it may also be added to MVD Settings as a new tab so that we don't need to deal with additional script and forms.

Post's attachments

Attachment icon BackupEmpployees1.zip 9.1 kb, 420 downloads since 2017-01-11 

Adam
God... please help me become the person my dog thinks I am.

Re: Backup

Adam, a couple of questions. Is the form you presented, to be used for just backup settings and the backup will be run somewhere else or is the form intended for the settings and running a backup too? The reason I ask is that the first checkbox kind of confused me. Also, if the user elects not to use a date stamp in the backup file name (the last checkbox), how are you going to distinguish the file names in the backup folder? I recommend using the date stamp for all backups.

Re: Backup

Hi EHW,


I see and understand your points. Please see the updated project file attached.


If user clicks on "Backup the database file" button on form1, backup folder within app folder automatically and copies the file there, if user clicks on "Set backup location" button on form1 then frmBDFSettings launched. If user changes backup location thereafter backups stored in newly defined location.


My above explanation from previous post doesn't seems to be clear enough. What I meant is;
If user clicks on "Backup the database file" button on form1 without changing backup location on frmBDFSettings application uses default backup settings as on frmBDFSettings and creates backup folder within application folder and backs up into backup folder created.


It'd also be nice to include backup location on "Backup completed" dialog win such as:
"Backup completed.
The File saved to:
D:\Application folder\backup "


-------------------

Post's attachments

Attachment icon BackupEmpployees2.zip 8.97 kb, 423 downloads since 2017-01-11 

Adam
God... please help me become the person my dog thinks I am.

Re: Backup

AD1408 wrote:

Hi Dmitry,


Thank you so much.........


The above provides the solution I was looking for.
One thing is missing tho... When overwriting on existing saved file there is no warning dialog such as "Are you sure you want to overwrite xyz.file"
(I thought windows would provide the warning dialog but it doesn't)


Just add this line

SaveDialog.Options := ofOverwritePrompt+ofHideReadOnly+ofEnableSizing;

after

SaveDialog.Filter := 'Database|*.db|Any file|*.*';
Dmitry.

Re: Backup

Hi Dmitry,


Currently the following script works via save as dialog.. Thanks to your kind help......


procedure MenuClick1 (Sender: string);
var
    SaveDialog: TSaveDialog;
    sFileName: string;
begin
    sFileName := 'Sqlite-' + FormatDateTime('dd-mm-yyyy hh-nn-ss', now)+'.db';

    SaveDialog := TSaveDialog.Create(Form1);
    SaveDialog.FileName := sFileName;
    SaveDialog.Filter := 'Database|*.db|Any file|*.*';
    SaveDialog.Options := ofOverwritePrompt+ofHideReadOnly+ofEnableSizing;

    if SaveDialog.Execute then
    begin
        CopyFile(ExtractFilePath(Application.ExeName)+'sqlite.db', SaveDialog.FileName);
        ShowMessage('Database file saved in to the specified folder');
    end;

    SaveDialog.Free;
end;

I found that it only saves/copies with hard coded file name. It's fine but user also should be able to save under the name they type in.
Is this possible?
At present it give the impression of saving with custom file name but it doesn't.

Adam
God... please help me become the person my dog thinks I am.

Re: Backup

Adam, I updated your BackupEmployees project with your database backup setting form. I'm a little confused as to the two options for backup destinations (radio buttons). Theoretically a user could backup one time into the app folder and the next time into another folder and so on. You could have mixed backups in different folders, which potentially could be cumbersome to determine the most current backup for a restore. Since MVD does not have radio buttons I used checkboxes. And yes, a user could potentially backup in two places at the same time, which isn't necessarily a bad thing to have double protection. It's totally controlled by the user though. The other options for app start backup and app exit backup as well as the automatic backups and Max count are all functioning. Hope it' meets your requirements. If not, maybe at least it will get you further along in the process.

Post's attachments

Attachment icon BackupEmpployees2_Updated.zip 593.35 kb, 423 downloads since 2017-01-12 

Re: Backup

AD1408 wrote:

I found that it only saves/copies with hard coded file name. It's fine but user also should be able to save under the name they type in.
Is this possible?
At present it give the impression of saving with custom file name but it doesn't.


What do you mean hard coded? You can change file name in the SaveDialog, I checked it, it works.

Dmitry.

Re: Backup

Hi Dmitry,


True, it saves...
I was naming the file for instance abcd and when I click on backup database menu item to get save as dialog window, saved abcd file wasn't on the list. However, when I change Save as type combo and choose Any file they are shown on the list with Type as File while files saved with default name type were DB File.
Shouldn't files saved with custom name such as abcd automatically be assigned to DB File type?

Adam
God... please help me become the person my dog thinks I am.

Re: Backup

ehwagner wrote:

Adam, I updated your BackupEmployees project with your database backup setting form. I'm a little confused as to the two options for backup destinations (radio buttons). Theoretically a user could backup one time into the app folder and the next time into another folder and so on. You could have mixed backups in different folders, which potentially could be cumbersome to determine the most current backup for a restore. Since MVD does not have radio buttons I used checkboxes. And yes, a user could potentially backup in two places at the same time, which isn't necessarily a bad thing to have double protection. It's totally controlled by the user though. The other options for app start backup and app exit backup as well as the automatic backups and Max count are all functioning. Hope it' meets your requirements. If not, maybe at least it will get you further along in the process.


Hi EHW,


Great work....  Thank you soooooooooooooooooo much.... Appreciated very much........


Unless I'm missing something there is one small glitch I could notice so far.
When only "Backup the database file in to a specified folder:" checkbox checked and location defined, Maximum backup count doesn't seems to be working. It writes more than specified max count like no max.


For two location checkboxes, I used the following code from Mathias so that they act as radio buttons and it seems to be working OK:

procedure frmBDFSettings_CheckBox1_OnClick (Sender: string);
begin
    if frmBDFSettings.CheckBox1.Checked = true then frmBDFSettings.CheckBox2.Checked := False;
end;

procedure frmBDFSettings_CheckBox2_OnClick (Sender: string);
begin
    if frmBDFSettings.CheckBox2.Checked = true then frmBDFSettings.CheckBox1.Checked := False;
end;
Adam
God... please help me become the person my dog thinks I am.

Re: Backup

Adam,


Actually it is working. I think you may be running into a situation whereby the max count is set to a lower number than the number of backup files already in the folder. It is removing the oldest file, but it does not remove additional files to bring it down to the max. I wasn't sure if you wanted to do that because a user could reduce the max count down to a very low number from a higher number and then you would lose all the backup files already stored. However, if this is acceptable to you, then use the attached project which will always maintain the max count of files as defined.


I know I could have used the radio button simulation code, but I was thinking that maybe a user may want to maintain a backup in the main folder and a duplicate copy in an alternative drive. But if you prefer to only select one backup location, then by all means use the simulation code. You will need to incorporate that code in the attached project though.

Post's attachments

Attachment icon BackupEmpployees2_Updated (2).zip 593.58 kb, 489 downloads since 2017-01-12 

Re: Backup

Hi EHW,

Once again, thank you soooooooooooooooooo much.... Appreciated very much........

Adam
God... please help me become the person my dog thinks I am.

Re: Backup

AD1408 wrote:

Hi Dmitry,


True, it saves...
I was naming the file for instance abcd and when I click on backup database menu item to get save as dialog window, saved abcd file wasn't on the list. However, when I change Save as type combo and choose Any file they are shown on the list with Type as File while files saved with default name type were DB File.
Shouldn't files saved with custom name such as abcd automatically be assigned to DB File type?

Hello.


Please add this line

SaveDialog.DefaultExt := 'db';
Dmitry.

Re: Backup

Than you so much Dmitry.....


Now, we have 2 backup options simple one using save as and comprehensive one kindly coded by ehwagner.


Once again, thank you very much guys

Adam
God... please help me become the person my dog thinks I am.