Last active
August 29, 2015 14:07
-
-
Save isdyy/c4cd96eed4fce9ee671d to your computer and use it in GitHub Desktop.
GAEのデータストアバックアップを直接読みだして何かする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import logging | |
import os | |
logger = logging.getLogger() | |
def read_record_from_backup(filename): | |
try: | |
from google.appengine.ext.mapreduce import records | |
except ImportError: | |
from google.appengine._internal.mapreduce import records | |
from google.appengine.api import files, datastore | |
from google.appengine.datastore import entity_pb | |
logger.info('Process %s', filename) | |
if filename.startswith('/gs/'): | |
# filename like '/gs/bucket-name/path/to/datastore_backup_xxxxxxx_output-9-attempt-1' | |
fh = files.BufferedFile(filename) | |
else: | |
# local | |
fh = open(filename, 'r') | |
reader = records.RecordsReader(fh) | |
reader.seek(0) | |
while True: | |
try: | |
record = reader.read() | |
if not record: | |
break | |
pb = entity_pb.EntityProto(contents=record) | |
entity = datastore.Entity.FromPb(pb) | |
# Filter entities | |
# if entity.get('some_property') != 'some_value': | |
# continue | |
# handle (write to datastore, etc...) | |
logger.info('-- key: %r, entity: %r', entity.key(), entity) | |
except EOFError: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Datastore Admin で作った Cloud Storage 上の datastore backup について、
***.backup_info
や***.SomeKind.backup_info
がないとツールからのリストアができないが、それぞれのバックアップファイルからレコードを直接読み出して操作することはできる。ごく部分的なリストアを行う場合とか。