|
a/scripts/backup_project.py |
|
b/scripts/backup_project.py |
|
... |
|
... |
45 |
with open(os.path.join(dirname, 'project.bson'), 'w') as fp:
|
45 |
with open(os.path.join(dirname, 'project.bson'), 'w') as fp:
|
46 |
_write_bson(fp, state(project).document)
|
46 |
_write_bson(fp, state(project).document)
|
47 |
c.project = project
|
47 |
c.project = project
|
48 |
app_config_ids = [
|
48 |
app_config_ids = [
|
49 |
ac._id for ac in M.AppConfig.query.find(dict(project_id=c.project._id)) ]
|
49 |
ac._id for ac in M.AppConfig.query.find(dict(project_id=c.project._id)) ]
|
|
|
50 |
visited_collections = {}
|
50 |
for name, cls in MappedClass._registry.iteritems():
|
51 |
for name, cls in MappedClass._registry.iteritems():
|
|
|
52 |
cname = cls.__mongometa__.name
|
|
|
53 |
sess = cls.__mongometa__.session
|
|
|
54 |
if sess is None:
|
|
|
55 |
log.info('Skipping %s which has no session', cls)
|
|
|
56 |
continue
|
|
|
57 |
dbname = sess.impl.db.name
|
|
|
58 |
fqname = cname + '/' + dbname
|
|
|
59 |
if fqname in visited_collections:
|
|
|
60 |
log.info('Skipping %s (already dumped collection %s in %s)',
|
|
|
61 |
cls, fqname, visited_collections[fqname])
|
|
|
62 |
continue
|
|
|
63 |
visited_collections[fqname] = cls
|
51 |
if 'project_id' in mapper(cls).property_index:
|
64 |
if 'project_id' in mapper(cls).property_index:
|
52 |
# Dump the things directly related to the project
|
65 |
# Dump the things directly related to the project
|
53 |
oq = cls.query.find(dict(project_id=project._id))
|
66 |
oq = cls.query.find(dict(project_id=project._id))
|
54 |
elif 'app_config_id' in mapper(cls).property_index:
|
67 |
elif 'app_config_id' in mapper(cls).property_index:
|
55 |
# ... and the things related to its apps
|
68 |
# ... and the things related to its apps
|
|
... |
|
... |
57 |
else:
|
70 |
else:
|
58 |
# Don't dump other things
|
71 |
# Don't dump other things
|
59 |
continue
|
72 |
continue
|
60 |
num_objs = oq.count()
|
73 |
num_objs = oq.count()
|
61 |
if num_objs == 0: continue
|
74 |
if num_objs == 0: continue
|
|
|
75 |
if not os.path.exists(os.path.join(dirname, dbname)):
|
|
|
76 |
os.mkdir(os.path.join(dirname, dbname))
|
62 |
fname = os.path.join(
|
77 |
fname = os.path.join(
|
63 |
dirname,
|
78 |
dirname,
|
|
|
79 |
dbname,
|
64 |
'%s.bson' % (cls.__mongometa__.name))
|
80 |
'%s.bson' % (cls.__mongometa__.name))
|
65 |
log.info('%s: dumping %s objects to %s',
|
81 |
log.info('%s: dumping %s objects to %s',
|
66 |
name, num_objs, fname)
|
82 |
name, num_objs, fname)
|
67 |
with open(os.path.join(dirname, fname), 'w') as fp:
|
83 |
with open(os.path.join(dirname, fname), 'w') as fp:
|
68 |
for obj in oq.ming_cursor: _write_bson(fp, obj)
|
84 |
for obj in oq.ming_cursor: _write_bson(fp, obj)
|