KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DBBackup


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Marc Wick.
22  * Contributor(s): troby@cavion.com.
23  */

24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import javax.management.MBeanServerConnection JavaDoc;
36 import javax.management.MBeanServerInvocationHandler JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.remote.JMXConnector JavaDoc;
39 import javax.management.remote.JMXConnectorFactory JavaDoc;
40 import javax.management.remote.JMXServiceURL JavaDoc;
41 import javax.naming.Context JavaDoc;
42 import javax.security.auth.Subject JavaDoc;
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 /**
50  * This class defines a DBBackup
51  *
52  * @author <a HREF="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
53  * @version 1.0
54  */

55 public class DBBackup
56 {
57
58   /**
59    * we call the native backup tool provided by the database vendor.
60    * <p>
61    * In this example we are calling the postgres pg_dump tool on a linux box.
62    * <p>
63    * Other daily maintenance tasks such as analyze could also be done here while
64    * the backend is offline.
65    *
66    * @return exit value of backup process
67    * @throws IOException an IOError during execution of backup
68    * @throws InterruptedException thread was interrupted while waiting for
69    * external backup to finish
70    */

71   public static int runDatabaseBackupTool(String JavaDoc backendpass,
72       String JavaDoc backenduser, String JavaDoc database_name) throws IOException JavaDoc,
73       InterruptedException JavaDoc
74   {
75
76     // mysql
77
String JavaDoc backupCommand = "/usr/bin/mysqlhotcopy --allowold --password="
78         + backendpass + " --user=" + backenduser + " " + database_name
79         + " /mybackupdirectory";
80     // or for postgres
81
// backupCommand = "/usr/bin/pg_dump mydb | /usr/bin/gzip > mydb.dmp.gz"
82

83     String JavaDoc[] args = {"/bin/bash", "-c", backupCommand};
84     Runtime JavaDoc rt = Runtime.getRuntime();
85     Process JavaDoc proc = rt.exec(args);
86     proc.waitFor();
87     BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(proc
88         .getInputStream()));
89     int exitStatus = proc.exitValue();
90     String JavaDoc line;
91     while ((line = in.readLine()) != null)
92     {
93       System.out.println("backup output: " + line);
94     }
95     return exitStatus;
96
97   }
98
99   /**
100    * disable a backend, take a backup with the native database tool and take the
101    * backend online again.
102    *
103    * @param databaseName - name of the database
104    * @param port - jmx port ot the cjdbc controller
105    * @param vdbuser - cjdbc virtual database user
106    * @param vdbpass - cjdbc virutal database password
107    * @param backenduser - backenduser
108    * @param backendpass -password of backenduser
109    * @throws Exception - problems with backup
110    */

111   public static void main(String JavaDoc[] args) throws Exception JavaDoc
112   {
113     String JavaDoc databaseName = args[0];
114     String JavaDoc port = args[1];
115     String JavaDoc vdbuser = args[2];
116     String JavaDoc vdbpass = args[3];
117     String JavaDoc backenduser = args[4];
118     String JavaDoc backendpass = args[5];
119     String JavaDoc host = "localhost";
120     JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("rmi", host, 0, "/jndi/jrmp");
121
122     Map JavaDoc environment = new HashMap JavaDoc();
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     // use username and password for authentication of connections
128
// with the controller, the values are compared to the ones
129
// specified in the controller.xml config file.
130
// this line is not required if no username/password has been configered
131
environment.put(JMXConnector.CREDENTIALS, PasswordAuthenticator
132         .createCredentials("jmxuser", "jmxpassword"));
133
134     JMXConnector JavaDoc connector = JMXConnectorFactory.connect(address, environment);
135
136     ObjectName JavaDoc db = JmxConstants.getVirtualDbObjectName(databaseName);
137
138     // we build a subject for authentication
139
VirtualDatabaseUser dbUser = new VirtualDatabaseUser(vdbuser, vdbpass);
140     Set JavaDoc principals = new HashSet JavaDoc();
141     principals.add(dbUser);
142     Subject JavaDoc subj = new Subject JavaDoc(true, principals, new HashSet JavaDoc(), new HashSet JavaDoc());
143
144     // we open a connection for this subject, all susequent calls with this
145
// connection will be executed on the behalf of our subject.
146
MBeanServerConnection JavaDoc delegateConnection = connector
147         .getMBeanServerConnection(subj);
148
149     // we create a proxy to the virtual database
150
VirtualDatabaseMBean proxy = (VirtualDatabaseMBean) MBeanServerInvocationHandler
151         .newProxyInstance(delegateConnection, db, VirtualDatabaseMBean.class,
152             false);
153
154     SimpleDateFormat JavaDoc fmt = new SimpleDateFormat JavaDoc("yyyy_MM_dd");
155     String JavaDoc checkpointName = fmt.format(new Date JavaDoc());
156
157     // we disable the backend and set a checkpoint
158
proxy.disableBackendWithCheckpoint(databaseName, checkpointName);
159
160     // we call the database specific backup tool for the backup
161
runDatabaseBackupTool(backendpass, backenduser, databaseName);
162
163     // we enable the backend again
164
proxy.enableBackendFromCheckpoint(databaseName);
165   }
166
167 }
168
Popular Tags