這次筆記主要記錄如何將 Google App Engine (GAE) 上的 Datastore 資料備份下來。

Google 文件中說明當你要利用 Bulk loader 備份資料時建議使用 Master/Slave datastore,不保證 High Replication datastore 可以成功備份(會提示high_replication_warning警告),且後者下載下來的資料不保證是最新的。即使 GAE App 在建立時就選擇了 High Replication (官方建議),但還是有方法完整備份。

筆者為了希望日後能將數據資料備份下來分析,因此另開一個 Master/Slave datastore,並在 GAE Dashboard 後台將 High Replication datastore 的 App 所有 entities 複製到新開的 Master/Slave datastore 的 App 中。再將 Master/Slave datastore 的 App 經由 bulk loader 備份下來成為 csv 通用格式。

在 Master/Slave datastore 的 App 打開複製 entities 到其他 App 權限的方式:
找到 App 根目錄下的 appengine_config.py (沒有則新建)
加入:
remoteapi_CUSTOM_ENVIRONMENT_AUTHENTICATION = ('HTTP_X_APPENGINE_INBOUND_APPID', ['source appid here'])

['source appid here'] 為複製來源的 GAE App Name



無論要下載資料庫中的資料或是複製資料到其他 App,都需在 app.yaml 中設定 remote_api,下面將一一描述備份方法:

{APP_NAME} 為您 GAE 的 App 名稱
{TABLE_NAME} 為您要備份的Table (在GAE Datastore中稱Kind),不填則為全部備份


1. 設定 App 的 remote_api (app.yaml)
builtins:
  - remote_api: on


2. 讓系統自動產生 Bulk Loader 設定檔
appcfg.py create_bulkloader_config --filename=bulkloader.yaml --url=http://{APP_NAME}.appspot.com/_ah/remote_api

3. 修改 bulkloader 設定檔 (bulkloader.yaml),kind為table名稱,connector為輸出格式(csv, xml..等),connector_options為附加選項,encoding預設為utf-8可不填
- kind: UserDB
  connector: csv
  connector_options:
  encoding: big5


4. 下載或上傳資料庫中資料:
appcfg.py download_data --application={APP_NAME} --url=http://{APP_NAME}.appspot.com/_ah/remote_api --filename=檔名.csv --config_file=bulkloader.yaml --kind={TABLE_NAME}

5. Done! 成功拿到 Datastore 備份囉!

出處:
http://code.google.com/appengine/docs/python/tools/uploadingdata.html#Downloading_and_Uploading_All_Data

arrow
arrow
    文章標籤
    Google AppEngine GAE Python
    全站熱搜

    DK 發表在 痞客邦 留言(0) 人氣()