import org.apache.commons.exec.ExecuteStreamHandler;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler;
+import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.vfs.FileContent;
import org.apache.commons.vfs.FileObject;
-import org.apache.commons.vfs.FileSystemManager;
-import org.apache.commons.vfs.VFS;
import org.argeo.ArgeoException;
/**
- * Runs a an OS command and save the output as a file. Typically used for MySQL
- * dumps
+ * Runs an OS command and save its standard output as a file. Typically used for
+ * MySQL or OpenLDAP dumps.
*/
-public class OsCallBackup implements Runnable {
-
+public class OsCallBackup extends AbstractAtomicBackup {
private final static Log log = LogFactory.getLog(OsCallBackup.class);
private String command;
private Map<String, String> variables = new HashMap<String, String>();
+ private Executor executor = new DefaultExecutor();
- private String target;
+ public OsCallBackup() {
+ }
- @Override
- public void run() {
- try {
- Executor executor = new DefaultExecutor();
+ public OsCallBackup(String name) {
+ super(name);
+ }
- CommandLine commandLine = CommandLine.parse(command, variables);
+ public OsCallBackup(String name, String command) {
+ super(name);
+ this.command = command;
+ }
+
+ @Override
+ public void writeBackup(FileObject targetFo) {
+ CommandLine commandLine = CommandLine.parse(command, variables);
+ ByteArrayOutputStream errBos = new ByteArrayOutputStream();
+ if (log.isTraceEnabled())
+ log.trace(commandLine.toString());
+ try {
// stdout
- FileSystemManager fsm = VFS.getManager();
- FileObject targetFo = fsm.resolveFile(target);
FileContent targetContent = targetFo.getContent();
-
// stderr
- ByteArrayOutputStream errBos = new ByteArrayOutputStream();
ExecuteStreamHandler streamHandler = new PumpStreamHandler(
targetContent.getOutputStream(), errBos);
-
executor.setStreamHandler(streamHandler);
-
- try {
- if (log.isDebugEnabled())
- log.debug(commandLine.toString());
-
- executor.execute(commandLine);
- } catch (ExecuteException e) {
- byte[] err = errBos.toByteArray();
- String errStr = new String(err);
- throw new ArgeoException("Process failed with exit value "
- + e.getExitValue() + ": " + errStr);
- }
+ executor.execute(commandLine);
+ } catch (ExecuteException e) {
+ byte[] err = errBos.toByteArray();
+ String errStr = new String(err);
+ throw new ArgeoException("Process " + commandLine
+ + " failed with exit value " + e.getExitValue() + ": "
+ + errStr, e);
} catch (Exception e) {
- throw new ArgeoException("Cannot backup to " + target
- + " with command " + command + " " + variables, e);
+ byte[] err = errBos.toByteArray();
+ String errStr = new String(err);
+ throw new ArgeoException("Process " + commandLine + " failed: "
+ + errStr, e);
+ } finally {
+ IOUtils.closeQuietly(errBos);
}
}
this.command = command;
}
- public void setVariables(Map<String, String> variables) {
- this.variables = variables;
+ protected String getCommand() {
+ return command;
}
- public void setTarget(String target) {
- this.target = target;
+ protected Map<String, String> getVariables() {
+ return variables;
}
- public static void main(String args[]) {
- OsCallBackup osCallBackup = new OsCallBackup();
- osCallBackup.setCommand("/usr/bin/mysqldump"
- + " --lock-tables --add-locks --add-drop-table"
- + " -u ${dbUser} --password=${dbPassword} --databases ${dbName}");
- Map<String, String> variables = new HashMap<String, String>();
- variables.put("dbUser", "root");
- variables.put("dbPassword", "");
- variables.put("dbName", "test");
- osCallBackup.setVariables(variables);
-
- osCallBackup
- .setTarget("/home/mbaudier/dev/src/commons/server/runtime/org.argeo.server.core/target/dump.sql");
+ public void setVariables(Map<String, String> variables) {
+ this.variables = variables;
+ }
- osCallBackup.run();
+ public void setExecutor(Executor executor) {
+ this.executor = executor;
}
+
}