> For the complete documentation index, see [llms.txt](https://ayakaleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ayakaleaf-pro.ayaka.space/on-premises/zh-tw/wei-hu/data-and-backups.md).

# 資料與備份

有時候，隨著我們讓 Overleaf 持續演進，我們需要變更資料庫中資料的結構；此時會使用遷移腳本來自動化這個流程。它們將已在 [overleaf.com](https://docs.overleaf.com/on-premises/configuration/overleaf-toolkit) 最先的那個執行個體上執行，而這是世界上最大的 Overleaf 執行個體，因此大多數情況應該已經遇過了，不過我們不對您的資料作任何保證。請確保您建立一份 **一致的** 您的資料備份 **之前** 升級您的執行個體。

{% hint style="info" %}
當升級到新的 Docker 映像時，任何尚未 **不要** 執行的遷移都會自動執行；這可能需要一些時間，視您的資料集大小而定，查看日誌尾端可讓您知道進度。更多資訊請參閱我們的 [日誌](https://docs.overleaf.com/on-premises/configuration/overleaf-toolkit/logging) 文件。
{% endhint %}

### 資料儲存

Overleaf Community Edition 和 Server Pro 會將其資料儲存在三個不同的地方：

* **MongoDB 資料庫：** 這裡存放使用者與專案資料。
* **Redis：** 作為運行中資料的高效能快取，主要儲存與專案編輯和協作相關的資訊。
* **Overleaf 檔案系統：** 儲存不可編輯的專案檔案（包含圖片），並在專案編譯期間充當暫時磁碟快取。

{% hint style="info" %}
這可能是 `~/sharelatex_data` 或 `~/overleaf_data`，視您執行個體設定的時間而定。
{% endhint %}

{% hint style="success" %}
對於專案檔案與完整專案歷史資料，我們也支援相容 S3 的儲存後端。
{% endhint %}

更多關於磁碟上資料夾配置的資訊，請參見「資料夾詳情」。

### 執行一致備份

在進行一致備份時，需要包含三個儲存區：

* MongoDB
* Redis
* Overleaf 檔案系統資料

為了產生一致備份， **必須** 在備份程序執行期間阻止使用者產生新資料。因此我們建議安排一個維護時段，在此期間使用者應無法存取該執行個體或編輯其專案。

在開始備份程序之前，您需要讓您的執行個體離線。從 Server Pro 開始 `3.5.0` 關機流程會自動關閉網站並中斷使用者連線。

要關閉您的執行個體，您需要執行 `bin/docker-compose stop sharelatex` 如果您使用的是 Toolkit 部署，或是 `docker compose stop sharelatex` 如果您使用的是 Docker Compose。

一旦 `sharelatex` 容器已停止，您就可以開始備份程序。

一旦備份程序 **成功** 完成，您將需要啟動 `sharelatex` 容器。要這麼做，請執行 `bin/docker-compose start sharelatex` 如果您使用的是 Toolkit 部署，或是 `docker compose start sharelatex` 如果您使用的是 Docker Compose。

{% hint style="danger" %}

* 備份應儲存在與您的 Overleaf 執行個體不同的伺服器上，理想情況下應位於完全不同的地點。
* 將資料庫複寫到多個 MongoDB 執行個體上或許能提供一些冗餘，但無法防止資料損毀。
* 測試您的備份是確保它們完整且可用的最佳方法。
  {% endhint %}

### MongoDB

MongoDB 內建一個名為 [mongodump](https://docs.mongodb.com/manual/reference/program/mongodump/) 的命令列工具，可用來建立儲存在資料庫中的使用者與專案資料備份。

### Overleaf 檔案系統資料

對於 Toolkit 部署，存放不可編輯檔案的路徑是在 `config/overleaf.rc` 使用 `OVERLEAF_DATA_PATH` 環境變數中指定，但視您的執行個體建立時間而定，這可能是 `data/sharelatex`.

使用像 **rsync** 這樣的工具遞迴複製此目錄，是確保建立完整備份所必需的。

### Redis

Redis 會在將使用者工作階段與待處理的文件更新寫入 MongoDB 之前先儲存它們。

Append Only File（AOF）持久化是 Redis 持久化的建議設定。

Toolkit 使用者預設已為 **新的** 安裝啟用 AOF；現有使用者可找到更多關於啟用 AOF [這裡](/on-premises/zh-tw/she-ding/overleaf-toolkit/redis.md#enabling-append-only-file-persistence).

如果您決定在啟用 AOF 持久化的同時繼續使用 RDB 快照，您可以將 RDB 檔案複製到安全位置作為備份。

### 在伺服器之間遷移資料

最好的情況下，您的新執行個體中還沒有任何有價值的資料。我們沒有用來合併各執行個體資料的流程。

假設新執行個體目前還沒有資料，您可以遵循以下步驟。概括來說，我們會產生以下項目的 tar 壓縮包： `mongo`, `redis` 以及 `overleaf` 磁碟區，將它複製到新伺服器，再在那裡重新解壓。

#### Toolkit

```bash
# 優雅地關閉舊執行個體
old-server$ bin/stop

# 建立 tar 壓縮包
old-server$ tar --create --file backup-old-server.tar config/ data/

# 將 backup-old-server.tar 檔案從舊伺服器複製到
# 新伺服器，使用任何適合的方法

# 優雅地關閉新執行個體（如果尚未啟動）
new-server$ bin/stop

# 移走新資料，您也可以將其刪除
new-server$ mkdir backup-new-server
new-server$ mv config/ data/ backup-new-server/

# 再次填入 config/data 目錄
new-server$ tar --extract --file backup-old-server.tar

# 啟動容器
new-server$ bin/up
```

#### Docker Compose

```bash
# 優雅地關閉舊執行個體
old-server$ docker stop sharelatex
old-server$ docker stop mongo redis

# 建立 tar 壓縮包
old-server$ tar --create --file backup-old-server.tar ~/OVERLEAF_data ~/mongo_data ~/redis_data

# 將 backup-old-server.tar 檔案從舊伺服器複製到
# 新伺服器，使用任何適合的方法

# 優雅地關閉新執行個體（如果尚未啟動）
new-server$ docker stop sharelatex
new-server$ docker stop mongo redis

# 移走新資料，您也可以將其刪除
new-server$ mkdir backup-new-server
new-server$ mv ~/OVERLEAF_data ~/mongo_data ~/redis_data backup-new-server/

# 再次填入資料目錄
new-server$ tar --extract --file backup-old-server.tar

# 啟動容器
new-server$ docker start mongo redis
new-server$ docker start sharelatex
```

視您的 **docker-compose.yml** 檔案而定，您可能需要調整以下項目的路徑： `mongo`, `redis`, `overleaf` 磁碟區。

{% hint style="info" %}
當以 root 使用者執行（或搭配 sudo）時，tar 會保留檔案擁有者/群組與權限，這在還原備份時至關重要。
{% endhint %}

### 資料夾詳情

{% hint style="info" %}
下列資料夾有額外提示：

* (b) 應包含於備份中；為確保一致性，最好在執行個體停止時進行
* (d) 可刪除
* (e) 暫存檔案；在執行個體停止時可刪除
  {% endhint %}

1. `~/mongo_data` (b)
   * mongodb 資料目錄
2. `~/redis_data` (b)
   * redis 資料庫資料目錄
3. `~/overleaf_data`
   1. bin
      1. synctex (d)
         * 在最新版本中未使用；先前使用的是自訂的 synctex 二進位檔（synctex 用於 .tex 檔案與 PDF 之間的來源對應）
   2. data
      1. cache (e)
         * 編譯用的二進位檔快取
      2. compiles (e)
         * LaTeX 編譯在此進行
      3. db.sqlite (d)
         * 在最新版本中未使用；先前儲存 clsi 快取詳細資訊（已移至簡單的記憶體對映，或改為掃描磁碟）
      4. db.sqlite-wal (d)
         * 在最新版本中未使用，請參見 db.sqlite
      5. output (e)
         * 用於向用戶端提供的 LaTeX 編譯輸出儲存
      6. template\_files (b)
         * 範本系統的圖片預覽（僅限 Server Pro）
      7. user\_files (b)
         * 專案的二進位檔
      8. history (b)
         * 完整專案歷史檔案
   3. tmp
      1. dumpFolder (e)
         * 處理 zip 檔案時產生的暫存檔案
      2. uploads (e)
         * 檔案上傳的緩衝區（binary file/new-project-from-zip 上傳）
      3. projectHistories (e)
         * 完整專案歷史遷移的暫存檔案


---

# 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 dynamically by asking a question.

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

```
GET https://ayakaleaf-pro.ayaka.space/on-premises/zh-tw/wei-hu/data-and-backups.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
