# 20.10.文件系统快照

FreeBSD 提供了一项与 [软更新（Soft Updates）](https://docs.freebsd.org/en/books/handbook/config/#soft-updates) 结合使用的功能：文件系统快照。

UFS 快照允许用户创建指定文件系统的镜像，并将其作为文件处理。如果你使用的是 [Z 文件系统（ZFS）](https://docs.freebsd.org/en/books/handbook/zfs/#)，请参考 [管理快照](https://docs.freebsd.org/en/books/handbook/zfs/#zfs-zfs-snapshot) 以了解如何使用快照。

快照文件必须在执行操作的文件系统中创建，每个文件系统最多可以创建 20 个快照。活动快照会记录在超级块中，因此它们在卸载和重新挂载操作以及系统重启后会保持持久性。当不再需要某个快照时，可以使用 [rm(1)](https://man.freebsd.org/cgi/man.cgi?query=rm\&sektion=1\&format=html) 删除它。尽管可以按任意顺序删除快照，但可能无法完全回收所有使用的空间，因为另一个快照可能会占用一些已释放的块。

不可更改的 `snapshot` 文件标志由 [mksnap\_ffs(8)](https://man.freebsd.org/cgi/man.cgi?query=mksnap_ffs\&sektion=8\&format=html) 在快照文件初始创建时设置。对于快照文件，[unlink(1)](https://man.freebsd.org/cgi/man.cgi?query=unlink\&sektion=1\&format=html) 例外，允许它们被删除。

快照是通过 [mount(8)](https://man.freebsd.org/cgi/man.cgi?query=mount\&sektion=8\&format=html) 创建的。要将 **/var** 的快照放入文件 **/var/snapshot/snap** 中，请使用以下命令：

```sh
# mount -u -o snapshot /var/snapshot/snap /var
```

或者，使用 [mksnap\_ffs(8)](https://man.freebsd.org/cgi/man.cgi?query=mksnap_ffs\&sektion=8\&format=html) 创建快照：

```sh
# mksnap_ffs /var /var/snapshot/snap
```

可以使用 [find(1)](https://man.freebsd.org/cgi/man.cgi?query=find\&sektion=1\&format=html) 查找文件系统中的快照文件，例如 **/var**：

```sh
# find /var -flags snapshot
```

创建快照后，它有多个用途：

* 一些管理员会使用快照文件进行备份，因为快照可以被传输到 CD 或磁带。
* 可以在快照上运行文件系统完整性检查工具 [fsck(8)](https://man.freebsd.org/cgi/man.cgi?query=fsck\&sektion=8\&format=html)。假设文件系统在挂载时是干净的，这将始终提供一个干净且不变的结果。
* 在快照上运行 [dump(8)](https://man.freebsd.org/cgi/man.cgi?query=dump\&sektion=8\&format=html) 会生成一个与文件系统和快照时间戳一致的转储文件。`dump(8)` 也可以使用 `-L` 选项在创建转储镜像后直接删除快照。
* 快照可以作为文件系统的冻结镜像进行挂载。要挂载快照 **/var/snapshot/snap**，可以执行以下命令：

  ```sh
  # mdconfig -a -t vnode -o readonly -f /var/snapshot/snap -u 4
  # mount -r /dev/md4 /mnt
  ```

现在，冻结的 **/var** 可以通过 **/mnt** 访问。一切都会保持快照创建时的状态。唯一的例外是，任何早期的快照将显示为空文件。要卸载快照，请执行：

```sh
# umount /mnt
# mdconfig -d -u 4
```

有关 `softupdates` 和文件系统快照的更多信息，包括技术论文，请访问 Marshall Kirk McKusick 的网站 <http://www.mckusick.com/>。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.bsdcn.org/di-20-zhang-cun-chu/20.10.-nei-cun-pan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
