FWH 16.06: MariaDb/MySql Backup and Restore (Updated 16.08)
Posted: Sat Aug 06, 2016 5:00 am
(Note: Enhancements and additions in FWH16.08 are noted in Red Color in this post)
FWMariaConnection object provides 3 methods for backup and restore operations. As already mentioned, all features of FWH co-exist with other libraries like TMySql, TDolphin, etc and also ADO. Applications mainly relying on other libs can also use these methods for backup and restore.
For using these features we need to obtain a new FWH connection and close after use. In case the application is already connected using TMySql or ADO, it is very easy to open FWH Connection without providing user credentials again.
oFWCon := mysql_Connect( oAdoConObject ) // from ADO
or
oFWCon := mysql_Connect( oMySql ) // from TMySql
or use the standard method of providing host,database,username,password
The backup/restore functions properly handle all types of fields, eg timestamps, autoincrement fields, memo fields with embedded CRLF and special characters, binary data like images, etc properly.
Sample table backed up and restored:
METHODS: (updated)
METHOD BackUp() // all parameters are optional.
Param-1: Source:
(a) Defaults to current database. Full database will be backedup
(b) cDataBasename: If the parameter is a character value, it is considered as Database name and the full database is backedup.
eg: oCn:BackUp( "fwh" )
(c) aTables: If the parameter is an array, it is treated as list of tables in the current database and all the tables in the array are backedup.
Param-2: Destination
(a) Defaults to current folder
(b) cFolderName: If the parameter is a character value without extension or terminated with '\', it is considered as folder name. The folder is created if it does not exist.
(c) cFileName: If the parameter is a character value with extension, it is considered a file name. The file is created if does not exist and is overwritten if exists.
Notes:
(a) & (b): Saving to folder.
The system is primarily designed for silent scheduled backups overnight. Depending on the date and time of backup, a sub-folder with the name of the weekday is created and the backup is saved with the name of the database and extention ".sql".
Example:
oCn:Backup( "fwh", "c:\mybackups" ) --> cBackupFilename
Assuming that this command is executed silently every night, each day the backups are created as:
c:\mybackups\sunday\fwh.sql
c:\mybackups\monday\fwh.sql, etc.
After one complete week, the previous week's file is overwritten. In other words, we always have one week's backup at any time. Since it is the general practice to run such a backup routine in the midnight around 00:00 hours or early small hours, the weekday name corresponds to the day before midnight.
Eg: Backup created at 5th August 2016 11:00 pm and also 6th August 2016 04:00 am are both saved to "friday" folder, indicating that the data is at the end of friday.
(c) Saving to a filename:
This option is suited for adhoc backups of selected tables.
Eg: oCn:Backup( { "states", "customer" }, "c:\mybackups\adhoc\stcust.bkp" )
Param-3: bProgress (optional)
If provided, this codeblck is evaluated for every table with table-name, serial-number, total-number-of-tables, time-in-seconds-from-start. It is generally not recommended to use this parameter since it only delays the process. On local host, even one million records table is saved within 5 seconds.
Param-4: Number of records per SQL. Optional. Default = 100 records.
Param-5: Maximum bytes in single SQL statement: Optional. Default = 4 MB.
Notes: These two parameters may be used keeping in view the max_packet_size permitted for the connection.
In case the table contains a blob or text (large memo) field, the number of records per sql is internally reduced to only 1 and this can not be changed.
METHOD BackupIndex( cBackupFile, [lView] ) --> aIndex // for information only
Every backup file contains an index of the tables saved. The information can be obtained and viewed using this method. If the optional second parameter is .t., the information is displayed in a browse.
Sample Browse:
METHOD Restore( cBackUpFile, [ aTables ], [bProgress], [cNewDB] ) --> nil
Param-1: Backup file name. This parameter is necessary
This file should be a valid backup file created by METHOD Backup (upto FWH 16.06)
If this file is a valid MySQLDump file, this method calls the method RestorefromSqlDump( cBackupFile, [bProgress] ) // 16.08
Param-2: One table name or an array of table names. Optional.
(a) If the second parameter is ommitted:
(i) If the backup file is for a full database, the entire database is created if does not exist on the server and all the tables are created/overwritten in that database.
(ii) If the backup file is for some tables only, all these tables are created/overwritten in the currently selected database on the server.
(b) If the second parameter is supplied:
All the tables specified, if exist in the backupfile, are created/overwritten in the currently selected database on the server.
Param-3: bProgress. Optional
If specified, this codeblock is evaluated with each table name.
Param-4: cNewDB Optional. Added in FWH 16.08
If specified for a full database backup, the backup will be restored with new database name : cNewDB. This parameter enables duplication of an existing database.
Explanation:
1) When cBackUpFile is Full Database Backup and 2nd and 4th parameters are omitted, full database and tables are restored to the same database from which the backup was made.
2) When cBackUpFile is Full Database Backup and 4th parameter is specified, new database cNewDB is created and all tables are restored to cNewDB.
3) If 2nd parameter is specified, table[s] named in the 2nd parameter are restored to the Currently logged in database, irrespective of the database from which the backup was made.
This enables copying some table[s] from one database to other database.
In the above example, table1 of db1 is copied as table1 in db2.
METHOD RestoreFromSqlDump( cFile, bProgress ) // New in FWH 16.08
This method enables restoring from a MySqlDump file.
FWMariaConnection object provides 3 methods for backup and restore operations. As already mentioned, all features of FWH co-exist with other libraries like TMySql, TDolphin, etc and also ADO. Applications mainly relying on other libs can also use these methods for backup and restore.
For using these features we need to obtain a new FWH connection and close after use. In case the application is already connected using TMySql or ADO, it is very easy to open FWH Connection without providing user credentials again.
oFWCon := mysql_Connect( oAdoConObject ) // from ADO
or
oFWCon := mysql_Connect( oMySql ) // from TMySql
or use the standard method of providing host,database,username,password
The backup/restore functions properly handle all types of fields, eg timestamps, autoincrement fields, memo fields with embedded CRLF and special characters, binary data like images, etc properly.
Sample table backed up and restored:
METHODS: (updated)
Code: Select all
METHOD BackUp( [source], [dest], [bProgress], [nRecsInBatch], [nMaxBufperSQL] )
--> cBackUpFileName
METHOD BackupIndex( cBackUpFileName, [lView] ) --> aIndex
METHOD Restore( cBackupFile, [aTables], [bProgress], [cNewDB] ) --> nil // cNewDB added FWH16.08
METHOD RestoreFromSqlDump( cFile, bProgress ) // New in FWH 16.08
Param-1: Source:
(a) Defaults to current database. Full database will be backedup
(b) cDataBasename: If the parameter is a character value, it is considered as Database name and the full database is backedup.
eg: oCn:BackUp( "fwh" )
(c) aTables: If the parameter is an array, it is treated as list of tables in the current database and all the tables in the array are backedup.
Param-2: Destination
(a) Defaults to current folder
(b) cFolderName: If the parameter is a character value without extension or terminated with '\', it is considered as folder name. The folder is created if it does not exist.
(c) cFileName: If the parameter is a character value with extension, it is considered a file name. The file is created if does not exist and is overwritten if exists.
Notes:
(a) & (b): Saving to folder.
The system is primarily designed for silent scheduled backups overnight. Depending on the date and time of backup, a sub-folder with the name of the weekday is created and the backup is saved with the name of the database and extention ".sql".
Example:
oCn:Backup( "fwh", "c:\mybackups" ) --> cBackupFilename
Assuming that this command is executed silently every night, each day the backups are created as:
c:\mybackups\sunday\fwh.sql
c:\mybackups\monday\fwh.sql, etc.
After one complete week, the previous week's file is overwritten. In other words, we always have one week's backup at any time. Since it is the general practice to run such a backup routine in the midnight around 00:00 hours or early small hours, the weekday name corresponds to the day before midnight.
Eg: Backup created at 5th August 2016 11:00 pm and also 6th August 2016 04:00 am are both saved to "friday" folder, indicating that the data is at the end of friday.
(c) Saving to a filename:
This option is suited for adhoc backups of selected tables.
Eg: oCn:Backup( { "states", "customer" }, "c:\mybackups\adhoc\stcust.bkp" )
Param-3: bProgress (optional)
If provided, this codeblck is evaluated for every table with table-name, serial-number, total-number-of-tables, time-in-seconds-from-start. It is generally not recommended to use this parameter since it only delays the process. On local host, even one million records table is saved within 5 seconds.
Param-4: Number of records per SQL. Optional. Default = 100 records.
Param-5: Maximum bytes in single SQL statement: Optional. Default = 4 MB.
Notes: These two parameters may be used keeping in view the max_packet_size permitted for the connection.
In case the table contains a blob or text (large memo) field, the number of records per sql is internally reduced to only 1 and this can not be changed.
METHOD BackupIndex( cBackupFile, [lView] ) --> aIndex // for information only
Every backup file contains an index of the tables saved. The information can be obtained and viewed using this method. If the optional second parameter is .t., the information is displayed in a browse.
Sample Browse:
METHOD Restore( cBackUpFile, [ aTables ], [bProgress], [cNewDB] ) --> nil
Param-1: Backup file name. This parameter is necessary
This file should be a valid backup file created by METHOD Backup (upto FWH 16.06)
If this file is a valid MySQLDump file, this method calls the method RestorefromSqlDump( cBackupFile, [bProgress] ) // 16.08
Param-2: One table name or an array of table names. Optional.
(a) If the second parameter is ommitted:
(i) If the backup file is for a full database, the entire database is created if does not exist on the server and all the tables are created/overwritten in that database.
(ii) If the backup file is for some tables only, all these tables are created/overwritten in the currently selected database on the server.
(b) If the second parameter is supplied:
All the tables specified, if exist in the backupfile, are created/overwritten in the currently selected database on the server.
Param-3: bProgress. Optional
If specified, this codeblock is evaluated with each table name.
Param-4: cNewDB Optional. Added in FWH 16.08
If specified for a full database backup, the backup will be restored with new database name : cNewDB. This parameter enables duplication of an existing database.
Explanation:
1) When cBackUpFile is Full Database Backup and 2nd and 4th parameters are omitted, full database and tables are restored to the same database from which the backup was made.
2) When cBackUpFile is Full Database Backup and 4th parameter is specified, new database cNewDB is created and all tables are restored to cNewDB.
3) If 2nd parameter is specified, table[s] named in the 2nd parameter are restored to the Currently logged in database, irrespective of the database from which the backup was made.
This enables copying some table[s] from one database to other database.
Code: Select all
oCn:SelectDB( "db1" )
oCn:BackUp( { "table1", "table2" }, "temp.sql" )
oCn:SelectDB( "db2" )
oCn:Restore( "temp.sql", { "table1" } )
METHOD RestoreFromSqlDump( cFile, bProgress ) // New in FWH 16.08
This method enables restoring from a MySqlDump file.