View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.maintenance.backup.vfs;
17  
18  import org.apache.commons.vfs2.FileObject;
19  import org.apache.commons.vfs2.FileSystemManager;
20  import org.apache.commons.vfs2.FileSystemOptions;
21  import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
22  import org.argeo.maintenance.MaintenanceException;
23  
24  /**
25   * Simplify atomic backups implementation, especially by managing VFS.
26   */
27  public abstract class AbstractAtomicBackup implements AtomicBackup {
28  	private String name;
29  	private String compression = "bz2";
30  
31  	protected abstract void writeBackup(FileObject targetFo);
32  
33  	public AbstractAtomicBackup() {
34  	}
35  
36  	public AbstractAtomicBackup(String name) {
37  		this.name = name;
38  	}
39  
40  	public void init() {
41  		if (name == null)
42  			throw new MaintenanceException("Atomic backup name must be set");
43  	}
44  
45  	public void destroy() {
46  
47  	}
48  
49  	@Override
50  	public String backup(FileSystemManager fileSystemManager,
51  			String backupsBase, BackupContext backupContext,
52  			FileSystemOptions opts) {
53  		if (name == null)
54  			throw new MaintenanceException("Atomic backup name must be set");
55  
56  		FileObject targetFo = null;
57  		try {
58  			if (backupsBase.startsWith("sftp:"))
59  				SftpFileSystemConfigBuilder.getInstance()
60  						.setStrictHostKeyChecking(opts, "no");
61  			if (compression == null || compression.equals("none"))
62  				targetFo = fileSystemManager.resolveFile(backupsBase + '/'
63  						+ backupContext.getRelativeFolder() + '/' + name, opts);
64  			else if (compression.equals("bz2"))
65  				targetFo = fileSystemManager.resolveFile("bz2:" + backupsBase
66  						+ '/' + backupContext.getRelativeFolder() + '/' + name
67  						+ ".bz2" + "!" + name, opts);
68  			else if (compression.equals("gz"))
69  				targetFo = fileSystemManager.resolveFile("gz:" + backupsBase
70  						+ '/' + backupContext.getRelativeFolder() + '/' + name
71  						+ ".gz" + "!" + name, opts);
72  			else
73  				throw new MaintenanceException("Unsupported compression "
74  						+ compression);
75  
76  			writeBackup(targetFo);
77  
78  			return targetFo.toString();
79  		} catch (Exception e) {
80  			throw new MaintenanceException("Cannot backup " + name + " to "
81  					+ targetFo, e);
82  		} finally {
83  			BackupUtils.closeFOQuietly(targetFo);
84  		}
85  	}
86  
87  	public void setName(String name) {
88  		this.name = name;
89  	}
90  
91  	public String getName() {
92  		return name;
93  	}
94  
95  	public void setCompression(String compression) {
96  		this.compression = compression;
97  	}
98  }