为什么 ZFS 回滚会破坏中间快照?

为什么 ZFS 回滚会破坏中间快照?

之前使用过 btrfs,我惊讶地发现回滚 ZFS 中的快照不仅会改变文件的“工作集”,还会改变文件的“工作集”。要求比回滚目标更新的任何快照也必须被销毁:

 zfs rollback [-Rfr] snapshot
   Roll back the given dataset to a previous snapshot.  When a dataset is rolled back, all
   data that has changed since the snapshot is discarded, and the dataset reverts to the
   state at the time of the snapshot.  By default, the command refuses to roll back to a
   snapshot other than the most recent one.  In order to do so, all intermediate snapshots
   and bookmarks must be destroyed by specifying the -r option.

为了比较,这里有一些描述非破坏性回滚到 btrfs 中的快照:

btrfs sub snap -r fs 快照
# ...在 fs 上做事
btrfs sub del fs # 此时你将失去那些事物你已经完成了
                 # 如果你想保留它们,只需重命名即可FS反而
btrfs 子快照快照 fs # 恢复快照作为读+写FS
btrfs sub del snapshot #删除不再需要的只读快照

非破坏性回滚到 ZFS 中的快照:

zfs snapshot pool/project/production@today
zfs clone pool/project/production@today pool/project/beta
# make changes to /pool/project/beta and test them
zfs promote pool/project/beta
zfs rename pool/project/production pool/project/legacy
zfs rename pool/project/beta pool/project/production
# once the legacy version is no longer needed, it can be destroyed
zfs destroy pool/project/legacy

“快照”在 btrfs 和 ZFS 中显然是不同的东西,但我想知道破坏性操作的优势zfs rollback是什么,特别是因为似乎没有独立的非破坏性回滚命令。我希望,除非明确请求,否则最常需要的回滚操作是仅影响文件当前状态的操作,而不是基于创建时间的任意标准的其他不相关的快照。

我可以想象几种原因(例如历史、性能、存储空间、实现的简单性),尽管没有多少让我觉得令人信服的,所以相关的背景信息将不胜感激!

答案1

我对zfs设计的理解,回滚的目的是立即撤消自上次快照以来的所有更改。由于这是破坏性的,安全措施是只允许返回一个快照。但是,您可以返回一个,然后返回一个,等等......以获取缩进的快照。然而,要意识到自该快照以来您将失去所有数据集机会。

但是,如果目的是将数据集中的文件访问回特定快照,您实际上可以随时访问它,而无需执行任何 zfs 命令。只需通过文件系统访问快照即可。

例如:(切换到root用户,即$ sudo -s bash

# zfs mount cypher-pool/data /data # mount dataset onto /data 
# cd /data                              # Where the dataset is mounted.
# cd .zfs                               # Note: system hidden directory
# cd snapshot                           # Location of all dataset snapshots
# ls                                    # List of current dataset snapshots
autosnap_2023-01-01_00:00:01_monthly
autosnap_2023-01-01_00:00:01_yearly
autosnap_2023-02-01_00:00:03_monthly
autosnap_2023-03-01_00:00:01_monthly
autosnap_2023-04-01_00:00:02_monthly
autosnap_2023-05-01_00:00:01_monthly
autosnap_2023-06-01_00:00:01_monthly
autosnap_2023-07-01_00:01:07_monthly
autosnap_2023-07-03_23:30:01_weekly
autosnap_2023-07-10_23:30:19_weekly
autosnap_2023-07-17_23:30:01_weekly
autosnap_2023-07-24_23:30:02_weekly
autosnap_2023-07-31_23:30:02_weekly
autosnap_2023-08-01_00:00:02_monthly
autosnap_2023-08-07_23:30:02_weekly
autosnap_2023-08-09_00:00:02_daily
autosnap_2023-08-10_00:00:04_daily
autosnap_2023-08-11_00:00:01_daily
autosnap_2023-08-12_00:00:03_daily
autosnap_2023-08-13_00:00:01_daily
autosnap_2023-08-14_00:00:01_daily
autosnap_2023-08-14_23:30:50_weekly
autosnap_2023-08-15_00:00:02_daily
autosnap_2023-08-15_07:00:02_hourly
autosnap_2023-08-15_08:00:02_hourly
autosnap_2023-08-15_09:00:02_hourly
autosnap_2023-08-15_10:00:01_hourly
# cd autosnap_2023-04-01_00:00:02_monthly
# ls 
(all my files in my dataset as of the snapshot on April 1st 2023)

据我所知,您可以操作和查看这些文件,就像它是普通文件系统的一部分一样。

如果您确实想将快照“恢复”到当前活动文件系统区域,则始终可以将文件从快照“rsync”或“cp”到活动区域。

拍摄当前状态的快照,以防出现问题。 (当然,使用您当前的日期和时间)

# zfs snapshot cypher-pool/data@backup_2023-08-21_16:20:42_backup

从类似的事情开始。 (使用前请确认选项)

# cd /data/.zfs/snapshot`
# rsync -Pav autosnap_2023-04-01_00:00:02_monthly/* /data/

注意:示例快照,就像由 ZFS Sanoid 创建的一样,https://github.com/jimsalterjrs/sanoid

相关内容