new HashMap<String,BackendCfg>(numBackends);
if (backUpAll.isPresent())
{
for (int i=0; i < numBackends; i++)
{
Backend b = backendList.get(i);
if (b.supportsBackup())
{
backendsToArchive.add(b);
configEntries.put(b.getBackendID(), entryList.get(i));
}
}
// We'll proceed as if we're backing up multiple backends in this case
// even if there's just one.
multiple = true;
}
else
{
// Iterate through the set of backends and pick out those that were
// requested.
HashSet<String> requestedBackends =
new HashSet<String>(backendList.size());
requestedBackends.addAll(backendID.getValues());
for (int i=0; i < numBackends; i++)
{
Backend b = backendList.get(i);
if (requestedBackends.contains(b.getBackendID()))
{
if (! b.supportsBackup())
{
Message message =
WARN_BACKUPDB_BACKUP_NOT_SUPPORTED.get(b.getBackendID());
logError(message);
}
else
{
backendsToArchive.add(b);
configEntries.put(b.getBackendID(), entryList.get(i));
requestedBackends.remove(b.getBackendID());
}
}
}
if (! requestedBackends.isEmpty())
{
for (String id : requestedBackends)
{
Message message = ERR_BACKUPDB_NO_BACKENDS_FOR_ID.get(id);
logError(message);
}
return 1;
}
// See if there are multiple backends to archive.
multiple = (backendsToArchive.size() > 1);
}
// If there are no backends to archive, then print an error and exit.
if (backendsToArchive.isEmpty())
{
Message message = WARN_BACKUPDB_NO_BACKENDS_TO_ARCHIVE.get();
logError(message);
return 1;
}
// Iterate through the backends to archive and back them up individually.
boolean errorsEncountered = false;
for (Backend b : backendsToArchive)
{
// Acquire a shared lock for this backend.
try
{
String lockFile = LockFileManager.getBackendLockFileName(b);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.acquireSharedLock(lockFile, failureReason))
{
Message message = ERR_BACKUPDB_CANNOT_LOCK_BACKEND.get(
b.getBackendID(), String.valueOf(failureReason));
logError(message);
errorsEncountered = true;
continue;
}
}
catch (Exception e)
{
Message message = ERR_BACKUPDB_CANNOT_LOCK_BACKEND.get(
b.getBackendID(), getExceptionMessage(e));
logError(message);
errorsEncountered = true;
continue;
}
Message message = NOTE_BACKUPDB_STARTING_BACKUP.get(b.getBackendID());
logError(message);
// Get the config entry for this backend.
BackendCfg configEntry = configEntries.get(b.getBackendID());
// Get the path to the directory to use for this backup. If we will be
// backing up multiple backends (or if we are backing up all backends,
// even if there's only one of them), then create a subdirectory for each
// backend.
String backupDirPath;
if (multiple)
{
backupDirPath = backupDirectory.getValue() + File.separator +
b.getBackendID();
}
else
{
backupDirPath = backupDirectory.getValue();
}
// If the directory doesn't exist, then create it. If it does exist, then
// see if it has a backup descriptor file.
BackupDirectory backupDir;
backupDirFile = new File(backupDirPath);
if (backupDirFile.exists())
{
String descriptorPath = backupDirPath + File.separator +
BACKUP_DIRECTORY_DESCRIPTOR_FILE;
File descriptorFile = new File(descriptorPath);
if (descriptorFile.exists())
{
try
{
backupDir =
BackupDirectory.readBackupDirectoryDescriptor(backupDirPath);
}
catch (ConfigException ce)
{
message = ERR_BACKUPDB_CANNOT_PARSE_BACKUP_DESCRIPTOR.get(
descriptorPath, ce.getMessage());
logError(message);
errorsEncountered = true;
try
{
String lockFile = LockFileManager.getBackendLockFileName(b);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.releaseLock(lockFile, failureReason))
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), String.valueOf(failureReason));
logError(message);
}
}
catch (Exception e)
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), getExceptionMessage(e));
logError(message);
}
continue;
}
catch (Exception e)
{
message = ERR_BACKUPDB_CANNOT_PARSE_BACKUP_DESCRIPTOR.get(
descriptorPath, getExceptionMessage(e));
logError(message);
errorsEncountered = true;
try
{
String lockFile = LockFileManager.getBackendLockFileName(b);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.releaseLock(lockFile, failureReason))
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), String.valueOf(failureReason));
logError(message);
}
}
catch (Exception e2)
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), getExceptionMessage(e2));
logError(message);
}
continue;
}
}
else
{
backupDir = new BackupDirectory(backupDirPath, configEntry.dn());
}
}
else
{
try
{
backupDirFile.mkdirs();
}
catch (Exception e)
{
message = ERR_BACKUPDB_CANNOT_CREATE_BACKUP_DIR.get(
backupDirPath, getExceptionMessage(e));
logError(message);
errorsEncountered = true;
try
{
String lockFile = LockFileManager.getBackendLockFileName(b);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.releaseLock(lockFile, failureReason))
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), String.valueOf(failureReason));
logError(message);
}
}
catch (Exception e2)
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), getExceptionMessage(e2));
logError(message);
}
continue;
}
backupDir = new BackupDirectory(backupDirPath, configEntry.dn());
}
// Create a backup configuration and determine whether the requested
// backup can be performed using the selected backend.
BackupConfig backupConfig = new BackupConfig(backupDir, backupID,
incremental.isPresent());
backupConfig.setCompressData(compress.isPresent());
backupConfig.setEncryptData(encrypt.isPresent());
backupConfig.setHashData(hash.isPresent());
backupConfig.setSignHash(signHash.isPresent());
backupConfig.setIncrementalBaseID(incrementalBase);
StringBuilder unsupportedReason = new StringBuilder();
if (! b.supportsBackup(backupConfig, unsupportedReason))
{
message = ERR_BACKUPDB_CANNOT_BACKUP.get(
b.getBackendID(), unsupportedReason.toString());
logError(message);
errorsEncountered = true;
try
{
String lockFile = LockFileManager.getBackendLockFileName(b);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.releaseLock(lockFile, failureReason))
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), String.valueOf(failureReason));
logError(message);
}
}
catch (Exception e2)
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), getExceptionMessage(e2));
logError(message);
}
continue;
}
// Perform the backup.
try
{
b.createBackup(backupConfig);
}
catch (DirectoryException de)
{
message = ERR_BACKUPDB_ERROR_DURING_BACKUP.get(
b.getBackendID(), de.getMessageObject());
logError(message);
errorsEncountered = true;
try
{
String lockFile = LockFileManager.getBackendLockFileName(b);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.releaseLock(lockFile, failureReason))
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), String.valueOf(failureReason));
logError(message);
}
}
catch (Exception e)
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), getExceptionMessage(e));
logError(message);
}
continue;
}
catch (Exception e)
{
message = ERR_BACKUPDB_ERROR_DURING_BACKUP.get(
b.getBackendID(), getExceptionMessage(e));
logError(message);
errorsEncountered = true;
try
{
String lockFile = LockFileManager.getBackendLockFileName(b);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.releaseLock(lockFile, failureReason))
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), String.valueOf(failureReason));
logError(message);
}
}
catch (Exception e2)
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), getExceptionMessage(e2));
logError(message);
}
continue;
}
// Release the shared lock for the backend.
try
{
String lockFile = LockFileManager.getBackendLockFileName(b);
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.releaseLock(lockFile, failureReason))
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), String.valueOf(failureReason));
logError(message);
errorsEncountered = true;
}
}
catch (Exception e)
{
message = WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND.get(
b.getBackendID(), getExceptionMessage(e));
logError(message);
errorsEncountered = true;
}
}