2011年2月9日水曜日

google app engine のデータストアをバックアップ、リストア(bulk loader ツール)

今日はGAEのデータストアのバックアップとリストアに挑戦。
(bulk loader ツール)

まずここを参照。
「データのアップロードとダウンロード」
http://code.google.com/intl/ja/appengine/docs/python/tools/uploadingdata.html

ふむふむ。
まずRemote API を有効にする必要があるとの事。
app.yamlの末尾に以下を追加
  1. builtins:  
  2. - remote_api: on  
  3. - datastore_admin: on  

・・・「datastore_admin: on」はついで。ダッシュボード側のデータストアのコピー操作ができるようになるおまじない。これは今回は使わないけどおまけw

で、コマンドプロンプトをだして(自分、Win環境ですが何か!?)

  1. appcfg.py update D:\gaestudy\hogehoge  

これは難なく成功。あ、ちなみに今回のappidは「hogehoge」
で次はと・・・
まずダウンロードしてみるか。ドキュメントにあるようにやってみる。
  1. appcfg.py download_data --application=hogehoge --kind=Greeting --url=http://hogehoge.appspot.com/_ah/remote_api --filename=D:\gaestudy\hogehoge\bbbbb.dump  

あ、ちなみに今回抽出したkind(テーブル?)はGreetingって名前。ま、掲示板。
bbbbb.dumpってファイルに出力。
できた。中身みたら、バイナリデータっぽい。
意外に簡単だなぁ

次アップロードいってみよう。まずダッシュボード側で既存のレコードを削除
で、ドキュメントにあるようにやってみる

  1. appcfg.py upload_data --application=hogehoge --kind=Greeting --filename=D:\gaestudy\hogehoge\bbbbb.dump D:\gaestudy\hogehoge  

ぬぉ、エラー!!!
  1. Application: hogehoge; version: 1.  
  2. Uploading data records.  
  3. [INFO    ] Logging to bulkloader-log-20110210.105147  
  4. [INFO    ] Throttling transfers:  
  5. [INFO    ] Bandwidth: 250000 bytes/second  
  6. [INFO    ] HTTP connections: 8/second  
  7. [INFO    ] Entities inserted/fetched/modified: 20/second  
  8. [INFO    ] Batch Size: 10  
  9. [INFO    ] Opening database: bulkloader-progress-20110210.105147.sql3  
  10. [INFO    ] Connecting to hogehoge.appspot.com/_ah/remote_api(/.*)  
  11. [ERROR   ] Exception during authentication  
  12. Traceback (most recent call last):  
  13.   File "C:\Program Files\Google\google_appengine\google\appengine\tools\bulkloader.py", line 3175, in Run  
  14.     self.request_manager.Authenticate()  
  15.   File "C:\Program Files\Google\google_appengine\google\appengine\tools\bulkloader.py", line 1187, in Authenticate  
  16.     remote_api_stub.MaybeInvokeAuthentication()  
  17.   File "C:\Program Files\Google\google_appengine\google\appengine\ext\remote_api\remote_api_stub.py", line 540, in MaybeInvokeAuthentication  
  18.     datastore_stub._server.Send(datastore_stub._path, payload=None)  
  19.   File "C:\Program Files\Google\google_appengine\google\appengine\tools\appengine_rpc.py", line 341, in Send  
  20.     f = self.opener.open(req)  
  21.   File "C:\Python25\lib\urllib2.py", line 387, in open  
  22.     response = meth(req, response)  
  23.   File "C:\Python25\lib\urllib2.py", line 498, in http_response  
  24.     'http', request, response, code, msg, hdrs)  
  25.   File "C:\Python25\lib\urllib2.py", line 425, in error  
  26.     return self._call_chain(*args)  
  27.   File "C:\Python25\lib\urllib2.py", line 360, in _call_chain  
  28.     result = func(*args)  
  29.   File "C:\Python25\lib\urllib2.py", line 506, in http_error_default  
  30.     raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)  
  31. HTTPError: HTTP Error 404: Not Found  
  32. [INFO    ] Authentication Failed  

「HTTPError: HTTP Error 404: Not Found」?

アップ先が見つからないっぽい?よくわからないので、まずappcfg.py自体の説明を読んでみる。
http://code.google.com/intl/ja/appengine/docs/python/tools/uploadinganapp.html

  1. appcfg.py help <action>  
  2. #指定されたアクションに関するヘルプ メッセージを出力し、終了します。  

というわけで、まぁhelpを見てみよう

  1. appcfg.py help upload_data  

で、出た結果がこれ

  1. >appcfg.py help upload_data  
  2. Usage: appcfg.py [options] upload_data >directory<  
  3.   
  4. Upload data records to datastore.  
  5.   
  6. The 'upload_data' command translates input records into datastore entities and  
  7. uploads them into your application's datastore.  
  8.   
  9. Options:  
  10.   -h, --help            Show the help message and exit.  
  11.   -q, --quiet           Print errors only.  
  12.   -v, --verbose         Print info level logs.  
  13.   --noisy               Print all logs.  
  14.   -s SERVER, --server=SERVER  
  15.                         The server to connect to.  
  16.   --insecure            Use HTTP when communicating with the server.  
  17.   -e EMAIL, --email=EMAIL  
  18.                         The username to use. Will prompt if omitted.  
  19.   -H HOST, --host=HOST  Overrides the Host header sent with all RPCs.  
  20.   --no_cookies          Do not save authentication cookies to local disk.  
  21.   --passin              Read the login password from stdin.  
  22.   -A APP_ID, --application=APP_ID  
  23.                         Override application from app.yaml file.  
  24.   -V VERSION, --version=VERSION  
  25.                         Override (major) version from app.yaml file.  
  26.   --filename=FILENAME   The name of the file containing the input data.  
  27.                         (Required)  
  28.   --kind=KIND           The kind of the entities to store.  
  29.   --url=URL             The location of the remote_api endpoint.  
  30.   --num_threads=NUM_THREADS  
  31.                         Number of threads to upload records with.  
  32.   --batch_size=BATCH_SIZE  
  33.                         Number of records to post in each request.  
  34.   --bandwidth_limit=BANDWIDTH_LIMIT  
  35.                         The maximum bytes/second bandwidth for transfers.  
  36.   --rps_limit=RPS_LIMIT  
  37.                         The maximum records/second for transfers.  
  38.   --http_limit=HTTP_LIMIT  
  39.                         The maximum requests/second for transfers.  
  40.   --db_filename=DB_FILENAME  
  41.                         Name of the progress database file.  
  42.   --auth_domain=AUTH_DOMAIN  
  43.                         The name of the authorization domain to use.  
  44.   --log_file=LOG_FILE   File to write bulkloader logs.  If not supplied then a  
  45.                         new log file will be created, named: bulkloader-log-  
  46.                         TIMESTAMP.  
  47.   --dry_run             Do not execute any remote_api calls  
  48.   --namespace=NAMESPACE  
  49.                         Namespace to use when accessing datastore.  
  50.   --has_header          Whether the first line of the input file should be  
  51.                         skipped  
  52.   --loader_opts=LOADER_OPTS  
  53.                         A string to pass to the Loader.initialize method.  
  54.   --config_file=CONFIG_FILE  
  55.                         Name of the configuration file.  

なるほど。とりあえず「--noisy」をつけて全ログを出してみるか

  1. appcfg.py upload_data --noisy --application=hogehoge --kind=Greeting --filename=D:\gaestudy\hogehoge\bbbbb.dump D:\gaestudy\hogehoge  

で出た結果↓

  1. Application: hogehoge; version: 1.  
  2. Uploading data records.  
  3. [INFO    ] Logging to bulkloader-log-20110210.105736  
  4. [INFO    ] Throttling transfers:  
  5. [INFO    ] Bandwidth: 250000 bytes/second  
  6. [INFO    ] HTTP connections: 8/second  
  7. [INFO    ] Entities inserted/fetched/modified: 20/second  
  8. [INFO    ] Batch Size: 10  
  9. [INFO    ] Opening database: bulkloader-progress-20110210.105736.sql3  
  10. [DEBUG   ] [Thread-1] RestoreThread: started  
  11. [DEBUG   ] [Thread-1] RestoreThread: exiting  
  12. [DEBUG   ] [Thread-2] RestoreThread: started  
  13. [DEBUG   ] [Thread-2] RestoreThread: exiting  
  14. [DEBUG   ] [Thread-3] WorkerThread: started  
  15. [DEBUG   ] [Thread-4] WorkerThread: started  
  16. [DEBUG   ] [Thread-5] WorkerThread: started  
  17. [DEBUG   ] [Thread-6] WorkerThread: started  
  18. [DEBUG   ] [Thread-7] WorkerThread: started  
  19. [DEBUG   ] [Thread-8] WorkerThread: started  
  20. [DEBUG   ] [Thread-9] WorkerThread: started  
  21. [DEBUG   ] [Thread-10] WorkerThread: started  
  22. [DEBUG   ] [Thread-11] WorkerThread: started  
  23. [DEBUG   ] [Thread-12] WorkerThread: started  
  24. [DEBUG   ] Configuring remote_api. url_path = /_ah/remote_api(/.*), servername = hogehoge.appspot.com  
  25. [DEBUG   ] Bulkloader using app_id: hogehoge  
  26. [INFO    ] Connecting to hogehoge.appspot.com/_ah/remote_api(/.*)  
  27. [ERROR   ] Exception during authentication  
  28. Traceback (most recent call last):  
  29.   File "C:\Program Files\Google\google_appengine\google\appengine\tools\bulkloader.py", line 3175, in Run  
  30.     self.request_manager.Authenticate()  
  31.   File "C:\Program Files\Google\google_appengine\google\appengine\tools\bulkloader.py", line 1187, in Authenticate  
  32.     remote_api_stub.MaybeInvokeAuthentication()  
  33.   File "C:\Program Files\Google\google_appengine\google\appengine\ext\remote_api\remote_api_stub.py", line 540, in MaybeInvokeAuthentication  
  34.     datastore_stub._server.Send(datastore_stub._path, payload=None)  
  35.   File "C:\Program Files\Google\google_appengine\google\appengine\tools\appengine_rpc.py", line 341, in Send  
  36.     f = self.opener.open(req)  
  37.   File "C:\Python25\lib\urllib2.py", line 387, in open  
  38.     response = meth(req, response)  
  39.   File "C:\Python25\lib\urllib2.py", line 498, in http_response  
  40.     'http', request, response, code, msg, hdrs)  
  41.   File "C:\Python25\lib\urllib2.py", line 425, in error  
  42.     return self._call_chain(*args)  
  43.   File "C:\Python25\lib\urllib2.py", line 360, in _call_chain  
  44.     result = func(*args)  
  45.   File "C:\Python25\lib\urllib2.py", line 506, in http_error_default  
  46.     raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)  
  47. HTTPError: HTTP Error 404: Not Found  
  48. [INFO    ] Authentication Failed  

なんか文字を連結してるっぽいところがある
[DEBUG ] Configuring remote_api. url_path = /_ah/remote_api(/.*), servername = hogehoge.appspot.com

?urlのpathなのにhttp://が入ってねーな。まぁそうはいっても中で補完してるよね、と思いたいが、んー、怪しい。
そういえば、さっきのコマンドにurl直接指定出来なかったっけ。もう一回help見てみる。
--url=URL The location of the remote_api endpoint.

あったあった。要するにurlを引数から作ってるなら、最初から絶対パスを渡してやれば間違いないのでは。
という事で付け足してみる。

  1. appcfg.py upload_data --noisy --application=hogehoge --kind=Greeting --filename=D:\gaestudy\hogehoge\bbbbb.dump --url=http://hogehoge.appspot.com/_ah/remote_api D:\gaestudy\hogehoge  

おおおお、いった。成功ログ↓

  1. Application: hogehoge; version: 1.  
  2. Uploading data records.  
  3. [INFO    ] Logging to bulkloader-log-20110210.110730  
  4. [INFO    ] Throttling transfers:  
  5. [INFO    ] Bandwidth: 250000 bytes/second  
  6. [INFO    ] HTTP connections: 8/second  
  7. [INFO    ] Entities inserted/fetched/modified: 20/second  
  8. [INFO    ] Batch Size: 10  
  9. [INFO    ] Opening database: bulkloader-progress-20110210.110730.sql3  
  10. [DEBUG   ] [Thread-1] RestoreThread: started  
  11. [DEBUG   ] [Thread-1] RestoreThread: exiting  
  12. [DEBUG   ] [Thread-2] RestoreThread: started  
  13. [DEBUG   ] [Thread-2] RestoreThread: exiting  
  14. [DEBUG   ] [Thread-3] WorkerThread: started  
  15. [DEBUG   ] [Thread-4] WorkerThread: started  
  16. [DEBUG   ] [Thread-5] WorkerThread: started  
  17. [DEBUG   ] [Thread-6] WorkerThread: started  
  18. [DEBUG   ] [Thread-7] WorkerThread: started  
  19. [DEBUG   ] [Thread-8] WorkerThread: started  
  20. [DEBUG   ] [Thread-9] WorkerThread: started  
  21. [DEBUG   ] [Thread-10] WorkerThread: started  
  22. [DEBUG   ] [Thread-11] WorkerThread: started  
  23. [DEBUG   ] [Thread-12] WorkerThread: started  
  24. [DEBUG   ] Configuring remote_api. url_path = /_ah/remote_api, servername = hogehoge.appspot.com  
  25. [DEBUG   ] Bulkloader using app_id: hogehoge  
  26. [INFO    ] Connecting to hogehoge.appspot.com/_ah/remote_api  
  27. [DEBUG   ] [Thread-13] ProgressTrackerThread: started  
  28. [DEBUG   ] [Thread-14] DataSourceThread: started  
  29. [INFO    ] Starting import; maximum 10 entities per post  
  30. [DEBUG   ] [Thread-14] DataSourceThread: exiting  
  31. [DEBUG   ] Waiting for worker threads to finish...  
  32. [DEBUG   ] [Thread-3] Got work item [1-7]  
  33. [DEBUG   ] [Thread-3] [1-7] Transferred 7 entities in 8.0 seconds  
  34. .[DEBUG   ] [Thread-5] WorkerThread: exiting  
  35. [DEBUG   ] [Thread-6] WorkerThread: exiting  
  36. [DEBUG   ] [Thread-7] WorkerThread: exiting  
  37. [DEBUG   ] [Thread-8] WorkerThread: exiting  
  38. [DEBUG   ] [Thread-9] WorkerThread: exiting  
  39. [DEBUG   ] [Thread-10] WorkerThread: exiting  
  40. [DEBUG   ] [Thread-11] WorkerThread: exiting  
  41. [DEBUG   ] [Thread-12] WorkerThread: exiting  
  42. [DEBUG   ] [Thread-4] WorkerThread: exiting  
  43. [DEBUG   ] Waiting for Anonymous_WorkerThread to exit  
  44. [DEBUG   ] [Thread-3] WorkerThread: exiting  
  45. [DEBUG   ] Waiting for Anonymous_WorkerThread to exit  
  46. [DEBUG   ] Waiting for Anonymous_WorkerThread to exit  
  47. [DEBUG   ] Waiting for Anonymous_WorkerThread to exit  
  48. [DEBUG   ] Waiting for Anonymous_WorkerThread to exit  
  49. [DEBUG   ] Waiting for Anonymous_WorkerThread to exit  
  50. [DEBUG   ] Waiting for Anonymous_WorkerThread to exit  
  51. [DEBUG   ] Waiting for Anonymous_WorkerThread to exit  
  52. [DEBUG   ] Waiting for Anonymous_WorkerThread to exit  
  53. [DEBUG   ] Waiting for Anonymous_WorkerThread to exit  
  54.   
  55. [DEBUG   ] Waiting for progress_thread to terminate...  
  56. [DEBUG   ] [Thread-13] ProgressTrackerThread: exiting  
  57. [DEBUG   ] ... done.  
  58. [INFO    ] 7 entites total, 0 previously transferred  
  59. [INFO    ] 7 entities (6381 bytes) transferred in 9.8 seconds  
  60. [INFO    ] All entities successfully transferred  

なんか時間かかったな。
GAEのdashboardに入ってDatastore Viewerの該当kindを見てみると、ちゃんと入ってる。

ちなみに、既存のデータは「ID/Name」をキーにして上書きされた。

dashboardで新しいレコードを入れてからアップロードしてみると、新しいレコードは消えない事を確認。

あと、既存の情報(アップロードファイルに含まれるレコード)を、dashboard側を変更してからアップしたら、変更したレコードはファイルの情報で上書きされた。

ただ、変更や新規がある時は解析?か何かに若干時間がかかるのか、たまに

  1. HTTPError: HTTP Error 500: Internal Server Error  

が出る事があったが、もう一度やるとうまくいった。まぁタイミングかな。気にするほどではないか。

ちなみにローカル環境にアップロードする場合はurlを変えるだけでいけた。

  1. appcfg.py upload_data --noisy --application=hogehoge --kind=Greeting --filename=D:\gaestudy\hogehoge\bbbbb.dump --url=http://localhost:8080/_ah/remote_api D:\gaestudy\hogehoge  

もちろん「Run」状態にして、urlが生きていないと入らない。
Development(SDK) Console で確認できる

http://localhost:8080/_ah/admin/

これでインポートとエクスポートは簡単になったなー。
次回は「Bulk Loader」を使ってcsvからのインポートやcsvでのエクスポートに挑戦予定。
これができるとcsv上で編集や追加した上で反映させられるので、かなり実用的になる。

免責
この記事やプログラムによって生じた事故・損害などは一切保証致しません。ご自身の責任でご使用ください。

子育てブログ「おとう日記」はじめました。
興味ある方、是非ご覧下さい!
おとう日記

コピペプログラマの倉庫を作りました。
サンプルプログラムなど置いておきますのでお立ち寄り下さい。
コピペプログラマ倉庫


良ければ↓投票お願いします↓ m(._.)m ペコッ
人気ブログランキングへ

2011年2月2日水曜日

GAE(google app engine) ssl のエラー 結局・・・解決した!

長かった。が解決した。
結論、リンク切れになってたインストーラーを見つけた。
ご丁寧に置いてくれていた人がいたのだ。

とはいえ、途中までモジュールからインストーラーを生成しようと頑張ったよ。
モジュールってこれね。

http://pypi.python.org/pypi/ssl

でもVS2003がないとかなんとかエラー吐かれるので、頑張って準備して、あれこれやってたらふと
・・・ん?なんだこのリンク。もしや・・・
偶然見つけて、いれてみたらビンゴだった。

コピペプログラマの倉庫に置いてあるから欲しい人はどうぞ。↓

http://sites.google.com/site/copypesouko/souko/--googleappengine

免責
この記事やプログラムによって生じた事故・損害などは一切保証致しません。ご自身の責任でご使用ください。

子育てブログ「おとう日記」はじめました。
興味ある方、是非ご覧下さい!
おとう日記

コピペプログラマの倉庫を作りました。
サンプルプログラムなど置いておきますのでお立ち寄り下さい。
コピペプログラマ倉庫


良ければ↓投票お願いします↓ m(._.)m ペコッ
人気ブログランキングへ