comparison src/jdbcpgbackup/JdbcPgBackup.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1 /* Copyright (c) 2012 Tomislav Gountchev <tomi@gountchev.net> */
2
3 package jdbcpgbackup;
4
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 public final class JdbcPgBackup {
10
11 public static final String USAGE =
12 "Usage: JdbcPgBackup -m dump|restore [-h hostname] [-p port] [-t (timing)] " +
13 "[-d database] [-U user] [-P password] [-f filename] [-o (schema only)] " +
14 "[-s schema[,schema...]] [-n schema[,schema...]] [-b batchsize]";
15
16 private static Map<String,String> parseArgs(String[] args) {
17 Map<String,String> params = new HashMap<String,String>();
18 for (int i = 0; i<args.length; i++) {
19 if (args[i].startsWith("-") && args[i].length() == 2) {
20 char option = args[i].charAt(1);
21 switch(option) {
22 case 'm':
23 params.put("mode", args[++i]);
24 break;
25 case 'h':
26 params.put("hostname", args[++i]);
27 break;
28 case 'p':
29 params.put("port", args[++i]);
30 break;
31 case 'd':
32 params.put("database", args[++i]);
33 break;
34 case 'U':
35 params.put("user", args[++i]);
36 break;
37 case 'P':
38 params.put("password", args[++i]);
39 break;
40 case 'f':
41 params.put("filename", args[++i]);
42 break;
43 case 's':
44 params.put("schemas", args[++i]);
45 break;
46 case 'n':
47 params.put("toschemas", args[++i]);
48 break;
49 case 'b':
50 params.put("batch", args[++i]);
51 break;
52 case 't':
53 params.put("debug", "true");
54 break;
55 case 'o':
56 params.put("nodata", "true");
57 break;
58 default:
59 throw new RuntimeException("invalid parameter: " + args[i]);
60 }
61 } else throw new RuntimeException("invalid parameters");
62 }
63 return params;
64 }
65
66 public static void main(String[] args) {
67 if (args.length == 0) {
68 System.err.println(USAGE);
69 System.exit(1);
70 }
71 Map<String, String> params = null;
72 try {
73 params = parseArgs(args);
74 } catch (RuntimeException e) {
75 System.err.println(USAGE);
76 System.err.println(e.getMessage());
77 System.exit(1);
78 }
79 ZipBackup backup = new ZipBackup(params);
80 try {
81 String[] schemas = null;
82 String[] toSchemas = null;
83 String schemasParam = params.get("schemas");
84 if (schemasParam != null) {
85 schemas = schemasParam.split(",");
86 }
87 String toSchemasParam = params.get("toschemas");
88 if (toSchemasParam != null) {
89 toSchemas = toSchemasParam.split(",");
90 if (schemas == null || schemas.length == 0 || schemas.length != toSchemas.length)
91 throw new RuntimeException("non-matching source schema (-s) and destination schema (-n) parameters");
92 }
93 String mode = params.get("mode");
94
95 if ("true".equals(params.get("debug")))
96 ZipBackup.setTimingOutput(System.err);
97
98 if ("dump".equals(mode)) {
99 boolean nodata = "true".equals(params.get("nodata"));
100 DataFilter dataFilter = nodata ? DataFilter.NO_DATA : DataFilter.ALL_DATA;
101 String batchS = params.get("batch");
102 int batch = batchS == null ? ZipBackup.DEFAULT_BATCH_SIZE : Integer.parseInt(batchS);
103 if (schemas == null) {
104 backup.dumpAll(dataFilter, batch);
105 } else {
106 backup.dump(Arrays.asList(schemas), dataFilter);
107 }
108 } else if ("restore".equals(mode)) {
109 if (schemas == null) {
110 backup.restoreAll();
111 } else if (toSchemas == null) {
112 for (String schema : schemas) {
113 backup.restoreSchema(schema);
114 }
115 } else {
116 for (int i=0; i<schemas.length; i++) {
117 backup.restoreSchemaTo(schemas[i], toSchemas[i]);
118 }
119 }
120 } else throw new RuntimeException("invalid mode: " + mode);
121 } catch (RuntimeException e) {
122 System.err.println("backup failed: " + e.getMessage());
123 e.printStackTrace(System.err);
124 System.exit(1);
125 }
126 System.exit(0);
127 }
128
129 }