neomigrate.py 2.59 KB
Newer Older
1
#!/usr/bin/env python
2 3 4
#
# neomaster - run a master node of NEO
#
5
# Copyright (C) 2006-2015  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 neo.lib.config import getOptionParser
21 22 23 24
import time
import os

# register options
25 26 27 28
parser = getOptionParser()
parser.add_option('-s', '--source', help='the source database')
parser.add_option('-d', '--destination', help='the destination database')
parser.add_option('-c', '--cluster', help='the NEO cluster name')
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

def main(args=None):
    # parse options
    (options, args) = parser.parse_args(args=args)
    source = options.source or None
    destination = options.destination or None
    cluster = options.cluster or None

    # check options
    if source is None or destination is None:
        raise RuntimeError('Source and destination databases must be supplied')
    if cluster is None:
        raise RuntimeError('The NEO cluster name must be supplied')

    # open storages
    from ZODB.FileStorage import FileStorage
    from neo.client.Storage import Storage as NEOStorage
    if os.path.exists(source):
47 48 49 50 51 52
        print("WARNING: This is not the recommended way to import data to NEO:"
              " you should use Imported 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.")
53
        src = FileStorage(file_name=source, read_only=True)
54
        dst = NEOStorage(master_nodes=destination, name=cluster,
55
                         logfile=options.logfile)
56
    else:
57
        src = NEOStorage(master_nodes=source, name=cluster,
58
                         logfile=options.logfile, read_only=True)
59 60 61 62 63
        dst = FileStorage(file_name=destination)

    # do the job
    print "Migrating from %s to %s" % (source, destination)
    start = time.time()
64
    dst.copyTransactionsFrom(src)
65 66 67
    elapsed = time.time() - start
    print "Migration done in %3.5f" % (elapsed, )