]> git.argeo.org Git - gpl/argeo-slc.git/blob - OsCallBackup.java
a72ce63f9a85705b6bdeff5b04a867b3fb28e04d
[gpl/argeo-slc.git] / OsCallBackup.java
1 package org.argeo.maintenance.backup.vfs;
2
3 import java.io.ByteArrayOutputStream;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.apache.commons.exec.CommandLine;
8 import org.apache.commons.exec.DefaultExecutor;
9 import org.apache.commons.exec.ExecuteException;
10 import org.apache.commons.exec.ExecuteStreamHandler;
11 import org.apache.commons.exec.Executor;
12 import org.apache.commons.exec.PumpStreamHandler;
13 import org.apache.commons.io.IOUtils;
14 import org.apache.commons.vfs2.FileContent;
15 import org.apache.commons.vfs2.FileObject;
16 import org.argeo.api.cms.CmsLog;
17
18 /**
19 * Runs an OS command and save its standard output as a file. Typically used for
20 * MySQL or OpenLDAP dumps.
21 */
22 public class OsCallBackup extends AbstractAtomicBackup {
23 private final static CmsLog log = CmsLog.getLog(OsCallBackup.class);
24
25 private String command;
26 private Map<String, String> variables = new HashMap<String, String>();
27 private Executor executor = new DefaultExecutor();
28
29 private Map<String, String> environment = new HashMap<String, String>();
30
31 /** Name of the sudo user, root if "", not sudo if null */
32 private String sudo = null;
33
34 public OsCallBackup() {
35 }
36
37 public OsCallBackup(String name) {
38 super(name);
39 }
40
41 public OsCallBackup(String name, String command) {
42 super(name);
43 this.command = command;
44 }
45
46 @Override
47 public void writeBackup(FileObject targetFo) {
48 String commandToUse = command;
49
50 // sudo
51 if (sudo != null) {
52 if (sudo.equals(""))
53 commandToUse = "sudo " + commandToUse;
54 else
55 commandToUse = "sudo -u " + sudo + " " + commandToUse;
56 }
57
58 CommandLine commandLine = CommandLine.parse(commandToUse, variables);
59 ByteArrayOutputStream errBos = new ByteArrayOutputStream();
60 if (log.isTraceEnabled())
61 log.trace(commandLine.toString());
62
63 try {
64 // stdout
65 FileContent targetContent = targetFo.getContent();
66 // stderr
67 ExecuteStreamHandler streamHandler = new PumpStreamHandler(targetContent.getOutputStream(), errBos);
68 executor.setStreamHandler(streamHandler);
69 executor.execute(commandLine, environment);
70 } catch (ExecuteException e) {
71 byte[] err = errBos.toByteArray();
72 String errStr = new String(err);
73 throw new MaintenanceException("Process " + commandLine + " failed (" + e.getExitValue() + "): " + errStr, e);
74 } catch (Exception e) {
75 byte[] err = errBos.toByteArray();
76 String errStr = new String(err);
77 throw new MaintenanceException("Process " + commandLine + " failed: " + errStr, e);
78 } finally {
79 IOUtils.closeQuietly(errBos);
80 }
81 }
82
83 public void setCommand(String command) {
84 this.command = command;
85 }
86
87 protected String getCommand() {
88 return command;
89 }
90
91 /**
92 * A reference to the environment variables that will be passed to the
93 * process. Empty by default.
94 */
95 protected Map<String, String> getEnvironment() {
96 return environment;
97 }
98
99 protected Map<String, String> getVariables() {
100 return variables;
101 }
102
103 public void setVariables(Map<String, String> variables) {
104 this.variables = variables;
105 }
106
107 public void setExecutor(Executor executor) {
108 this.executor = executor;
109 }
110
111 public void setSudo(String sudo) {
112 this.sudo = sudo;
113 }
114
115 }