A backup and restoration method and system for operating system
Through the recursive subvolume search algorithm and the snapshot function of the Btrfs file system, the problem of backup and restore device dependence, large storage space occupancy and insufficient internal processing in the subvolume in the existing technology is solved, and the rapid and automated operating system backup and restore are realized, and container applications are supported.
Patent Information
- Application Number
- CN202111455841.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-01
- Publication Date
- 2025-08-26
- Estimated Expiration
- 2041-12-01
AI Technical Summary
The existing backup and restore solution requires additional equipment and storage devices. The backup process is inconvenient. The Btrfs file system cannot realize system-level restoration, and the subvolumes inside the subvolume cannot be processed automatically, and the backup takes up a large storage space.
The recursive subvolume search algorithm is used to combine the snapshot function of the Btrfs file system to automatically discover and back up all subvolumes in the operating system, including internal subvolumes, and backup and restore through software, reduce dependence on physical devices and optimize storage space utilization.
It realizes fast and automated operating system backup and restore without relying on additional devices, improves backup efficiency and disk utilization, and supports compatibility of container applications.
Smart Images

Figure CN114138560B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a backup and restoration method and system for an operating system, belonging to the field of computer information technology. Background Art
[0002] In the ever-developing Internet era, high-performance Linux cluster systems require stable data collection and transmission in real-time businesses running 24 / 7, placing extremely high demands on the stable operation of servers. In addition, in scenarios such as edge computing, hardware facilities running Linux systems are also needed to provide efficient and stable services to the outside world. How to further ensure the normal operation of the system? The currently more widely used cluster technology can effectively improve the redundancy of the Linux system. However, the downtime of a few systems, hardware failures, network instability, human errors leading to data or file loss, etc., will to some extent affect the normal operation of the business. After an abnormality occurs in the real-time business system, how to restore the system as quickly as possible has gradually become the primary consideration of business maintenance personnel.
[0003] Existing backup solutions generally include system-level backup and file-level backup. System-level backup is to regularly back up the entire system and directly roll back the system in case of failure; file-level backup generally backs up files regularly and restores the backup files in case of failure or file loss.
[0004] These backup solutions can reduce business interruptions and data loss caused by system anomalies to a certain extent, but there are still many problems; for example, existing system-level backup solutions generally require additional equipment (such as system repair disks, etc.), or require additional partitions (such as backup and restore partitions, etc.), so this backup and restore method generally requires additional backup and restore equipment, and the backup and restore process is relatively inconvenient; in addition, the existing system-level backup and file-level backup methods require the use of additional storage devices to store the backup and restore system and backup files, and these storage devices need to be connected during restoration, and the storage space occupied by the backup may be large.
[0005] The Btrfs (Butter FS) file system provides the subvolume snapshot function, which can be used to back up the system. In Btrfs, subvolumes are the basic storage and management units. In the Btrfs root partition, Btrfs subvolumes can be used directly as directories. In the operating system, Btrfs subvolumes can be mounted as partitions. Using the Btrfs file system for file-level backup is relatively simple and convenient. Store the files to be backed up in a Btrfs subvolume and take a snapshot of the subvolume to back up the files.
[0006] You can also install the Linux system's root partition file system in a Btrfs subvolume, and perform system-level file backup by taking a snapshot of the subvolume; however, Btrfs only provides storage management functions, but does not have boot management functions. After using Btrfs to take a subvolume snapshot, you can only save the files in the subvolume, but cannot restore the system and then start it. In other words, Btrfs can only achieve system-level backup, not system-level restoration.
[0007] In addition, Btrfs only provides single-layer subvolume operations and cannot operate on subvolumes within subvolumes at the same time; subvolumes within subvolumes mean that, assuming the path of subvolume A in the operating system is / mnt / btrfs / A, Btrfs is used to create subvolume B in subvolume A, and the path in the operating system is / mnt / btrfs / A / B, and subvolume B is the subvolume within subvolume A; in Btrfs, take a snapshot of subvolume A to generate subvolume C. Assuming the path of subvolume C in the operating system is / mnt / btrfs / C, there is / mnt / btrfs / C in subvolume C. / B directory, but there is no content under this directory, that is, when taking a snapshot of subvolume A, the subvolume inside subvolume A is not snapshotted; at the same time, directly deleting subvolume A will fail because there are subvolumes inside subvolume A; if the operating system is installed in a Btrfs subvolume, many applications (such as the container application Docker) will automatically create subvolumes in the system's root partition file system subvolume. When using Btrfs directly to take a snapshot backup of the root partition file system subvolume, due to the mechanism of Btrfs itself, the subvolumes inside the root partition file system subvolume created by the application will not be backed up.
[0008] In summary, existing backup and restore solutions have the following problems:
[0009] 1) Additional backup and restoration equipment and storage devices are required;
[0010] 2) Physical access to the devices to be backed up and restored is required to perform backup and restoration;
[0011] 3) Btrfs provides the ability to take subvolume snapshots, but does not provide the ability to boot into a snapshot subvolume. Also, it cannot automatically process subvolumes within subvolumes.
[0012] 4) The backup may take up a lot of storage space. Summary of the Invention
[0013] The purpose of the present invention is to provide a backup and restoration method and system for an operating system, which realizes automatic backup and other processing of internal sub-volumes, realizes backup through software, does not require physical contact with the device, reduces the disk space occupied by the backup files, and improves backup efficiency and disk utilization.
[0014] To achieve the above objectives, the present invention is implemented by adopting the following technical solutions:
[0015] In a first aspect, the present invention provides a method for backing up and restoring an operating system, comprising:
[0016] In response to receiving the backup instruction, mounting the Btrfs file system partition of the operating system disk file to the first mount point of the operating system, using a recursive subvolume search algorithm to find all internal subvolumes within the normal subvolumes, creating a snapshot in the subvolume to be backed up using a snapshot function provided by the Btrfs file system, unmounting the Btrfs file system partition, and obtaining a snapshot containing the backup content, wherein the subvolume to be backed up includes the normal subvolume and the internal subvolume;
[0017] In response to receiving the restore instruction, the content of the snapshot to be restored is restored to the operating system according to the snapshot information to be restored contained in the restore instruction.
[0018] Combined with the first aspect, further, a recursive subvolume search algorithm is used to find all internal subvolumes:
[0019] Read the information of all subvolumes and save the subvolume name, subvolume ID, and subvolume ID where the subvolume is located in the database;
[0020] Use the subvolume ID of the subvolume to be backed up as the keyword to perform an internal subvolume search to obtain an internal subvolume list.
[0021] Traverse the internal subvolume list, use each internal subvolume as a key, and repeat the internal subvolume search until there are no internal subvolumes;
[0022] The internal subvolume search is as follows: searching the database for all rows where the subvolume is located and the subvolume ID is the keyword, and returning the subvolume name and the subvolume ID as a list.
[0023] In combination with the first aspect, further, the content in the snapshot to be restored is all the content of the operating system and / or the content of a partition;
[0024] Restoring all the contents of the operating system is a root partition restoration operation: Mount the Btrfs file system partition of the operating system disk file to the operating system's second mount point, mount the snapshot to be restored, the operating system runtime file system, and the operating system boot partition to the operating system's third mount point, enter the file system of the snapshot to be restored through chroot, reinstall the boot and update the boot, unmount the operating system boot partition, the operating system runtime file system, the snapshot to be restored, and the Btrfs file system partition in sequence, restart the operating system, and complete the root partition restoration;
[0025] Restoring a partition is a normal partition restore: back up the Btrfs file system partition of the operating system disk file and the configuration file of the fourth mount point, modify the configuration file, change the original subvolume under the fourth mount point to the snapshot to be restored, unmount the original subvolume under the fourth mount point, and mount the snapshot to be restored to the fourth mount point to complete the normal partition restoration.
[0026] In combination with the first aspect, further comprising the step of backup deletion:
[0027] When deleting a snapshot storing backup content, a recursive subvolume search algorithm is used to find all internal subvolumes in the snapshot, and all internal subvolumes are deleted at the same time, and then the snapshot storing backup content is deleted.
[0028] In combination with the first aspect, further comprising: configuring a software environment in the operating system.
[0029] In a second aspect, the present invention further provides an operating system backup and restoration system, comprising:
[0030] A backup module is configured to, in response to receiving a backup instruction, mount the Btrfs file system partition of the operating system disk file to the first mount point of the operating system, use a recursive subvolume search algorithm to find all internal subvolumes within normal subvolumes, create a snapshot in the subvolume to be backed up using the snapshot function provided by the Btrfs file system, unmount the Btrfs file system partition, and obtain a snapshot containing the backup content, where the subvolume to be backed up includes normal subvolumes and internal subvolumes;
[0031] The restoration module is configured to, in response to receiving a restoration instruction, restore the content of the snapshot to be restored to the operating system according to the snapshot information to be restored contained in the restoration instruction.
[0032] Compared with the prior art, the present invention has the following beneficial effects:
[0033] The present invention provides an operating system backup and restoration method and system, which use a recursive subvolume search algorithm to find internal subvolumes within all ordinary subvolumes, create snapshots in subvolumes that need to be backed up through the snapshot function provided by the Btrfs file system, and realize automatic backup and other processing of internal subvolumes in combination with the Btrfs file system through the recursive subvolume search algorithm, thereby achieving compatibility with applications (such as containers) that automatically create internal subvolumes; by mounting and unmounting the Btrfs file system partition of the operating system disk file, combined with the snapshot function, backup is achieved through software without physical contact with the device; in response to receiving a restore instruction, the content of the snapshot to be restored is restored to the operating system according to the snapshot information to be restored contained in the restore instruction; restoration is achieved through software, without the need for additional backup and restoration equipment and storage devices, and the operating system can be directly backed up while the operating system is running; at the same time, the Btrfs subvolume mechanism greatly reduces the disk space occupied by the backup file, thereby improving backup efficiency and disk utilization. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 This is a schematic diagram of an operating system backup and restoration system provided by an embodiment of the present invention;
[0035] Figure 2 Schematic diagram of the logical structure of the backup process provided by an embodiment of the present invention;
[0036] Figure 3 is a flowchart of an execution of a recursive subvolume search algorithm provided by an embodiment of the present invention;
[0037] Figure 4 The present invention provides a flowchart of a backup and restoration method for an operating system. DETAILED DESCRIPTION
[0038] The present invention will be further described below in conjunction with the accompanying drawings. The following embodiments are only used to more clearly illustrate the technical solutions of the present invention and are not intended to limit the scope of protection of the present invention.
[0039] Example 1
[0040] like Figure 4 As shown, an embodiment of the present invention provides a method for backing up and restoring an operating system, including the steps of backing up and restoring.
[0041] S1. In response to receiving a backup instruction, mount the Btrfs file system partition of the operating system disk file to the first mount point of the operating system, use a recursive subvolume search algorithm to find all internal subvolumes within ordinary subvolumes, create a snapshot in the subvolume to be backed up using the snapshot function provided by the Btrfs file system, unmount the Btrfs file system partition, and obtain a snapshot containing the backup content, where the subvolume to be backed up includes ordinary subvolumes and internal subvolumes.
[0042] In this embodiment, the operating system is deployed first, and the operating system is installed on the physical platform to provide underlying support for backup and restoration. When installing the operating system, Btrfs must be selected as the disk file system.
[0043] The disk files are divided into two partitions, the boot partition and the Btrfs file system partition. The Btrfs file system partition contains a root subvolume and a home partition subvolume. The root subvolume is the root partition of the Linux system, including the Linux root file system. After the system starts, the root subvolume is mounted under the system's / directory. The home partition subvolume is the partition corresponding to the / home directory in the Linux system. After the system starts, the subvolume is mounted under the / home directory. The boot partition is used to boot the operating system in the root subvolume.
[0044] When configuring the operating system, you can also save other directories that need to be backed up as Btrfs subvolumes in the Btrfs file system partition. Then, in the operating system's partition mount configuration, specify that the Btrfs subvolume be mounted to a specified directory.
[0045] In this embodiment, a software environment required for executing the backup and restoration method of the operating system needs to be installed in the operating system.
[0046] The backup creation method includes the following steps:
[0047] 1) Partition mounting, 2) Subvolume snapshot, 3) Partition unmounting; First, mount the Btrfs file system partition to an empty directory on the system (the first mount point), and then perform a snapshot operation on the subvolume that needs to be backed up. The subvolume snapshot operation means saving the current state of the subvolume and generating a new subvolume. The data in the new subvolume is the data in the atomic volume when the subvolume snapshot operation is performed. The new subvolume is called a snapshot. The generated new subvolume has nothing to do with the original subvolume. After the subvolume snapshot is completed, unmount the Btrfs file system partition; the above-mentioned first mount point can be an empty directory created under the / tmp directory.
[0048] When taking a subvolume snapshot, if there is a subvolume inside the subvolume, Btrfs cannot automatically take a snapshot of the subvolume inside the subvolume.
[0049] Assume that the root subvolume currently booted is @. After installing the container software, the software will automatically create multiple subvolumes, including the subvolume @ / var / lib / docker / btrfs / subvolumes / 0fd1f73aa8a99fbc9ba7848661cb0008271585601f22a6023acbafc0d081287c (hereinafter referred to as the 0fd1f73aa8a99fbc9ba7848661cb0008271585601f22a6023ac... subvolume inside @). The upper subvolume of these subvolumes is @. If you take a snapshot of the @ subvolume and generate a snapshot @root, then @root / v There will be no content in the ar / lib / docker / btrfs / subvolumes / 0fd1f73aa8a99fbc9ba7848661cb0008271585601f22a6023acbafc0d081287c directory, and Btrfs will not automatically take snapshots of the 0fd1f73aa8a99fbc9ba7848661cb0008271585601f22a6023ac... subvolumes in @. Similarly, if you directly delete @subvolume, the Btrfs deletion operation will fail because there are subvolumes in @subvolume. You need to delete the subvolumes in @ first and then delete @subvolume.
[0050] Therefore, when taking a subvolume snapshot or deleting a subvolume, to provide container compatibility, a recursive subvolume search algorithm is used to find all subvolumes within the subvolume. The steps of the recursive subvolume search algorithm are:
[0051] Read the information of all subvolumes and save the subvolume name, subvolume ID, and subvolume ID where the subvolume is located in the database;
[0052] Use the subvolume ID of the subvolume to be backed up as the keyword to perform an internal subvolume search to obtain an internal subvolume list.
[0053] Traverse the internal subvolume list, use each internal subvolume as a key, and repeat the internal subvolume search until there are no internal subvolumes;
[0054] The internal subvolume search is as follows: searching the database for all rows where the subvolume is located and the subvolume ID is the keyword, and returning the subvolume name and the subvolume ID as a list.
[0055] In one embodiment of backup creation, the partition subvolume to be backed up is @, and the backup name is initialized. First, the Btrfs file system partition / dev / sda2 is mounted to the system's / mnt / btrfs directory. If a backup and restore system is deployed and the Btrfs file system partition is set to automatically mount, there is no need to mount the partition when creating a backup. Then, a snapshot is taken of the @ subvolume to generate a new subvolume. The new subvolume is the snapshot. The name of the new subvolume is the result of hashing the backup name and the timestamp, assuming it is 6AA732E2808CA0CF0792A950517B68FB. Then, a recursive subvolume search algorithm is used on @ to obtain the subvolumes inside the @ subvolume. Assume that the obtained internal subvolumes are @ / var / docker, @ / var / docker / ubuntu, and @ / var / docker / centos. Then, a snapshot is taken of the internal subvolume. The subvolume name of the newly generated snapshot is:
[0056] 6AA732E2808CA0CF0792A950517B68FB / var / docker
[0057] 6AA732E2808CA0CF0792A950517B68FB / var / docker / ubuntu
[0058] 6AA732E2808CA0CF0792A950517B68FB / var / docker / centos
[0059] When the subvolume snapshot is complete, the partition is unmounted. If a backup and restore system is deployed and automatic mounting of Btrfs file system partitions is set, the partition is not unmounted when the backup is created, and the backup and restore system records information such as the backup name, backup time, atomic volume ID, and snapshot subvolume ID.
[0060] In Btrfs, subvolumes have their own IDs. Through Btrfs, you can obtain the IDs of all subvolumes and the ID of the subvolume to which they belong (hereinafter referred to as the parent subvolume ID). For example, the ID of @ is 256, the ID of @ / var / docker is 268, and the subvolume ID of @ / var / docker is 256. Through the subvolume ID and the subvolume's parent subvolume ID, you can obtain all internal subvolume information within the subvolume.
[0061] S2. In response to receiving the restore instruction, restore the content of the snapshot to be restored to the operating system according to the snapshot information to be restored contained in the restore instruction.
[0062] The contents of the snapshot to be restored include all contents of the operating system and / or the contents of a certain partition.
[0063] Restoring all the contents of the operating system is a root partition restoration operation: mount the Btrfs file system partition of the operating system disk file to the second mount point of the operating system, mount the snapshot to be restored, the operating system runtime file system, and the operating system boot partition to the third mount point of the operating system, use chroot to enter the file system of the snapshot to be restored, reinstall the boot and update the boot, unmount the operating system boot partition, the operating system runtime file system, the snapshot to be restored, and the Btrfs file system partition in sequence, restart the operating system, and complete the root partition restoration.
[0064] Root partition restoration means that, assuming that the current boot is the root subvolume @, there is a backup root subvolume @root. The process of restoring the root subvolume @root is called root partition restoration. After the restoration, restart the system and the system will boot the root partition file system in the @root subvolume.
[0065] Specifically, after the operating system is installed, the Btrfs file system contains the @ subvolume and @home subvolume, where the @ subvolume is the root subvolume, which stores the root partition file system of the Linux system. After the system starts, this subvolume is mounted to the / directory of the Linux system, and the @home subvolume is mounted to the / home directory of the system. When backing up the operating system, the @ subvolume is backed up to generate the @root subvolume. When restoring the root partition, it is restored to the @root subvolume. After restarting the system, the contents of the @root subvolume are mounted to the / directory of the Linux system, and the system started is the system in the @root subvolume.
[0066] Restoring the root partition can be summarized as follows: 1) partition mounting, 2) subvolume mounting, 3) runtime file system mounting, 4) boot partition mounting, 5) root file system switching, 6) boot creation and update, 7) boot partition unmounting, 8) runtime file system unmounting, 9) subvolume unmounting, 10) partition unmounting, 11) system restart.
[0067] An embodiment of root partition restoration is as follows: assuming that the subvolume mounted on the current operating system / directory is subvolume 6AA732E2808CA0CF0792A950517B68FB, and the subvolume to be restored is subvolume @, during the partition restoration process, the Btrfs file system partition / dev / sda2 is first mounted to the system's / mnt / btrfs directory. If a backup and restore system is deployed and the Btrfs file system partition is set to automatically mount, there is no need to mount the partition when creating a backup; then the subvolume / mnt / btrfs / @ is mounted to the system's / tmp / mount01 directory; and the runtime file systems / run, / proc, / sys, etc. are mounted to / tmp / mount01 / boot / EFI directory; then mount the boot partition / dev / sda1 to the system's / tmp / mount01 / boot / EFI directory; then switch to the file system in / tmp / mount01 through chroot; create a boot in the file system of / tmp / mount01, install the boot to / tmp / mount01 / boot / EFI (that is, / dev / sda1), and update the system boot information; then unmount the / sys, / proc, / run and other file systems and the / tmp / mount01 / boot / EFI partition in turn, and then unmount the @ subvolume and Btrfs file system partition / dev / sda2 mounted under / tmp / mount01, and finally restart the system; after the system restarts, boot again to enter the Linux system in the subvolume @, and the root partition restoration is completed.
[0068] Restoring a partition is a normal partition restore: back up the Btrfs file system partition of the operating system disk file and the configuration file of the fourth mount point, modify the configuration file, change the original subvolume under the fourth mount point to the snapshot to be restored, unmount the original subvolume under the fourth mount point, and mount the snapshot to be restored to the fourth mount point to complete the normal partition restoration.
[0069] Normal partition restoration means restoring a partition to its previous backup state without restoring the entire operating system. For example, if the @home subvolume is backed up as @bootHome and mounted under the / home directory, a normal partition restoration will mount the @bootHome subvolume under the / home directory without affecting other partitions and contents of the operating system.
[0070] An embodiment of ordinary partition restoration is: assuming that the subvolume DA4E154B739EF901737A051FC58E5B98 is currently mounted under / home, and the subvolume to be restored is subvolume @home, then the restoration process is: first, copy the directory and partition mapping information configuration file / etc / fstab and name it / etc / fstab.bak; then modify / etc / fstab and change the subvolume mounted on the startup / home directory to @home; then unmount the subvolume mounted under the current / home directory, and remount the subvolume partition through the modified configuration file. If the mounting fails, / etc / fstab is overwritten by / etc / fstab.bak, and the user is prompted that the restoration failed. If the mounting is successful, the ordinary partition restoration is completed.
[0071] During restoration, root partition restoration and normal partition restoration can be performed simultaneously, that is, the boot subvolume and mounted subvolumes can be modified at the same time.
[0072] There are two operating modes for normal partition restoration: the first is to restore only normal partitions, and the second is to restore normal partitions at the same time as restoring the root partition.
[0073] The steps to restore only a normal partition are: 1) back up the configuration file, 2) modify the configuration file, 3) unmount the original subvolume, and 4) mount the new partition.
[0074] When restoring only a common partition, first back up the partition and mount point configuration files. Then modify the configuration files to change the subvolume corresponding to the fourth mount point to the subvolume to be restored. After the modification is complete, unmount the atomic volume and then mount the new partition (that is, mount the subvolume to be restored to the corresponding mount point).
[0075] The process of restoring normal partitions at the same time as restoring the root partition is to perform the following operations: back up the configuration file, modify the configuration file, and mount the new partition after step 5 (root file system switching) and before step 6 (boot creation and update) of the root partition restoration procedure. The remaining operations are the same as those for restoring the root partition.
[0076] An operating system backup and restoration method provided by an embodiment of the present invention further includes the step of deleting the backup:
[0077] When deleting a snapshot storing backup content, a recursive subvolume search algorithm is used to find all internal subvolumes in the snapshot, and all internal subvolumes are deleted at the same time, and then the snapshot storing backup content is deleted.
[0078] Backup deletion specifically includes: 1) partition mounting, 2) subvolume deletion, and 3) partition unmounting. First, mount the Btrfs file system partition to a directory on the system, then delete the subvolume corresponding to the backup to be deleted. After the subvolume is deleted, unmount the Btrfs file system partition.
[0079] In one embodiment of the backup deletion method, the partition subvolume to be deleted is 6AA732E2808CA0CF0792A950517B68FB. First, the Btrfs file system partition / dev / sda2 is mounted to the system's / mnt / btrfs directory. If a backup and restore system is deployed and automatic mounting of Btrfs file system partitions is set, there is no need to mount the partition when creating a backup. Then, a recursive subvolume search algorithm is used on 6AA732E2808CA0CF0792A950517B68FB to obtain the subvolume inside the 6AA732E2808CA0CF0792A950517B68FB subvolume. Assume that the obtained internal subvolume is:
[0080] 6AA732E2808CA0CF0792A950517B68FB / var / docker
[0081] 6AA732E2808CA0CF0792A950517B68FB / var / docker / ubuntu
[0082] 6AA732E2808CA0CF0792A950517B68FB / var / docker / centos
[0083] All internal subvolumes are traversed in reverse order and deleted one by one. After all internal subvolumes are deleted, subvolume 6AA732E2808CA0CF0792A950517B68FB is deleted, and then the partition is unmounted. If a backup and restore system is deployed and automatic mounting of Btrfs file system partitions is set, the partition will not be unmounted when creating a backup.
[0084] An operating system backup and restoration method provided by an embodiment of the present invention also includes backup management, including backup ID management, backup hierarchy relationship management, and backup status management.
[0085] Backup ID management means that users need to specify a backup name for each backup. The backup name can be repeated and is used to concisely describe the function of this backup. After each backup is executed, a new subvolume (i.e., snapshot) is generated. The name of the snapshot is related to the backup name.
[0086] The backup ID is generated by concatenating the backup name with the current timestamp and performing a hash operation to generate a 32-bit backup ID consisting of letters and numbers. The subvolume name generated for this backup is the backup ID.
[0087] Backup hierarchical relationship management refers to recording the hierarchical relationship between backups through a database and displaying it in a user-friendly form.
[0088] The backup hierarchy refers to the relationship between subvolumes and their parent and subvolumes. The parent and subvolume relationship of a subvolume is as follows: if a snapshot of subvolume A is taken to generate subvolume B, then the parent and subvolume of subvolume B is subvolume A, and subvolume B is a sub-subvolume of subvolume A. During the operating system backup and restore process, starting from the initial subvolume (for the root subvolume, the initial subvolume is subvolume @), all subvolumes form a tree structure (backup tree).
[0089] Maintain the hierarchical relationship between backups. After a subvolume snapshot is taken, there is no longer a relationship between the snapshot and the parent and subvolumes. Therefore, when a subvolume is deleted, its sub-subvolumes are not affected. When deleting subvolumes, the hierarchical relationship between subvolumes needs to be maintained. Specifically, when deleting a subvolume, if the subvolume has a parent and subvolume, a virtual subvolume @V is generated as the root of the backup tree.
[0090] Backup status management refers to recording the current status of each backup. The backup status includes ACTIVE 、 BACKUP、VIRTUAL , where the status of the currently booted backup subvolume or the currently mounted backup subvolume is ACTIVE , the status of the virtual subvolume is VIRTUAL , the status of the remaining subvolumes is BACKUP .
[0091] After the operating system is installed, the status of the initial subvolumes (such as @, @home) is ACTIVE When backing up the operating system, the status of the newly created backup subvolume is BACKUP ; After the operating system is restored, the restored subvolume status changes to ACTIVE , the status of the atomic volume becomes BACKUP .
[0092] An operating system backup and restoration method provided by an embodiment of the present invention can realize rapid backup and restoration of the entire operating system in seconds; it solves the problem that Btrfs can only create subvolume snapshots but cannot enable the operating system to start from the subvolume snapshots. At the same time, the recursive subvolume search algorithm disclosed in the embodiment of the present invention solves the problem that Btrfs cannot automatically snapshot and delete subvolumes within subvolumes, and realizes compatibility with applications that create internal subvolumes (such as containers).
[0093] An embodiment of the present invention provides an operating system backup and restoration method that uses the Btrfs subvolume mechanism to greatly reduce the disk space occupied by backup files, thereby improving backup efficiency and disk utilization. No additional equipment is required, and backup is achieved through software by mounting and unmounting the Btrfs file system partition of the operating system disk file, combined with the snapshot function, so that the system can be directly backed up while the system is running.
[0094] Example 2
[0095] An embodiment of the present invention provides an operating system backup and restoration system, comprising:
[0096] A backup module is configured to, in response to receiving a backup instruction, mount the Btrfs file system partition of the operating system disk file to the first mount point of the operating system, use a recursive subvolume search algorithm to find all internal subvolumes within normal subvolumes, create a snapshot in the subvolume to be backed up using the snapshot function provided by the Btrfs file system, unmount the Btrfs file system partition, and obtain a snapshot containing the backup content, where the subvolume to be backed up includes normal subvolumes and internal subvolumes;
[0097] The restoration module is configured to, in response to receiving a restoration instruction, restore the content of the snapshot to be restored to the operating system according to the snapshot information to be restored contained in the restoration instruction.
[0098] Example 3
[0099] An embodiment of the present invention provides an operating system backup and restoration system, such as Figure 1 As shown, the backup and restore system of the operating system runs on the Linux operating system, and is divided into a core layer, an interface layer, and an application layer from bottom to top, with an operating system layer and an infrastructure layer connected below. The infrastructure layer provides the underlying hardware platform for the backup and restore system, and the operating system layer runs on the infrastructure layer, providing a software platform for the backup and restore system.
[0100] The core layer provides core functions such as Btrfs subvolume snapshots, boot creation, and subvolume deletion. The interface layer implements fast backup and restoration of the Linux operating system in the operating system layer by calling the core layer. The interface layer uses high-level programming languages to encapsulate the functions of the core algorithm layer, and adds functions such as backup ID management, backup hierarchy relationship management, and backup status management. The application layer provides an interface for users to use, and users can back up and restore the Linux operating system through the application layer.
[0101] The core layer is written in Shell language and is used to perform operating system-level operations, such as disk partition mounting, subvolume creation, subvolume snapshots, log generation, configuration file modification, boot installation, boot update, and other operations.
[0102] The interface layer is written in Python and all data is stored in a SQLite database.
[0103] The backup and restoration system of the operating system runs on the operating system installed in the Btrfs file system. In the Btrfs file system, a subvolume is created for installing the root file system and other partition files of the operating system.
[0104] For example, the disk device is / dev / sda, and the disk is divided into two partitions, the EFI partition / dev / sda1 and the Btrfs file system partition / dev / sda2. A subvolume @ is created in / dev / sda2 to save the root partition file system of the Linux system. After the system starts, the subvolume @ is mounted to the / directory of the Linux system; a subvolume @home is created in / dev / sda2 to save the home directory data of the Linux system. After the system starts, the subvolume @home is mounted to the / home directory of the Linux system. The user can also create other subvolumes in / dev / sda2 and mount them to the corresponding directory of the system; when the system starts, / dev / sda2 is mounted to the system, for example, to / mnt / btrfs. The user can enter the directory and see all subvolumes. The subvolumes can be used directly as directories.
[0105] The backup and restoration of the operating system are achieved through operations on subvolumes. System backup is achieved through subvolume snapshots, and system restoration is achieved through configuration file modification, boot creation and update, etc.
[0106] An operating system backup and restoration system provided by an embodiment of the present invention also provides functions of backup ID management, backup level management, and backup status management, which can be implemented through the backup management in an operating system backup and restoration method described in Example 1.
[0107] Backup ID management means that users need to specify a backup name for each backup. The backup name can be repeated and is used to concisely describe the function of this backup. After each backup is executed, a new subvolume (i.e., snapshot) is generated. The name of the snapshot is related to the backup name.
[0108] One embodiment of backup ID management is to take a snapshot of the @subvolume. The user specifies the backup name as INIT, the current timestamp is 1634190846, and the backup name and timestamp are concatenated. After hash calculation, the backup ID is obtained as 89C52AF68194BF10870B914755F7AA33. The subvolume name of the created snapshot subvolume is 89C52AF68194BF10870B914755F7AA33.
[0109] Backup hierarchical relationship management refers to recording the hierarchical relationship between backups through a database and displaying it in a user-friendly form; an embodiment can be found in Figure 2 After the system is installed, the @ subvolume exists. The root node of the backup tree is the @ subvolume, and the backup name is @. Three backup operations are performed on the @ subvolume to generate three backups, namely the backup system, the test snapshot, and the backup system, which are the three nodes under @; a backup operation is performed on the first backup system to generate an update system backup; two backup operations are performed on the second backup system to generate an installation database backup and an installation Python backup; the currently booted subvolume is the subvolume in the installation Python backup, and the user can restore to any subvolume in this tree diagram through the operating system's restore operation.
[0110] Backup status management refers to recording the current status of each backup. The backup status includes ACTIVE 、 BACKUP、VIRTUAL , where the status of the currently booted backup subvolume or the currently mounted backup subvolume is ACTIVE , the status of the virtual subvolume is VIRTUAL , the status of the remaining subvolumes is BACKUP .
[0111] One embodiment of backup status management is that after the operating system is installed, the subvolume mounted under the Linux system / directory is subvolume @, and the subvolume mounted under the / home directory is @home. Backing up @ generates subvolume 89C52AF68194BF10870B914755F7AA33, then the status of subvolume @ and subvolume @home is ACTIVE , the status of subvolume 89C52AF68194BF10870B914755F7AA33 is BACKUP When the user restores the 89C52AF68194BF10870B914755F7AA33 subvolume and restarts the system, the status of the 89C52AF68194BF10870B914755F7AA33 subvolume becomes ACTIVE , the status of @subvolume becomes BACKUP ; When the user deletes subvolume @ (this subvolume is the first root partition subvolume after the system is installed, and it has no parent subvolume), the status of subvolume @ changes to VIRTUAL , in this case, the subvolume @ is only used for display convenience and no longer represents the actual subvolume.
[0112] In an embodiment of the present invention, a backup and restore system for an operating system is provided. When a user issues a backup and restore system (i.e., creating a subvolume snapshot) or deletes a backup (i.e., deleting a subvolume) instruction to the interface layer, the interface layer first executes a recursive subvolume search algorithm, using the ID of the subvolume to be backed up or deleted as input. Figure 3 As shown, the algorithm execution process is:
[0113] 301) First, initialize the subvolume list, sort all subvolume names, subvolume IDs, and IDs of the subvolumes above the subvolume in ascending order using the subvolume ID as the key, save them in the subvolume list, and then go to 302);
[0114] 302) Initialize the result list. If the result list is empty, go to 303);
[0115] 303-1) If the subvolume list traversal is complete, go to 303-2), otherwise, go to 303-3);
[0116] 303-2) Return the result list and the sub-process ends;
[0117] 303-3) Read the next element of the subvolume list (the first element is read for the first time), and go to 303-4);
[0118] 303-4) If the upper sub-volume ID of the current element is equal to the sub-volume ID to be checked, go to 303-5), otherwise go to 303-1);
[0119] 303-5) Add the current sub-volume information to the result list and go to 303-6);
[0120] 303-6) Call the sub-procedure with the current sub-volume ID and the current result list as parameters Get the underlying subvolume , after the sub-process is completed, go to 303-1).
[0121] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware. Furthermore, the present application may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0122] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0123] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.
[0124] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0125] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the technical principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A method for backing up and restoring an operating system, characterized in that: include: In response to receiving the backup instruction, mounting the Btrfs file system partition of the operating system disk file to the first mount point of the operating system, using a recursive subvolume search algorithm to find all internal subvolumes within the normal subvolumes, creating a snapshot in the subvolume to be backed up using a snapshot function provided by the Btrfs file system, unmounting the Btrfs file system partition, and obtaining a snapshot containing the backup content, wherein the subvolume to be backed up includes the normal subvolume and the internal subvolume; In response to receiving the restore instruction, restoring the content of the snapshot to be restored to the operating system according to the snapshot information to be restored contained in the restore instruction; Use a recursive subvolume search algorithm to find all internal subvolumes: Read the information of all subvolumes and save the subvolume name, subvolume ID, and subvolume ID where the subvolume is located in the database; Use the subvolume ID of the subvolume to be backed up as the keyword to perform an internal subvolume search to obtain an internal subvolume list. Traverse the internal subvolume list, use each internal subvolume as a key, and repeat the internal subvolume search until there are no internal subvolumes; The internal subvolume search is as follows: searching the database for all rows where the subvolume ID is the keyword, and returning the subvolume name and subvolume ID as a list; The contents of the snapshot to be restored are all contents of the operating system and / or the contents of a certain partition; Restoring all the contents of the operating system is a root partition restoration operation: Mount the Btrfs file system partition of the operating system disk file to the operating system's second mount point, mount the snapshot to be restored, the operating system runtime file system, and the operating system boot partition to the operating system's third mount point, enter the file system of the snapshot to be restored through chroot, reinstall the boot and update the boot, unmount the operating system boot partition, the operating system runtime file system, the snapshot to be restored, and the Btrfs file system partition in sequence, restart the operating system, and complete the root partition restoration; Restoring a partition is a normal partition restore: back up the Btrfs file system partition of the operating system disk file and the configuration file of the fourth mount point, modify the configuration file, change the original subvolume under the fourth mount point to the snapshot to be restored, unmount the original subvolume under the fourth mount point, and mount the snapshot to be restored to the fourth mount point to complete the normal partition restoration.
2. The operating system backup and restoration method according to claim 1, characterized in that: It also includes the steps for backing up and deleting: When deleting a snapshot storing backup content, a recursive subvolume search algorithm is used to find all internal subvolumes in the snapshot, and all internal subvolumes are deleted at the same time, and then the snapshot storing backup content is deleted.
3. The operating system backup and restoration method according to claim 1, wherein: Also includes: Configure the software environment in the operating system.
4. A backup and restore system for an operating system, characterized in that: include: A backup module is configured to, in response to receiving a backup instruction, mount the Btrfs file system partition of the operating system disk file to the first mount point of the operating system, use a recursive subvolume search algorithm to find all internal subvolumes within normal subvolumes, create a snapshot in the subvolume to be backed up using the snapshot function provided by the Btrfs file system, unmount the Btrfs file system partition, and obtain a snapshot containing the backup content, where the subvolume to be backed up includes normal subvolumes and internal subvolumes; Restoration module: configured to, in response to receiving a restore instruction, restore the contents of the snapshot to be restored to the operating system according to the snapshot information to be restored contained in the restore instruction; Here, a recursive subvolume search algorithm is used to find all internal subvolumes: Read the information of all subvolumes and save the subvolume name, subvolume ID, and subvolume ID where the subvolume is located in the database; Use the subvolume ID of the subvolume to be backed up as the keyword to perform an internal subvolume search to obtain an internal subvolume list. Traverse the internal subvolume list, use each internal subvolume as a key, and repeat the internal subvolume search until there are no internal subvolumes; The internal subvolume search is as follows: searching the database for all rows where the subvolume ID is the keyword, and returning the subvolume name and subvolume ID as a list; The contents of the snapshot to be restored are all contents of the operating system and / or the contents of a certain partition; Restoring all the contents of the operating system is a root partition restoration operation: Mount the Btrfs file system partition of the operating system disk file to the operating system's second mount point, mount the snapshot to be restored, the operating system runtime file system, and the operating system boot partition to the operating system's third mount point, enter the file system of the snapshot to be restored through chroot, reinstall the boot and update the boot, unmount the operating system boot partition, the operating system runtime file system, the snapshot to be restored, and the Btrfs file system partition in sequence, restart the operating system, and complete the root partition restoration; Restoring a partition is a normal partition restore: back up the Btrfs file system partition of the operating system disk file and the configuration file of the fourth mount point, modify the configuration file, change the original subvolume under the fourth mount point to the snapshot to be restored, unmount the original subvolume under the fourth mount point, and mount the snapshot to be restored to the fourth mount point to complete the normal partition restoration.
Citation Information
Patent Citations
System backup and restoring method based on Btrfs file system
CN106502830A
Snapshot rollback tool, method and equipment
CN111625499A