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.regex.Matcher ; 28 import java.util.regex.Pattern ; 29 30 import org.continuent.sequoia.common.exceptions.BackupException; 31 import org.continuent.sequoia.controller.backend.DatabaseBackend; 32 33 64 public class MySQLTarBackuper extends TarBackuper 65 { 66 private static final String DEFAULT_MYSQL_PORT = "3306"; 67 68 private static final String DEFAULT_MYSQL_HOST = "localhost"; 69 70 73 public String getDumpFormat() 74 { 75 return "MySQL tar dump"; 76 } 77 78 83 public Date backup(DatabaseBackend backend, String login, String password, 84 String dumpName, String path, ArrayList tables) throws BackupException 85 { 86 String url = backend.getURL(); 87 if (!url.startsWith("jdbc:mysql:")) 88 { 89 throw new BackupException("Unsupported db url " + url); 90 } 91 return super.backup(backend, login, password, dumpName, path, tables); 92 } 93 94 99 public void restore(DatabaseBackend backend, String login, String password, 100 String dumpName, String path, ArrayList tables) throws BackupException 101 { 102 String url = backend.getURL(); 103 if (!url.startsWith("jdbc:mysql:")) 104 { 105 throw new BackupException("Unsupported db url " + url); 106 } 107 MySQLUrlInfo info = new MySQLUrlInfo(url); 108 109 if (logger.isDebugEnabled()) 111 logger.debug("Dropping database '" + info.getDbName() + "'"); 112 113 String mysqladminExecutablePath = (optionsMap.containsKey("mysqlbindir") 114 ? (String ) optionsMap.get("mysqlbindir") + File.separator 115 : "") 116 + "mysqladmin"; 117 try 118 { 119 if (nativeCmdExec.executeNativeCommand(mysqladminExecutablePath + " -h " 120 + info.getHost() + " --port=" + info.getPort() + " -f -u" + login 121 + " --password=" + password + " drop " + info.getDbName(), null, 122 null, 0, getIgnoreStdErrOutput()) != 0) 123 { 124 printErrors(); 127 } 128 } 129 catch (Exception e) 130 { 131 logger.error("Failed to drop database (tar command may fail afterwards)", 132 e); 133 } 134 135 super.restore(backend, login, password, dumpName, path, tables); 136 } 137 138 141 protected class MySQLUrlInfo 142 { 143 private boolean isLocal; 144 145 private String host; 146 147 private String port; 148 149 private String dbName; 150 151 private Pattern pattern = Pattern 153 .compile("jdbc:mysql:((//([a-zA-Z0-9_\\-.]+|\\[[a-fA-F0-9:]+])((:(\\d+))|))/|)([a-zA-Z][a-zA-Z0-9_\\-]*)(\\?.*)?"); 154 155 Matcher matcher; 156 157 164 public MySQLUrlInfo(String url) 165 { 166 matcher = pattern.matcher(url); 167 168 if (matcher.matches()) 169 { 170 if (matcher.group(3) != null) 171 host = matcher.group(3); 172 else 173 host = DEFAULT_MYSQL_HOST; 174 175 if (matcher.group(6) != null) 176 port = matcher.group(6); 177 else 178 port = DEFAULT_MYSQL_PORT; 179 180 dbName = matcher.group(7); 181 } 182 } 183 184 190 public String getHostParametersString() 191 { 192 if (isLocal) 193 { 194 return ""; 195 } 196 else 197 { 198 return "-h " + host + " --port=" + port; 199 } 200 } 201 202 207 public String getDbName() 208 { 209 return dbName; 210 } 211 212 217 public String getHost() 218 { 219 return host; 220 } 221 222 227 public String getPort() 228 { 229 return port; 230 } 231 232 239 public boolean isLocal() 240 { 241 return isLocal; 242 } 243 244 } 245 246 } 247 | Popular Tags |