1 package org.apache.archiva.redback.struts2.action.admin;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork2.Preparable;
23 import org.apache.archiva.redback.rbac.RBACManager;
24 import org.apache.archiva.redback.rbac.Resource;
25 import org.apache.archiva.redback.struts2.action.AbstractSecurityAction;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.archiva.redback.keys.KeyManager;
28 import org.apache.archiva.redback.management.DataManagementTool;
29 import org.apache.archiva.redback.users.UserManager;
30 import org.apache.archiva.redback.integration.interceptor.SecureAction;
31 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
32 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
33 import org.apache.archiva.redback.integration.role.RoleConstants;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Controller;
37 import javax.inject.Inject;
38 import javax.inject.Named;
40 import java.io.IOException;
41 import java.text.SimpleDateFormat;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.Date;
45 import java.util.List;
46 import java.util.Locale;
52 @Controller( "backup-restore" )
54 public class BackupRestoreAction
55 extends AbstractSecurityAction
56 implements SecureAction, Preparable
58 public final static String CUSTOM_ERROR = "custom_error";
63 private File applicationHome = new File( "data" );
69 private DataManagementTool dataManagementTool;
75 @Named( value = "rBACManager#jdo" )
76 private RBACManager rbacManager;
82 @Named( value = "userManager#jdo" )
83 private UserManager userManager;
89 @Named( value = "keyManager#jdo" )
90 private KeyManager keyManager;
92 private File backupDirectory;
94 private String restoreDirectory;
96 private List<BackupRecord> previousBackups;
98 private boolean confirmed;
100 public static final String BACKUP_DIRECTORY = "user-backup-directory";
106 retrievePreviousBackups();
111 public String backup()
115 File backupDirectory = getTimestampedBackupDirectory();
116 backupDirectory.mkdirs();
118 log.info( "Backing up security database to {}", backupDirectory );
119 this.backupDatabase( backupDirectory );
121 log.info( "Done backing up security database" );
126 public String restore()
129 if ( StringUtils.isEmpty( restoreDirectory ) )
131 addActionError( getText( "backupRestore.backup.empty.error" ) );
135 File restoreDirectory = new File( this.restoreDirectory );
137 boolean fileExists = restoreDirectory.exists() && restoreDirectory.isDirectory();
138 boolean isValidBackup = false;
142 BackupRecord record = new BackupRecord( restoreDirectory );
143 isValidBackup = record.isValidBackup();
148 log.warn( "Backup: " + this.restoreDirectory + " not found." );
149 addActionError( getText( "backupRestore.backup.error" ) );
150 retrievePreviousBackups();
153 else if ( !isValidBackup )
155 log.warn( "Backup: " + this.restoreDirectory + " is not a valid backup directory." );
156 addActionError( getText( "backupRestore.backup.error" ) );
157 retrievePreviousBackups();
161 log.info( "Restoring security database from {}", this.restoreDirectory );
162 this.eraseDatabase();
163 this.restoreDatabase( restoreDirectory );
164 log.info( "Done restoring security database" );
170 private void backupDatabase( File backupDirectory )
174 dataManagementTool.backupKeyDatabase( keyManager, backupDirectory );
175 dataManagementTool.backupRBACDatabase( rbacManager, backupDirectory );
176 dataManagementTool.backupUserDatabase( userManager, backupDirectory );
179 private void eraseDatabase()
181 dataManagementTool.eraseKeysDatabase( keyManager );
182 dataManagementTool.eraseRBACDatabase( rbacManager );
183 dataManagementTool.eraseUsersDatabase( userManager );
186 private void restoreDatabase( File backupDirectory )
190 dataManagementTool.restoreKeysDatabase( keyManager, backupDirectory );
191 dataManagementTool.restoreRBACDatabase( rbacManager, backupDirectory );
192 dataManagementTool.restoreUsersDatabase( userManager, backupDirectory );
195 public String getRestoreDirectory()
197 return restoreDirectory;
200 public void setRestoreDirectory( String restoreDirectory )
202 this.restoreDirectory = restoreDirectory;
205 private File getTimestampedBackupDirectory()
207 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMdd.HHmmss", Locale.US );
208 return new File( this.backupDirectory, dateFormat.format( new Date() ) );
211 public File getBackupDirectory()
213 return backupDirectory;
216 public List<BackupRecord> getPreviousBackups()
218 return previousBackups;
221 public void prepare()
223 backupDirectory = this.getFile( BACKUP_DIRECTORY );
224 retrievePreviousBackups();
227 private void retrievePreviousBackups()
229 previousBackups = new ArrayList<BackupRecord>();
230 File[] files = backupDirectory.listFiles();
233 for ( int i = 0; i < files.length; i++ )
237 if ( f.isDirectory() && !f.getName().startsWith( "." ) )
239 BackupRecord record = new BackupRecord( f );
241 if ( record.isValidBackup() )
243 previousBackups.add( record );
248 Collections.sort( previousBackups );
251 public boolean isConfirmed()
256 public void setConfirmed( boolean confirmed )
258 this.confirmed = confirmed;
262 public SecureActionBundle initSecureActionBundle()
263 throws SecureActionException
265 SecureActionBundle bundle = new SecureActionBundle();
266 bundle.setRequiresAuthentication( true );
267 bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_MANAGE_DATA, Resource.GLOBAL );
271 public File getFile( String filename )
273 if ( filename == null )
280 if ( filename != null && filename.length() != 0 )
282 f = new File( filename );
284 if ( !f.isAbsolute() )
286 f = new File( applicationHome, filename );
292 return f.getCanonicalFile();
294 catch ( IOException e )