Switch to unified view

a/scripts/backup_project.py b/scripts/backup_project.py
1
import os
1
import os
2
import sys
2
import sys
3
import json
3
import struct
4
import logging
4
import logging
5
5
6
from pylons import g
6
from pylons import c
7
from pymongo.bson import BSON
7
8
8
from ming.orm import state, session
9
from ming.orm import MappedClass, state, mapper
9
10
10
from pymongo.json_util import default
11
from allura import model as M
11
from allura import model as M
12
12
13
log = logging.getLogger(__name__)
13
log = logging.getLogger(__name__)
14
15
MONGO_HOME=os.environ.get('MONGO_HOME', '/usr')
16
MONGO_DUMP=os.path.join(MONGO_HOME, 'bin/mongodump')
17
MONGO_RESTORE=os.path.join(MONGO_HOME, 'bin/mongorestore')
18
14
19
def main():
15
def main():
20
    if len(sys.argv) not in (2,3):
16
    if len(sys.argv) not in (2,3):
21
        log.error('Usage: %s <shortname> [<backup_dir>]', sys.argv[0])
17
        log.error('Usage: %s <shortname> [<backup_dir>]', sys.argv[0])
22
        return 1
18
        return 1
...
...
36
            os.getcwd(), dirname)
32
            os.getcwd(), dirname)
37
    log.info('Backing up %s to %s', pname, backup_dir)
33
    log.info('Backing up %s to %s', pname, backup_dir)
38
    dump_project(project, backup_dir)
34
    dump_project(project, backup_dir)
39
    return 0
35
    return 0
40
36
37
def _write_bson(fp, doc):
38
    bson = BSON.from_dict(doc)
39
    fp.write(struct.pack('!l', len(bson)))
40
    fp.write(bson)
41
41
def dump_project(project, dirname):
42
def dump_project(project, dirname):
42
    os.system('%s --db %s -o %s' % (
43
    if not os.path.exists(dirname):
43
            MONGO_DUMP, project.database, dirname))
44
        os.mkdir(dirname)
44
    with open(os.path.join(dirname, 'project.json'), 'w') as fp:
45
    with open(os.path.join(dirname, 'project.bson'), 'w') as fp:
45
        json.dump(state(project).document, fp, default=default)
46
        _write_bson(fp, state(project).document)
47
    c.project = project
48
    app_config_ids = [
49
        ac._id for ac in M.AppConfig.query.find(dict(project_id=c.project._id)) ]
50
    for name, cls in MappedClass._registry.iteritems():
51
        if 'project_id' in mapper(cls).property_index:
52
            # Dump the things directly related to the project
53
            oq = cls.query.find(dict(project_id=project._id))
54
        elif 'app_config_id' in mapper(cls).property_index:
55
            # ... and the things related to its apps
56
            oq = cls.query.find(dict(app_config_id={'$in':app_config_ids}))
57
        else:
58
            # Don't dump other things
59
            continue
60
        num_objs = oq.count()
61
        if num_objs == 0: continue
62
        fname = os.path.join(
63
            dirname,
64
            '%s.bson' % (cls.__mongometa__.name))
65
        log.info('%s: dumping %s objects to %s',
66
                 name, num_objs, fname)
67
        with open(os.path.join(dirname, fname), 'w') as fp:
68
            for obj in oq.ming_cursor: _write_bson(fp, obj)
46
69
47
if __name__ == '__main__':
70
if __name__ == '__main__':
48
    sys.exit(main())
71
    sys.exit(main())