2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.server
.backup
;
18 import org
.apache
.commons
.vfs
.FileObject
;
19 import org
.apache
.commons
.vfs
.FileSystemManager
;
20 import org
.apache
.commons
.vfs
.FileSystemOptions
;
21 import org
.apache
.commons
.vfs
.provider
.sftp
.SftpFileSystemConfigBuilder
;
22 import org
.argeo
.ArgeoException
;
25 * Simplify atomic backups implementation, especially by managing VFS.
27 public abstract class AbstractAtomicBackup
implements AtomicBackup
{
29 private String compression
= "bz2";
31 protected abstract void writeBackup(FileObject targetFo
);
33 public AbstractAtomicBackup() {
36 public AbstractAtomicBackup(String name
) {
42 throw new ArgeoException("Atomic backup name must be set");
45 public void destroy() {
50 public String
backup(FileSystemManager fileSystemManager
,
51 String backupsBase
, BackupContext backupContext
,
52 FileSystemOptions opts
) {
54 throw new ArgeoException("Atomic backup name must be set");
56 FileObject targetFo
= null;
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
);
73 throw new ArgeoException("Unsupported compression "
76 writeBackup(targetFo
);
78 return targetFo
.toString();
79 } catch (Exception e
) {
80 throw new ArgeoException("Cannot backup " + name
+ " to "
83 BackupUtils
.closeFOQuietly(targetFo
);
87 public void setName(String name
) {
91 public String
getName() {
95 public void setCompression(String compression
) {
96 this.compression
= compression
;