neomigrate.py 2.97 KB
Newer Older
1
#!/usr/bin/env python
2
#
3
# neomigrate - import/export data between NEO and a FileStorage
4
#
5
# Copyright (C) 2006-2019  Nexedi SA
6
#
7 8 9 10
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
11
#
12 13 14 15 16 17
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19

20
from __future__ import print_function
21 22
import time
import os
23
from neo.lib.app import buildOptionParser
24

25 26 27 28 29 30 31
import_warning = (
    "WARNING: This is not the recommended way to import data to NEO:"
    " you should use the Importer backend instead.\n"
    "NEO also does not implement IStorageRestoreable interface, which"
    " means that undo information is not preserved when using this tool:"
    " conflict resolution could happen when undoing an old transaction."
)
32

33 34 35 36 37 38 39 40 41 42 43 44 45
@buildOptionParser
class NEOMigrate(object):

    from neo.lib.config import OptionList

    @classmethod
    def _buildOptionParser(cls):
        parser = cls.option_parser
        parser.description = "NEO <-> FileStorage conversion tool"
        parser('c', 'cluster', required=True, help='the NEO cluster name')
        parser.bool('q', 'quiet', help='print nothing to standard output')
        parser.argument('source', help='the source database')
        parser.argument('destination', help='the destination database')
46

47 48 49 50 51
    def __init__(self, config):
        self.name = config.pop('cluster')
        self.source = config.pop('source')
        self.destination = config.pop('destination')
        self.quiet = config.pop('quiet', False)
52

53 54 55 56 57 58 59 60 61 62 63 64
        from ZODB.FileStorage import FileStorage
        from neo.client.Storage import Storage as NEOStorage
        if os.path.exists(self.source):
            if not self.quiet:
                print(import_warning)
            self.src = FileStorage(file_name=self.source, read_only=True)
            self.dst = NEOStorage(master_nodes=self.destination, name=self.name,
                                  **config)
        else:
            self.src = NEOStorage(master_nodes=self.source, name=self.name,
                                  read_only=True, **config)
            self.dst = FileStorage(file_name=self.destination)
65

66 67 68 69 70 71 72 73
    def run(self):
        if not self.quiet:
            print("Migrating from %s to %s" % (self.source, self.destination))
            start = time.time()
        self.dst.copyTransactionsFrom(self.src)
        if not self.quiet:
            elapsed = time.time() - start
            print("Migration done in %3.5f" % elapsed)
74

75 76 77 78

def main(args=None):
    config = NEOMigrate.option_parser.parse(args)
    NEOMigrate(config).run()