Data backup method, data backup system and intelligent card
By dynamically selecting backup strategies in the smart card and combining the transaction flag area and control area, the contradiction between performance and storage space utilization in smart card data backup during power failure protection is resolved, achieving efficient and secure data update and recovery.
Patent Information
- Application Number
- CN202511619005.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-06
- Publication Date
- 2026-02-06
AI Technical Summary
Existing smart card data backup methods, while ensuring power loss protection, struggle to balance performance and storage space utilization. Commonly used backup methods suffer from long write times or large storage space requirements.
The backup strategy is dynamically selected based on the data usage frequency. By setting up a transaction flag area, transaction control area, double-page backup area and transaction backup data area in the non-volatile memory of the smart card, a combination of high-performance backup strategy and high space utilization backup strategy is adopted to realize intelligent data backup and optimized allocation of storage resources.
A balance is struck between backup performance and storage space utilization, ensuring the atomicity and security of data recovery after unexpected power outages of smart cards, thus achieving efficient and secure data updates.
Smart Images

Figure CN121478552A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of smart card storage technology, and in particular to a data backup method, a data backup system, and a smart card. Background Technology
[0002] With the widespread application of smart cards in communication, finance, and identity authentication scenarios, their internal non-volatile memory needs frequent data updates. To ensure data consistency and reliability in abnormal situations such as power outages, smart card systems typically incorporate data backup and transaction protection mechanisms.
[0003] In the existing technology, common backup methods include: one is to copy the original data to the transaction backup area before the data is updated. This method takes up less space but takes a long time to write; the other is to avoid multiple erase and write operations by using double-page alternating storage. This method is faster but takes up more storage space. Summary of the Invention
[0004] In view of this, the purpose of this application is to provide a data backup method, a data backup system and a smart card that dynamically selects a backup strategy based on the data usage frequency, thereby ensuring power failure protection while taking into account performance and storage space utilization.
[0005] In a first aspect, this application provides a data backup method applied to a smart card; the smart card includes non-volatile memory, and a transaction flag area is pre-set in the non-volatile memory; the data backup method includes: In response to the received transaction start instruction, a transaction start flag is written to the transaction flag area, and the corresponding transaction identifier is set for the current transaction; the transaction start instruction carries the data information of the data object to be updated.
[0006] Determine whether the usage frequency of the data object to be updated exceeds a preset usage frequency threshold.
[0007] If the usage frequency exceeds the preset usage frequency threshold, the data object to be updated will be backed up according to the preset high-performance backup strategy; if the usage frequency does not exceed the preset usage frequency threshold, the data object to be updated will be backed up according to the preset high space utilization backup strategy.
[0008] After the backup is completed, a transaction commit flag is written to the transaction flag area to indicate that the current transaction has ended.
[0009] In an optional implementation, a transaction control area is further provided in the non-volatile memory; after the backup is completed, the method further includes: Write the transaction identifier and its corresponding backup record into the transaction control area.
[0010] In an optional implementation, the non-volatile memory further includes a transaction backup data area; the method also includes: After power-on again, check whether the transaction flag area includes the transaction start flag and the transaction commit flag.
[0011] If the transaction flag area includes a transaction start flag but does not include a transaction commit flag, it is determined that the target transaction corresponding to the transaction start flag is in an execution interruption state.
[0012] Traverse the transaction control area to obtain the target backup record corresponding to the target transaction.
[0013] Identify the backup strategy in the target backup record.
[0014] If the backup strategy is the preset high-performance backup strategy, the original data of the data object to be updated corresponding to the target transaction is kept in a valid state.
[0015] If the backup strategy is a preset high space utilization backup strategy, the original backup data in the transaction backup data area will be restored to the storage location of the data object to be updated based on the target backup record.
[0016] In an optional implementation, if the backup strategy is a preset high space utilization backup strategy, after the step of restoring the original backup data in the transaction backup data area to the storage location of the data object to be updated according to the target backup record, the method further includes: Delete the transaction start flag from the transaction flag area.
[0017] In an optional implementation, the non-volatile memory is further provided with a double-page backup area; each data object in the double-page backup area is provided with two storage pages.
[0018] If the usage frequency exceeds the preset usage frequency threshold, the steps for backing up the data objects to be updated according to the preset high-performance backup strategy include: Identify the page state of the stored page; the page state includes valid state and invalid state.
[0019] Write the new data of the data object to be updated to the first storage page, which is in an invalid state.
[0020] Update the page state of the first storage page to a valid state, and update the page state of the second storage page, which is in a valid state, to an invalid state.
[0021] In an optional implementation, a transaction backup data area is also provided in the non-volatile memory.
[0022] If the usage frequency does not exceed the preset usage frequency threshold, the steps for backing up the data objects to be updated according to the preset high space utilization backup strategy include: Before erasing and writing the storage location of the data object to be updated, the original data of the data object to be updated is written to the transaction backup data area to obtain the backup original data.
[0023] In an optional implementation, after the step of writing a transaction start flag in the transaction flag area in response to a received transaction start instruction, the method further includes: Determine whether the first available storage space of the non-volatile memory meets the space requirements for executing a preset high-performance backup strategy.
[0024] If the first available storage space is insufficient to meet the space requirements, the data objects to be updated will be backed up according to the preset high space utilization backup strategy.
[0025] In an optional implementation, a dual-page backup area is also provided in the non-volatile memory.
[0026] Before the step of backing up the data objects to be updated according to the preset high-performance backup strategy, the method also includes: Determine whether the second available storage space in the dual-page backup area meets the target storage space requirements of the data object to be updated.
[0027] If the second available storage space does not meet the target storage space requirements, the data objects to be updated will be backed up according to the preset high space utilization backup strategy.
[0028] Secondly, this application provides a data backup system applied to a smart card; the smart card includes a non-volatile memory, and the non-volatile memory has a pre-set transaction flag area; the data backup system includes: The startup module is used to respond to the received transaction startup command by writing a transaction start flag in the transaction flag area and setting the corresponding transaction identifier for the current transaction; the transaction startup command carries the data information of the data object to be updated.
[0029] The execution module is used to determine whether the usage frequency of the data object to be updated exceeds a preset usage frequency threshold.
[0030] The execution module is also used to back up the data object to be updated according to a preset high-performance backup strategy if the usage frequency exceeds a preset usage frequency threshold; and to back up the data object to be updated according to a preset high space utilization backup strategy if the usage frequency does not exceed the preset usage frequency threshold.
[0031] The commit module is used to write a transaction commit flag in the transaction flag area after the backup is completed, to indicate that the current transaction has ended.
[0032] Thirdly, this application provides a smart card, including a smart card body and a non-volatile memory disposed in the smart card body; it also includes a data backup system as described in the above embodiments; the data backup system is disposed on the smart card body.
[0033] This application provides a data backup method, a data backup system, and a smart card. By dynamically selecting between a high-performance backup strategy and a high-space-utilization backup strategy based on the usage frequency of the data object and the available storage space, an effective balance can be achieved between backup performance and storage space utilization. This enables intelligent backup solutions and optimized allocation of storage resources. Furthermore, combined with the power-loss recovery mechanism of the transaction flag area and the transaction control area, the atomicity and security of data recovery after an unexpected power outage are guaranteed.
[0034] Other features and advantages of this application will be set forth in the following description and will be apparent in part from the description or may be learned by practicing the application.
[0035] To make the above-mentioned objectives, features and advantages of this application more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description
[0036] To more clearly illustrate the technical solutions in the specific embodiments of this application or the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0037] Figure 1 This is a schematic diagram of a smart card provided in an embodiment of this application; Figure 2 A schematic diagram of a data backup system provided in an embodiment of this application; Figure 3 A flowchart illustrating the data backup method provided in this application embodiment; Figure 4 A flowchart illustrating the data backup method after power-on as provided in this application embodiment; Figure 5 A flowchart illustrating the backup process of the preset high-performance backup strategy provided in this application embodiment.
[0038] Icons: 1-Smart card body; 2-Non-volatile memory; 3-Data backup system; 4-Startup module; 5-Execution module; 6-Submission module. Detailed Implementation
[0039] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0040] To facilitate understanding of this embodiment, the embodiments of this application will be described in detail below.
[0041] This application provides a smart card, referring to... Figure 1 The smart card includes a smart card body 1 and a non-volatile memory 2 disposed in the smart card body 1; it also includes a data backup system 3; the data backup system 3 is deployed on the smart card body 1.
[0042] Here, the smart card includes a smart card body 1 and a non-volatile memory 2 disposed within the smart card body 1. The non-volatile memory 2 can be Flash memory or EEPROM (Electrically Erasable Programmable Read-Only Memory), etc. The smart card body 1 is the physical carrier of the smart card, on which chips, processors, memory, and communication interfaces can be deployed.
[0043] To achieve efficient and secure data backup, the smart card also includes a data backup system 3, which is installed inside the smart card body 1. For example, the data backup can be achieved by the processor on the smart card body 1 executing a specific set of instructions stored in the non-volatile memory 2.
[0044] To facilitate the operation of the data backup system 3, the non-volatile memory 2 is pre-divided into functional areas, which may include: Transaction Flag Area, Transaction Control Area, Dual-Page Backup Area, and Transaction BackupData Area.
[0045] 1. The transaction flag area is used to atomically record the macroscopic state of a transaction.
[0046] Specifically, when a transaction begins, a transaction start flag is written to this area. When a transaction completes all operations normally, a transaction commit flag is written to this area.
[0047] By checking whether a start flag and a commit flag exist in the area, it is possible to determine whether the previous transaction was fully executed after the smart card is powered off and restarted.
[0048] 2. The transaction control area is used to record detailed backup information for each data object within a transaction.
[0049] Specifically, when a data object is backed up, its corresponding transaction identifier (TID) and backup record (such as backup strategy type, backup data location, etc.) are associated and written to this area.
[0050] The transaction control area is key to power failure recovery. The system can traverse this area to find all backup records belonging to a specific interrupted transaction, thereby enabling accurate recovery.
[0051] 3. The dual-page backup area is used to implement preset high-performance backup strategies (such as AB page backup).
[0052] In a two-page backup area, each data object corresponds to two storage pages (e.g., page A and page B). At any given time, only one page is valid, and the other is invalid. When writing new data, it is always written to the invalid page.
[0053] 4. The transaction backup data area is used to implement preset high space utilization backup strategies (such as general backup).
[0054] Specifically, when a high space utilization backup strategy is adopted, the original data (old data) of the data object is first copied to this area for backup, and then the original location is erased and overwritten.
[0055] The data stored in the transaction backup data area is used to write back to the original location during power failure recovery.
[0056] The smart card provided in this application embodiment, through its built-in data backup system, can intelligently and dynamically select between a high-performance backup strategy and a high-space utilization backup strategy based on the usage frequency of data objects and available storage space, thereby achieving an effective balance between backup performance and storage space utilization. Furthermore, combined with the management of the transaction flag area and control area, it ensures the atomicity and security of data updates in the event of an unexpected power outage.
[0057] Based on the above embodiments, this application provides a data backup system 3, which is installed inside the smart card body 1, as shown in the reference. Figure 2 Data backup system 3 includes: The startup module 4 is used to respond to the received transaction startup command, write the transaction start flag in the transaction flag area, and set the corresponding transaction identifier for the current transaction; the transaction startup command carries the data information of the data object to be updated.
[0058] Here, the startup module 4 initiates a transaction processing flow. Specifically, the startup module 4 responds to a transaction start command (e.g., from an upper-layer application), which carries data information (e.g., data identifier, new data content, etc.) of the data object to be updated. Upon response, the startup module 4 writes a transaction start flag into a preset transaction flag area in the non-volatile memory 2 to mark the start of a new transaction. Simultaneously, it assigns a corresponding transaction identifier (TID) to this new transaction, which uniquely identifies the transaction and all its contained data operations.
[0059] Execution module 5 is used to determine whether the usage frequency of the data object to be updated exceeds the preset usage frequency threshold.
[0060] Execution module 5 is also used to back up the data object to be updated according to a preset high-performance backup strategy if the usage frequency exceeds a preset usage frequency threshold; and to back up the data object to be updated according to a preset high space utilization backup strategy if the usage frequency does not exceed the preset usage frequency threshold.
[0061] Here, execution module 5 is used to perform data backup operations. Execution module 5 is connected to startup module 4 and obtains data information from the transaction startup instruction.
[0062] Execution module 5 is also used to determine whether the usage frequency of the data object to be updated exceeds a preset usage frequency threshold.
[0063] If the usage frequency exceeds the preset usage frequency threshold (for example, it is determined to be high-frequency data), the system will back up the data object to be updated according to the preset high-performance backup strategy.
[0064] If the usage frequency does not exceed the preset usage frequency threshold (for example, it is determined to be low-frequency data), the system will back up the data object to be updated according to the preset high space utilization backup strategy.
[0065] The commit module 6 is used to write a transaction commit flag in the transaction flag area after the backup is completed, to indicate that the current transaction has ended.
[0066] Here, the commit module 6 is used to commit the transaction after its execution, thus completing the transaction loop. The commit module 6 is connected to the execution module 5. After the execution module 5 completes the backup operation of all data objects in the transaction, the commit module 6 writes the transaction commit flag in the transaction flag area to indicate that the current transaction has been completely and successfully executed.
[0067] In an optional implementation, the non-volatile memory 2 is further provided with a transaction control area; the commit module 6 is further configured to: Write the transaction identifier and its corresponding backup record into the transaction control area.
[0068] Here, the transaction identifier of the current transaction and its corresponding backup record (e.g., the strategy used for this backup, the location of the data in the backup area, etc.) are associated and written into the transaction control area.
[0069] In an optional implementation, the non-volatile memory 2 is further provided with a transaction backup data area; the execution module 5 is further configured to: After power-on again, check whether the transaction flag area includes the transaction start flag and the transaction commit flag.
[0070] If the transaction flag area includes a transaction start flag but does not include a transaction commit flag, it is determined that the target transaction corresponding to the transaction start flag is in an execution interruption state.
[0071] Traverse the transaction control area to obtain the target backup record corresponding to the target transaction.
[0072] Identify the backup strategy in the target backup record.
[0073] If the backup strategy is the preset high-performance backup strategy, the original data of the data object to be updated corresponding to the target transaction is kept in a valid state.
[0074] If the backup strategy is a preset high space utilization backup strategy, the original backup data in the transaction backup data area will be restored to the storage location of the data object to be updated based on the target backup record.
[0075] Here, execution module 5 also carries out the power-off recovery function after the system is powered on again.
[0076] In an optional implementation, execution module 5 is further configured to: Delete the transaction start flag from the transaction flag area.
[0077] Here, after restoring the original data according to the high space utilization backup strategy, the transaction start flag in the transaction flag area is deleted. The transaction flag area is then restored to a clean state so that the system can execute subsequent new transactions normally.
[0078] In an optional implementation, the non-volatile memory 2 is further provided with a double-page backup area; each data object in the double-page backup area is provided with two storage pages.
[0079] Execution module 5 is also used to execute preset high-performance backup strategies, including: Identify the page state of the stored page; the page state includes valid state and invalid state.
[0080] Write the new data of the data object to be updated to the first storage page, which is in an invalid state.
[0081] Update the page state of the first storage page to a valid state, and update the page state of the second storage page, which is in a valid state, to an invalid state.
[0082] In an optional implementation, the non-volatile memory 2 is further provided with a transaction backup data area.
[0083] Execution module 5 is also used to execute a preset high space utilization backup strategy, including: Before erasing and writing the storage location of the data object to be updated, the original data of the data object to be updated is written to the transaction backup data area to obtain the backup original data.
[0084] In an optional implementation, execution module 5 is further configured to provide a space optimization strategy: Determine whether the first available storage space of the non-volatile memory 2 meets the space requirements for executing the preset high-performance backup strategy.
[0085] If the first available storage space is insufficient to meet the space requirements, the data objects to be updated will be backed up according to the preset high space utilization backup strategy.
[0086] In an optional implementation, the non-volatile memory 2 is further provided with a double-page backup area.
[0087] Module 5 is also used to provide another space optimization strategy: Determine whether the second available storage space in the dual-page backup area meets the target storage space requirements of the data object to be updated.
[0088] If the second available storage space does not meet the target storage space requirements, the data objects to be updated will be backed up according to the preset high space utilization backup strategy.
[0089] The data backup system provided in this application embodiment achieves an effective balance between backup performance and storage space utilization by intelligently selecting between high-performance backup strategies and high-space-utilization backup strategies based on the usage frequency of data objects. At the same time, it achieves atomic management of data updates and safe recovery from power failures based on the transaction flags written.
[0090] Based on the above embodiments, this application provides a data backup method applied to a smart card; the smart card includes non-volatile memory, and a transaction flag area is pre-set in the non-volatile memory; refer to Figure 3 The data backup method provided in this application includes: Step S101: In response to the received transaction start instruction, write the transaction start flag in the transaction flag area and set the corresponding transaction identifier for the current transaction; the transaction start instruction carries the data information of the data object to be updated.
[0091] Here, when the upper-layer application or operating system of the smart card needs to perform an atomic update of one or more data objects, a transaction initiation instruction is launched. The transaction initiation instruction carries the data information of the data object to be updated, which may include, but is not limited to: the unique identifier ID of the data object to be updated, the new data content, the data length, etc.
[0092] When the system responds to a transaction start command, it writes a transaction start flag into the transaction flag area. The transaction start flag is a status marker used to indicate that a transaction has been started but not yet committed.
[0093] In one specific embodiment, the transaction start flag can be a preset character written at a specific offset in the transaction flag area. In other embodiments, it can be a specific status bit in the transaction flag area changed from idle to busy, or a version number can be written.
[0094] When responding to a transaction start command, the system also assigns a corresponding transaction identifier (TID) to the newly started transaction. The transaction identifier is a unique number for this transaction, used to track all data operations belonging to this transaction during transaction execution, and serves as an associated index for writing backup records in the transaction control area.
[0095] In one specific embodiment, the transaction identifier can be a globally incrementing counter read from non-volatile memory. In other feasible embodiments, it can also be a unique ID generated based on a system timestamp or a random number.
[0096] Step S102: Determine whether the usage frequency of the data object to be updated exceeds the preset usage frequency threshold.
[0097] Here, after a transaction starts, the system needs to select an appropriate backup strategy for each data object to be updated carried in the transaction, and determine whether the usage frequency of the data object to be updated exceeds a preset usage frequency threshold.
[0098] The usage frequency can be metadata associated with the data object. For example, the system can maintain an access counter in non-volatile memory for each (or each class of) data object, incrementing the counter each time the data object is read or updated. In other embodiments, the usage frequency can also be directly specified by the upper-layer application based on business logic when initiating a transaction (e.g., marking certain key configuration data as high-frequency).
[0099] The preset usage frequency threshold is a standard value used to distinguish between high-frequency and low-frequency data. The preset usage frequency threshold can be pre-configured in the smart card by the system administrator according to the application scenario (e.g., read / write pressure, memory lifespan requirements).
[0100] In an alternative implementation, an available storage space check can be performed before determining the usage frequency of the data object to be updated. If the storage space (e.g., the total available space of non-volatile memory or specific backup area space) is determined to be insufficient, the system can skip the usage frequency check and directly force the use of a preset high space utilization backup strategy to prioritize ensuring system stability.
[0101] Step S103: If the usage frequency exceeds the preset usage frequency threshold, back up the data object to be updated according to the preset high-performance backup strategy; if the usage frequency does not exceed the preset usage frequency threshold, back up the data object to be updated according to the preset high space utilization backup strategy.
[0102] Here, different backup strategies are executed based on the judgment results.
[0103] If the usage frequency exceeds the preset usage frequency threshold (i.e., high-frequency data), the system will back up the data objects to be updated according to the preset high-performance backup strategy.
[0104] High performance here refers to the update operation itself having low system performance overhead and fast write speed. In a specific embodiment, the preset high-performance backup strategy can be a two-page backup (AB page backup) strategy. In other feasible embodiments, the high-performance backup strategy can also be other strategies with low update overhead and concentrated switching overhead, such as shadow page mechanisms based on offsets or indexes.
[0105] If the usage frequency does not exceed the preset usage frequency threshold (i.e., low-frequency data), the system will back up the data objects to be updated according to the preset high space utilization backup strategy.
[0106] High space utilization refers to the fact that a preset high space utilization backup strategy requires less additional storage space. In one specific embodiment, the preset high space utilization backup strategy can be a general backup (or in-place update backup) strategy. In other feasible embodiments, the high space utilization backup strategy can also be a log-based backup, such as backing up only the differences in the data, or only recording reverse operation logs for rollback, etc.
[0107] Step S104: After the backup is completed, write the transaction commit flag in the transaction flag area to indicate that the current transaction has ended.
[0108] Here, all data objects to be updated within the transaction have been written or switched according to the selected strategy, and the system executes the transaction commit.
[0109] Specifically, a transaction commit flag is written into the transaction flag area. The transaction commit flag is a status marker indicating that the transaction has been fully and successfully completed. In one specific embodiment, the transaction commit flag may be a character different from the transaction start flag (e.g., rewriting the START flag to COMMIT) written at a specific offset in the transaction flag area. In other embodiments, the transaction start flag may be directly erased, restoring the transaction flag area to an idle state; or a committed status bit may be written.
[0110] Once the transaction commit flag is successfully written, it means that all update operations in this transaction have been completed and solidified.
[0111] In an optional implementation, a transaction control area is further provided in the non-volatile memory; after step S104, the method further includes: Write the transaction identifier and its corresponding backup record into the transaction control area.
[0112] Here, the transaction identifier (TID) of the current transaction is associated with a newly created backup record, and this information is written to the transaction control area.
[0113] Because a transaction may contain updates to multiple data objects, the system performs a write operation for each backed-up data object during transaction execution. This results in a unique transaction identifier potentially corresponding to multiple backup records in the transaction control area, each backup record detailing a specific data object update operation within the transaction.
[0114] In one specific embodiment, the backup record is a data structure whose contents include, but are not limited to: 1. Data object identifier, used to uniquely identify which data object is being updated this time, such as the ID or address of the data object in non-volatile memory.
[0115] 2. The backup scheme adopted should be clearly recorded, specifying whether the backup used a preset high-performance backup strategy or a preset high space utilization backup strategy.
[0116] 3. Backup data information, the content of which depends on the backup scheme used.
[0117] If a preset high space utilization backup strategy is used, this information needs to record in detail the specific storage location and data length of the original data being backed up in the transaction backup data area.
[0118] If a default high-performance backup strategy is used, this information can be empty, or it can record the identifier of the new page being written.
[0119] Backup records provide a detailed rollback log for any unexpected power outages that may occur during transaction execution (such as when a smart card is removed). When the system powers on again and detects a transaction interruption, it can traverse the transaction control area using the transaction identifier to accurately locate all backup records belonging to the interrupted transaction. Based on the information in each record, all modified data objects can be safely restored to their state before the transaction began, thus ensuring the atomicity of the transaction.
[0120] In an optional implementation, the non-volatile memory also includes a transaction backup data area; see reference Figure 4 The method further includes the following steps S201-S206.
[0121] Step S201: After powering on again, identify whether the transaction flag area includes a transaction start flag and a transaction commit flag.
[0122] Here, each time the smart card is powered on again (e.g., when the card reader is inserted), the system performs initialization, identifies the status of the transaction flag area, and checks whether the transaction flag area includes the transaction start flag and the transaction commit flag.
[0123] Step S202: If the transaction flag area includes a transaction start flag but does not include a transaction commit flag, determine that the target transaction corresponding to the transaction start flag is in an execution interruption state.
[0124] Here, if the transaction flag area is empty, or contains both the transaction start flag and the transaction commit flag (or only the transaction commit flag), it indicates that the previous transaction has been fully committed and the system can start normally.
[0125] If the transaction flag area includes a transaction start flag but does not include a transaction commit flag, then the target transaction corresponding to the transaction start flag is in an interrupted state. This indicates that in the last operation, a transaction started, but the system unexpectedly lost power before it was marked as committed.
[0126] Step S203: Traverse the transaction control area and obtain the target backup record corresponding to the target transaction.
[0127] Here, once an interrupted transaction is identified, it's necessary to determine which operations the target transaction performed. The system will traverse the transaction control area based on the target transaction's transaction identifier (TID), which can be stored in the transaction flag area along with the transaction start flag.
[0128] Retrieve all target backup records corresponding to the transaction identifier of the target transaction. Since a transaction may contain updates to multiple data objects (corresponding to multiple backup records).
[0129] Step S204: Identify the backup strategy in the target backup record.
[0130] Here, each backup record stores the specific strategy used for that backup.
[0131] Step S205: If the backup strategy is a preset high-performance backup strategy, keep the original data of the data object to be updated corresponding to the target transaction in a valid state.
[0132] Here, if the backup strategy is a preset high-performance backup strategy (e.g., a two-page backup strategy), it is determined that the valid state of the original data (e.g., page A) has not been changed, and the writing of new data (e.g., page B) (even if it has been completed) has not yet taken effect. Therefore, there is no need to perform any data copying or state rollback operations; this record can simply be skipped, and the original data retains its valid state before the transaction began.
[0133] Step S206: If the backup strategy is a preset high space utilization backup strategy, restore the original backup data in the transaction backup data area to the storage location of the data object to be updated according to the target backup record.
[0134] Here, if the backup strategy is a preset high-space-utilization backup strategy (e.g., a general backup strategy), the original storage location of the data object may have been partially or completely overwritten by the new data, resulting in an inconsistent and corrupted state. Therefore, data recovery is required. Based on information in the target backup record (e.g., the storage address and length of the original data in the transaction backup data area), the system reads the complete original backup data from the transaction backup data area and copies it back to the storage location of the data object to be updated.
[0135] Based on the above operations, regardless of the backup strategy, all data objects affected by the interrupted transaction are restored to their state before the transaction started.
[0136] In an optional implementation, after step S206, the method further includes: Delete the transaction start flag from the transaction flag area.
[0137] Here, by deleting (or erasing) the transaction start flag, the transaction flag area is restored to a clean, empty state, indicating that the recovery work of the interrupted transaction has been completed. The smart card can safely begin to accept and execute new transactions, while also avoiding the unnecessary recovery process being triggered again on the next power-on.
[0138] In an optional implementation, the non-volatile memory is further provided with a double-page backup area; each data object in the double-page backup area is provided with two storage pages (or storage blocks).
[0139] Here, a double-page backup area is also pre-configured in the non-volatile memory.
[0140] In the dual-page backup area, each data object to be updated that is designated (e.g., based on high usage frequency) to use this strategy is allocated two physically independent storage pages. These two storage pages (referred to as page A and page B) are dedicated to storing that data object and operate in a switching manner to ensure that at least one page always stores the currently valid data.
[0141] Reference Figure 5 Step S103 includes the following steps S301-S303.
[0142] Step S301: Identify the page state of the stored page; the page state includes a valid state and an invalid state.
[0143] Here, before performing a write operation, the system must first determine which of the two storage pages (page A and page B) is currently in use and which is free. Page states include valid and invalid states.
[0144] A valid state means that the data contained in the storage page is the latest, correct, and available for the system to read.
[0145] An invalid state means that the data contained in the storage page is outdated (i.e., data from the previous version) or is free, so the page can be used to write new data.
[0146] In one specific embodiment, state identification can be achieved by reading specific state information stored in the header of each page. For example, the state information can be a simple state flag (e.g., 0x00 for valid, 0xFF for invalid); in another preferred embodiment, the state information can be a version number or an incrementing counter, by comparing the version numbers of two pages, identifying the page with the newer (or larger) version number as valid, and the other as invalid.
[0147] Step S302: Write the new data of the data object to be updated into the first storage page that is in an invalid state.
[0148] Here, after determining which storage page is invalid, that page is temporarily designated as the first storage page. The new data for the data object to be updated, obtained from the transaction initiation command, is then completely written into this first storage page.
[0149] Step S303: Update the page state of the first storage page to a valid state, and update the page state of the second storage page, which is in a valid state, to an invalid state.
[0150] Here, after the new data write operation on the first storage page is completed, the page state is switched immediately to make the new data effective.
[0151] Update the page state of the first storage page (i.e., the page where new data has just been written) to a valid state.
[0152] Update the state of the second storage page (i.e., the page that was valid before the start of this transaction and stores old data) to invalid state.
[0153] In a specific embodiment employing a version number mechanism, a new version number, higher than the version number in the second storage page, is written to the header of the first storage page. Through this action, the first storage page, upon its next identification, will be deemed valid because it possesses the latest version number, thus achieving an atomic switch between old and new data.
[0154] In an optional implementation, the non-volatile memory also includes a transaction backup data area. This transaction backup data area is a shared backup space independent of the original storage location of the data object. Its main purpose is to temporarily store the original data of the data object before performing an in-place update, thus ensuring power-loss safety.
[0155] If the usage frequency does not exceed a preset usage frequency threshold (for example, the data is determined to be low-frequency access data), the steps for backing up the data objects to be updated according to the preset high space utilization backup strategy include: Before erasing and writing the storage location of the data object to be updated, the original data of the data object to be updated is written to the transaction backup data area to obtain the backup original data.
[0156] Here, before performing an erase / write operation on the storage location of the data object to be updated (i.e., its original storage address in non-volatile memory) to write new data, the system first performs a pre-backup action.
[0157] The pre-backup action involves completely copying and writing all the original data (i.e., old data) currently stored in the storage location to the transaction backup data area.
[0158] This data, which has been completely copied to the transaction backup data area, is the original backup data.
[0159] By performing a full backup before erasing and rewriting, it is ensured that the original data of the data object always has a safe and complete backup during the transaction execution process.
[0160] If the smart card experiences an unexpected power outage after this step is completed but before the entire transaction is committed (e.g., a power outage while erasing or writing new data to the original location), the system can retrieve the original backup data from the transaction backup data area through the subsequent power-off recovery mechanism upon the next power-on, and restore (i.e. copy back) it to the original storage location of the data object, thereby safely rolling the data object back to the state before the transaction began.
[0161] The default high space utilization backup strategy does not require permanently allocating double the storage space for each data object. Instead, multiple (or even all) data objects using this strategy can share the same transactional backup data area, occupying that space only temporarily during transaction execution, thus achieving higher storage space utilization. Compared to the default high performance backup strategy, the default high space utilization backup strategy requires additional data copying operations during backup, resulting in a relatively larger performance overhead.
[0162] In an optional implementation, after step S101, the method further includes the following steps S401-S402.
[0163] Step S401: Determine whether the first available storage space of the non-volatile memory meets the space requirements for executing the preset high-performance backup strategy.
[0164] Here, the first available storage space can be understood as the globally available space on non-volatile memory, or the total available space reserved for performing backup operations. Space requirements refer to the additional storage resources consumed by executing a preset high-performance backup strategy. A preset high-performance backup strategy typically requires allocating an additional storage page of approximately its own size for the data object to be updated.
[0165] Step S402: If the first available storage space does not meet the space requirements, back up the data objects to be updated according to the preset high space utilization backup strategy.
[0166] Here, if the remaining space of the entire non-volatile memory is below a certain threshold and insufficient to allocate a new storage page, the data object to be updated will be backed up according to the preset high space utilization backup strategy.
[0167] Specifically, if global storage space is critically low, the system will prioritize (or force) the use of a preset backup strategy that consumes less space and has high space utilization.
[0168] In an optional implementation, the non-volatile memory is further provided with a dual-page backup area. Before the step of backing up the data object to be updated according to a preset high-performance backup strategy in step S402, the method further includes the following steps S501-S502.
[0169] Step S501: Determine whether the second available storage space of the dual-page backup area meets the target storage space requirements of the data object to be updated.
[0170] Here, the second available storage space specifically refers to the remaining available space within the dedicated double-page backup area (e.g., the number of remaining free storage pages). The target storage space requirement refers to the space (e.g., one storage page) that the data object to be updated needs to occupy within this area.
[0171] Step S502: If the second available storage space does not meet the target storage space requirements, back up the data objects to be updated according to the preset high space utilization backup strategy.
[0172] Here, if the global space is still sufficient, but the dedicated double-page backup area has been filled by other high-frequency data objects, the data objects to be updated will be backed up according to the preset high space utilization backup strategy.
[0173] Therefore, even for high-frequency data, when its dedicated high-performance backup resource pool (i.e., the two-page backup area) is exhausted, the system can intelligently downgrade it to a preset high-space-utilization backup strategy. This ensures that even when the two-page backup area is full, the update task for high-frequency data can still be completed safely and atomically.
[0174] This application provides a data backup method that dynamically selects between a high-performance backup strategy and a high-space-utilization backup strategy based on the usage frequency of the data object and the available storage space. This achieves an effective balance between backup performance and storage space utilization, thereby realizing intelligent backup schemes and optimized allocation of storage resources. Furthermore, combined with the power-loss recovery mechanism of the transaction flag area and transaction control area, it ensures the atomicity and security of data recovery after the smart card experiences an unexpected power outage.
[0175] The computer program product provided in this application includes a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the methods described in the preceding method embodiments. For specific implementation details, please refer to the method embodiments, which will not be repeated here.
[0176] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the system and apparatus described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0177] Furthermore, in the description of the embodiments of this application, unless otherwise expressly specified and limited, the terms "installation," "connection," and "linking" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal connection of two components. Those skilled in the art can understand the specific meaning of the above terms in this application based on the specific circumstances.
[0178] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0179] In the description of this application, it should be noted that the terms "center," "upper," "lower," "left," "right," "vertical," "horizontal," "inner," and "outer," etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are used only for the convenience of describing this application and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this application. Furthermore, the terms "first," "second," and "third" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.
[0180] Finally, it should be noted that the above-described embodiments are merely specific implementations of this application, used to illustrate the technical solutions of this application, and not to limit them. The protection scope of this application is not limited thereto. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments within the scope of the technology disclosed in this application, or make equivalent substitutions for some of the technical features. Such modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application, and should all be covered within the protection scope of this application.
Claims
1. A data backup method, characterized in that, Applied to smart cards; The smart card includes a non-volatile memory, and the non-volatile memory has a pre-set transaction flag area; The data backup method includes: In response to the received transaction start instruction, a transaction start flag is written to the transaction flag area, and a corresponding transaction identifier is set for the current transaction; the transaction start instruction carries the data information of the data object to be updated; Determine whether the usage frequency of the data object to be updated exceeds a preset usage frequency threshold; If the usage frequency exceeds the preset usage frequency threshold, the data object to be updated is backed up according to the preset high-performance backup strategy; if the usage frequency does not exceed the preset usage frequency threshold, the data object to be updated is backed up according to the preset high space utilization backup strategy. After the backup is completed, a transaction commit flag is written to the transaction flag area to indicate that the current transaction has ended.
2. The data backup method according to claim 1, characterized in that, The non-volatile memory also includes a transaction control area; After the backup is completed, the method further includes: Write the transaction identifier and its corresponding backup record into the transaction control area.
3. The data backup method according to claim 2, characterized in that, The non-volatile memory also includes a transaction backup data area; the method further includes: After power-on again, identify whether the transaction flag area includes the transaction start flag and the transaction commit flag; If the transaction flag area includes the transaction start flag but does not include the transaction commit flag, it is determined that the target transaction corresponding to the transaction start flag is in an execution interruption state. Traverse the transaction control area to obtain the target backup record corresponding to the target transaction; Identify the backup strategy in the target backup record; If the backup strategy is the preset high-performance backup strategy, the original data of the data object to be updated corresponding to the target transaction is kept in a valid state. If the backup strategy is the preset high space utilization backup strategy, the original backup data in the transaction backup data area is restored to the storage location of the data object to be updated according to the target backup record.
4. The data backup method according to claim 3, characterized in that, If the backup strategy is the preset high space utilization backup strategy, after the step of restoring the original backup data in the transaction backup data area to the storage location of the data object to be updated according to the target backup record, the method further includes: Delete the transaction start flag from the transaction flag area.
5. The data backup method according to claim 1, characterized in that, The non-volatile memory is also provided with a dual-page backup area; each data object in the dual-page backup area is provided with two storage pages. If the usage frequency exceeds the preset usage frequency threshold, the step of backing up the data object to be updated according to the preset high-performance backup strategy includes: Identify the page state of the stored page; the page state includes a valid state and an invalid state; Write the new data of the data object to be updated into the first storage page that is in the invalid state; Update the page state of the first storage page to the valid state, and update the page state of the second storage page, which is in the valid state, to the invalid state.
6. The data backup method according to claim 1, characterized in that, The non-volatile memory also includes a transaction backup data area; If the usage frequency does not exceed the preset usage frequency threshold, the step of backing up the data object to be updated according to the preset high space utilization backup strategy includes: Before erasing and writing the storage location of the data object to be updated, the original data of the data object to be updated is written into the transaction backup data area to obtain the backup original data.
7. The data backup method according to claim 1, characterized in that, Following the step of writing a transaction start flag in the transaction flag area in response to a received transaction start instruction, the method further includes: Determine whether the first available storage space of the non-volatile memory meets the space requirements for executing the preset high-performance backup strategy; If the first available storage space does not meet the space requirement, the data object to be updated is backed up according to the preset high space utilization backup strategy.
8. The data backup method according to claim 1, characterized in that, The non-volatile memory is also provided with a dual-page backup area; Before the step of backing up the data object to be updated according to a preset high-performance backup strategy, the method further includes: Determine whether the second available storage space of the dual-page backup area meets the target storage space requirements of the data object to be updated; If the second available storage space does not meet the target storage space requirement, the data object to be updated is backed up according to the preset high space utilization backup strategy.
9. A data backup system, characterized in that, Applied to smart cards; The smart card includes a non-volatile memory, and the non-volatile memory has a pre-set transaction flag area; The data backup system includes: The startup module is used to respond to a received transaction startup command by writing a transaction start flag in the transaction flag area and setting a corresponding transaction identifier for the current transaction; the transaction startup command carries the data information of the data object to be updated; The execution module is used to determine whether the usage frequency of the data object to be updated exceeds a preset usage frequency threshold; The execution module is further configured to back up the data object to be updated according to a preset high-performance backup strategy if the usage frequency exceeds the preset usage frequency threshold; and to back up the data object to be updated according to a preset high space utilization backup strategy if the usage frequency does not exceed the preset usage frequency threshold. The commit module is used to write a transaction commit flag in the transaction flag area after the backup is completed, so as to indicate that the current transaction has ended.
10. A smart card, characterized in that, It includes a smart card body and a non-volatile memory disposed in the smart card body; it also includes the data backup system as described in claim 9; the data backup system is disposed on the smart card body.
Citation Information
Patent Citations
File backup method and device, equipment and storage medium
CN117312051A