-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseBackUpRestore.java
More file actions
202 lines (157 loc) · 5.03 KB
/
DatabaseBackUpRestore.java
File metadata and controls
202 lines (157 loc) · 5.03 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.InvalidAlgorithmParameterException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.CommandLine;
public class DatabaseBackUpRestore {
// inner
private static class BackUpDecryptionKey {
private final byte[] sKey;
private final byte[] randomIv;
public BackUpDecryptionKey(byte[] sKey, byte[] randomIv){
this.sKey = sKey;
this.randomIv = randomIv;
}
public byte[] getSecretKeyBytes() {
return this.sKey;
}
public byte[] getRandomIvBytes() {
return this.randomIv;
}
}
private static Cipher initializeDecryptionCipher(BackUpDecryptionKey dKey)
throws InvalidKeyException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
NoSuchPaddingException {
SecretKey sKey = new SecretKeySpec(dKey.getSecretKeyBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(dKey.getRandomIvBytes());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, sKey, ivSpec);
System.out.println("Cipher initialized");
return cipher;
}
private static void restore(String backUpPath) {
BufferedWriter writer = null;
BufferedReader reader = null;
FileInputStream fileIstream = null;
try {
File backUpFile = new File(backUpPath);
fileIstream = new FileInputStream(backUpFile);
byte[] encodedKey = new byte[16];
byte[] ivByteString = new byte[16];
fileIstream.read(encodedKey);
fileIstream.read(ivByteString);
//System.out.println(encodedKey + " " + ivByteString);
BackUpDecryptionKey backUpDecKey = new BackUpDecryptionKey(encodedKey, ivByteString);
try {
Cipher cipher = initializeDecryptionCipher(backUpDecKey);
String restoreFileName = backUpFile.getName() + ".sql";
try {
// new SQL dump
writer =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(// write SQL dump to a new file
restoreFileName
)
)
);
// encrypted archive
reader =
new BufferedReader(
new InputStreamReader(// byte stream --> char stream
new GZIPInputStream(// decompress
new CipherInputStream(// decrypt before decompressing
fileIstream, cipher
)
)
)
);
char[] buff = new char[8192];
//long sizeRead = 0L;
int len;
System.out.println("Decrypting ...");
while((len = reader.read(buff)) != -1) {
//sizeRead += (long) len;
writer.write(buff, 0, len);
}
System.out.println();
System.out.println("Back up (sql) : " + restoreFileName);
System.out.println("Done");
} catch(IOException ex) {
System.out.println(ex);
} finally {
try { reader.close(); writer.close(); } catch(IOException ex) {}
}
} catch(InvalidKeyException ex) {
System.out.println(ex);
} catch(InvalidAlgorithmParameterException ex) {
System.out.println(ex);
} catch(NoSuchPaddingException ex) {
System.out.println(ex);
} catch(NoSuchAlgorithmException ex) {
System.out.println(ex);
}
}
/*catch(SQLException ex) {
System.out.println(ex);
} catch(ClassNotFoundException ex) {
System.out.println(ex);
}
*/
catch(IOException ex) {
System.out.println(ex);
} finally {
try {
fileIstream.close();
if (writer != null)
writer.close();
if(reader != null)
reader.close();
} catch(IOException ex) {}
}
}
private static String parseCommandLineArgs(String[] args) {
Options options = new Options();
Option fileOpt = new Option("f", "file", true, "path to the back up file to be restored");
fileOpt.setRequired(true);
options.addOption(fileOpt);
CommandLineParser cliParser = new DefaultParser();
HelpFormatter hFormatter = new HelpFormatter();
CommandLine cmdLine;
try {
cmdLine = cliParser.parse(options, args);
return cmdLine.getOptionValue("file");
} catch(ParseException ex) {
System.out.println(ex.getMessage());
hFormatter.printHelp("utility-name", options);
//System.exit(0);
return null;
}
}
public static void main(String[] args) {
String path = parseCommandLineArgs(args);
if (path != null)
restore(path);
}
}