blob: 00e64aa339effa528f4ef32a5a9280247f8c6255 [file] [log] [blame]
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.backup;
import com.sun.enterprise.backup.util.BackupUtils;
import com.sun.enterprise.util.io.FileUtils;
import java.io.*;
import java.util.List;
import java.util.Date;
/**
*
* @author Byron Nevins
*/
public class BackupManager extends BackupRestoreManager {
public BackupManager(BackupRequest req) throws BackupException {
super(req);
}
//////////////////////////////////////////////////////////////////////
public final String backup() throws BackupException {
StringBuilder mesg = new StringBuilder();
String statusString = writeStatus();
if (!request.terse) {
String backupTime = new Date(request.timestamp).toString();
mesg.append(StringHelper.get("backup-res.SuccessfulBackup",
request.domainName, backupTime));
}
try {
ZipStorage zs = new ZipStorage(request);
zs.store();
// TODO: RSH - Recycle files. I'm not sure if this is the precise
// place to do the recycling, but we probably need to do it somewhere
// in this module since BackupFilenameManager is module private. We
// should do the recycling after the backup completes. I think it
// should be safe to recycle after a successful or unsuccessful backup
// (assuming a failed backup doesn't leave a corrupt ZIP file).
BackupFilenameManager bfm =
new BackupFilenameManager(getBackupDirectory(request),
request.domainName);
List<File> recycleFiles = bfm.getRecycleFiles(request.recycleLimit);
if (recycleFiles.size() > 0 && request.verbose) {
mesg.append("\n");
mesg.append(StringHelper.get("backup-res.recycle",
request.recycleLimit));
mesg.append("\n");
}
for (File f : recycleFiles) {
if (request.verbose) {
mesg.append(StringHelper.get("backup-res.recycleDelete", f));
mesg.append("\n");
}
if (!f.delete()) {
mesg.append(StringHelper.get("backup-res.recycleBadDelete", f));
mesg.append("\n");
}
}
if (request.verbose) {
mesg.append("\n\n");
mesg.append(statusString);
}
//XXX: This needs to be fixed such that if an error occurs
// it is propogated up such that the command exits with
// the proper exit code.
return mesg.toString();
}
finally {
status.delete();
BackupUtils.protect(request.backupFile);
}
}
////////////////////////////////////////////////////////////////////////
void init() throws BackupException {
super.init();
if(request.backupFile != null)
throw new BackupException("backup-res.InternalError",
"No backupFilename may be specified for a backup -- it is reserved for restore operations only.");
if(!FileUtils.safeIsDirectory(request.domainDir))
throw new BackupException("backup-res.NoDomainDir",
request.domainDir);
File backupDir = getBackupDirectory(request);
// mkdirs may fail if the directory exists or it could not be created.
if (!backupDir.mkdirs()) {
// If it doesn't exist then it is an error.
if(!FileUtils.safeIsDirectory(backupDir))
throw new BackupException("backup-res.NoBackupDirCantCreate",
backupDir);
}
BackupFilenameManager bfmgr =
new BackupFilenameManager(backupDir, request.domainName);
request.backupFile = bfmgr.next();
// get customized description if user hasn't specified one
if(request.description == null || request.description.length() <= 0)
request.description = bfmgr.getCustomizedDescription();
}
///////////////////////////////////////////////////////////////////////////
private String writeStatus() {
status = new Status();
return status.write(request);
}
///////////////////////////////////////////////////////////////////////////
Status status;
}