blob: 7c05b9167753dd6be802fcda021b676720937022 (
plain) (
tree)
|
|
package org.openslx.taskmanager.tasks;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.openslx.satserver.util.Constants;
import org.openslx.taskmanager.api.SystemCommandTask;
import com.google.gson.annotations.Expose;
public class BackupRestore extends SystemCommandTask
{
@Expose
private String mode;
@Expose
private String backupFile;
@Expose
private boolean restoreOpenslx;
@Expose
private boolean restoreDozmod;
@Expose
private String password;
@Expose
private String destination;
private Output status = new Output();
private static AtomicBoolean isRunning = new AtomicBoolean();
@Override
protected boolean initTask()
{
this.setStatusObject( this.status );
if ( mode == null ) {
status.addMessage( "No mode given." );
return false;
}
if ( !mode.equals( "backup" ) && !mode.equals( "restore" ) && !mode.equals( "test" ) ) {
status.addMessage( "Invalid mode: " + mode );
return false;
}
if ( backupFile == null && ( mode.equals( "restore" ) || mode.equals( "test" ) ) ) {
status.addMessage( "No backup file given to restore/test!" );
return false;
}
this.timeoutSeconds = 180;
return true;
}
@Override
protected String[] initCommandLine()
{
if (!isRunning.compareAndSet( false, true )) {
status.addMessage( "Another operation is already in progress." );
return null;
}
List<String> args = new ArrayList<>();
args.add( "/usr/bin/sudo" );
args.add( "-n" );
args.add( "-u" );
args.add( "root" );
if ( mode.equals( "restore" ) ) {
// Restore
args.add( Constants.BASEDIR + "/scripts/system-restore" );
if ( backupFile != null ) {
args.add( backupFile );
}
if ( restoreDozmod ) {
args.add( "dozmod" );
}
if ( restoreOpenslx ) {
args.add( "openslx" );
}
args.add( "--restore" );
if ( password != null ) {
args.add( "--decrypt" );
args.add( "TM_PW_ENV_VAR" );
}
} else if ( mode.equals( "test" ) ) {
// Test archive encryption / archive format
args.add( Constants.BASEDIR + "/scripts/system-restore" );
args.add( backupFile );
args.add( "--test" );
if ( password != null ) {
args.add( "--decrypt" );
args.add( "TM_PW_ENV_VAR" );
}
} else {
// Backup
args.add( Constants.BASEDIR + "/scripts/system-backup" );
if ( password != null ) {
args.add( "--encrypt" );
args.add( "TM_PW_ENV_VAR" );
}
if ( destination != null ) {
args.add( "--destination" );
args.add( destination );
}
}
return args.toArray( new String[ args.size() ] );
}
@Override
protected void initEnvironment( Map<String, String> environment )
{
if ( password != null ) {
environment.put( "TM_PW_ENV_VAR", password );
}
}
@Override
protected boolean processEnded( int exitCode )
{
if ( backupFile != null ) {
try {
new File( backupFile ).delete();
} catch ( Exception e ) {
}
}
isRunning.set( false );
return exitCode == 0 && ( mode.equals( "test" ) || mode.equals( "restore" ) || status.backupFile != null );
}
@Override
protected void processStdOut( String line )
{
if ( line.startsWith( "Location: " ) ) {
status.backupFile = line.substring( 10 );
} else {
status.addMessage( line );
}
}
@Override
protected void processStdErr( String line )
{
status.addMessage( line );
}
class Output
{
private String messages = null;
public String backupFile = null;
private void addMessage( String str )
{
if ( messages == null ) {
messages = str;
} else {
messages += "\n" + str;
}
}
}
}
|