]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.core/src/main/java/org/argeo/server/backup/OsCallBackup.java
Merge: Add configurable sudo for OS call backups
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.core / src / main / java / org / argeo / server / backup / OsCallBackup.java
index 989c29a6872ddebda58f78fd307ff0459aac89a2..816157cfd4a2be750eb7dc0db7f2292afd60cf08 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.argeo.server.backup;
 
 import java.io.ByteArrayOutputStream;
@@ -28,6 +43,11 @@ public class OsCallBackup extends AbstractAtomicBackup {
        private Map<String, String> variables = new HashMap<String, String>();
        private Executor executor = new DefaultExecutor();
 
+       private Map<String, String> environment = new HashMap<String, String>();
+
+       /** Name of the sudo user, root if "", not sudo if null */
+       private String sudo = null;
+
        public OsCallBackup() {
        }
 
@@ -42,7 +62,17 @@ public class OsCallBackup extends AbstractAtomicBackup {
 
        @Override
        public void writeBackup(FileObject targetFo) {
-               CommandLine commandLine = CommandLine.parse(command, variables);
+               String commandToUse = command;
+
+               // sudo
+               if (sudo != null) {
+                       if (sudo.equals(""))
+                               commandToUse = "sudo " + commandToUse;
+                       else
+                               commandToUse = "sudo -u " + sudo + " " + commandToUse;
+               }
+
+               CommandLine commandLine = CommandLine.parse(commandToUse, variables);
                ByteArrayOutputStream errBos = new ByteArrayOutputStream();
                if (log.isTraceEnabled())
                        log.trace(commandLine.toString());
@@ -54,7 +84,7 @@ public class OsCallBackup extends AbstractAtomicBackup {
                        ExecuteStreamHandler streamHandler = new PumpStreamHandler(
                                        targetContent.getOutputStream(), errBos);
                        executor.setStreamHandler(streamHandler);
-                       executor.execute(commandLine);
+                       executor.execute(commandLine, environment);
                } catch (ExecuteException e) {
                        byte[] err = errBos.toByteArray();
                        String errStr = new String(err);
@@ -78,6 +108,14 @@ public class OsCallBackup extends AbstractAtomicBackup {
                return command;
        }
 
+       /**
+        * A reference to the environment variables that will be passed to the
+        * process. Empty by default.
+        */
+       protected Map<String, String> getEnvironment() {
+               return environment;
+       }
+
        protected Map<String, String> getVariables() {
                return variables;
        }
@@ -90,4 +128,8 @@ public class OsCallBackup extends AbstractAtomicBackup {
                this.executor = executor;
        }
 
+       public void setSudo(String sudo) {
+               this.sudo = sudo;
+       }
+
 }