| | |
| | | */ |
| | | public class BackUpDB extends TaskTool |
| | | { |
| | | |
| | | private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); |
| | | |
| | | /** |
| | |
| | | INFO_BACKUPDB_TOOL_DESCRIPTION.get()); |
| | | argParser.setShortToolDescription(REF_SHORT_DESC_BACKUP.get()); |
| | | |
| | | |
| | | // Initialize all the command-line argument types and register them with the parser. |
| | | try |
| | | { |
| | |
| | | return 1; |
| | | } |
| | | |
| | | |
| | | // If we should just display usage or version information, |
| | | // then print it and exit. |
| | | if (argParser.usageOrVersionDisplayed()) |
| | |
| | | return 1; |
| | | } |
| | | |
| | | |
| | | // Checks the version - if upgrade required, the tool is unusable |
| | | try |
| | | { |
| | |
| | | } |
| | | |
| | | return process(argParser, initializeServer, out, err); |
| | | |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void addTaskAttributes(List<RawAttribute> attributes) |
| | | { |
| | |
| | | && !arg.getValue().equals(arg.getDefaultValue()); |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public String getTaskObjectclass() { |
| | | return "ds-task-backup"; |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public Class<?> getTaskClass() { |
| | | return BackupTask.class; |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | protected int processLocal(boolean initializeServer, |
| | | PrintStream out, |
| | | PrintStream err) { |
| | | |
| | | // Make sure that the backup directory exists. If not, then create it. |
| | | File backupDirFile = new File(backupDirectory.getValue()); |
| | | if (! backupDirFile.exists()) |
| | |
| | | Backend<?> b = backendList.get(i); |
| | | if (requestedBackends.contains(b.getBackendID())) |
| | | { |
| | | if (!b.supports(BackendOperation.BACKUP)) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_BACKUP_NOT_SUPPORTED, b.getBackendID()); |
| | | } |
| | | else |
| | | if (b.supports(BackendOperation.BACKUP)) |
| | | { |
| | | backendsToArchive.add(b); |
| | | configEntries.put(b.getBackendID(), entryList.get(i)); |
| | | requestedBackends.remove(b.getBackendID()); |
| | | } |
| | | else |
| | | { |
| | | logger.warn(WARN_BACKUPDB_BACKUP_NOT_SUPPORTED, b.getBackendID()); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | 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()) |
| | | { |
| | |
| | | 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 |
| | | if (!acquireSharedLock(b)) |
| | | { |
| | | String lockFile = LockFileManager.getBackendLockFileName(b); |
| | | StringBuilder failureReason = new StringBuilder(); |
| | | if (! LockFileManager.acquireSharedLock(lockFile, failureReason)) |
| | | { |
| | | logger.error(ERR_BACKUPDB_CANNOT_LOCK_BACKEND, b.getBackendID(), failureReason); |
| | | errorsEncountered = true; |
| | | continue; |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.error(ERR_BACKUPDB_CANNOT_LOCK_BACKEND, b.getBackendID(), getExceptionMessage(e)); |
| | | errorsEncountered = true; |
| | | continue; |
| | | } |
| | | |
| | | |
| | | logger.info(NOTE_BACKUPDB_STARTING_BACKUP, b.getBackendID()); |
| | | |
| | | |
| | | // 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 |
| | |
| | | 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; |
| | |
| | | { |
| | | logger.error(ERR_BACKUPDB_CANNOT_PARSE_BACKUP_DESCRIPTOR, descriptorPath, ce.getMessage()); |
| | | errorsEncountered = true; |
| | | |
| | | try |
| | | { |
| | | String lockFile = LockFileManager.getBackendLockFileName(b); |
| | | StringBuilder failureReason = new StringBuilder(); |
| | | if (! LockFileManager.releaseLock(lockFile, failureReason)) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), failureReason); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), getExceptionMessage(e)); |
| | | } |
| | | |
| | | releaseSharedLock(b); |
| | | continue; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.error(ERR_BACKUPDB_CANNOT_PARSE_BACKUP_DESCRIPTOR, descriptorPath, getExceptionMessage(e)); |
| | | errorsEncountered = true; |
| | | |
| | | try |
| | | { |
| | | String lockFile = LockFileManager.getBackendLockFileName(b); |
| | | StringBuilder failureReason = new StringBuilder(); |
| | | if (! LockFileManager.releaseLock(lockFile, failureReason)) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), failureReason); |
| | | } |
| | | } |
| | | catch (Exception e2) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), getExceptionMessage(e2)); |
| | | } |
| | | |
| | | releaseSharedLock(b); |
| | | continue; |
| | | } |
| | | } |
| | |
| | | { |
| | | logger.error(ERR_BACKUPDB_CANNOT_CREATE_BACKUP_DIR, backupDirPath, getExceptionMessage(e)); |
| | | errorsEncountered = true; |
| | | |
| | | try |
| | | { |
| | | String lockFile = LockFileManager.getBackendLockFileName(b); |
| | | StringBuilder failureReason = new StringBuilder(); |
| | | if (! LockFileManager.releaseLock(lockFile, failureReason)) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), failureReason); |
| | | } |
| | | } |
| | | catch (Exception e2) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), getExceptionMessage(e2)); |
| | | } |
| | | |
| | | releaseSharedLock(b); |
| | | 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, |
| | |
| | | { |
| | | logger.error(ERR_BACKUPDB_CANNOT_BACKUP, b.getBackendID()); |
| | | errorsEncountered = true; |
| | | |
| | | try |
| | | { |
| | | String lockFile = LockFileManager.getBackendLockFileName(b); |
| | | StringBuilder failureReason = new StringBuilder(); |
| | | if (! LockFileManager.releaseLock(lockFile, failureReason)) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), failureReason); |
| | | } |
| | | } |
| | | catch (Exception e2) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), getExceptionMessage(e2)); |
| | | } |
| | | |
| | | unlockBackend(b); |
| | | continue; |
| | | } |
| | | |
| | | |
| | | // Perform the backup. |
| | | try |
| | | { |
| | |
| | | { |
| | | logger.error(ERR_BACKUPDB_ERROR_DURING_BACKUP, b.getBackendID(), de.getMessageObject()); |
| | | errorsEncountered = true; |
| | | |
| | | try |
| | | { |
| | | String lockFile = LockFileManager.getBackendLockFileName(b); |
| | | StringBuilder failureReason = new StringBuilder(); |
| | | if (! LockFileManager.releaseLock(lockFile, failureReason)) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), failureReason); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), getExceptionMessage(e)); |
| | | } |
| | | |
| | | unlockBackend(b); |
| | | continue; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.error(ERR_BACKUPDB_ERROR_DURING_BACKUP, b.getBackendID(), getExceptionMessage(e)); |
| | | errorsEncountered = true; |
| | | |
| | | try |
| | | { |
| | | String lockFile = LockFileManager.getBackendLockFileName(b); |
| | | StringBuilder failureReason = new StringBuilder(); |
| | | if (! LockFileManager.releaseLock(lockFile, failureReason)) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), failureReason); |
| | | } |
| | | } |
| | | catch (Exception e2) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), getExceptionMessage(e2)); |
| | | } |
| | | |
| | | unlockBackend(b); |
| | | continue; |
| | | } |
| | | |
| | | if (!releaseSharedLock(b)) |
| | | { |
| | | errorsEncountered = true; |
| | | } |
| | | } |
| | | |
| | | // Release the shared lock for the backend. |
| | | // Print a final completed message, indicating whether there were any errors |
| | | // in the process. |
| | | if (errorsEncountered) |
| | | { |
| | | logger.info(NOTE_BACKUPDB_COMPLETED_WITH_ERRORS); |
| | | return 1; |
| | | } |
| | | logger.info(NOTE_BACKUPDB_COMPLETED_SUCCESSFULLY); |
| | | return 0; |
| | | } |
| | | |
| | | private boolean acquireSharedLock(Backend<?> b) |
| | | { |
| | | try |
| | | { |
| | | String lockFile = LockFileManager.getBackendLockFileName(b); |
| | | StringBuilder failureReason = new StringBuilder(); |
| | | if (!LockFileManager.acquireSharedLock(lockFile, failureReason)) |
| | | { |
| | | logger.error(ERR_BACKUPDB_CANNOT_LOCK_BACKEND, b.getBackendID(), failureReason); |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.error(ERR_BACKUPDB_CANNOT_LOCK_BACKEND, b.getBackendID(), getExceptionMessage(e)); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | private boolean releaseSharedLock(Backend<?> b) |
| | | { |
| | | try |
| | | { |
| | | String lockFile = LockFileManager.getBackendLockFileName(b); |
| | |
| | | if (! LockFileManager.releaseLock(lockFile, failureReason)) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), failureReason); |
| | | errorsEncountered = true; |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), getExceptionMessage(e)); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | private void unlockBackend(Backend<?> b) |
| | | { |
| | | try |
| | | { |
| | | String lockFile = LockFileManager.getBackendLockFileName(b); |
| | | StringBuilder failureReason = new StringBuilder(); |
| | | if (!LockFileManager.releaseLock(lockFile, failureReason)) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), failureReason); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.warn(WARN_BACKUPDB_CANNOT_UNLOCK_BACKEND, b.getBackendID(), getExceptionMessage(e)); |
| | | errorsEncountered = true; |
| | | } |
| | | } |
| | | |
| | | |
| | | // Print a final completed message, indicating whether there were any errors |
| | | // in the process. |
| | | int ret = 0; |
| | | if (errorsEncountered) |
| | | { |
| | | logger.info(NOTE_BACKUPDB_COMPLETED_WITH_ERRORS); |
| | | ret = 1; |
| | | } |
| | | else |
| | | { |
| | | logger.info(NOTE_BACKUPDB_COMPLETED_SUCCESSFULLY); |
| | | } |
| | | return ret; |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public String getTaskId() { |
| | | return backupIDString != null ? backupIDString.getValue() : null; |