1 24 25 import java.io.BufferedReader ; 26 import java.io.IOException ; 27 import java.io.InputStreamReader ; 28 import java.text.SimpleDateFormat ; 29 import java.util.Date ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import javax.management.MBeanServerConnection ; 36 import javax.management.MBeanServerInvocationHandler ; 37 import javax.management.ObjectName ; 38 import javax.management.remote.JMXConnector ; 39 import javax.management.remote.JMXConnectorFactory ; 40 import javax.management.remote.JMXServiceURL ; 41 import javax.naming.Context ; 42 import javax.security.auth.Subject ; 43 44 import org.objectweb.cjdbc.common.jmx.JmxConstants; 45 import org.objectweb.cjdbc.common.jmx.mbeans.VirtualDatabaseMBean; 46 import org.objectweb.cjdbc.common.users.VirtualDatabaseUser; 47 import org.objectweb.cjdbc.controller.authentication.PasswordAuthenticator; 48 49 55 public class DBBackup 56 { 57 58 71 public static int runDatabaseBackupTool(String backendpass, 72 String backenduser, String database_name) throws IOException , 73 InterruptedException 74 { 75 76 String backupCommand = "/usr/bin/mysqlhotcopy --allowold --password=" 78 + backendpass + " --user=" + backenduser + " " + database_name 79 + " /mybackupdirectory"; 80 83 String [] args = {"/bin/bash", "-c", backupCommand}; 84 Runtime rt = Runtime.getRuntime(); 85 Process proc = rt.exec(args); 86 proc.waitFor(); 87 BufferedReader in = new BufferedReader (new InputStreamReader (proc 88 .getInputStream())); 89 int exitStatus = proc.exitValue(); 90 String line; 91 while ((line = in.readLine()) != null) 92 { 93 System.out.println("backup output: " + line); 94 } 95 return exitStatus; 96 97 } 98 99 111 public static void main(String [] args) throws Exception 112 { 113 String databaseName = args[0]; 114 String port = args[1]; 115 String vdbuser = args[2]; 116 String vdbpass = args[3]; 117 String backenduser = args[4]; 118 String backendpass = args[5]; 119 String host = "localhost"; 120 JMXServiceURL address = new JMXServiceURL ("rmi", host, 0, "/jndi/jrmp"); 121 122 Map environment = new HashMap (); 123 environment.put(Context.INITIAL_CONTEXT_FACTORY, 124 "com.sun.jndi.rmi.registry.RegistryContextFactory"); 125 environment.put(Context.PROVIDER_URL, "rmi://" + host + ":" + port); 126 127 environment.put(JMXConnector.CREDENTIALS, PasswordAuthenticator 132 .createCredentials("jmxuser", "jmxpassword")); 133 134 JMXConnector connector = JMXConnectorFactory.connect(address, environment); 135 136 ObjectName db = JmxConstants.getVirtualDbObjectName(databaseName); 137 138 VirtualDatabaseUser dbUser = new VirtualDatabaseUser(vdbuser, vdbpass); 140 Set principals = new HashSet (); 141 principals.add(dbUser); 142 Subject subj = new Subject (true, principals, new HashSet (), new HashSet ()); 143 144 MBeanServerConnection delegateConnection = connector 147 .getMBeanServerConnection(subj); 148 149 VirtualDatabaseMBean proxy = (VirtualDatabaseMBean) MBeanServerInvocationHandler 151 .newProxyInstance(delegateConnection, db, VirtualDatabaseMBean.class, 152 false); 153 154 SimpleDateFormat fmt = new SimpleDateFormat ("yyyy_MM_dd"); 155 String checkpointName = fmt.format(new Date ()); 156 157 proxy.disableBackendWithCheckpoint(databaseName, checkpointName); 159 160 runDatabaseBackupTool(backendpass, backenduser, databaseName); 162 163 proxy.enableBackendFromCheckpoint(databaseName); 165 } 166 167 } 168 | Popular Tags |