-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RestoreExistingClusterCli.java
148 lines (133 loc) · 5.58 KB
/
RestoreExistingClusterCli.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.coreedge.backup;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.neo4j.commandline.admin.AdminCommand;
import org.neo4j.commandline.admin.CommandFailed;
import org.neo4j.commandline.admin.IncorrectUsage;
import org.neo4j.commandline.admin.OutsideWorld;
import org.neo4j.coreedge.convert.ConversionVerifier;
import org.neo4j.coreedge.convert.ConvertClassicStoreToCoreCommand;
import org.neo4j.dbms.DatabaseManagementSystemSettings;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Args;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.util.Converters;
import org.neo4j.restore.RestoreDatabaseCommand;
import org.neo4j.server.configuration.ConfigLoader;
import static org.neo4j.dbms.DatabaseManagementSystemSettings.database_path;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.record_format;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
public class RestoreExistingClusterCli implements AdminCommand
{
public static class Provider extends AdminCommand.Provider
{
public Provider()
{
super( "restore-existing-cluster" );
}
@Override
public Optional<String> arguments()
{
return Optional.of( "--from=<backup-directory> --database=<database-name> --cluster-seed=<cluster-seed> " +
"[--force]" );
}
@Override
public String description()
{
return "Restores a database backed up using the neo4j-backup tool to be used as an instance of a " +
"new cluster. Takes the seed output from `restore-new-cluster` as an argument. ";
}
@Override
public AdminCommand create( Path homeDir, Path configDir, OutsideWorld outsideWorld )
{
return new RestoreExistingClusterCli( homeDir, configDir );
}
}
private final Path homeDir;
private final Path configDir;
public RestoreExistingClusterCli( Path homeDir, Path configDir )
{
this.homeDir = homeDir;
this.configDir = configDir;
}
@Override
public void execute( String[] incomingArguments ) throws IncorrectUsage, CommandFailed
{
String databaseName;
String fromPath;
String clusterSeed;
boolean forceOverwrite;
Args args = Args.parse( incomingArguments );
try
{
databaseName = args.interpretOption( "database", Converters.mandatory(), s -> s );
fromPath = args.interpretOption( "from", Converters.mandatory(), s -> s );
clusterSeed = args.interpretOption( "cluster-seed", Converters.mandatory(), s -> s );
forceOverwrite = args.getBoolean( "force", Boolean.FALSE, true );
}
catch ( IllegalArgumentException e )
{
throw new IncorrectUsage( e.getMessage() );
}
try
{
Config config = loadNeo4jConfig( homeDir, configDir, databaseName );
restoreDatabase( databaseName, fromPath, forceOverwrite, config );
convertStore( config, clusterSeed );
}
catch ( IOException | TransactionFailureException e )
{
throw new RuntimeException( e );
}
}
private static Config loadNeo4jConfig( Path homeDir, Path configDir, String databaseName )
{
ConfigLoader configLoader = new ConfigLoader( settings() );
Config config = configLoader.loadConfig( Optional.of( homeDir.toFile() ),
Optional.of( configDir.resolve( "neo4j.conf" ).toFile() ) );
return config.with( stringMap( DatabaseManagementSystemSettings.active_database.name(), databaseName ) );
}
private static void convertStore( Config config, String seed ) throws IOException, TransactionFailureException
{
ConvertClassicStoreToCoreCommand convert = new ConvertClassicStoreToCoreCommand( new ConversionVerifier() );
convert.convert( config.get( database_path ), config.get( record_format ), seed );
}
private static void restoreDatabase( String databaseName, String fromPath, boolean forceOverwrite, Config config )
throws IOException
{
new RestoreDatabaseCommand( new DefaultFileSystemAbstraction(), new File( fromPath ), config, databaseName,
forceOverwrite ).execute();
}
public static List<Class<?>> settings()
{
List<Class<?>> settings = new ArrayList<>();
settings.add( GraphDatabaseSettings.class );
settings.add( DatabaseManagementSystemSettings.class );
return settings;
}
}