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 89071b18a8f594ac13c88cb5e51c8fc8c5989139..816157cfd4a2be750eb7dc0db7f2292afd60cf08 100644 (file)
@@ -45,6 +45,9 @@ public class OsCallBackup extends AbstractAtomicBackup {
 
        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() {
        }
 
@@ -59,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());
@@ -115,4 +128,8 @@ public class OsCallBackup extends AbstractAtomicBackup {
                this.executor = executor;
        }
 
+       public void setSudo(String sudo) {
+               this.sudo = sudo;
+       }
+
 }