Skip to content

Instantly share code, notes, and snippets.

@isdyy
Last active August 29, 2015 14:07
Show Gist options
  • Save isdyy/c4cd96eed4fce9ee671d to your computer and use it in GitHub Desktop.
Save isdyy/c4cd96eed4fce9ee671d to your computer and use it in GitHub Desktop.
GAEのデータストアバックアップを直接読みだして何かする
# -*- 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
@isdyy
Copy link
Author

isdyy commented Oct 19, 2014

Datastore Admin で作った Cloud Storage 上の datastore backup について、***.backup_info***.SomeKind.backup_info がないとツールからのリストアができないが、それぞれのバックアップファイルからレコードを直接読み出して操作することはできる。ごく部分的なリストアを行う場合とか。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment