1 16 package org.outerj.daisy.backupTool; 17 18 import java.io.File ; 19 import java.net.InetAddress ; 20 import java.net.Socket ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 public class BackupManager { 25 private static BackupManager instance; 26 27 private static JMXRepositoryLocker locker; 28 29 private static File backupLocation; 30 31 private List backupInstances; 32 33 private List entryLoaders; 34 35 private boolean isRepositoryLocked; 36 37 private BackupManager(File backupLocation, JMXRepositoryLocker locker) throws Exception { 38 if (backupLocation != null && backupLocation.exists()) 39 BackupManager.backupLocation = backupLocation; 40 else 41 throw new Exception ("Backuplocation is not set or does not exist"); 42 43 if (locker != null) 44 BackupManager.locker = locker; 45 else 46 throw new Exception ("Repository locker is not set"); 47 48 backupInstances = new ArrayList (); 49 entryLoaders = new ArrayList (); 50 } 51 52 public static BackupManager getInstance() throws Exception { 53 if (instance == null) { 54 instance = new BackupManager(BackupManager.backupLocation, BackupManager.locker); 55 } 56 return instance; 57 } 58 59 public BackupInstance createBackupInstance() throws Exception { 60 BackupInstance buInstance = new BackupInstance(backupLocation); 61 for (int i = 0; i < entryLoaders.size(); i++) { 62 ((EntryLoader) entryLoaders.get(i)).createEntries(buInstance); 63 } 64 backupInstances.add(buInstance); 65 return buInstance; 66 } 67 68 public BackupInstance loadBackupInstance(String backupName) throws Exception { 69 BackupInstance buInstance = new BackupInstance(backupLocation, backupName); 70 71 for (int i = 0; i < entryLoaders.size(); i++) { 72 ((EntryLoader) entryLoaders.get(i)).reloadEntries(buInstance); 73 } 74 75 backupInstances.add(buInstance); 76 return buInstance; 77 } 78 79 public void rehash(BackupInstance buInstance) throws Exception { 80 buInstance.invalidate(); 81 BackupHelper.saveDocument(buInstance.getInstanceFile(), buInstance.getInstanceDocument()); 82 } 83 84 public void registerEntryLoader(EntryLoader el) { 85 entryLoaders.add(el); 86 } 87 88 public static File getBackupLocation() { 89 return backupLocation; 90 } 91 92 public static void setBackupLocation(File backupLocation) { 93 BackupManager.backupLocation = backupLocation; 94 } 95 96 public static JMXRepositoryLocker getLocker() { 97 return locker; 98 } 99 100 public static void setLocker(JMXRepositoryLocker locker) { 101 BackupManager.locker = locker; 102 } 103 104 public void backup(BackupInstance buInstance) throws Exception { 105 for (int i = 0; i < buInstance.entryCount(); i++) { 106 buInstance.getEntry(i).backupInit(); 107 } 108 try { 109 if (isRepositoryOn()) { 110 System.out.println("Locking repository ..."); 111 locker.lock(); 112 isRepositoryLocked = locker.isLocked(); 113 } else { 114 throw new Exception ("Could not connect to repository. Please turn on the repository server and try backing up again"); 115 } 116 117 System.out.println("Copying data to backup destination..."); 118 for (int i = 0; i < buInstance.entryCount(); i++) { 119 buInstance.getEntry(i).backup(); 120 } 121 122 } finally { 123 if (isRepositoryLocked) { 124 System.out.println("Unlocking repository ..."); 125 locker.unlock(); 126 } 127 } 128 129 System.out.println("Post-processing backup..."); 130 for (int i = 0; i < buInstance.entryCount(); i++) { 131 buInstance.getEntry(i).backupCleanup(); 132 } 133 134 BackupHelper.saveDocument(buInstance.getInstanceFile(), buInstance.getInstanceDocument()); 135 System.out.println("Done."); 136 } 137 138 public void restore(BackupInstance buInstance) throws Exception { 139 if (!isRepositoryOn()) { 140 System.out.println("Restoring backup-" + buInstance.getName() + " ..."); 141 for (int i = 0; i < buInstance.entryCount(); i++) { 142 buInstance.getEntry(i).restore(); 143 } 144 System.out.println("Restored backup-" + buInstance.getName() + "."); 145 } else { 146 throw new Exception ("Repository server is running. Please shut it down before restoring a backup."); 147 } 148 } 149 150 private boolean isRepositoryOn() throws Exception { 151 boolean isOn; 152 try { 153 Socket socket = new Socket (InetAddress.getByName(locker.getRepositoryHost()), locker.getRepositoryPort()); 154 socket.close(); 155 isOn = true; 156 } catch (Exception e) { 157 isOn = false; 158 } 159 return isOn; 160 } 161 } 162 | Popular Tags |