Wednesday, March 16, 2022

Backup Progress, How?

 I have been wondering how to do this as we need to allow users to perform backup and give them a good estimate on when said backup will be completed; as in my end where the database is external (MariaDB), then I need to run an external MySQLDump.exe.  The beauty of this is while backup is in progress running on a separate thread, they can continue working on their app without waiting for the backup completion.

Why is it important for me to show a progress bar as an indicator that the backup process has been completed?  Well it is because if they quit the app ahead of the backup completion, it will also quit the backup process and the backup will most probably be incomplete as harddisk writing is done gradually by the backup process.  So we need something like this that shows them when the backup process is still in progress:




But again how?  The backup completion is affected by file size and whether said database is local or on a remote location.  So originally, we perform guesstimates of how long it normally takes but that is not good enough.  And we definitely cannot use:

DO WHILE .T.
   IF FSIZE(m.lcBackup) > 0
      Exit
   ENDIF
ENDDO

As depending on your RAM and free memory, a diskwrite for the backup may happen several times gradually increasing its size; and when the first HDD write happens, your app with the approach above will deem it already complete.  

This morning, an idea suddenly hit me and so I tried this trick now and it is working good, so I am sharing the trick here now.  

For a backup progress and its possible completion to be determined, we have to at least know how much MB the backup will consume.  So here is how:

1.  Check your archive folder and get all the backup files there via ADIR().
2.  Get the latest backup via checking its date and time stamps
3.  Get the filesize of that latest backup.  

Now, we have a basis of a possible size of the next backup. Either it will be of the same size but most probably it will be a bit bigger.  And that will be your basis for your progress bar indicator as well as possible completion.  I have a timer on my main form which checks the users that remain online every 5 seconds so I put this there too:

If gomyapp.isbackup = 1
      * Check if previous max backup bytes is not zero
      If Thisform._bytes > 0
            * Compare new size written vs last backup size
            Set Compatible On
            Local lnWidth, lnSize
            lnSize = Fsize(gomyapp.archive)
            lnWidth =  Int((m.lnSize/Thisform._bytes)*133)
            Thisform.coBackup.shpProgress.Width = m.lnWidth
            If m.lnWidth >= 133
                  Thisform.coBackup.Visible = .F.
                  gomyapp.isbackup = 0
            Endif
            Set Compatible Off
      Else
            * 6 minutes
            Thisform.coBackup.shpProgress.Width = Int((Thisform._backctr/360)*133)
            If m.lnWidth >= 133
                  Thisform.coBackup.Visible = .F.
                  gomyapp.isbackup = 0
            Endif
      Endif
Endif

Thisform._bytes holds the bytes of the latest backup file.  That is what I use now for comparison with the new backup as bytes are written down to the file in batches.  So it is slowly getting bigger, and with it the progress bar is increasing too.


Now if you notice, I took the possibility that Thisform._bytes will have zero value.  That will happen if there is no backup file yet; meaning they will perform the very first backup process.  In that case I have that ELSE which is geared for 6 minutes, just to ensure it will be completed. You can increase or decrease the number of minutes by doing an initial backup test, check how long the backup took to complete and use the number of minutes on the above instead of that 360 value.  I did the 6 minutes because our server is on the cloud and the speed of completion is dependent on the connection speed plus the size of the database.  Truth is it is just taking a few minutes on my end for a 126MB backup file but I am just playing safe on that initial backup run.  After that, the system will no longer switch onto that as there are now files for comparison.

So there it is.  A way to give a user a good estimate of a backup completion.  HTH!



1 comment: