KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > backup > backupers > TarBackuper


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.backup.backupers;
23
24 import java.io.File JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Iterator JavaDoc;
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 /**
34  * Generic Tar backuper that uses the tar command to grab a directory that
35  * contains the entire database content. Only works with operating systems
36  * providing tar/untar commands to backup the database directory. Commands used
37  * are 'tar cvfz ...' and 'tar xvfz ...'.
38  * <p>
39  * Options for this backuper are (datadir is the only mandatory option):
40  *
41  * <pre>
42  * - bindir: path to the 'tar' command (if not in classpath)
43  * - datadir: path to the data directory where dbs can be found (we expect the
44  * directory to be named after the virtual database name). For example, if
45  * datadir=/var/lib/mysql/data we expect the virtual database db1 data to be
46  * found in /var/lib/mysql/data/db1
47  * - dumpServer: address to bind the dump server
48  * </pre>
49  *
50  * Configuration example:
51  * <p>
52  * <Backuper backuperName="tar"
53  * className="org.continuent.sequoia.controller.backup.backupers.TarBackuper"
54  * options="datadir=/var/lib/mysql/data"/>
55  * <p>
56  * Be sure that the user executing Sequoia has read/write access to the database
57  * data directory.
58  *
59  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
60  */

61 public class TarBackuper extends AbstractBackuper
62 {
63   static Trace logger = Trace.getLogger(TarBackuper.class
64                                                 .getName());
65   /** end user logger */
66   static Trace endUserLogger = Trace
67                                                 .getLogger("org.continuent.sequoia.enduser");
68
69   /** CommandExec instance for running native commands. * */
70   protected NativeCommandExec nativeCmdExec = new NativeCommandExec();
71
72   /**
73    * Creates a new <code>TarBackuper</code> object
74    */

75   public TarBackuper()
76   {
77   }
78
79   /**
80    * @see org.continuent.sequoia.controller.backup.Backuper#getDumpFormat()
81    */

82   public String JavaDoc getDumpFormat()
83   {
84     return "Tar compressed dump";
85   }
86
87   /**
88    * @see org.continuent.sequoia.controller.backup.Backuper#backup(org.continuent.sequoia.controller.backend.DatabaseBackend,
89    * java.lang.String, java.lang.String, java.lang.String,
90    * java.lang.String, java.util.ArrayList)
91    */

92   public Date JavaDoc backup(DatabaseBackend backend, String JavaDoc login, String JavaDoc password,
93       String JavaDoc dumpName, String JavaDoc path, ArrayList JavaDoc tables) throws BackupException
94   {
95     try
96     {
97       File JavaDoc pathDir = new File JavaDoc(path);
98       if (!pathDir.exists())
99       {
100         pathDir.mkdirs();
101         pathDir.mkdir();
102       }
103       String JavaDoc dumpPath = getDumpPhysicalPath(path, dumpName);
104       if (logger.isDebugEnabled())
105       {
106         logger.debug("Dumping " + backend.getURL() + " in " + dumpPath);
107       }
108
109       String JavaDoc executablePath = (optionsMap.containsKey("bindir")
110           ? (String JavaDoc) 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 JavaDoc((String JavaDoc) 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 JavaDoc e)
127     {
128       String JavaDoc msg = "Error while performing backup";
129       logger.error(msg, e);
130       throw new BackupException(msg, e);
131     }
132
133     return new Date JavaDoc();
134   }
135
136   /**
137    * @see org.continuent.sequoia.controller.backup.Backuper#restore(org.continuent.sequoia.controller.backend.DatabaseBackend,
138    * java.lang.String, java.lang.String, java.lang.String,
139    * java.lang.String, java.util.ArrayList)
140    */

141   public void restore(DatabaseBackend backend, String JavaDoc login, String JavaDoc password,
142       String JavaDoc dumpName, String JavaDoc path, ArrayList JavaDoc tables) throws BackupException
143   {
144     try
145     {
146       File JavaDoc pathDir = new File JavaDoc(path);
147       if (!pathDir.exists())
148       {
149         pathDir.mkdirs();
150         pathDir.mkdir();
151       }
152       String JavaDoc dumpPath = getDumpPhysicalPath(path, dumpName);
153       if (logger.isDebugEnabled())
154       {
155         logger.debug("Restoring " + backend.getURL() + " from " + dumpPath);
156       }
157
158       String JavaDoc executablePath;
159       executablePath = (optionsMap.containsKey("bindir") ? (String JavaDoc) optionsMap
160           .get("bindir")
161           + File.separator : "")
162           + "tar";
163
164       int exitValue = nativeCmdExec.executeNativeCommand(executablePath
165           + " xvfz " + dumpPath, null, null, 0, new File JavaDoc((String JavaDoc) 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 JavaDoc e)
176     {
177       String JavaDoc msg = "Error while performing restore";
178       logger.error(msg, e);
179       throw new BackupException(msg, e);
180     }
181   }
182
183   /**
184    * @see org.continuent.sequoia.controller.backup.Backuper#deleteDump(java.lang.String,
185    * java.lang.String)
186    */

187   public void deleteDump(String JavaDoc path, String JavaDoc dumpName) throws BackupException
188   {
189     File JavaDoc toRemove = new File JavaDoc(getDumpPhysicalPath(path, dumpName));
190     if (logger.isDebugEnabled())
191       logger.debug("Deleting compressed dump " + toRemove);
192     toRemove.delete();
193   }
194
195   /**
196    * @see org.continuent.sequoia.controller.backup.backupers.AbstractBackuper#setOptions(java.lang.String)
197    */

198   public void setOptions(String JavaDoc options)
199   {
200     super.setOptions(options);
201     if (!optionsMap.containsKey("datadir"))
202       throw new RuntimeException JavaDoc(
203           "No datadir specified for Tar backuper, unable to operate without this information.");
204   }
205
206   /**
207    * Get the dump physical path from its logical name
208    *
209    * @param path the path where the dump is stored
210    * @param dumpName dump logical name
211    * @return path to zip file
212    */

213   private String JavaDoc getDumpPhysicalPath(String JavaDoc path, String JavaDoc dumpName)
214   {
215     return path + File.separator + dumpName + ".tgz";
216   }
217
218   protected void printErrors()
219   {
220     ArrayList JavaDoc errors = nativeCmdExec.getStderr();
221     Iterator JavaDoc it = errors.iterator();
222     while (it.hasNext())
223     {
224       String JavaDoc msg = (String JavaDoc) it.next();
225       logger.info(msg);
226       endUserLogger.error(msg);
227     }
228   }
229 }
Popular Tags