Commit 55b22a10 authored by Daniel Eggert's avatar Daniel Eggert
Browse files

replaced db config backup with config transfer

parent 678cecf9
package de.potsdam.gfz.gms;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.cli.CommandLine;
......@@ -13,37 +14,33 @@ import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import de.potsdam.gfz.gms.database.SceneDatabase;
import org.postgis.PGgeometry;
public class DbUtils {
private static final String OP_BACKUP_CONFIG = "b";
private static final String OP_RESTORE_CONFIG = "r";
// private static final String OP_UPDATE_SCENE_STATUS = "u";
private static final String FILENAME_PROPERTIES = "dbs.properties";
private static final String OP_TRANSFER_CONFIG = "c";
private static final String OP_SCENE_STATE = "s";
public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption(Option.builder(OP_BACKUP_CONFIG).longOpt("backup-cfg").argName("file").desc("stores a copy of the gms config table in the given file")
.numberOfArgs(1).build());
options.addOption(Option.builder(OP_RESTORE_CONFIG).longOpt("restore-cfg").argName("file").desc("restores the db config table from the given file")
.numberOfArgs(1).build());
// options.addOption(Option.builder(OP_UPDATE_SCENE_STATUS).longOpt("update-scene-status").hasArg(false)
// .desc("updates the proc_level of the scenes to match the downloaded scenes on the harddrive").build());
options.addOption(Option.builder(OP_TRANSFER_CONFIG).longOpt("transfer-cfg")
.desc("transfers the config key:values from the source to the destination database (defined in the " + FILENAME_PROPERTIES + " file)").hasArg(false).build());
options.addOption(Option.builder(OP_SCENE_STATE).longOpt("transfer-scene-state")
.desc("transfers the scenes.proc_level status from the source to the destination database (defined in the " + FILENAME_PROPERTIES + " file)").hasArg(false)
.build());
DefaultParser parser = new DefaultParser();
try {
CommandLine cli = parser.parse(options, args);
if (cli.hasOption(OP_BACKUP_CONFIG)) {
backupConfig(cli.getOptionValue(OP_BACKUP_CONFIG));
} else if (cli.hasOption(OP_RESTORE_CONFIG)) {
if (cli.hasOption(OP_TRANSFER_CONFIG)) {
transferConfig();
} else if (cli.hasOption(OP_SCENE_STATE)) {
System.err.println("not yet implemented");
restoreConfig(cli.getOptionValue(OP_RESTORE_CONFIG));
// } else if (cli.hasOption(OP_UPDATE_SCENE_STATUS)) {
// System.err.println("not yet implemented");
// updateSceneStatus();
transferSceneState();
} else {
printHelp(options);
}
......@@ -59,117 +56,80 @@ public class DbUtils {
hf.printHelp("java -jar gms-db-utils.jar", options, true);
}
private static void backupConfig(String filepath) throws IOException {
System.out.println("creating backup of gms config table");
private static void transferConfig() throws IOException, SQLException {
// establish connections
Connection srcDb = getSrcDb();
Connection dstDb = getDstDb();
SceneDatabase db = SceneDatabase.getInstance();
// clear config in dst db
dstDb.prepareStatement("truncate config;").executeUpdate();
// get current config
Properties props = new Properties();
props.putAll(db.getConfig());
// get config from src db
ResultSet rs = srcDb.prepareStatement("select * from config;").executeQuery();
PreparedStatement pst = dstDb.prepareStatement("insert into config values (?, ?);");
// copy entries to dst db
while (rs.next()) {
pst.setString(1, rs.getString(1));
pst.setString(2, rs.getString(2));
pst.executeUpdate();
}
props.store(new FileOutputStream(filepath), "backup of gms db config table");
dstDb.commit();
}
private static void transferSceneState() throws IOException {
}
private static void restoreConfig(String filepath) throws IOException {
Map<String, String> dbCfg = SceneDatabase.getInstance().getConfig();
Properties localCfg = new Properties();
localCfg.load(new FileInputStream(filepath));
private static final String SRC_DB_PROPS_KEY_PREFIX = "src_db_";
private static final String DEST_DB_PROPS_KEY_PREFIX = "dest_db_";
private static final String DB_HOST_KEY = "host";
private static final String DB_PORT_KEY = "port";
private static final String DB_USER_KEY = "user";
private static final String DB_PASS_KEY = "pass";
private static final String DB_DBNAME_KEY = "database";
for (Entry<String, String> entry : dbCfg.entrySet()) {
if (!entry.getValue().equals(localCfg.getProperty(entry.getKey()))) {
System.err.println("not matching cfg values found for key '" + entry.getKey() + "'");
System.err.println("dbCfg value: " + entry.getValue());
System.err.println("localCfg value: " + localCfg.getProperty(entry.getKey()));
}
}
private static Connection getSrcDb() throws IOException {
return getDbConnection(SRC_DB_PROPS_KEY_PREFIX);
}
private static Connection getDstDb() throws IOException {
return getDbConnection(DEST_DB_PROPS_KEY_PREFIX);
}
private static Connection getDbConnection(String prefix) throws IOException {
Properties props = new Properties();
props.load(DbUtils.class.getResourceAsStream("/" + FILENAME_PROPERTIES));
String host = props.getProperty(prefix + DB_HOST_KEY);
String port = props.getProperty(prefix + DB_PORT_KEY);
String user = props.getProperty(prefix + DB_USER_KEY);
String pass = props.getProperty(prefix + DB_PASS_KEY);
String dbName = props.getProperty(prefix + DB_DBNAME_KEY);
return getConnection(host, port, user, pass, dbName);
}
/**
* won't work since there is no general way to determine the filename from the ids or vice versa
* @throws SQLException
*/
// private static void updateSceneStatus() throws SQLException, IOException {
// // reset all scenes to METADATA proc_level
// // resetScenesToMetadataLevel();
//
// SceneDatabase db = SceneDatabase.getInstance();
//
// final PreparedStatement[] psts = new PreparedStatement[3];
// psts[0] = db.prepareCustomStatement("update scenes set proc_level='DOWNLOADED'::proc_level where satelliteid=? and fi")
//
// Map<String, String> cfg = db.getConfig();
//
// final Path downloadFolder = Paths.get(cfg.get("path_data_root") + cfg.get("foldername_download"));
//
// @SuppressWarnings("unchecked")
// final Map<String, Short>[] idMaps = new HashMap[3];
//
// idMaps[0] = getNameIdMap("satellites");
// idMaps[1] = getNameIdMap("sensors");
// idMaps[2] = getNameIdMap("subsystems");
//
// Files.walkFileTree(downloadFolder, new FileVisitor<Path>() {
//
// short[] ids = new short[3];
//
// @Override
// public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
// if(dir.equals(downloadFolder)) {
// return FileVisitResult.CONTINUE;
// }
//
// Arrays.fill(ids, (short) 0);
// Path relPath = downloadFolder.relativize(dir);
//
// for (int i = 0; i < relPath.getNameCount(); ++i) {
// String name = relPath.getName(i).toString();
// if (!idMaps[i].containsKey(name)) {
// System.err.println("skipping dir " + name);
// return FileVisitResult.SKIP_SUBTREE;
// }
//
// ids[i] = idMaps[i].get(name);
// }
//
// return FileVisitResult.CONTINUE;
// }
//
// @Override
// public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// System.out.println(Arrays.toString(ids) + ": " + file.getFileName().toString());
// return FileVisitResult.CONTINUE;
// }
//
// @Override
// public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
// // TODO Auto-generated method stub
// return null;
// }
//
// @Override
// public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
// // TODO Auto-generated method stub
// return null;
// }
// });
// }
// private static void resetScenesToMetadataLevel() throws SQLException {
// System.out.println("resetting all scenes back to proc_level METADATA");
// SceneDatabase.getInstance().prepareCustomStatement("update scenes set proc_level='METADATA'::proc_level;").executeUpdate();
// }
//
// private static Map<String, Short> getNameIdMap(String tableName) throws SQLException {
// System.out.println("getting name id mapping for '" + tableName + "'");
// ResultSet rs = SceneDatabase.getInstance().placeCustomQuery("select id, name from " + tableName + ";");
// Map<String, Short> mapping = new HashMap<>();
//
// while (rs.next()) {
// mapping.put(rs.getString(2), rs.getShort(1));
// }
// return mapping;
// }
private static Connection getConnection(String host, String port, String user, String pass, String dbName) {
String url = "jdbc:postgresql://" + host + ":" + port + "/" + dbName;
try {
// load the driver
Class.forName("org.postgresql.Driver");
// establish connection
Connection connection = DriverManager.getConnection(url, user, pass);
connection.setAutoCommit(false);
// register postgis geometry object
((org.postgresql.PGConnection) connection).addDataType("geometry", PGgeometry.class);
((org.postgresql.PGConnection) connection).addDataType("geography", PGgeometry.class);
// return it
return connection;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment