Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Shuky/ed8728f8eb6187475b9a to your computer and use it in GitHub Desktop.

Select an option

Save Shuky/ed8728f8eb6187475b9a to your computer and use it in GitHub Desktop.
App-engine Backup Selective restoration - This script helps you restore only a specific namespace from an appengine cron backup for all namespaces
###
### This script helps you restore only a specific namespace from an appengine cron backup for all namespaces
###
# Make sure App Engine SDK is available
import sys
import cStringIO
import os
#sys.path.append('c:\\Program Files (x86)\\Google\\google_appengine')
#sys.path.append('c:\\Program Files (x86)\\Google\\google_appengine\\lib')
#sys.path.append('c:\\Program Files (x86)\\Google\\google_appengine\\lib\\yaml')
#sys.path.append('c:\\Program Files (x86)\\Google\\google_appengine\\google\\net')
#sys.path.append('c:\\Program Files (x86)\\Google\\google_appengine\\google\\net\\proto')
#sys.path.append('c:\\Program Files (x86)\\Google\\google_appengine\\google\\appengine\\ext')
from google.appengine.api.files import records
from google.appengine.datastore import entity_pb
from google.appengine.api import datastore
from google.appengine.ext.datastore_admin import backup_pb2
### 1. pull backup files from google cloud storage to local computer (use gsutil)
### 2. install python-remote-api (appengine-enable-remote-api-python)
### 3. connect to your app with the python interactive shell and load this script
### takes a shitty regular appengine google cloud backup file and restores only a namespace from it
### file prefix looks something like this:
### ahZzfnN0b3JlZS1wcm9kdWN0aW9uLWlsckELEhxfQUVfRGF0YXN414QWRtaW5fT3BlcmF0aW9uGI241FFX0JhY2t1cF9JbmZvcm1hdGlvbhgBDA
### (where files are ahZzfnN0b3JlZS1wcm9kdWN0aW9uLWlsckELEhxfQUVfRGF0YXN414QWRtaW5fT3BlcmF0aW9uGI241FFX0JhY2t1cF9JbmZvcm1hdGlvbhgBDA.Kind.backup_info)
def restoreKind( namespace, kind, file_prefix ):
raw = open(file_prefix+'.'+kind+'.backup_info', 'rb')
backup = backup_pb2.Backup()
backup.ParseFromString(raw.read())
for f in (backup.kind_info[0].file):
try:
f = f.replace("/gs/<your-bucket-name>/", "<your-local-path>"); #change this for the files to be found
#print f
curr = file(f, "rb");
reader = records.RecordsReader(cStringIO.StringIO(curr.read()))
#print reader
for record in reader:
#count = count + 1
entity_proto = entity_pb.EntityProto(contents=record)
entity = datastore.Entity.FromPb(entity_proto)
if (entity.namespace() == namespace):
#print entity
datastore.Put(entity);
#Entity is available as a dictionary!
except Exception, e:
print e;
###
# restores wanted kinds for selected namespace
# file_prefix - see above
# namespace - the namespace you want to restore
# kinds - a kinds array of the kinds you want to restore
def restoreKinds(file_prefix, namespace, kinds):
for kind in (kinds):
restoreKind(namespace, kind, file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment