-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackup.java
More file actions
129 lines (109 loc) · 4.15 KB
/
Backup.java
File metadata and controls
129 lines (109 loc) · 4.15 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
package classes;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.ResultSet;
import java.util.ArrayList;
/**
*
* @author Harsha Indunil P.A
*/
// <editor-fold defaultstate="collapsed" desc="MySQL Database Backup and Restore">
public class DatabaseBackup {
public static int SaveBackup(String path){
BufferedWriter writer = null;
ArrayList<String> tables = new ArrayList<String>();
try {
//File
File logFile = new File(path);
writer = new BufferedWriter(new FileWriter(logFile));
ResultSet r = DB.Search("Show tables");
while(r.next()){
tables.add(r.getString("Tables_in_"+DB.dbName));
}
writer.write("-- MYSQL BACKUP BY <HARSHA INDUNIL>" ); //Change the Name
writer.write("\n");
writer.write("\n");
for (String table : tables){
writer.write("-- Insert data for table `"+table+"`");
writer.write("\n");
int i=0; //recorde count
//Delete all data from table
String SQLQInsert="DELETE FROM `"+table+"`;\n";
String colNames="";
ResultSet r4 = DB.Search("SHOW columns FROM "+ table);
while(r4.next()){
colNames+=r4.getString("Field")+",";
}
colNames=colNames.substring(0, colNames.length()-1);
SQLQInsert += "LOCK TABLES `"+table+"` WRITE;\n"
+ "INSERT INTO `"+table+"` (" +colNames+ ") VALUES ";
ResultSet r3 = DB.Search("Select * From "+ table);
while(r3.next()){
String SQLQ="";
ResultSet r2 = DB.Search("SHOW columns FROM "+ table);
while(r2.next()){
if(SQLQ.isEmpty()){
SQLQ += "('";
}else{
SQLQ += "'";
}
SQLQ += r3.getString(r2.getString("Field")).trim();
SQLQ += "',";
i++;
}
SQLQ=SQLQ.substring(0, SQLQ.length()-1);
SQLQ += ")";
SQLQInsert += SQLQ + ",";
}
SQLQInsert = SQLQInsert.substring(0, SQLQInsert.length()-1);
SQLQInsert += ";";
SQLQInsert += "\nUNLOCK TABLES;";
if(i>0){
writer.write(SQLQInsert);
writer.write("\n\n");
}else{
writer.write("-- No recode in `"+table+"`");
writer.write("\n\n");
}
}
//can add date time
writer.write("-- Backup completed");
writer.write("\n");
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
try {
// Close the writer regardless of what happens...
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.gc();
return 1; // Success
}
public static int RestoreBackup(String path){
try {
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
while ((line = br.readLine()) != null) {
if(!(line.trim().isEmpty())){
if(!(line.trim().substring(0, 2).equals("--"))){
DB.Update(line);
}
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
return 0;
}
System.gc();
return 1;
}
}
// </editor-fold>