1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.maintenance.backup.vfs;
17
18 import java.io.ByteArrayOutputStream;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.apache.commons.exec.CommandLine;
23 import org.apache.commons.exec.DefaultExecutor;
24 import org.apache.commons.exec.ExecuteException;
25 import org.apache.commons.exec.ExecuteStreamHandler;
26 import org.apache.commons.exec.Executor;
27 import org.apache.commons.exec.PumpStreamHandler;
28 import org.apache.commons.io.IOUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.commons.vfs2.FileContent;
32 import org.apache.commons.vfs2.FileObject;
33 import org.argeo.maintenance.MaintenanceException;
34
35
36
37
38
39 public class OsCallBackup extends AbstractAtomicBackup {
40 private final static Log log = LogFactory.getLog(OsCallBackup.class);
41
42 private String command;
43 private Map<String, String> variables = new HashMap<String, String>();
44 private Executor executor = new DefaultExecutor();
45
46 private Map<String, String> environment = new HashMap<String, String>();
47
48
49 private String sudo = null;
50
51 public OsCallBackup() {
52 }
53
54 public OsCallBackup(String name) {
55 super(name);
56 }
57
58 public OsCallBackup(String name, String command) {
59 super(name);
60 this.command = command;
61 }
62
63 @Override
64 public void writeBackup(FileObject targetFo) {
65 String commandToUse = command;
66
67
68 if (sudo != null) {
69 if (sudo.equals(""))
70 commandToUse = "sudo " + commandToUse;
71 else
72 commandToUse = "sudo -u " + sudo + " " + commandToUse;
73 }
74
75 CommandLine commandLine = CommandLine.parse(commandToUse, variables);
76 ByteArrayOutputStream errBos = new ByteArrayOutputStream();
77 if (log.isTraceEnabled())
78 log.trace(commandLine.toString());
79
80 try {
81
82 FileContent targetContent = targetFo.getContent();
83
84 ExecuteStreamHandler streamHandler = new PumpStreamHandler(targetContent.getOutputStream(), errBos);
85 executor.setStreamHandler(streamHandler);
86 executor.execute(commandLine, environment);
87 } catch (ExecuteException e) {
88 byte[] err = errBos.toByteArray();
89 String errStr = new String(err);
90 throw new MaintenanceException("Process " + commandLine + " failed (" + e.getExitValue() + "): " + errStr, e);
91 } catch (Exception e) {
92 byte[] err = errBos.toByteArray();
93 String errStr = new String(err);
94 throw new MaintenanceException("Process " + commandLine + " failed: " + errStr, e);
95 } finally {
96 IOUtils.closeQuietly(errBos);
97 }
98 }
99
100 public void setCommand(String command) {
101 this.command = command;
102 }
103
104 protected String getCommand() {
105 return command;
106 }
107
108
109
110
111
112 protected Map<String, String> getEnvironment() {
113 return environment;
114 }
115
116 protected Map<String, String> getVariables() {
117 return variables;
118 }
119
120 public void setVariables(Map<String, String> variables) {
121 this.variables = variables;
122 }
123
124 public void setExecutor(Executor executor) {
125 this.executor = executor;
126 }
127
128 public void setSudo(String sudo) {
129 this.sudo = sudo;
130 }
131
132 }