backup_file - File objects with automated backup

Backup a file or directory to make editing reversible.

Implement the context manager protocol, so as to be suitable to be used with the with statement. When used in this fashion changes are discarded when an exception is thrown.

class BackupDir(dir_, ext='.BAK', mode=1)[source]

Move or copy a directory that needs to be recreated or modified.

__enter__()[source]

When the controlling with statement is entered, create the backup directory.

__exit__(exc_type, exc_val, exc_tb)[source]

When the controlling with statement is exited normally discard the backup directory, otherwise restore it to its original place.

__init__(dir_, ext='.BAK', mode=1)[source]

Prepare to backup the dir_ directory.

The backup will be created in dir_’s parent directory, which must be writable, with extension ext. If mode is MOVE, the default, the original directory will be moved to the backup destination; if mode is COPY it will be copied there.

commit()[source]

Discard the backup, i.e. keep the supposedly modified file.

rollback()[source]

Replace the original file with the backup copy.

save()[source]

Create a backup copy of the original directory.

class BackupFile(file_, ext='.BAK', dir='.', mode=2)[source]

Implements a read only file object used to automatically back up a file that has to be modified.

__enter__()[source]

When the controlling with statement is entered, create the backup file.

__exit__(exc_type, exc_val, exc_tb)[source]

When the controlling with statement is exited normally discard the backup file, otherwise restore it to its original place.

__init__(file_, ext='.BAK', dir='.', mode=2)[source]

Prepare to backup file_, either a file-like object or a path.

The backup file will be created in directory dir with extension ext. If mode is COPY the original file will be copied to the backup destination; if mode is MOVE it will be moved there.

close()[source]

Close the backup file and release the corresponding reference.

The backup file may not be reopened.

commit()[source]

Discard the backup, i.e. keep the supposedly modified file.

name

The name of the file to be backed up.

open(mode=4)[source]

Open the backup file for reading. mode may be either TEXT or BINARY.

rollback()[source]

Replace the original file with the backup copy.

save()[source]

Create a backup copy of the original file.

Throw SaveError if it wasn’t possible.

exception MissingBackupError[source]

raised when a backup file or directory isn’t found.

exception NotSavedError[source]

Raised when commit or rollback is called on an inactive BackUpFile or BackUpDirectory.

exception RemovalError[source]

Raised to signal errors in the removal of backup files or directories.

exception SaveError[source]

Raised when a backup file or directory could not be created.