1 21 22 package org.continuent.sequoia.controller.backup.backupers; 23 24 import java.io.File ; 25 import java.util.ArrayList ; 26 import java.util.Date ; 27 import java.util.Iterator ; 28 29 import org.continuent.sequoia.common.exceptions.BackupException; 30 import org.continuent.sequoia.common.log.Trace; 31 import org.continuent.sequoia.controller.backend.DatabaseBackend; 32 33 61 public class TarBackuper extends AbstractBackuper 62 { 63 static Trace logger = Trace.getLogger(TarBackuper.class 64 .getName()); 65 66 static Trace endUserLogger = Trace 67 .getLogger("org.continuent.sequoia.enduser"); 68 69 70 protected NativeCommandExec nativeCmdExec = new NativeCommandExec(); 71 72 75 public TarBackuper() 76 { 77 } 78 79 82 public String getDumpFormat() 83 { 84 return "Tar compressed dump"; 85 } 86 87 92 public Date backup(DatabaseBackend backend, String login, String password, 93 String dumpName, String path, ArrayList tables) throws BackupException 94 { 95 try 96 { 97 File pathDir = new File (path); 98 if (!pathDir.exists()) 99 { 100 pathDir.mkdirs(); 101 pathDir.mkdir(); 102 } 103 String dumpPath = getDumpPhysicalPath(path, dumpName); 104 if (logger.isDebugEnabled()) 105 { 106 logger.debug("Dumping " + backend.getURL() + " in " + dumpPath); 107 } 108 109 String executablePath = (optionsMap.containsKey("bindir") 110 ? (String ) optionsMap.get("bindir") + File.separator 111 : "") 112 + "tar"; 113 114 int exitValue = nativeCmdExec.executeNativeCommand(executablePath 115 + " cvfz " + dumpPath + " " + backend.getVirtualDatabaseName() 116 + File.separator, null, null, 0, new File ((String ) optionsMap 117 .get("datadir")), getIgnoreStdErrOutput()); 118 119 if (exitValue != 0) 120 { 121 printErrors(); 122 throw new BackupException( 123 "tar dump execution did not complete successfully!"); 124 } 125 } 126 catch (Exception e) 127 { 128 String msg = "Error while performing backup"; 129 logger.error(msg, e); 130 throw new BackupException(msg, e); 131 } 132 133 return new Date (); 134 } 135 136 141 public void restore(DatabaseBackend backend, String login, String password, 142 String dumpName, String path, ArrayList tables) throws BackupException 143 { 144 try 145 { 146 File pathDir = new File (path); 147 if (!pathDir.exists()) 148 { 149 pathDir.mkdirs(); 150 pathDir.mkdir(); 151 } 152 String dumpPath = getDumpPhysicalPath(path, dumpName); 153 if (logger.isDebugEnabled()) 154 { 155 logger.debug("Restoring " + backend.getURL() + " from " + dumpPath); 156 } 157 158 String executablePath; 159 executablePath = (optionsMap.containsKey("bindir") ? (String ) optionsMap 160 .get("bindir") 161 + File.separator : "") 162 + "tar"; 163 164 int exitValue = nativeCmdExec.executeNativeCommand(executablePath 165 + " xvfz " + dumpPath, null, null, 0, new File ((String ) optionsMap 166 .get("datadir")), getIgnoreStdErrOutput()); 167 168 if (exitValue != 0) 169 { 170 printErrors(); 171 throw new BackupException( 172 "tar execution did not complete successfully!"); 173 } 174 } 175 catch (Exception e) 176 { 177 String msg = "Error while performing restore"; 178 logger.error(msg, e); 179 throw new BackupException(msg, e); 180 } 181 } 182 183 187 public void deleteDump(String path, String dumpName) throws BackupException 188 { 189 File toRemove = new File (getDumpPhysicalPath(path, dumpName)); 190 if (logger.isDebugEnabled()) 191 logger.debug("Deleting compressed dump " + toRemove); 192 toRemove.delete(); 193 } 194 195 198 public void setOptions(String options) 199 { 200 super.setOptions(options); 201 if (!optionsMap.containsKey("datadir")) 202 throw new RuntimeException ( 203 "No datadir specified for Tar backuper, unable to operate without this information."); 204 } 205 206 213 private String getDumpPhysicalPath(String path, String dumpName) 214 { 215 return path + File.separator + dumpName + ".tgz"; 216 } 217 218 protected void printErrors() 219 { 220 ArrayList errors = nativeCmdExec.getStderr(); 221 Iterator it = errors.iterator(); 222 while (it.hasNext()) 223 { 224 String msg = (String ) it.next(); 225 logger.info(msg); 226 endUserLogger.error(msg); 227 } 228 } 229 } | Popular Tags |