1 package org
.argeo
.slc
.backup
.vfs
;
3 import java
.io
.ByteArrayOutputStream
;
4 import java
.util
.HashMap
;
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
;
19 * Runs an OS command and save its standard output as a file. Typically used for
20 * MySQL or OpenLDAP dumps.
22 public class OsCallBackup
extends AbstractAtomicBackup
{
23 private final static CmsLog log
= CmsLog
.getLog(OsCallBackup
.class);
25 private String command
;
26 private Map
<String
, String
> variables
= new HashMap
<String
, String
>();
27 private Executor executor
= new DefaultExecutor();
29 private Map
<String
, String
> environment
= new HashMap
<String
, String
>();
31 /** Name of the sudo user, root if "", not sudo if null */
32 private String sudo
= null;
34 public OsCallBackup() {
37 public OsCallBackup(String name
) {
41 public OsCallBackup(String name
, String command
) {
43 this.command
= command
;
47 public void writeBackup(FileObject targetFo
) {
48 String commandToUse
= command
;
53 commandToUse
= "sudo " + commandToUse
;
55 commandToUse
= "sudo -u " + sudo
+ " " + commandToUse
;
58 CommandLine commandLine
= CommandLine
.parse(commandToUse
, variables
);
59 ByteArrayOutputStream errBos
= new ByteArrayOutputStream();
60 if (log
.isTraceEnabled())
61 log
.trace(commandLine
.toString());
65 FileContent targetContent
= targetFo
.getContent();
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
);
79 IOUtils
.closeQuietly(errBos
);
83 public void setCommand(String command
) {
84 this.command
= command
;
87 protected String
getCommand() {
92 * A reference to the environment variables that will be passed to the
93 * process. Empty by default.
95 protected Map
<String
, String
> getEnvironment() {
99 protected Map
<String
, String
> getVariables() {
103 public void setVariables(Map
<String
, String
> variables
) {
104 this.variables
= variables
;
107 public void setExecutor(Executor executor
) {
108 this.executor
= executor
;
111 public void setSudo(String sudo
) {