> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/server-administration/backup/backup-file.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 25.2. 檔案系統層級備份 \#

### 25.2. 檔案系統層級備份 [#](#backup-file) <a href="#backup-file" id="backup-file"></a>

另一種備份策略是直接複製 PostgreSQL 用來在資料庫中儲存資料的檔案；[Section 18.2](https://docs.postgresql.tw/19/server-administration/runtime/creating-cluster.md) 說明了這些檔案所在的位置。你可以使用任何你偏好的方法 來進行檔案系統備份，例如：

```

tar -cf backup.tar /usr/local/pgsql/data
```

不過，這個方法有兩項限制，使其不切實際， 或至少不如 pg\_dump 方法：

1. 資料庫伺服器*必須*先關閉，才能取得可用的備份。 半調子的做法，例如僅禁止所有連線，是*行不通*的 （部分原因是 `tar` 及類似工具並不會對檔案系統狀態 進行原子式的快照，另一部分原因則是伺服器內部的緩衝機制）。 關於如何停止伺服器的資訊，可參閱 [Section 18.5](https://docs.postgresql.tw/19/server-administration/runtime/server-shutdown.md)。不用說， 在還原資料之前，你同樣也需要先關閉伺服器。
2. 若你已深入研究過資料庫的檔案系統配置細節，你可能會想嘗試 只從對應的檔案或目錄中，備份或還原個別的資料表或資料庫。 這是*行不通*的，因為這些檔案中所包含的資訊， 若缺少記錄了所有交易提交狀態的提交日誌檔 `pg_xact/*`，就無法使用。資料表檔案唯有搭配這項 資訊才可用。當然，也不可能只還原一個資料表以及相關聯的 `pg_xact` 資料，因為那會使資料庫叢集中所有其他 資料表都無法使用。因此，檔案系統備份只適用於整個資料庫 叢集的完整備份與還原。

另一種檔案系統備份的做法，是在檔案系統支援該功能的前提下 （並且你也信任其實作正確無誤），對資料目錄製作一份 「一致性快照」。典型的程序是先對存放資料庫的磁碟區 製作一份「凍結快照」，然後將整個資料目錄（而不只是部分內容， 理由同上）從該快照複製到備份裝置，接著再釋放該凍結快照。 即使資料庫伺服器正在執行中，這個方法依然可行。然而， 以這種方式建立的備份，其所儲存的資料庫檔案狀態， 就如同資料庫伺服器未正常關閉一樣；因此，當你在還原的資料上 啟動資料庫伺服器時，它會認為前一個伺服器執行個體發生當機， 並會重播 WAL 日誌。這並不是問題，只要你知道會有這種情況即可 （並務必將 WAL 檔案一併納入備份中）。你可以在製作快照之前 執行一次 `CHECKPOINT`，以縮短還原所需的時間。

若你的資料庫分散在多個檔案系統上，可能就沒有辦法針對所有 磁碟區取得完全同步的凍結快照。舉例來說，若你的資料檔案與 WAL 日誌位於不同磁碟，或是資料表空間位於不同的檔案系統上， 就可能無法使用快照備份，因為這些快照*必須*是同步的。 在這類情況下，於信任一致性快照技術之前，請務必仔細閱讀 你所使用的檔案系統文件。

若無法取得同步快照，其中一個選項是將資料庫伺服器關閉 足夠長的時間，以便建立所有的凍結快照。另一個選項則是 執行持續歸檔式的基礎備份（[Section 25.3.2](https://docs.postgresql.tw/19/server-administration/backup/continuous-archiving.md#backup-base-backup)）， 因為這類備份不受備份期間檔案系統變動的影響。這需要 僅在備份過程中啟用持續歸檔；還原時則採用持續歸檔還原 （[Section 25.3.5](https://docs.postgresql.tw/19/server-administration/backup/continuous-archiving.md#backup-pitr-recovery)）。

另一個選項是使用 rsync 來執行檔案系統備份。做法是先在 資料庫伺服器執行期間執行一次 rsync， 接著將資料庫伺服器關閉足夠長的時間，執行一次 `rsync --checksum`。 （之所以需要 `--checksum`，是因為 `rsync` 只能 以一秒為單位判斷檔案的修改時間精細度。）第二次執行的 rsync 會比第一次快，因為需要傳輸的資料相對較少，且由於伺服器 當時已關閉，最終結果會是一致的。這個方法可讓檔案系統備份 在最短的停機時間內完成。

請注意，檔案系統備份的容量通常會比 SQL 傾印大。 （舉例來說，pg\_dump 不需要傾印索引的內容， 只需要傾印重建索引所需的指令。）然而，製作檔案系統備份 的速度可能較快。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/backup-file.html)（原文版本：18.6；核對日期：2026-09-28）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/server-administration/backup/backup-file.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
