用于备份appengine数据存储区的建议策略

Ril*_*ark 7 google-app-engine database-backups google-cloud-datastore

现在我使用remote_api并appcfg.py download_data每晚拍摄我的数据库的快照.这需要很长时间(6小时)并且很昂贵.如果没有滚动我自己的基于更改的备份(我不敢做那样的事情),那么确保我的数据安全无故障的最佳选择是什么?

PS:我认识到谷歌的数据可能比我的更安全.但是,如果有一天我不小心写了一个删除它的程序呢?

Dre*_*ars 3

我想你已经基本确定了所有的选择。

  1. 相信谷歌不会丢失您的数据,并希望您不会意外地指示他们销毁它。
  2. 使用 执行完整备份download_data,如果成本过高,频率可能低于每晚一次。
  3. 推出您自己的增量备份解决方案。

选项3实际上是一个有趣的想法。您需要所有实体上的修改时间戳,并且您不会捕获已删除的实体,但否则使用remote_api和游标是非常可行的。

编辑

这是一个与remote_api 一起使用的简单增量下载器。再次强调,它不会注意到已删除的实体,并且假设所有实体都将上次修改时间存储在名为 Updated_at 的属性中。使用它需要您自担风险。

import os
import hashlib
import gzip
from google.appengine.api import app_identity
from google.appengine.ext.db.metadata import Kind
from google.appengine.api.datastore import Query
from google.appengine.datastore.datastore_query import Cursor

INDEX = 'updated_at'
BATCH = 50
DEPTH = 3

path = ['backups', app_identity.get_application_id()]
for kind in Kind.all():
  kind = kind.kind_name
  if kind.startswith('__'):
    continue
  while True:
    print 'Fetching %d %s entities' % (BATCH, kind)
    path.extend([kind, 'cursor.txt'])
    try:
      cursor = open(os.path.join(*path)).read()
      cursor = Cursor.from_websafe_string(cursor)
    except IOError:
      cursor = None
    path.pop()
    query = Query(kind, cursor=cursor)
    query.Order(INDEX)
    entities = query.Get(BATCH)
    for entity in entities:
      hash = hashlib.sha1(str(entity.key())).hexdigest()
      for i in range(DEPTH):
        path.append(hash[i])
      try:
        os.makedirs(os.path.join(*path))
      except OSError:
        pass
      path.append('%s.xml.gz' % entity.key())
      print 'Writing', os.path.join(*path)
      file = gzip.open(os.path.join(*path), 'wb')
      file.write(entity.ToXml())
      file.close()
      path = path[:-1-DEPTH]
    if entities:
      path.append('cursor.txt')
      file = open(os.path.join(*path), 'w')
      file.write(query.GetCursor().to_websafe_string())
      file.close()
      path.pop()
    path.pop()
    if len(entities) < BATCH:
      break
Run Code Online (Sandbox Code Playgroud)