> For the complete documentation index, see [llms.txt](https://overleaf-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://overleaf-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。

對於 Redis 持久化，建議使用 Append Only File（AOF）持久化設定。

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

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

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

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

假設新執行個體目前還沒有資料，以下是你可以遵循的一些步驟。大致上，我們會製作一個 tar 檔，內容包含 `mongo`, `redis` 以及 `Overleaf` 各個 volume，將其複製到新伺服器上，再在那裡解壓回來。

#### 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` 各個 volume 的路徑。

{% 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. 快取（e）
         * 用於編譯的二進位檔快取
      2. 編譯（e）
         * LaTeX 編譯會在這裡進行
      3. db.sqlite（d）
         * 在最新版本中未使用；先前儲存 clsi 快取詳細資料（不是改為簡單的記憶體對應表，就是掃描磁碟）
      4. db.sqlite-wal（d）
         * 在最新版本中未使用，請參閱 db.sqlite
      5. 輸出（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://overleaf-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.
