2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.cms
.internal
.backup
;
18 import java
.io
.ByteArrayOutputStream
;
19 import java
.util
.HashMap
;
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
.cms
.CmsException
;
36 * Runs an OS command and save its standard output as a file. Typically used for
37 * MySQL or OpenLDAP dumps.
39 public class OsCallBackup
extends AbstractAtomicBackup
{
40 private final static Log log
= LogFactory
.getLog(OsCallBackup
.class);
42 private String command
;
43 private Map
<String
, String
> variables
= new HashMap
<String
, String
>();
44 private Executor executor
= new DefaultExecutor();
46 private Map
<String
, String
> environment
= new HashMap
<String
, String
>();
48 /** Name of the sudo user, root if "", not sudo if null */
49 private String sudo
= null;
51 public OsCallBackup() {
54 public OsCallBackup(String name
) {
58 public OsCallBackup(String name
, String command
) {
60 this.command
= command
;
64 public void writeBackup(FileObject targetFo
) {
65 String commandToUse
= command
;
70 commandToUse
= "sudo " + commandToUse
;
72 commandToUse
= "sudo -u " + sudo
+ " " + commandToUse
;
75 CommandLine commandLine
= CommandLine
.parse(commandToUse
, variables
);
76 ByteArrayOutputStream errBos
= new ByteArrayOutputStream();
77 if (log
.isTraceEnabled())
78 log
.trace(commandLine
.toString());
82 FileContent targetContent
= targetFo
.getContent();
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 CmsException("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 CmsException("Process " + commandLine
+ " failed: " + errStr
, e
);
96 IOUtils
.closeQuietly(errBos
);
100 public void setCommand(String command
) {
101 this.command
= command
;
104 protected String
getCommand() {
109 * A reference to the environment variables that will be passed to the
110 * process. Empty by default.
112 protected Map
<String
, String
> getEnvironment() {
116 protected Map
<String
, String
> getVariables() {
120 public void setVariables(Map
<String
, String
> variables
) {
121 this.variables
= variables
;
124 public void setExecutor(Executor executor
) {
125 this.executor
= executor
;
128 public void setSudo(String sudo
) {