#backup #backup-file #backup-utility #directory #utility

backitup

Stupidly simple crate for backing up files and directories

2 releases

0.1.1 Sep 13, 2023
0.1.0 Jun 27, 2023

#1503 in Filesystem

30 downloads per month
Used in gcenter

MIT license

16KB
205 lines

Back It Up!

Stupidly simple crate for backing up files and directories.

What is this?

This crate helps in a common scenario where an application generates an output file that could overwrite another file. Using the backup function provided by this crate, you can create backups before any overwriting happens, ensuring the data remains safe. Back It Up! makes sure that even if you write an output file with the same name repeatedly, no data will be lost.

Usage

Add the following dependency to your Cargo.toml file:

[dependencies]
backitup = "0.1.0"

Import the crate in your Rust code:

use backitup::backup;

Creating a Backup

To create a backup of a file or directory, use the backup function. The function takes the path to the file or directory as an argument and returns the path to the backup file if successful, or an error if the backup operation fails.

Note that the content of the file (or directory) is not copied, the file (or directory) is simply renamed.

use crate::backitup::backup;

let path = "data.txt";
match backup(path) {
    Ok(backup_path) => println!("Backup created: {:?}", backup_path),
    Err(err) => eprintln!("Failed to create backup: {:?}", err),
}

Name of the Backup

The backup file or directory name is generated based on the original path, appending a timestamp in the format "YYYY-MM-DD-HH-MM-SS". If multiple backups are created within the same second, additional information about the microseconds will be appended. The backup name follows the pattern:

For files: "#<parent_directory>/<filename>-<timestamp>(-<microseconds>)#"

For directories: "#<parent_directory>/<directory_name>-<timestamp>(-<microseconds>)#"

For instance, file data.txt backed up on 2023/06/27 at 21:01:13 (local time) will be renamed as `#data.txt-2023-06-27-21-01-13#.

License

This crate is distributed under the terms of the MIT license.

Dependencies

~1MB
~18K SLoC