OpenResty Edge 資料庫備份

1. 前言

我們使用 PostgreSQL 作為 Edge 的資料庫。我們通常稱 Edge Admin 所使用的資料庫為 Edge Admin DB。 Edge Admin DB 記憶體儲了所有應用的配置資訊,一旦資料庫損壞將無法恢復,因此需要對 Edge Admin DB 進行定時備份。

我們還有一個 Edge Log Server 的資料庫,我們稱它為 Edge Log Server DB。 Edge Log Server DB 中儲存了日誌和統計指標資料,我們也需要對該資料庫進行定時備份。

2. 下載指令碼

我們提供了一個備份資料庫的指令碼,支援備份 Edge Admin DB 到指定的本機目錄,也支援同步備份檔案到遠端機器上。

登入到 Edge Admin DB 的機器上,執行以下命令下載指令碼到當前目錄。

curl -O https://openresty.com/client/oredge/edge-admin-database-backup.sh

我們提供了一個備份資料庫的指令碼,支援備份 Edge Log Server DB 到指定的本機目錄,也支援同步備份檔案到遠端機器上。

登入到 Edge Log Server DB 的機器上,執行以下命令下載指令碼到當前目錄。

curl -O https://openresty.com/client/oredge/edge-log-server-database-backup.sh

3. 手動執行指令碼

指令碼用法如下:

Usage:
    sudo bash edge-admin-database-backup.sh [backup_dir] [backup_count] [remote_user?]@[remote_host?]:[remote_port?] [remote_dir?]

    backup_dir         the local directory for database backup files
    backup_count       number of backup files to keep, old backups will be cleaned up automatically
    remote_host        the ssh host of the remote machine used for rsync synchronization backups (optional)
    remote_user        the ssh user of the remote machine (optional)
    remote_port        the ssh port of the remote machine (optional)
    remote_dir         the remote directory for database backup files (optional)

example:
    sudo bash edge-admin-database-backup.sh /local/db_backup 10
    sudo bash edge-admin-database-backup.sh /local/db_backup 10 192.168.0.2 /remote/db_backup
    sudo bash edge-admin-database-backup.sh /local/db_backup 10 192.168.0.2:1022 /remote/db_backup
    sudo bash edge-admin-database-backup.sh /local/db_backup 10 root@192.168.0.2:1022 /remote/db_backup
Usage:
    sudo bash edge-log-server-database-backup.sh [backup_dir] [backup_count] [remote_user?]@[remote_host?]:[remote_port?] [remote_dir?]

    backup_dir         the local directory for database backup files
    backup_count       number of backup files to keep, old backups will be cleaned up automatically
    remote_host        the ssh host of the remote machine used for rsync synchronization backups (optional)
    remote_user        the ssh user of the remote machine (optional)
    remote_port        the ssh port of the remote machine (optional)
    remote_dir         the remote directory for database backup files (optional)

example:
    sudo bash edge-log-server-database-backup.sh /local/db_backup 10
    sudo bash edge-log-server-database-backup.sh /local/db_backup 10 192.168.0.2 /remote/db_backup
    sudo bash edge-log-server-database-backup.sh /local/db_backup 10 192.168.0.2:1022 /remote/db_backup
    sudo bash edge-log-server-database-backup.sh /local/db_backup 10 root@192.168.0.2:1022 /remote/db_backup

有幾個需要注意的地方:

  • 指令碼執行儘量選擇在業務不繁忙的時候進行,執行時長與資料量有關。
  • backup_dir 需要和 Edge Admin DB 在不同硬碟上,否則資料庫所在硬碟損壞後,備份檔案也會損壞。
  • backup_count 需要根據當前硬碟容量來調整,當超出數量限制之後,每次備份後就會清理最老的備份檔案。
  • remote_host 我們強烈建議將備份檔案也同步到其他機器上,本地的備份檔案也有可能會丟失。

指令碼執行成功後,最後會輸出:

> Edge Admin database backup successfully!

4. 配置 crontab

手動執行指令碼並驗證成功以後,我們可以透過 crontab 配置定時執行備份。

這裡我們提供一個每天定時備份的例子:

0 1 * * * sudo bash /path/to/edge-admin-database-backup.sh  /local/db_backup 10 192.168.0.2 /remote/db_backup >> /tmp/backup.log 2>&1
0 1 * * * sudo bash /path/to/edge-log-server-database-backup.sh  /local/db_backup_2 10 192.168.0.2 /remote/db_backup_2 >> /tmp/backup.log 2>&1

定時執行的備份指令碼的輸出在 /tmp/backup.log 中,可以檢視 /tmp/backup.log 或者檢視備份目錄的檔案確認任務執行成功。

5. 執行資料庫恢復

如果 Edge Admin DB 出了故障,需要恢復時,登入 Edge Admin DB 執行 pg_restore,下面是一個示例:

export PATH=/usr/local/openresty/postgresql/bin:/usr/local/openresty-postgresql12/bin:$PATH
gzip -dkc /local/db_backup/or_edge_admin-2022-10-01 > /local/db_backup/or_edge_admin-2022-10-01.sql
psql -Upostgres -f /local/db_backup/or_edge_admin-2022-10-01.sql

如果 Edge Log Server DB 出了故障,需要恢復時,登入 Edge Log Server DB 執行 pg_restore,下面是一個示例:

export PATH=/usr/local/openresty/postgresql/bin:/usr/local/openresty-postgresql12/bin:$PATH
gzip -dkc /local/db_backup_2/or_edge_log_server-2022-10-01 > /local/db_backup_2/or_edge_log_server-2022-10-01.sql
psql -Upostgres -f /local/db_backup_2/or_edge_log_server-2022-10-01.sql