backupFile function
Provide a very simple mechanism to backup a single file.
The backup is placed in '.bak' subdirectory under the passed
pathToFile
's directory.
Be cautious that you don't nest backups of the same file in your code as we always use the same backup target. Instead use withFileProtection.
We also renamed the backup to '
If a file at pathToFile
doesn't exist then a BackupFileException
is thrown unless you pass the ignoreMissing
flag.
Implementation
Future<void> backupFile(String pathToFile, {bool ignoreMissing = false}) async {
if (!exists(pathToFile)) {
throw BackupFileException(
'The backup file ${truepath(pathToFile)} is missing',
);
}
final pathToBackupFile = _backupFilePath(pathToFile);
if (exists(pathToBackupFile)) {
await delete(pathToBackupFile);
}
if (!exists(dirname(pathToBackupFile))) {
await createDir(dirname(pathToBackupFile));
}
verbose(() => 'Backing up ${truepath(pathToFile)}');
await copy(pathToFile, pathToBackupFile);
}