public class BackupStore 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.
| Constructor and Description |
|---|
BackupStore()
Generates a BackupStore with a default prefix of ".p2bu" for backup directory and
probe file.
|
BackupStore(java.io.File buParentDirectory,
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 moving it to the backup store (for later (optional) restore) but 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.
|
protected void |
finalize()
Since a socket port is used to create a unique number, the socket
must be closed if this instance is garbage collected and the user
of the instance has not either restored or discarded.
|
java.lang.String |
getBackupName()
Returns the unique backup name (this is the name of generated backup directories).
|
java.io.File |
getBackupRoot() |
protected java.lang.String |
getTimeStamp() |
protected void |
moveToBackup(java.io.File file,
java.io.File buFile)
Move/rename file to a backup file.
|
protected boolean |
moveToBackupStore(java.io.File file,
java.io.File buFile) |
protected void |
renameInPlace(java.io.File file) |
void |
restore()
Restores all backup files from backup store.
|
public BackupStore()
genUnique() for more info.public BackupStore(java.io.File buParentDirectory,
java.lang.String prefix)
genUnique() for more info.buParentDirectory - - name of directory where the backup directory should be created - if null, java.io.tmpdir is usedprefix - - prefix used for human identification of backup directoriesprotected void finalize()
throws java.lang.Throwable
finalize in class java.lang.Objectjava.lang.Throwablepublic 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 existprotected void moveToBackup(java.io.File file,
java.io.File buFile)
throws java.io.IOException
file - source file to move; should already exist and must not be directorybuFile - destination backup file to move to; should not exist and must be a directoryjava.io.IOException - if the backup operation failsprotected boolean moveToBackupStore(java.io.File file,
java.io.File buFile)
protected void renameInPlace(java.io.File file)
protected java.lang.String getTimeStamp()
public 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 void backupCopyAll(java.io.File file)
throws java.io.IOException
backupCopyAll in interface IBackupStorefile - java.io.IOExceptionpublic 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 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 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 IBackupStore