summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/BackupRestore.java
blob: 7c05b9167753dd6be802fcda021b676720937022 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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;
			}
		}
	}

}