1 21 22 package org.continuent.sequoia.controller.backup.backupers; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import java.util.Date ; 28 29 import org.continuent.sequoia.common.exceptions.BackupException; 30 import org.continuent.sequoia.common.log.Trace; 31 import org.continuent.sequoia.common.util.FileManagement; 32 import org.continuent.sequoia.controller.backend.DatabaseBackend; 33 import org.continuent.sequoia.controller.backup.DumpTransferInfo; 34 35 46 public class DerbyEmbeddedBackuper extends AbstractBackuper 47 { 48 static Trace logger = Trace.getLogger(DerbyEmbeddedBackuper.class.getName()); 49 50 53 public DerbyEmbeddedBackuper() 54 { 55 } 56 57 60 public String getDumpFormat() 61 { 62 return "Derby embedded compressed dump"; 63 } 64 65 70 public Date backup(DatabaseBackend backend, String login, String password, 71 String dumpName, String path, ArrayList tables) throws BackupException 72 { 73 String derbyPath = getDerbyPath(backend, true); 74 75 try 76 { 77 File pathDir = new File (path); 78 if (!pathDir.exists()) 79 { 80 pathDir.mkdirs(); 81 pathDir.mkdir(); 82 } 83 84 if (logger.isDebugEnabled()) 85 logger.debug("Archiving " + derbyPath + " in " + path + File.separator 86 + dumpName + Zipper.ZIP_EXT); 87 88 Zipper.zip(getDumpPhysicalPath(path, dumpName), derbyPath, 89 Zipper.STORE_PATH_FROM_ZIP_ROOT); 90 } 91 catch (Exception e) 92 { 93 String msg = "Error while performing backup"; 94 logger.error(msg, e); 95 throw new BackupException(msg, e); 96 } 97 98 return new Date (System.currentTimeMillis()); 99 } 100 101 106 public void restore(DatabaseBackend backend, String login, String password, 107 String dumpName, String path, ArrayList tables) throws BackupException 108 { 109 String derbyPath = getDerbyPath(backend, false); 110 111 File derbyDir = new File (derbyPath); 112 113 if (FileManagement.deleteDir(derbyDir)) 115 logger.info("Existing Derby directory " + derbyPath 116 + " has been deleted."); 117 118 derbyDir.mkdirs(); 120 derbyDir.mkdir(); 121 122 try 124 { 125 if (logger.isDebugEnabled()) 126 logger.debug("Uncompressing dump"); 127 Zipper.unzip(getDumpPhysicalPath(path, dumpName), derbyPath); 128 } 129 catch (Exception e) 130 { 131 String msg = "Error while uncompressing dump"; 132 logger.error(msg, e); 133 throw new BackupException(msg, e); 134 } 135 } 136 137 141 public void deleteDump(String path, String dumpName) throws BackupException 142 { 143 File toRemove = new File (getDumpPhysicalPath(path, dumpName)); 144 if (logger.isDebugEnabled()) 145 logger.debug("Deleting compressed dump " + toRemove); 146 toRemove.delete(); 147 } 148 149 156 private String getDumpPhysicalPath(String path, String dumpName) 157 { 158 return path + File.separator + dumpName + Zipper.ZIP_EXT; 159 } 160 161 170 private String getDerbyPath(DatabaseBackend backend, boolean checkPath) 171 throws BackupException 172 { 173 String url = backend.getURL(); 174 if (!url.startsWith("jdbc:derby:")) 175 throw new BackupException("Unsupported url " + url 176 + " expecting jdbc:derby:pathToDb"); 177 178 String derbyPath = url.substring(11); 181 int semicolon = derbyPath.indexOf(';'); 183 if (semicolon > -1) 184 derbyPath = derbyPath.substring(0, semicolon); 185 186 if (checkPath) 187 { 188 File checkDerbyPath = new File (derbyPath); 189 if (!checkDerbyPath.isDirectory()) 190 throw new BackupException( 191 "Directory " 192 + derbyPath 193 + " does not exist. This might be due to an unsupported URL format (expectin jdbc:derby:pathToDb)"); 194 } 195 196 return derbyPath; 197 } 198 199 203 public void fetchDump(DumpTransferInfo dumpTransferInfo, String path, 204 String dumpName) throws BackupException, IOException 205 { 206 super.fetchDump(dumpTransferInfo, path, dumpName + Zipper.ZIP_EXT); 207 } 208 209 } 210 | Popular Tags |