A database real-time backup and recovery method and related equipment
By creating and accessing the primary database and backup database in different partitions of nonvolatile memory, real-time backup and recovery are realized, solving the data loss problem caused by the inability to realize real-time backup in the prior art, and improving the reliability and stability of the database.
Patent Information
- Application Number
- CN202411484209.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-23
- Publication Date
- 2025-05-23
- Estimated Expiration
- 2044-10-23
AI Technical Summary
The existing database backup measures cannot achieve real-time backup, resulting in inconsistent backup database data and the main database data in the event of abnormal power outage, resulting in data loss.
By creating the primary database and backup database in different partitions of nonvolatile memory and accessing it through memory mapping, real-time synchronous updates between the primary database and the backup database, including self-test, recovery and modification operations.
Ensure the integrity and consistency of data, avoid data loss caused by abnormal power outages and other reasons, and improve the reliability and stability of the system.
Smart Images

Figure CN119025342B_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of databases, and in particular relates to a database real-time backup and recovery method and related equipment. Background Art
[0002] At present, the lightweight database SQLite is widely used in embedded devices. The database SQLite plays an important role in the data storage and processing process. The use of this database brings convenience to the data processing of the application.
[0003] Due to software compatibility, memory tampering, system locks and other reasons, SQLite databases are easily damaged, affecting the stable operation of system software and causing data loss. Especially in data-sensitive scenarios, data loss can cause immeasurable losses. Existing database backup measures usually use external devices for backup, such as external storage or servers. Although the backup function can be realized with the help of external devices, it is impossible to achieve real-time backup. In the event of an abnormal power outage, it is easy to cause inconsistency between the backup database data and the main database data, resulting in data loss. Alternatively, the main database uses an in-memory database method, and the backup database uses an external storage actual database method. Although the main database data processing speed is fast, it is also more prone to damage. In the event of an abnormal power outage, if the backup database is damaged, the data will be completely lost.
[0004] It can be seen that the existing database backup measures cannot achieve real-time backup. When an abnormal power outage occurs, it is easy to cause inconsistency between the backup database data and the main database data, resulting in data loss. Summary of the invention
[0005] The present invention provides a database real-time backup and recovery method and related equipment to solve the technical problem that the existing database backup measures cannot achieve real-time backup, which easily leads to inconsistency between the backup database data and the main database data in the event of abnormal power failure, resulting in data loss.
[0006] In order to achieve the above object, the present invention adopts the following technical solution:
[0007] A database real-time backup and recovery method, applied to SQLite database, comprising:
[0008] The master database performs master database self-check and recovery operations;
[0009] The main database performs the main database modification operation. After the main database modification operation is completed, the backup process startup instruction and the data corresponding to the main database modification operation are output to complete the main business process;
[0010] The backup database performs self-check and recovery operations according to the backup process startup instruction;
[0011] The backup database performs backup database modification operations according to the data corresponding to the primary database modification operations to complete the backup process;
[0012] The main database and the backup database are created and stored in different partitions of the non-volatile memory, and are both accessed through memory mapping; the backup database is created by copying the main database.
[0013] Furthermore, the creation process of the primary database and the backup database includes:
[0014] When it is determined that the master database does not exist, creating the master database in the first partition of the non-volatile memory;
[0015] Copying the main database to the second partition of the non-volatile memory to create a backup database;
[0016] The database file name in the main database is named after the first record value of the index field.
[0017] Furthermore, in the process of creating the master database, the maximum value of a single database file is set according to the device storage plan.
[0018] Furthermore, the specific steps of the master database self-check and recovery operation include:
[0019] Conduct self-inspection of the master database according to the inspection contents;
[0020] If there is an abnormality in the main database, the normal data page of the backup database is used to overwrite the abnormal data page of the main database;
[0021] The specific steps of the backup database self-check and recovery operation include:
[0022] Perform a self-check on the backup database based on the inspection content;
[0023] If there is an abnormality in the backup database, the abnormal data page of the backup database is overwritten with the normal data page of the primary database;
[0024] If both the primary database and the backup database have abnormal conditions, and the abnormal data pages of the primary database are the same as those of the backup database, the abnormal data pages of the primary database and the backup database are deleted at the same time, and the normal data pages are retained.
[0025] Furthermore, the specific steps of the master database performing the master database modification operation include:
[0026] When the main database storage is not full, connect to the main database in the main business process, execute the main database modification script, and determine the execution result of the main database write action;
[0027] If the execution result of the main database write operation is normal, the backup process startup instruction and the data corresponding to the main database modification operation are output to complete the main business process;
[0028] If the execution result of the main database write action is abnormal, the main database self-check and recovery operation is performed and the main database modification script is re-executed to complete the main business process; if the execution result of the main database write action is still abnormal after executing the first preset number of main database self-check and recovery operations and the main database modification script, the main database write operation failure is returned, and the data corresponding to the main database modification script is stored in an ordinary file.
[0029] Furthermore, when the main database storage is full, the current main database is closed, and the current main database is changed to a historical database, the current backup database is deleted, and the main database and the corresponding backup database are recreated at the same time.
[0030] Furthermore, the specific steps of performing the backup database modification operation on the backup database include:
[0031] When the execution result of the write action on the primary database is normal, the backup database is connected in the backup process, and the backup database modification script is executed according to the data corresponding to the modification operation of the primary database, and the execution result of the write action on the backup database is determined at the same time;
[0032] If the execution result of the backup database write action is normal, the backup process is completed;
[0033] If the execution result of the backup database write action is abnormal, a backup database self-check and recovery operation is performed and the backup database modification script is re-executed to complete the backup process; if the execution result of the backup database write action is still abnormal after executing the second preset number of backup database self-check and recovery operations and the backup database modification script, the backup database write operation failure is returned, and the data corresponding to the backup database modification script is stored in an ordinary file.
[0034] A database real-time backup and recovery system, comprising:
[0035] The main database self-check and recovery module is used for the main database to perform self-check and recovery operations;
[0036] The main database modification operation module is used for the main database to perform the main database modification operation. After the main database modification operation is completed, the backup process startup instruction and the data corresponding to the main database modification operation are output to complete the main business process;
[0037] The backup database self-check and recovery module is used to perform backup database self-check and recovery operations according to the backup process startup instruction;
[0038] The backup database modification operation module is used for performing backup database modification operations on the backup database according to the data corresponding to the primary database modification operation to complete the backup process;
[0039] The main database and the backup database are created and stored in different partitions of the non-volatile memory, and are both accessed through memory mapping; the backup database is created by copying the main database.
[0040] A device comprising:
[0041] Memory for storing computer programs;
[0042] The processor is used to execute the steps of the above database real-time backup and recovery method.
[0043] A computer-readable storage medium stores a computer program, which is used to implement the steps of the above-mentioned database real-time backup and recovery method when executed by a processor.
[0044] Compared with the prior art, the present invention has the following beneficial effects:
[0045] The present invention also provides a database real-time backup and recovery method. The method realizes fast reading, writing and synchronization of data by creating and storing the main database and the backup database in different partitions of the non-volatile memory respectively and accessing them by memory mapping. When the main database is modified, the backup process of the backup database is synchronously triggered to ensure that the backup database can reflect the latest status of the main database in real time and accurately; the real-time backup and self-check recovery mechanism effectively avoids data inconsistency and data loss problems caused by abnormal power failures and the like, and is particularly suitable for application scenarios with high data sensitivity and real-time requirements; because both the main and backup databases are accessed by memory mapping, the time consumption of system calls is reduced, thereby ensuring the stable operation of the system software and the integrity and security of the data. The method can significantly improve the backup and recovery capabilities of the SQLite database, and provide users with a more stable and reliable data protection solution.
[0046] Preferably, in the present invention, by copying the main database to the second partition of the non-volatile memory, the initial state of the backup database is consistent with the main database; by storing the two databases in different partitions, the possibility of data conflicts is reduced and the reliability of the system is improved; the data file name in the main database is named after the first record value of the index field, which facilitates the completion of database data query work.
[0047] Preferably, in the present invention, the maximum value of a single database file is set according to the device storage plan, which helps to disperse the storage of massive data, reduce the probability of database damage, and increase the database access speed.
[0048] Preferably, in the present invention, anomalies in the database are discovered and repaired in a timely manner through a self-check mechanism, thereby ensuring the integrity and consistency of the data; when the same anomaly occurs in the primary database and the backup database at the same time, the abnormal data page is deleted and the normal data page is retained, thereby effectively preventing the propagation of data errors and improving the success rate of data recovery.
[0049] Preferably, in the present invention, the main database modification operation includes the judgment of the execution result of the main database write action, exception handling and retry mechanism, etc., which ensures the correctness and reliability of the main database modification operation; at the same time, the data that failed to be modified is stored in ordinary files, providing additional protection for data recovery.
[0050] Further preferably, in the present invention, when the main database is full, the problem of insufficient storage space is effectively solved by closing the current main database and creating a new main database and a backup database.
[0051] Preferably, in the present invention, the backup database modification operation includes the judgment of the execution result of the backup database write action, exception handling and retry mechanism, etc., to ensure that the backup database can accurately and timely reflect the modification status of the main database; at the same time, the data that failed to be modified is stored in ordinary files, which provides additional protection for data recovery, thereby further enhancing the system's fault tolerance and data recovery capabilities. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] Figure 1 A diagram of the operating environment of a database real-time backup and recovery method provided by an embodiment of the present invention;
[0053] Figure 2 A schematic diagram of a process of accessing a database file through memory mapping provided by an embodiment of the present invention;
[0054] Figure 3 A schematic diagram of database distribution provided in an embodiment of the present invention;
[0055] Figure 4 A flowchart of a database real-time backup and recovery method provided by an embodiment of the present invention;
[0056] Figure 5 A flowchart of creating a database provided by an embodiment of the present invention;
[0057] Figure 6 A flowchart of database self-checking and recovery provided by an embodiment of the present invention;
[0058] Figure 7 A flowchart of a master database data modification operation provided by an embodiment of the present invention;
[0059] Figure 8 A flowchart of a backup database data modification operation provided by an embodiment of the present invention;
[0060] Fig. 9 A flowchart of data query provided by an embodiment of the present invention;
[0061] Fig.10 A flowchart of a database real-time backup and recovery method provided by the present invention;
[0062] Fig.11 A schematic diagram of the structure of a database real-time backup and recovery system provided by the present invention. DETAILED DESCRIPTION
[0063] The present invention will be further described below in conjunction with the embodiments and drawings:
[0064] Example 1
[0065] As described in the background technology, due to software compatibility, memory trampling, system locks and other reasons, the SQLITE database is easily damaged, affecting the stable operation of the system software and causing data loss. Especially in data-sensitive scenarios, data loss can cause immeasurable losses. Existing database backup measures usually use external devices for backup, such as external storage or servers; although the backup function can be achieved with the help of external devices, since real-time backup cannot be achieved, in the event of an abnormal power outage, it is easy to cause the backup database data and the main database data to be inconsistent, resulting in data loss; or, the main database uses a memory database method, and the backup database uses an external storage actual database method. Although the main database data processing speed is fast, it is also more prone to damage. In the event of an abnormal power outage, if the backup database is also damaged, the data will be completely lost.
[0066] In the above data backup measures, the memory database is usually used as the main business database, and the external storage is used as the backup database. For example, Chinese patent CN102456066A discloses a device based on the Android system and a method for data storage, CN107193871A discloses a database repair method and device, and CN102945195A discloses a primary-backup redundant replication method based on the SQLite database, which has the following defects:
[0067] First, although the in-memory database has a fast processing speed, it is more susceptible to damage and data loss.
[0068] Second, non-real-time backup, abnormal power outages, or damage to the backup database will lead to an increase in the amount of data loss.
[0069] Third, as the amount of data stored in a single database file increases, the data storage and access time will increase, and the database file will be frequently read and written, making it more likely to be damaged. Moreover, due to the large amount of data, it is difficult to recover after damage and the recovery time is too long.
[0070] Fourth, using a server to back up and restore data has a relatively high latency and requires additional configuration of an external server.
[0071] In order to solve the problems that the device cannot access the database normally due to the damage of the database file structure during long-term operation, or the database is damaged due to abnormal power failure during the access process, the present invention provides a database real-time backup and recovery method. The method uses memory mapping to achieve real-time backup and real-time automatic recovery of the main database and the backup database through process isolation on two physical partitions, thereby achieving the purpose of reliable and stable operation of system data.
[0072] In order to better understand the database real-time backup and recovery method provided in this embodiment, the SQLite database is now described:
[0073] SQLite database is a lightweight embedded relational database management system developed by the open source software organization SQLite Consortium, which was founded by D. Richard Hipp. SQLite database occupies an important position in the database field with its unique characteristics and wide application scenarios. The design goal of SQLite database is embedded, and it has been used in many embedded products. It takes up very little resources and only requires a few hundred kilobytes (several hundred KB) of memory in embedded devices. SQLite database can support mainstream operating systems such as Windows / Linux / Unix, and can be combined with many programming languages, such as Tcl, C#, PHP, Java, etc. It also has an ODBC interface. And compared with Mysql and PostgreSQL, two world-renowned open source database management systems, SQLite database has a faster processing speed.
[0074] like Figure 1 As shown, this embodiment provides a database real-time backup and recovery method, which relies on running Figure 1 In the operating environment shown.
[0075] like Figure 2As shown, the SQLite database in a device runs on a Linux / Unix / Windows system, and at least two storage partitions are created in the non-volatile memory. At the same time, the database is accessed in a memory mapping manner.
[0076] like Figure 3 As shown, Figure 3 The overall design of the database; in this embodiment, the size of a single database file can be reasonably set according to the device storage plan, wherein the device storage plan specifically includes the size of the main business and the storage medium. On partition 1 (i.e., the first partition), there is a main database, historical database 1, ..., historical database n; on partition 2 (i.e., the second partition), there is a backup database, historical database n+1, ..., historical database n+m; wherein m and n are both positive integers; only the main database will be backed up, and the historical database will set the deletion condition according to the size of the storage space, and delete the older historical database files. The reason for adopting the above design is that the modification operation of the main database file is relatively frequent, and the probability of damage is relatively high, while the data of the historical database has been basically processed, and most of them are only query operations, and the probability of damage is relatively low; the above modification operation specifically includes the add / delete / modify operation for the data.
[0077] In this embodiment, the database file name is named after the content of the first record of a certain index field, so as to facilitate rapid search and positioning by binary search after sorting during query.
[0078] like Figure 4 As shown, this embodiment provides a database real-time backup and recovery method, including:
[0079] The database access method is to connect to the database through memory mapping, that is, mapping the database file on the partition to the memory for direct reading and writing, reducing the resources and time consumed by system IO calls, achieving fast access to the database, and storing data safely and quickly on the storage medium.
[0080] Before performing read (query) or write (add / delete / modify) operations on the primary (backup) database, an integrity check is required. The check content includes "out-of-order records, missing pages, incorrect records, missing indexes, unique constraints, non-empty constraints", etc. If the check finds an abnormality, the normal data pages of the normal (primary / backup) database file will be used to restore the abnormal data pages of the abnormal (backup / primary) database file.
[0081] When the database integrity check is normal, the main database performs a write operation to complete the main business process. After the main database completes the write operation, the backup process is started, and the backup database is accessed to perform real-time backup synchronously. All read operations are implemented by accessing the main database file and the historical database file. Here, multiple verifications have been performed. After synchronously executing multiple batches of the same SQL statements, the two independent data files, that is, the file contents in the two independent databases, namely the main database and the backup database, are completely consistent when compared in binary format.
[0082] The database real-time backup and recovery method provided in this embodiment is further explained in conjunction with the accompanying drawings, including the following specific steps:
[0083] Step 1: Figure 5 As shown in the figure, the process of creating the primary database and the backup database is as follows:
[0084] (1) Prepare the SQL (add / delete / modify) statements that need to be processed;
[0085] (2) Determine whether the main database exists. If not, create the main database on partition 1. The database file name is named after the first record value of a certain index field. Create a table structure and set the maximum value of a single database file according to the device storage plan, that is, according to the size of the main business and storage medium.
[0086] (3) Back up the main database on partition 1 on partition 2 to create a backup database.
[0087] Step 2: If Figure 6 As shown, the self-check and recovery process of the primary database and backup database is as follows:
[0088] (1) Use the "PRAGMA integrity_check" SQL statement to perform integrity checks on the primary database and the backup database, including "out-of-order records, missing pages, incorrect records, missing indexes, unique constraints, non-null constraints", etc.
[0089] (2) If the primary database is abnormal and the backup database is normal, the primary database is restored using the backup database. Specifically, the primary database and the backup database are connected through memory mapping, and according to the abnormal location marked in the integrity check, the abnormal data page in the abnormal primary database is overwritten and restored with the normal data page of the backup database;
[0090] (3) If the backup database is abnormal and the primary database is normal, the backup database is restored using the primary database. Specifically, the primary database and the backup database are connected through memory mapping, and according to the abnormal position marked in the integrity check, the abnormal data page in the abnormal backup database is overwritten and restored with the normal data page of the primary database;
[0091] (4) If both the primary database and the backup database are abnormal, the primary database and the backup database will recover each other. Specifically, the primary database and the backup database are connected through memory mapping, and according to the abnormal position marked in the integrity check, the normal data page corresponding to the primary database is used to overwrite the abnormal data page in the backup database at the same position, and the normal data page corresponding to the backup database is used to overwrite the abnormal data page in the primary database at the same position; if the primary database and the backup database are consistent, the abnormal data page is removed and the normal data page is retained to ensure the correctness of most data pages.
[0092] In this embodiment, memcpy (ie, a memory copy function used by C and C++) is used to implement overwriting of the normal data pages of the backup database on the primary database and overwriting of the normal data pages of the primary database on the backup database.
[0093] Step 3: If Figure 7 As shown, the main database modification operation includes the data addition / deletion / modification operation of the main database, and the specific steps are as follows:
[0094] (1) If the main database storage is full, close the current main database, change the current main database to the historical database, delete the current backup database, and recreate the main database and the corresponding backup database according to step 1;
[0095] (2) If the main database storage is not full, the main business process connects to the main database through memory mapping, executes the add / delete / modify script, and determines the execution result of the write action of the main database;
[0096] (3) If the addition / deletion / modification of the main database fails, the self-check and recovery process of the main database is performed according to step 2. Try up to 3 times. If it still fails, the main database write operation fails and the data is stored in a normal file for subsequent use to complete the recovery data. Generally, when the above problems occur, there is usually a hardware problem.
[0097] Step 4: Figure 8 As shown, the backup database modification operation is also called the database data backup operation, wherein the backup database modification operation includes the backup database data add / delete / modify operation, and the specific steps are as follows:
[0098] (1) If the write operation to the primary database is successful, the added / deleted / modified data is carried, a backup process is created, and the backup database is connected through memory mapping in the backup process. At this time, the primary process can continue business-related interactions, and the backup action has no effect on the main business process; thus completing the backup process.
[0099] (2) If the backup database addition / deletion / modification fails, perform the backup database self-check and recovery process according to step 2, and try up to 3 times. If it still fails, return to the backup database write operation failure, and store the data in a normal file for subsequent use to complete the recovery data. Generally, when the above problems occur, there is usually a hardware problem.
[0100] In this embodiment, after the backup process completes the backup process, the backup process self-destructs;
[0101] At this point, a set of real-time data backup and recovery processes in the SQLite database has been completed.
[0102] like Fig. 9 As shown, in this embodiment, step 5 is also included: database read (query) operation, and the specific steps are as follows:
[0103] (1) Confirm the index field data in the query conditions;
[0104] (2) Sort in ascending order according to the file name string;
[0105] (3) Use the binary search method to find out which two database files the index field is between, and determine that the former is the database where the record is located;
[0106] (4) After connecting to the main database or historical database through memory mapping, quickly load the corresponding record information;
[0107] (5) If there is an abnormality in accessing the primary database, perform the primary database self-check and recovery process according to step 2.
[0108] In an embedded device, when processing data, a single database file is set to a reasonable size, and is stored according to a main database file, a backup database file, and multiple historical database file modes, and the database file to be queried is determined by a fixed index and a binary search method; the method provided in the embodiment implements real-time backup and recovery of the main database and the backup database by means of memory mapping, thereby ensuring stable use of data in the device.
[0109] In order to solve the problems that the database file structure is damaged during the long-term operation of the device, resulting in the inability to access normally, or the database is damaged due to abnormal power failure during the access process, this embodiment provides a database real-time backup and recovery method, which solves the above problems through real-time rapid data backup and rapid recovery, and through memory mapping, the main database and the backup database are backed up and restored in real time to ensure the stable use of data in the device; this method has the following beneficial effects:
[0110] First, by reducing the size of a single database file, the probability of database damage is reduced and the database access speed is increased;
[0111] Second, the use of memory mapping to access the database improves the database reading and writing speed and ensures the security of database data;
[0112] Third, the real-time backup and real-time recovery methods enhance the reliability of system operation, and users are unaware of it. It can be seen that this method improves the robustness and availability of the database management system and protects users from data loss and service interruptions.
[0113] In summary, the present invention provides a database real-time backup and recovery method, which has the following advantages compared to existing database backup measures:
[0114] This method accesses the main database and backup database located in different memory partitions through memory mapping, ensuring the speed and efficiency of data access. At the same time, this method realizes the real-time synchronous update of the main database and the backup database, including self-checking, recovery and modification operations, ensuring the integrity and consistency of the data. In particular, when an abnormality occurs in the database, it can automatically use the normal data page of the other database to overwrite and repair, further enhancing the reliability of the data and the stability of the system. In addition, this method also takes into account the management of database storage capacity, including the maximum value setting of the database file and the processing strategy when the storage is full, effectively avoiding the risk of data loss due to insufficient storage. Overall, this technical solution significantly improves the data protection capability of the SQLite database and provides a strong guarantee for the stable operation of the application system.
[0115] Example 2
[0116] like Fig.10 As shown, this embodiment provides a database real-time backup and recovery method, including the following steps:
[0117] S1: The main database performs self-check and recovery operations on the main database; the main database and the backup database are created and stored in different partitions of the non-volatile memory, and are accessed through memory mapping; the backup database is created by copying the main database.
[0118] Specifically, the process of creating the primary database and the backup database includes:
[0119] When it is determined that the master database does not exist, creating the master database in the first partition of the non-volatile memory;
[0120] Copying the main database to the second partition of the non-volatile memory to create a backup database;
[0121] The data file name in the main database is named after the first record value of the index field.
[0122] In particular, during the creation of the master database, the maximum size of a single database file is set according to the device storage plan.
[0123] Specifically, the specific steps of the master database self-check and recovery operation include:
[0124] Conduct self-inspection of the master database according to the inspection contents;
[0125] When an abnormal situation occurs in the primary database, the normal data pages of the backup database are used to overwrite the abnormal data pages of the primary database.
[0126] S2: The main database performs a main database modification operation. After the main database modification operation is completed, the backup process startup instruction and the data corresponding to the main database modification operation are output to complete the main business process;
[0127] Specifically, the specific steps of the master database modification operation include:
[0128] When the main database storage is not full, connect to the main database in the main business process, execute the main database modification script, and determine the execution result of the main database write action;
[0129] If the execution result of the main database write operation is normal, the backup process startup instruction and the data corresponding to the main database modification operation are output to complete the main business process;
[0130] If the execution result of the main database write action is abnormal, the main database self-check and recovery operation is performed and the main database modification script is re-executed to complete the main business process; if the execution result of the main database write action is still abnormal after executing the first preset number of main database self-check and recovery operations and the main database modification script, the main database write operation failure is returned, and the data corresponding to the main database modification script is stored in an ordinary file.
[0131] When the main database storage is full, close the current main database, change the current main database to a historical database, delete the current backup database, and recreate the main database and the corresponding backup database.
[0132] S3: The backup database performs self-check and recovery operations according to the backup process startup instruction;
[0133] Here, the specific steps of the backup database self-check and recovery operation include:
[0134] Perform a self-check on the backup database based on the inspection content;
[0135] When an abnormal situation occurs in the backup database, the abnormal data page of the backup database is overwritten with the normal data page of the primary database;
[0136] When both the primary database and the backup database have abnormal situations, and the abnormal data pages of the primary database are the same as those of the backup database, the abnormal data pages of the primary database and the backup database are deleted at the same time, and the normal data pages are retained.
[0137] S4: The backup database performs a backup database modification operation according to the data corresponding to the primary database modification operation to complete the backup process;
[0138] Specifically, the specific steps of backing up the database and performing the backup database modification operation include:
[0139] When the execution result of the write action on the primary database is normal, the backup database is connected in the backup process, and the backup database modification script is executed according to the data corresponding to the modification operation of the primary database, and the execution result of the write action on the backup database is determined at the same time;
[0140] If the execution result of the backup database write action is normal, the backup process is completed;
[0141] If the execution result of the backup database write action is abnormal, a backup database self-check and recovery operation is performed and the backup database modification script is re-executed to complete the backup process; if the execution result of the backup database write action is still abnormal after executing the second preset number of backup database self-check and recovery operations and the backup database modification script, the backup database write operation failure is returned, and the data corresponding to the backup database modification script is stored in an ordinary file.
[0142] like Fig.11 As shown, this embodiment also provides a database real-time backup and recovery system, including: a main database self-check and recovery module, which is used for the main database to perform main database self-check and recovery operations; a main database modification operation module, which is used for the main database to perform main database modification operations. After the main database modification operation is completed, the backup process startup instruction and the data corresponding to the main database modification operation are output to complete the main business process; the backup database self-check and recovery module, which is used for the backup database to perform backup database self-check and recovery operations according to the backup process startup instruction; the backup database modification operation module, which is used for the backup database to perform backup database modification operations according to the data corresponding to the main database modification operation to complete the backup process; wherein, the main database and the backup database are created and stored in different partitions of the non-volatile memory, and are both accessed through memory mapping; the backup database is created by copying the main database.
[0143] The present invention also provides a device, comprising: a memory for storing a computer program; and a processor for implementing the steps of the database real-time backup and recovery method when executing the computer program.
[0144] When the processor executes the computer program, the above-mentioned steps of real-time backup and recovery of the database are implemented, for example: the main database performs a main database self-check and recovery operation; the main database performs a main database modification operation, and after the main database modification operation is completed, outputs a backup process startup instruction and data corresponding to the main database modification operation to complete the main business process; the backup database performs a backup database self-check and recovery operation according to the backup process startup instruction; the backup database performs a backup database modification operation according to the data corresponding to the main database modification operation to complete the backup process; wherein the main database and the backup database are created and stored in different partitions of the non-volatile memory, and are both accessed through memory mapping; the backup database is created by copying the main database.
[0145] Alternatively, the processor implements the functions of each module in the above-mentioned system when executing the computer program, for example: a main database self-check and recovery module, which is used for the main database to perform main database self-check and recovery operations; a main database modification operation module, which is used for the main database to perform main database modification operations. After the main database modification operation is completed, a backup process startup instruction and data corresponding to the main database modification operation are output to complete the main business process; a backup database self-check and recovery module, which is used for the backup database to perform backup database self-check and recovery operations according to the backup process startup instruction; a backup database modification operation module, which is used for the backup database to perform backup database modification operations according to the data corresponding to the main database modification operation to complete the backup process; wherein, the main database and the backup database are created and stored in different partitions of the non-volatile memory, and are both accessed through memory mapping; the backup database is created by copying the main database.
[0146] Exemplarily, the computer program may be divided into one or more modules / units, which are stored in the memory and executed by the processor to complete the present invention. The one or more modules / units may be a series of computer program instruction segments capable of completing preset functions, and the instruction segments are used to describe the execution process of the computer program in the database real-time backup and recovery device. For example, the computer program can be divided into a main database self-check and recovery module, a main database modification operation module, a backup database self-check and recovery module and a backup database modification operation module; the specific functions of each module are as follows: the main database self-check and recovery module is used for the main database to perform main database self-check and recovery operations; the main database modification operation module is used for the main database to perform main database modification operations. After the main database modification operation is completed, the backup process startup instruction and the data corresponding to the main database modification operation are output to complete the main business process; the backup database self-check and recovery module is used for the backup database to perform backup database self-check and recovery operations according to the backup process startup instruction; the backup database modification operation module is used for the backup database to perform backup database modification operations according to the data corresponding to the main database modification operation to complete the backup process; wherein, the main database and the backup database are created and stored in different partitions of the non-volatile memory, and are both accessed through memory mapping; the backup database is created by copying the main database.
[0147] The database real-time backup and recovery device can be a computing device such as a desktop computer, a notebook, a PDA, and a cloud server. The database real-time backup and recovery device may include, but is not limited to, a processor and a memory. Those skilled in the art will appreciate that the above are examples of database real-time backup and recovery devices and do not constitute a limitation on the database real-time backup and recovery device. The database real-time backup and recovery device may include more components than the above, or a combination of certain components, or different components. For example, the database real-time backup and recovery device may also include input and output devices, network access devices, buses, etc.
[0148] The processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc. The processor is the control center of the database real-time backup and recovery, and uses various interfaces and lines to connect various parts of the entire database real-time backup and recovery device.
[0149] The memory can be used to store the computer program and / or module, and the processor implements various functions of the database real-time backup and recovery device by running or executing the computer program and / or module stored in the memory and calling the data stored in the memory.
[0150] The memory may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the mobile phone (such as audio data, a phone book, etc.), etc. In addition, the memory may include a high-speed random access memory, and may also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0151] The present invention also provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method for real-time database backup and recovery are implemented.
[0152] If the modules / units integrated in the real-time database backup and recovery system are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium.
[0153] Based on such understanding, all or part of the processes in the above-mentioned real-time database backup and recovery method of the present invention can also be completed by instructing relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by a processor, the steps of the above-mentioned real-time database backup and recovery method can be realized. Among them, the computer program includes computer program code, and the computer program code can be in the form of source code, object code, executable file or preset intermediate form, etc.
[0154] The computer-readable storage medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disc, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal, and software distribution medium, etc.
[0155] It should be noted that the content included in the computer-readable storage medium can be appropriately increased or decreased according to the requirements of legislation and patent practice within the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, the computer-readable storage medium does not include electrical carrier signals and telecommunication signals.
[0156] The above embodiments are only one of the implementation manners capable of realizing the technical solution of the present invention. The scope of protection required by the present invention is not only limited by this embodiment, but also includes any changes, substitutions and other implementation manners that are easily conceivable by those skilled in the art within the technical scope disclosed by the present invention.
Claims
1. A database real-time backup and recovery method, applied to SQLite database, characterized in that: include: The master database performs master database self-check and recovery operations; The main database performs the main database modification operation. After the main database modification operation is completed, the backup process startup instruction and the data corresponding to the main database modification operation are output to complete the main business process; The backup database performs self-check and recovery operations according to the backup process startup instruction; The backup database performs backup database modification operations according to the data corresponding to the primary database modification operations to complete the backup process; The main database and the backup database are created and stored in different partitions of the non-volatile memory, and are both accessed through memory mapping; the backup database is created by copying the main database; The specific steps of the master database self-check and recovery operation include: Conduct self-inspection of the master database according to the inspection contents; If there is an abnormality in the main database, the normal data page of the backup database is used to overwrite the abnormal data page of the main database; The specific steps of the backup database self-check and recovery operation include: Perform a self-check on the backup database based on the inspection content; If there is an abnormality in the backup database, the abnormal data page of the backup database is overwritten with the normal data page of the primary database; If both the primary database and the backup database have abnormal conditions, and the abnormal data pages of the primary database are the same as those of the backup database, the abnormal data pages of the primary database and the backup database are deleted at the same time, and the normal data pages are retained.
2. The database real-time backup and recovery method according to claim 1, characterized in that: The creation process of the primary database and the backup database includes: When it is determined that the master database does not exist, creating the master database in the first partition of the non-volatile memory; Copying the main database to the second partition of the non-volatile memory to create a backup database; The database file name in the main database is named after the first record value of the index field.
3. The database real-time backup and recovery method according to claim 2, characterized in that: When creating the master database, set the maximum size of a single database file based on the device storage plan.
4. The database real-time backup and recovery method according to claim 1, characterized in that: The specific steps of the master database performing the master database modification operation include: When the main database storage is not full, connect to the main database in the main business process, execute the main database modification script, and determine the execution result of the main database write action; If the execution result of the main database write operation is normal, the backup process startup instruction and the data corresponding to the main database modification operation are output to complete the main business process; If the execution result of the main database write action is abnormal, the main database self-check and recovery operation is performed and the main database modification script is re-executed to complete the main business process; if the execution result of the main database write action is still abnormal after executing the first preset number of main database self-check and recovery operations and the main database modification script, the main database write operation failure is returned, and the data corresponding to the main database modification script is stored in an ordinary file.
5. The database real-time backup and recovery method according to claim 4, characterized in that: When the main database storage is full, close the current main database, change the current main database to a historical database, delete the current backup database, and recreate the main database and the corresponding backup database.
6. The database real-time backup and recovery method according to claim 4, characterized in that: The specific steps for backing up the database and performing the modification operation on the backup database include: When the execution result of the write action on the primary database is normal, the backup database is connected in the backup process, and the backup database modification script is executed according to the data corresponding to the modification operation of the primary database, and the execution result of the write action on the backup database is determined at the same time; If the execution result of the backup database write action is normal, the backup process is completed; If the execution result of the backup database write action is abnormal, a backup database self-check and recovery operation is performed and the backup database modification script is re-executed to complete the backup process; if the execution result of the backup database write action is still abnormal after executing the second preset number of backup database self-check and recovery operations and the backup database modification script, the backup database write operation failure is returned, and the data corresponding to the backup database modification script is stored in an ordinary file.
7. A database real-time backup and recovery system, characterized in that: include: The main database self-check and recovery module is used for the main database to perform self-check and recovery operations; The main database modification operation module is used for the main database to perform the main database modification operation. After the main database modification operation is completed, the backup process startup instruction and the data corresponding to the main database modification operation are output to complete the main business process; The backup database self-check and recovery module is used to perform backup database self-check and recovery operations according to the backup process startup instruction; The backup database modification operation module is used for performing backup database modification operations on the backup database according to the data corresponding to the primary database modification operation to complete the backup process; The main database and the backup database are created and stored in different partitions of the non-volatile memory, and are both accessed through memory mapping; the backup database is created by copying the main database; The specific steps of the master database self-check and recovery operation include: Conduct self-inspection of the master database according to the inspection contents; If there is an abnormality in the main database, the normal data page of the backup database is used to overwrite the abnormal data page of the main database; The specific steps of the backup database self-check and recovery operation include: Perform a self-check on the backup database based on the inspection content; If there is an abnormality in the backup database, the abnormal data page of the backup database is overwritten with the normal data page of the primary database; If both the primary database and the backup database have abnormal conditions, and the abnormal data pages of the primary database are the same as those of the backup database, the abnormal data pages of the primary database and the backup database are deleted at the same time, and the normal data pages are retained.
8. A device, characterized in that: include: Memory for storing computer programs; A processor is used to implement the steps of the database real-time backup and recovery method according to any one of claims 1 to 6 when executing the computer program.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, it is used to implement the steps of the database real-time backup and recovery method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Android-system-based device and data storage method
CN102456066A
Master-standby redundancy replication method based on SQLite databases
CN102945195A
Database restoration method and device
CN107193871A
Web development environment-oriented relational database data backtracking method
CN107145403A
Method for backing up mysql database and recovering verification availability based on xtrabackup
CN112540875A