public class SimpleBackupStore extends java.lang.Object implements IBackupStore
backup(File) and
backupDirectory(File) methods to move files to backup instead of
deleting them. A file that is backed up should not be deleted - it is simply
moved out of the way. Use backupCopy(File) to move the file out of
harms way, but keep a copy of it in the original location. The methods
backupAll(File) and backupCopyAll(File) backs up an entire
structure.
When backup is finished - the user should either call restore() to
put all of the files back, or call discard() to remove all of the
backed up "copies".
If restore() or discard() is not called the backup files
will never be deleted.
The backup store does not synchronize directories - actions that write new
files are responsible for removing them. Overwriting existing files should be
done by first backing up the file, and then creating a new file. Modifying a
file, should be done by using backupCopy(File) or first making a
copy, then backing up the original, and then renaming the copy.
File.renameTo(File) and handling of permissions is operating system
dependent. It is expected that a Un*x type system retains the permissions as
a file is moved to the backup store and later gets restored. Backup
directories are created as they are needed and will (at least on Un*x)
inherit the permissions from its parent directory.
If a rename can not be performed, the backup store will make a copy and
delete the original file. This makes it possible to backup and restore across
volume boundaries.
When restoring directories they will be created with permissions in a
platform specific way (on UN*IX they will inherit the permissions of the
parent directory).
IllegalArgumentException
is thrown when attempting an operation that is considered "wrong use", and an
IllegalStateException or subclass thereof is thrown on an overall
wrong use of BackupStore (i.e. attempt to backup when store has been
restored). Some cases of "wrong use" can not be differentiated from I/O
errors (like a "file not found" as this could be caused by an entire disk
disappearing - in these case an IOException is thrown.
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
BACKUP_FILE_EXTENSION |
static java.lang.String |
DIR_PLACEHOLDER |
| Constructor and Description |
|---|
SimpleBackupStore()
Generates a BackupStore with a default prefix of ".p2bu" for backup directory
and probe file.
|
SimpleBackupStore(java.io.File buStoreParent,
java.lang.String prefix)
Generates a BackupStore with a specified prefix for backup directories and
probe file.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
backup(java.io.File file)
Backup the file by moving it to the backup store (for later (optional)
restore).
|
void |
backupAll(java.io.File file)
Backs up a file, or everything under a directory.
|
boolean |
backupCopy(java.io.File file)
Backup the file by leaving a copy of the contents in the original location.
|
void |
backupCopyAll(java.io.File file)
Backs up a file, or everything under a directory.
|
boolean |
backupDirectory(java.io.File file)
Performs backup of an empty directory.
|
void |
discard()
Discards and closes this BackupStore.
|
java.lang.String |
getBackupName()
Returns the unique backup name (this is the name of generated backup
directories).
|
java.io.File |
getBackupRoot() |
protected void |
move(java.nio.file.Path source,
java.nio.file.Path target)
A generic file operation that attempts to move a file.
|
void |
restore()
Restores all backup files from backup store.
|
protected java.nio.file.Path |
toBackupPath(java.nio.file.Path path)
Converts a source path to a backup path.
|
protected java.nio.file.Path |
toInPlaceBackupPath(java.nio.file.Path path)
Converts a path to an in-place backup path.
|
protected java.nio.file.Path |
toInPlaceSourcePath(java.nio.file.Path buPath)
Converts a in-place backup path to the original source path.
|
protected java.nio.file.Path |
toSourcePath(java.nio.file.Path buPath)
Converts a backup file to the original source file.
|
public static final java.lang.String BACKUP_FILE_EXTENSION
public static final java.lang.String DIR_PLACEHOLDER
public SimpleBackupStore()
public SimpleBackupStore(java.io.File buStoreParent,
java.lang.String prefix)
buStoreParent - Parent under which the backup store will be created. If
null, java.io.tmpdir is usedprefix - Prefix used for human identification of backup stores.public java.lang.String getBackupName()
getBackupName in interface IBackupStorepublic java.io.File getBackupRoot()
public boolean backup(java.io.File file)
throws java.io.IOException
backupDirectory(File).
A file (path) can only be backed up once per BackupStore instance. When the
file is backed up, it is moved to a directory under this BackupStore
instance's directory with a relative path corresponding to the original
relative path from the backup root e.g. the file /A/B/C/foo.txt could be
moved to /A/.p2bu_ffffff_ffffff/B/C/foo.txt when /A is the backup root.
If a directory is first backed up, and later replaced by a regular file, and
this file is backed up (or vice versa) - an IllegalArgumentException
is thrown
A backup can not be performed on a closed BackupStore.backup in interface IBackupStorefile - - the file (or directory) to backupjava.io.IOException - - if the backup operation fails, or the
file does not existClosedBackupStoreException - - if the BackupStore has been closedjava.lang.IllegalArgumentException - - on type mismatch (file vs. directory) of
earlier backup, or if file does not existpublic boolean backupDirectory(java.io.File file)
throws java.io.IOException
backupDirectory in interface IBackupStorefile - - the (empty) directory to back upjava.lang.IllegalArgumentException - if file is not a directory, or is not empty.java.io.IOException - if directory can not be moved to the backup
store, or if the directory is not writeablepublic void backupAll(java.io.File file)
throws java.io.IOException
backupAll in interface IBackupStorefile - - file to backup or directoryjava.io.IOException - if backup operation failedpublic boolean backupCopy(java.io.File file)
throws java.io.IOException
IllegalArgumentException.
A file (path) can only be backed up once per BackupStore instance. When the
file is backed up, it is moved to a directory under this BackupStore
instance's directory with a relative path corresponding to the original
relative path from the backup root e.g. the file /A/B/C/foo.txt could be
moved to /A/.p2bu_ffffff_ffffff/B/C/foo.txt when /A is the backup root.
If a directory is first backed up, and later replaced by a regular file, and
this file is backed up (or vice versa) - an IllegalArgumentException
is thrown
A backup can not be performed on a closed BackupStore.backupCopy in interface IBackupStorefile - - the file (or directory) to backupjava.io.IOException - if the backup operation fails, or the file
does not existClosedBackupStoreException - if the BackupStore has been closedjava.lang.IllegalArgumentException - on type mismatch (file vs. directory) of
earlier backup, or if file is a Directorypublic void backupCopyAll(java.io.File file)
throws java.io.IOException
backupCopyAll in interface IBackupStorefile - java.io.IOExceptionpublic void restore()
throws java.io.IOException
restore in interface IBackupStorejava.io.IOException - if the backup was not fully restored -
unrestored items have been logged.ClosedBackupStoreException - if the backup is already closed.public void discard()
discard in interface IBackupStoreprotected java.nio.file.Path toBackupPath(java.nio.file.Path path)
throws java.io.IOException
file - a source file that needs to be backed upjava.io.IOExceptionprotected java.nio.file.Path toSourcePath(java.nio.file.Path buPath)
buPath - an absolute file under buStoreRoot to which some
content is backed up.protected java.nio.file.Path toInPlaceBackupPath(java.nio.file.Path path)
path - protected java.nio.file.Path toInPlaceSourcePath(java.nio.file.Path buPath)
path - protected void move(java.nio.file.Path source,
java.nio.file.Path target)
throws java.io.IOException
java.io.IOException