Subversion Repository Public Repository

TransPort_Tracking

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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bestray.transtracking.views;

import com.bestray.transtracking.exceptions.TransTrackException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author user2
 */
public class DatabaseBackupAndRestore extends javax.swing.JPanel {

    /**
     * Creates new form MysqlBackupAndRestore
     */
    Properties properties;
    Backup bk=new Backup();
    public DatabaseBackupAndRestore() {
        initComponents();
        try{
            properties=new Properties();
            FileInputStream fis=new FileInputStream(System.getProperty("user.home")+File.separator+"transtrack"+"/transtrack.properties");
            properties.load(fis);
            fis.close();
        }catch(IOException ie){
            throw new TransTrackException(" Failed to load the Driver");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        DbBackButton = new javax.swing.JButton();
        DbRestoreButton = new javax.swing.JButton();
        BackUpPannel = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        BackupPathTb = new javax.swing.JTextField();
        BackupPath = new javax.swing.JButton();
        BackupButton = new javax.swing.JButton();
        RestorePannel = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        RestorePathTb = new javax.swing.JTextField();
        RestorePath = new javax.swing.JButton();
        RestoreButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(200, 225, 250));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(1037, 600));

        jPanel1.setBackground(new java.awt.Color(200, 225, 250));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setOpaque(false);
        jPanel1.setPreferredSize(new java.awt.Dimension(522, 236));

        DbBackButton.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 12)); // NOI18N
        DbBackButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/database16.png"))); // NOI18N
        DbBackButton.setText("DB Backup");
        DbBackButton.setOpaque(false);
        DbBackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DbBackButtonActionPerformed(evt);
            }
        });

        DbRestoreButton.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 12)); // NOI18N
        DbRestoreButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/databaserestore16.png"))); // NOI18N
        DbRestoreButton.setText("DB Restore");
        DbRestoreButton.setOpaque(false);
        DbRestoreButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DbRestoreButtonActionPerformed(evt);
            }
        });

        BackUpPannel.setBackground(new java.awt.Color(200, 225, 250));
        BackUpPannel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Backup Utility", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", 1, 12))); // NOI18N
        BackUpPannel.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 12)); // NOI18N
        BackUpPannel.setOpaque(false);
        BackUpPannel.setPreferredSize(new java.awt.Dimension(472, 231));
        BackUpPannel.setVisible(false);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Destination :");

        BackupPathTb.setEditable(false);
        BackupPathTb.setText("");

        BackupPath.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
        BackupPath.setText("Choose");
        BackupPath.setOpaque(false);
        BackupPath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackupPathActionPerformed(evt);
            }
        });

        BackupButton.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
        BackupButton.setText("BackUp");
        BackupButton.setOpaque(false);
        BackupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackupButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout BackUpPannelLayout = new javax.swing.GroupLayout(BackUpPannel);
        BackUpPannel.setLayout(BackUpPannelLayout);
        BackUpPannelLayout.setHorizontalGroup(
            BackUpPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(BackUpPannelLayout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addGroup(BackUpPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(BackUpPannelLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BackupPathTb, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BackupPath))
                    .addComponent(BackupButton))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        BackUpPannelLayout.setVerticalGroup(
            BackUpPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, BackUpPannelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(BackUpPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(BackupPathTb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BackupPath))
                .addGap(18, 18, 18)
                .addComponent(BackupButton)
                .addContainerGap())
        );

        RestorePannel.setBackground(new java.awt.Color(200, 225, 250));
        RestorePannel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Restore Utility", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", 1, 12))); // NOI18N
        RestorePannel.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 12)); // NOI18N
        RestorePannel.setOpaque(false);
        RestorePannel.setPreferredSize(new java.awt.Dimension(472, 231));
        RestorePannel.setVisible(false);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("Source :");

        RestorePathTb.setEditable(false);
        RestorePathTb.setText("");

        RestorePath.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
        RestorePath.setText("Choose");
        RestorePath.setOpaque(false);
        RestorePath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RestorePathActionPerformed(evt);
            }
        });

        RestoreButton.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
        RestoreButton.setText("Restore");
        RestoreButton.setOpaque(false);
        RestoreButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RestoreButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout RestorePannelLayout = new javax.swing.GroupLayout(RestorePannel);
        RestorePannel.setLayout(RestorePannelLayout);
        RestorePannelLayout.setHorizontalGroup(
            RestorePannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RestorePannelLayout.createSequentialGroup()
                .addGroup(RestorePannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(RestorePannelLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RestorePathTb, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RestorePath))
                    .addGroup(RestorePannelLayout.createSequentialGroup()
                        .addContainerGap(183, Short.MAX_VALUE)
                        .addComponent(RestoreButton)
                        .addGap(0, 173, Short.MAX_VALUE)))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        RestorePannelLayout.setVerticalGroup(
            RestorePannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, RestorePannelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(RestorePannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(RestorePathTb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RestorePath))
                .addGap(18, 18, 18)
                .addComponent(RestoreButton)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(271, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(DbBackButton)
                        .addGap(97, 97, 97)
                        .addComponent(DbRestoreButton))
                    .addComponent(RestorePannel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BackUpPannel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(270, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {DbBackButton, DbRestoreButton});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {BackUpPannel, RestorePannel});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DbBackButton)
                    .addComponent(DbRestoreButton))
                .addGap(18, 18, Short.MAX_VALUE)
                .addComponent(BackUpPannel, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 2, Short.MAX_VALUE)
                .addComponent(RestorePannel, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {DbBackButton, DbRestoreButton});

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/database.png"))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14)); // NOI18N
        jLabel2.setText("Mysql DB Backup & Restore");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 1017, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(248, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void DbBackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DbBackButtonActionPerformed
        
           BackUpPannel.setVisible(true);
           RestorePannel.setVisible(false);
       
    }//GEN-LAST:event_DbBackButtonActionPerformed

    private void DbRestoreButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DbRestoreButtonActionPerformed
            BackUpPannel.setVisible(false);
            RestorePannel.setVisible(true);
    }//GEN-LAST:event_DbRestoreButtonActionPerformed
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
    Date date;
    private void BackupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackupButtonActionPerformed
   
        try{

        int processComplete; 
        String databaseName="transtrack";
        String userName=properties.getProperty("user");
        String password=properties.getProperty("password");
        String dest=BackupPathTb.getText();
        if(dest.equals("")){
            JOptionPane.showMessageDialog(null, "Choose The Location to Store");
        }else{ 
            date = new Date();
            String dateString = sdf.format(date);
            Process runtimeProcess = Runtime.getRuntime().exec(properties.getProperty("mysqlpath")+"\\bin\\mysqldump -u "+userName+" -p"+password+" "+databaseName+" -r "+dest+"\\"+dateString+"transtrack_backup.sql");
        
            processComplete = runtimeProcess.waitFor();
       
            
        
            if(processComplete==1){

                JOptionPane.showMessageDialog(null, "Backup Failed");
            }

            else if(processComplete==0){

                JOptionPane.showMessageDialog(null,"\n Backup created Successfully..");
                BackupPathTb.setText("");
            
            }
        }
    }catch(Exception e){

        JOptionPane.showMessageDialog(null,e);

    }
   
    }//GEN-LAST:event_BackupButtonActionPerformed

    private void BackupPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackupPathActionPerformed
        BackupPathTb.setText(getPath("BACKUP"));
    }//GEN-LAST:event_BackupPathActionPerformed

    private void RestoreButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RestoreButtonActionPerformed
        
    try{
        
        String databaseName="transtrack";
        String userName=properties.getProperty("user");
        String password=properties.getProperty("password");
        int processComplete;
        String source=RestorePathTb.getText();
        if(source.equals("")){
            JOptionPane.showMessageDialog(null, "Choose The Backup File");
        }else{
            String[] executeCmd = new String[]{properties.getProperty("mysqlpath")+"\\bin\\mysql", databaseName, "-u" + userName, "-p" + password, "-e", "  source "+source+"" };
        
            Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        
        
            processComplete = runtimeProcess.waitFor();
    
            if(processComplete==1){

                JOptionPane.showMessageDialog(null, "Restore Failed");

            }

            else if(processComplete==0){

                JOptionPane.showMessageDialog(null, "Restore Completed");
                RestorePathTb.setText("");

             }
        }
    

    }catch(Exception ex){

        JOptionPane.showMessageDialog(null, ex);

    }

    }//GEN-LAST:event_RestoreButtonActionPerformed

    private void RestorePathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RestorePathActionPerformed
        RestorePathTb.setText(getPath("RESTORE"));
    }//GEN-LAST:event_RestorePathActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel BackUpPannel;
    private javax.swing.JButton BackupButton;
    private javax.swing.JButton BackupPath;
    private javax.swing.JTextField BackupPathTb;
    private javax.swing.JButton DbBackButton;
    private javax.swing.JButton DbRestoreButton;
    private javax.swing.JButton RestoreButton;
    private javax.swing.JPanel RestorePannel;
    private javax.swing.JButton RestorePath;
    private javax.swing.JTextField RestorePathTb;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
   
    public String getPath(String type) {
       
         String path = "";
         JFileChooser fc = null;
         if (fc == null) {
                fc = new JFileChooser();
                if(type.equalsIgnoreCase("RESTORE")){
                    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                }
                else if(type.equalsIgnoreCase("BACKUP")){
                    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                }
                fc.setAcceptAllFileFilterUsed(false);
        }
        int returnVal = fc.showDialog(null, "Open");
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            path = file.getAbsolutePath();
        }
       return path;
    }
    
   
    
}

Commits for TransPort_Tracking/TransPortTracking/src/main/java/com/bestray/transtracking/views/DatabaseBackupAndRestore.java

Diff revisions: vs.
Revision Author Commited Message
1 girijabapi picture girijabapi Sat 28 Jul, 2018 05:29:14 +0000