Search in sources :

Example 1 with BackupStrategy

use of jetbrains.exodus.backup.BackupStrategy in project xodus by JetBrains.

the class FileSystemBlobVaultOld method getBackupStrategy.

@NotNull
@Override
public BackupStrategy getBackupStrategy() {
    return new BackupStrategy() {

        @Override
        public Iterable<VirtualFileDescriptor> getContents() {
            return new Iterable<VirtualFileDescriptor>() {

                @NotNull
                @Override
                public Iterator<VirtualFileDescriptor> iterator() {
                    final Deque<FileDescriptor> queue = new LinkedList<>();
                    queue.add(new FileDescriptor(location, blobsDirectory + File.separator));
                    return new Iterator<VirtualFileDescriptor>() {

                        int i = 0;

                        int n = 0;

                        File[] files;

                        FileDescriptor next;

                        String currentPrefix;

                        @Override
                        public boolean hasNext() {
                            if (next != null) {
                                return true;
                            }
                            while (i < n) {
                                final File file = files[i++];
                                final String name = file.getName();
                                if (file.isDirectory()) {
                                    queue.push(new FileDescriptor(file, currentPrefix + file.getName() + File.separator));
                                } else if (file.isFile()) {
                                    final long fileSize = file.length();
                                    if (fileSize == 0)
                                        continue;
                                    if (name.endsWith(blobExtension)) {
                                        next = new FileDescriptor(file, currentPrefix, fileSize);
                                        return true;
                                    } else if (name.equalsIgnoreCase(VERSION_FILE)) {
                                        next = new FileDescriptor(file, currentPrefix, fileSize, false);
                                        return true;
                                    }
                                } else if (file.exists()) {
                                    // something strange with filesystem
                                    throw new EntityStoreException("File or directory expected: " + file.toString());
                                }
                            }
                            if (queue.isEmpty()) {
                                return false;
                            }
                            final FileDescriptor fd = queue.pop();
                            files = IOUtil.listFiles(fd.getFile());
                            currentPrefix = fd.getPath();
                            i = 0;
                            n = files.length;
                            next = fd;
                            return true;
                        }

                        @Override
                        public FileDescriptor next() {
                            if (!hasNext()) {
                                throw new NoSuchElementException();
                            }
                            final FileDescriptor result = next;
                            next = null;
                            return result;
                        }

                        @Override
                        public void remove() {
                            throw new UnsupportedOperationException();
                        }
                    };
                }
            };
        }
    };
}
Also used : LongIterator(jetbrains.exodus.core.dataStructures.hash.LongIterator) BackupStrategy(jetbrains.exodus.backup.BackupStrategy) VirtualFileDescriptor(jetbrains.exodus.backup.VirtualFileDescriptor) VirtualFileDescriptor(jetbrains.exodus.backup.VirtualFileDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with BackupStrategy

use of jetbrains.exodus.backup.BackupStrategy in project xodus by JetBrains.

the class CompressBackupUtil method backup.

/**
 * For specified {@linkplain Backupable} {@code source} and {@code target} backup file, does backup.
 * Typically, {@code source} is an {@linkplain Environment} or an {@linkplain PersistentEntityStore}
 * instance. Set {@code zip = true} to create {@code .zip} backup file, otherwise {@code .tar.gz} file will be created.
 *
 * <p>{@linkplain Environment} and {@linkplain PersistentEntityStore} instances don't require any specific actions
 * (like, e.g., switching to read-only mode) to do backups and get consistent copies of data within backups files.
 * So backup can be performed on-the-fly not affecting database operations.
 *
 * @param source an instance of {@linkplain Backupable}
 * @param target target backup file (either .zip or .tag.gz)
 * @param zip    {@code true} to create {@code .zip} backup file, rather than {@code .tar.gz} one
 * @return backup file the same as specified {@code target}
 * @throws Exception something went wrong
 */
@NotNull
public static File backup(@NotNull final Backupable source, @NotNull final File target, final boolean zip) throws Exception {
    if (target.exists()) {
        throw new IOException("Backup file already exists:" + target.getAbsolutePath());
    }
    final BackupStrategy strategy = source.getBackupStrategy();
    strategy.beforeBackup();
    try (OutputStream output = new BufferedOutputStream(new FileOutputStream(target))) {
        final ArchiveOutputStream archive;
        if (zip) {
            final ZipArchiveOutputStream zipArchive = new ZipArchiveOutputStream(output);
            zipArchive.setLevel(Deflater.BEST_COMPRESSION);
            archive = zipArchive;
        } else {
            archive = new TarArchiveOutputStream(new GZIPOutputStream(output));
        }
        try (ArchiveOutputStream aos = archive) {
            for (final VirtualFileDescriptor fd : strategy.getContents()) {
                if (strategy.isInterrupted()) {
                    break;
                }
                if (fd.hasContent()) {
                    final long fileSize = Math.min(fd.getFileSize(), strategy.acceptFile(fd));
                    if (fileSize > 0L) {
                        archiveFile(aos, fd, fileSize);
                    }
                }
            }
        }
        if (strategy.isInterrupted()) {
            logger.info("Backup interrupted, deleting \"" + target.getName() + "\"...");
            IOUtil.deleteFile(target);
        } else {
            logger.info("Backup file \"" + target.getName() + "\" created.");
        }
    } catch (Throwable t) {
        strategy.onError(t);
        throw ExodusException.toExodusException(t, "Backup failed");
    } finally {
        strategy.afterBackup();
    }
    return target;
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) BackupStrategy(jetbrains.exodus.backup.BackupStrategy) VirtualFileDescriptor(jetbrains.exodus.backup.VirtualFileDescriptor) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

BackupStrategy (jetbrains.exodus.backup.BackupStrategy)2 VirtualFileDescriptor (jetbrains.exodus.backup.VirtualFileDescriptor)2 NotNull (org.jetbrains.annotations.NotNull)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 LongIterator (jetbrains.exodus.core.dataStructures.hash.LongIterator)1 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)1 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)1 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)1