RCHBUWFZUS567KHXQ3XMBVYIH3QX366HMMKMR56JTZBSZFK4XT4AC
use std::{os::unix::prelude::FileTypeExt, path::PathBuf};
use lazy_static::lazy_static;
use same_file::Handle;
use walkdir::{DirEntry, WalkDir};
lazy_static! {
static ref ROOT: PathBuf = "/home/wo".into();
}
fn main() {
let files =
WalkDir::new(ROOT.as_path())
.into_iter()
.filter_entry(|e| match should_be_excluded(e) {
Ok(exclude) => !exclude,
Err(err) => {
eprintln!("{err}: {}", e.path().display());
true
}
});
for result in files {
match result {
Ok(entry) => println!("{}", entry.path().display()),
Err(error) => eprintln!("Error: {error}"),
}
}
}
fn should_be_excluded(entry: &DirEntry) -> Result<bool, std::io::Error> {
lazy_static! {
static ref EXCLUDED_DIRS: Vec<Handle> = [
".cache",
".vscode/extensions",
".cargo",
".config/Code/CachedData",
".npm/_cacache",
".rustup"
]
.into_iter()
.filter_map(|f| {
let p = ROOT.join(f);
Handle::from_path(p).ok()
})
.collect();
}
let file_type = entry.file_type();
if file_type.is_fifo()
|| file_type.is_block_device()
|| file_type.is_char_device()
|| file_type.is_socket()
{
return Ok(true);
}
let path = Handle::from_path(entry.path())?;
for excluded_dir in EXCLUDED_DIRS.iter() {
if path == *excluded_dir {
return Ok(true);
}
}
// Generated files
if is_dir_along_with_file(entry, "target", "Cargo.toml") {
return Ok(true);
}
if is_dir_along_with_file(entry, "node_modules", "package.json") {
return Ok(true);
}
Ok(false)
}
fn is_dir_along_with_file(entry: &DirEntry, dir_name: &str, file_name: &str) -> bool {
if entry.file_type().is_dir() && entry.file_name().to_str() == Some(dir_name) {
let file_exists = entry
.path()
.parent()
.map(|p| {
let mut p = p.to_owned();
p.push(file_name);
p.is_file()
})
.unwrap_or(false);
if file_exists {
return true;
}
}
false
}
Paths generator for `borg` backup tool
======================================
`backup-files` walks through filesystem and print all files that should be sent to backup.
Pipe this command output to `borg create --path-from-stdin {borg-repository}` (Borg version 1.2 or newer required) to perform backup.
`backup-files` lists only files from home directory (`~/`), ignores some "cache" directories (like `~/.cache`) and some "generated" directories (like `node_modules` if `package.json` is also present).
Currently `backup-files` does not provide any configuration possibilities. All configuration must be done in the source code.
This script is early stage of development. Nevertheless it does its job well for me right now.
[package]
name = "backup-files"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lazy_static = "1.4"
same-file = "1.0"
walkdir = "2.3"
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "backup-files"
version = "0.1.0"
dependencies = [
"lazy_static",
"same-file",
"walkdir",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "walkdir"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
target