package jfconfig; /** * Created : Apr 17, 2012 * * @author pquiring */ import java.io.*; import java.util.*; import javaforce.*; public class BackupPanel extends javax.swing.JPanel { /** * Creates new form BackupPanel */ public BackupPanel() { initComponents(); } /** * 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() { jToolBar1 = new javax.swing.JToolBar(); back = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); personalBackup = new javax.swing.JButton(); personalRestore = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); backupSystem = new javax.swing.JButton(); restoreSystem = new javax.swing.JButton(); jToolBar1.setFloatable(false); jToolBar1.setRollover(true); back.setText("< Back"); back.setFocusable(false); back.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); back.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); back.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { backActionPerformed(evt); } }); jToolBar1.add(back); jLabel1.setText("Personal Files:"); personalBackup.setText("Backup"); personalBackup.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { personalBackupActionPerformed(evt); } }); personalRestore.setText("Restore"); personalRestore.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { personalRestoreActionPerformed(evt); } }); jLabel2.setText("System Image:"); backupSystem.setText("Backup"); backupSystem.setEnabled(false); backupSystem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { backupSystemActionPerformed(evt); } }); restoreSystem.setText("Restore"); restoreSystem.setEnabled(false); restoreSystem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { restoreSystemActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(jLabel2) .addGroup(layout.createSequentialGroup() .addGap(12, 12, 12) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(backupSystem, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(restoreSystem, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(personalBackup, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(personalRestore, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addContainerGap(218, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(personalBackup, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(personalRestore, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(backupSystem, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(restoreSystem, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 228, Short.MAX_VALUE)) ); }// </editor-fold>//GEN-END:initComponents private void personalBackupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_personalBackupActionPerformed BackupDialog dialog = new BackupDialog(null, true, true); if (dialog.nodest) { JF.showError("Error", "No media to backup to was detected!\nPlease insert a removable storage unit and try again."); return; } dialog.setVisible(true); if (!dialog.accepted) return; ArrayList<String> cmd = new ArrayList<String>(); cmd.add("tar"); cmd.add("c" + (dialog.getCompression() == 0 ? "z" : "j") + "fp"); cmd.add(getDestFilename(dialog, true)); cmd.add("-C"); cmd.add(System.getenv("HOME")); cmd.add("."); cmd.add("--exclude=./.gvfs"); cmd.add("--exclude=./.fuse"); JFTask task = new JFTask() { private int current = 0, total = 0; private boolean finding; public boolean work() { String cmd[] = (String[])this.getProperty("cmd"); ShellProcess sp = new ShellProcess(); sp.addListener(this); sp.keepOutput(false); finding = true; this.setLabel("Determining # files"); this.setProgress(-1); sp.run(new String[] {"find", System.getenv("HOME")}, false); this.setLabel("Backing up files"); finding = false; sp.run(cmd, false); // JFLog.log("cmd=" + sp.command); this.setLabel("Backup complete!"); this.setProgress(100); return true; } public void shellProcessOutput(String str) { if (finding) { total += str.split("\n").length - 1; } else { current += str.split("\n").length - 1; this.setProgress(current * 100 / total); } } }; task.setProperty("cmd", cmd.toArray(new String[0])); ProgressDialog pd = new ProgressDialog(null, true, task); pd.setVisible(true); }//GEN-LAST:event_personalBackupActionPerformed private void backupSystemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backupSystemActionPerformed BackupDialog dialog = new BackupDialog(null, true, false); if (dialog.nodest) { JF.showError("Error", "No media to backup to was detected!\nPlease insert a removable storage unit and try again."); return; } dialog.setVisible(true); if (!dialog.accepted) return; ArrayList<String> cmd = new ArrayList<String>(); cmd.add("sudo"); cmd.add("tar"); cmd.add("c" + (dialog.getCompression() == 0 ? "z" : "j") + "fp"); cmd.add(getDestFilename(dialog, false)); cmd.add("-C"); cmd.add("/"); cmd.add("."); cmd.add("--exclude=/tmp"); cmd.add("--exclude=/mnt"); cmd.add("--exclude=/media"); cmd.add("--exclude=/proc"); cmd.add("--exclude=/sys"); cmd.add("--exclude=/lost+found"); JFTask task = new JFTask() { private int current = 0, total = 0; private boolean finding; public boolean work() { String cmd[] = (String[])this.getProperty("cmd"); ShellProcess sp = new ShellProcess(); sp.addListener(this); sp.keepOutput(false); finding = true; this.setLabel("Determining # files"); this.setProgress(-1); sp.run(new String[] {"sudo", "find", "/"}, false); this.setLabel("Backing up files"); finding = false; sp.run(cmd, false); JFLog.log("cmd=" + sp.command); this.setLabel("Backup complete!"); this.setProgress(100); return true; } public void shellProcessOutput(String str) { if (finding) { total += str.split("\n").length - 1; } else { current += str.split("\n").length - 1; this.setProgress(current * 100 / total); } } }; task.setProperty("cmd", cmd.toArray(new String[0])); ProgressDialog pd = new ProgressDialog(null, true, task); pd.setVisible(true); }//GEN-LAST:event_backupSystemActionPerformed private void personalRestoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_personalRestoreActionPerformed RestoreDialog dialog = new RestoreDialog(null, true, true, getBackupName(true)); if (dialog.nosrc) { JF.showError("Error", "No media to restore from was detected!\nPlease insert a removable storage unit and try again."); return; } dialog.setVisible(true); if (!dialog.accepted) return; ArrayList<String> cmd = new ArrayList<String>(); cmd.add("tar"); cmd.add("x" + getCompressionType(dialog.getSource()) + "f"); cmd.add(dialog.getSource()); cmd.add("-C"); if (dialog.getUseDiffFolder()) { cmd.add(dialog.getDiffFolder()); } else { cmd.add(System.getenv("HOME")); } if (dialog.isOverwrite()) cmd.add("--overwrite"); JFTask task = new JFTask() { public boolean work() { String cmd[] = (String[])this.getProperty("cmd"); this.setLabel("Restoring files..."); this.setProgress(-1); ShellProcess sp = new ShellProcess(); sp.addListener(this); sp.run(cmd, false); JFLog.log("cmd=" + sp.command); this.setLabel("Restore complete!"); this.setProgress(100); return true; } }; task.setProperty("cmd", cmd.toArray(new String[0])); ProgressDialog pd = new ProgressDialog(null, true, task); pd.setVisible(true); }//GEN-LAST:event_personalRestoreActionPerformed private void restoreSystemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restoreSystemActionPerformed RestoreDialog dialog = new RestoreDialog(null, true, false, getBackupName(false)); if (dialog.nosrc) { JF.showError("Error", "No media to restore from was detected!\nPlease insert a removable storage unit and try again."); return; } dialog.setVisible(true); if (!dialog.accepted) return; ArrayList<String> cmd = new ArrayList<String>(); cmd.add("sudo"); cmd.add("tar"); cmd.add("x" + getCompressionType(dialog.getSource()) + "f"); cmd.add(dialog.getSource()); cmd.add("-C"); cmd.add(dialog.getDest()); cmd.add("--overwrite"); JFTask task = new JFTask() { public boolean work() { String cmd[] = (String[])this.getProperty("cmd"); this.setLabel("Restoring files..."); this.setProgress(-1); ShellProcess sp = new ShellProcess(); sp.addListener(this); sp.run(cmd, false); JFLog.log("cmd=" + sp.command); this.setLabel("Restore complete!"); this.setProgress(100); return true; } }; task.setProperty("cmd", cmd.toArray(new String[0])); ProgressDialog pd = new ProgressDialog(null, true, task); pd.setVisible(true); }//GEN-LAST:event_restoreSystemActionPerformed private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed ConfigApp.This.setPanel(new MainPanel()); }//GEN-LAST:event_backActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton back; private javax.swing.JButton backupSystem; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JToolBar jToolBar1; private javax.swing.JButton personalBackup; private javax.swing.JButton personalRestore; private javax.swing.JButton restoreSystem; // End of variables declaration//GEN-END:variables private String getDestFilename(BackupDialog dialog, boolean personal) { String dest = "/media/" + dialog.getDest(); if (personal) { dest += "/Backups/Personal/"; } else { dest += "/Backups/System/"; } new File(dest).mkdirs(); dest += getBackupName(personal); dest += "-"; dest += getDateTime(); dest += ".tar"; dest += (dialog.getCompression() == 0 ? ".gz" : ".bz2"); return dest; } private String getBackupName(boolean personal) { if (personal) { return System.getenv("USER"); } else { ShellProcess sp = new ShellProcess(); return sp.run(new String [] {"hostname"}, false).replaceAll("\n", ""); } } private String getDateTime() { Calendar cal = Calendar.getInstance(); return String.format("%04d-%02d-%02d--%02d-%02d-%02d" ,cal.get(Calendar.YEAR) ,cal.get(Calendar.MONTH)+1 ,cal.get(Calendar.DAY_OF_MONTH) ,cal.get(Calendar.HOUR) ,cal.get(Calendar.MINUTE) ,cal.get(Calendar.SECOND) ); } private String getCompressionType(String file) { if (file.endsWith(".tar.gz")) return "z"; if (file.endsWith(".tar.bz2")) return "j"; return ""; } }