-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseBackUpService.java
337 lines (260 loc) · 8.58 KB
/
DatabaseBackUpService.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Set;
import java.util.HashSet;
import java.util.Base64;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.PreparedStatement;
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.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.InvalidAlgorithmParameterException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
/**
* @author Mwendwa Reuben
*
*/
public class DatabaseBackUpService {
// file system
private final static String BACK_UP_DIR = "/BACKUPDIR/";
private final static String LOGS_DIR = "/var/log/";
private final static String LOG_FILE_NAME = "backup_service.log";
private final static String[] DATABASES = {"Database1", "Database2"}; // a list of all databases to backup
// logging
private final static Logger LOG = Logger.getLogger(DatabaseBackUpService.class);
// binaries
private final static String MYSQL_DUMP_BIN = "/usr/bin/mysqldump";
// connection
private final static String USER = "BACKUPUSER"; // mysql user name
private static String PWD;
private final static String STRING = "BACKUPUSERPASSWORD"; // mysql user password
private static void initializeFileSystem()
throws IOException {
// logs dir
Path logsPath = Paths.get(LOGS_DIR);
Path backUpPath = Paths.get(BACK_UP_DIR);
if(!Files.exists(logsPath)) {
// create, if it does not exist
Files.createDirectories(logsPath);
}
if(!Files.exists(backUpPath)) {
Files.createDirectories(backUpPath);
}
}
private static void initializeLogger() {
Logger rootLogger = Logger.getRootLogger();
rootLogger.setLevel(Level.DEBUG);
try {
// log to file
rootLogger.addAppender(
new RollingFileAppender(
new PatternLayout(
"%d{ISO8601} [%-5p] %x - %m%n"),
LOGS_DIR + LOG_FILE_NAME
)
);
} catch(IOException ex) {
// do nothing
}
}
private static void createSubDirs(String year, String month)
throws IOException {
Path yearMonthSubDir = Paths.get(
BACK_UP_DIR + year + "/" + month);
if(!Files.exists(yearMonthSubDir)) {
Files.createDirectories(yearMonthSubDir);
}
}
private static String exceptionOrigin(Exception exception) {
return (
exception.getStackTrace()[0].getClassName() +
"." +
exception.getStackTrace()[0].getMethodName()
);
}
private static SecretKey generateSecretKey()
throws NoSuchAlgorithmException {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
return kgen.generateKey();
}
private static IvParameterSpec createIvParameterSpec(byte[] randomData) {
return new IvParameterSpec(randomData);
}
private static Cipher createEncryptionCipher(SecretKey secretKey, IvParameterSpec ivParamSpec)
throws InvalidKeyException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
NoSuchPaddingException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParamSpec);
return cipher;
}
private static String formatDate(String formatStr, Date rightNow) {
return new SimpleDateFormat(formatStr).format(rightNow);
}
private static void backUp(String dumpFileName) {
String[] args = new String[]{
MYSQL_DUMP_BIN,
"-u " + USER, "-p" + PWD,
"-h 192.168.20.227",
"--routines","--triggers","--force","--opt",
"--max_allowed_packet=2G","--single-transaction","--quick",
"--databases " + String.join(" ", DATABASES)
};
// year and month directories
Date now = new Date();
String year = formatDate("yyyy", now);
String month = formatDate("MM", now);
try {
createSubDirs(year, month);
// absolute path
String dumpFileAbsPath = String.join(
"",
new String[]{
BACK_UP_DIR, year,
"/", month, "/" + dumpFileName
}
);
BufferedReader reader = null;
BufferedWriter writer = null;
try {
final Process proc = Runtime.getRuntime().exec(String.join(" ", args));
byte[] iv = new byte[128/8];
SecureRandom srandom = SecureRandom.getInstance("SHA1PRNG");
srandom.nextBytes(iv);
IvParameterSpec ivspec = createIvParameterSpec(iv);
SecretKey sKey = generateSecretKey();
Cipher cipher = createEncryptionCipher(sKey, ivspec);
reader = new BufferedReader(
new InputStreamReader(
proc.getInputStream()
)
);
try {
FileOutputStream fileOstream = new FileOutputStream(dumpFileAbsPath);
// write encryption/decryption key first
fileOstream.write(sKey.getEncoded(), 0, 16);
fileOstream.write(iv, 0, iv.length);
writer = new BufferedWriter(
new OutputStreamWriter(
new GZIPOutputStream(
new CipherOutputStream(
fileOstream, cipher
)
)
)
);
try {
char[] buff = new char[8192];
int size;
while((size = reader.read(buff)) != -1) {
writer.write(buff, 0, size);
}
reader.close();
writer.close();
} catch(IOException ex) {
LOG.error(ex.toString() + " [ in ] " + exceptionOrigin(ex));
}
} catch (IOException ex) {
LOG.error(ex.toString() + " [ in ] " + exceptionOrigin(ex));
}
} catch(InvalidKeyException ex) {
LOG.error(ex.toString() + " [ in ] " + exceptionOrigin(ex));
} catch(NoSuchAlgorithmException ex) {
LOG.error(ex.toString() + " [ in ] " + exceptionOrigin(ex));
} catch(InvalidAlgorithmParameterException ex) {
LOG.error(ex.toString() + " [ in ] " + exceptionOrigin(ex));
} catch(NoSuchPaddingException ex) {
LOG.error(ex.toString() + " [ in ] " + exceptionOrigin(ex));
} catch(IOException ex) {
LOG.error(ex.toString() + " [ in ] " + exceptionOrigin(ex));
}
} catch (IOException ex) {
LOG.error(ex.toString() + " [ in ] " + exceptionOrigin(ex));
}
}
private static void backUpServiceCallBack() {
Date rightNow = new Date();
LOG.info("Back up started at [ " + rightNow.toString() + " ]");
String dumpFileName = "BACK_UP_" + formatDate("yyyyMMdd_HHmmss", rightNow) + "_GZ_AES";
//Set<String> databases = databases();
LOG.info("Creating back up for [ " + String.join("|", DATABASES) + " ]");
backUp(dumpFileName);
LOG.info("Back up [ " + dumpFileName + " ] completed at [ " + new Date().toString() + " ]");
LOG.info("Done");
LOG.info(
"-----------------------------------------------------------" +
"-----------------------------------------------------------"
);
}
public static long initialDelay() {
Calendar now = Calendar.getInstance();
GregorianCalendar later = new GregorianCalendar(
now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE) + 1, // the following day
1, 0, 0
);// 0100hrs
return Duration.between(
later.toInstant(), now.toInstant()).abs().toMinutes();
}
public static void startService() {
ScheduledExecutorService service =
Executors.newSingleThreadScheduledExecutor();
try {
service.scheduleAtFixedRate(
new Runnable(){
@Override
public void run() {
// dump
backUpServiceCallBack();
}
}, 1/*initialDelay()*/, 1440, TimeUnit.SECONDS
);
} finally {
// incase of error / successful exit
}
}
public static void main(String[] args) {
try {
initPassword();
initializeFileSystem();
initializeLogger();
LOG.info("Back up service started.");
startService();
} catch(IOException ex) {
// could not initialize back up or log directories or could not
// initialize the logger
ex.printStackTrace();
}
}
}