mirror of
https://github.com/Dummi26/rembackup.git
synced 2025-03-10 13:43:53 +01:00
when no target is specified, show an extra warning and just build an index.
with this, you can use rembackup on the `target` server to generate a new index if, for some reason, your local index gets messed up and has to be regenerated
This commit is contained in:
parent
f4cd0851dd
commit
5e03e584db
@ -1,4 +1,7 @@
|
|||||||
use std::{fs, io, path::Path};
|
use std::{
|
||||||
|
fs, io,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{indexchanges::IndexChange, repr_file::ReprFile};
|
use crate::{indexchanges::IndexChange, repr_file::ReprFile};
|
||||||
|
|
||||||
@ -8,7 +11,7 @@ use crate::{indexchanges::IndexChange, repr_file::ReprFile};
|
|||||||
pub fn apply_indexchanges(
|
pub fn apply_indexchanges(
|
||||||
source: &Path,
|
source: &Path,
|
||||||
index: &Path,
|
index: &Path,
|
||||||
target: &Path,
|
target: &Option<PathBuf>,
|
||||||
changes: &Vec<IndexChange>,
|
changes: &Vec<IndexChange>,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
let o = apply_indexchanges_int(source, index, target, changes);
|
let o = apply_indexchanges_int(source, index, target, changes);
|
||||||
@ -18,7 +21,7 @@ pub fn apply_indexchanges(
|
|||||||
pub fn apply_indexchanges_int(
|
pub fn apply_indexchanges_int(
|
||||||
source: &Path,
|
source: &Path,
|
||||||
index: &Path,
|
index: &Path,
|
||||||
target: &Path,
|
target: &Option<PathBuf>,
|
||||||
changes: &Vec<IndexChange>,
|
changes: &Vec<IndexChange>,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
let len_width = changes.len().to_string().len();
|
let len_width = changes.len().to_string().len();
|
||||||
@ -32,6 +35,7 @@ pub fn apply_indexchanges_int(
|
|||||||
for (i, change) in changes.iter().enumerate() {
|
for (i, change) in changes.iter().enumerate() {
|
||||||
match change {
|
match change {
|
||||||
IndexChange::AddDir(dir) => {
|
IndexChange::AddDir(dir) => {
|
||||||
|
let ok = if let Some(target) = target {
|
||||||
let t = target.join(dir);
|
let t = target.join(dir);
|
||||||
if let Some(e) = fs::create_dir(&t).err().and_then(|e| {
|
if let Some(e) = fs::create_dir(&t).err().and_then(|e| {
|
||||||
if matches!(e.kind(), io::ErrorKind::AlreadyExists) {
|
if matches!(e.kind(), io::ErrorKind::AlreadyExists) {
|
||||||
@ -41,33 +45,65 @@ pub fn apply_indexchanges_int(
|
|||||||
}
|
}
|
||||||
}) {
|
}) {
|
||||||
eprintln!("\n[warn] couldn't create directory {t:?}: {e}");
|
eprintln!("\n[warn] couldn't create directory {t:?}: {e}");
|
||||||
|
false
|
||||||
} else {
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
};
|
||||||
|
if ok {
|
||||||
fs::create_dir(&index.join(dir))?;
|
fs::create_dir(&index.join(dir))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
IndexChange::AddFile(file, index_file) => {
|
IndexChange::AddFile(file, index_file) => {
|
||||||
|
let ok = if let Some(target) = target {
|
||||||
let s = source.join(file);
|
let s = source.join(file);
|
||||||
let t = target.join(file);
|
let t = target.join(file);
|
||||||
if let Err(e) = fs::copy(&s, &t) {
|
if let Err(e) = fs::copy(&s, &t) {
|
||||||
eprintln!("\n[warn] couldn't copy file from {s:?} to {t:?}: {e}");
|
eprintln!("\n[warn] couldn't copy file from {s:?} to {t:?}: {e}");
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
};
|
||||||
|
if ok {
|
||||||
fs::write(&index.join(file), index_file.save())?;
|
fs::write(&index.join(file), index_file.save())?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
IndexChange::RemoveFile(file) => {
|
IndexChange::RemoveFile(file) => {
|
||||||
let i = index.join(file);
|
let i = index.join(file);
|
||||||
|
let ok = if let Some(target) = target {
|
||||||
let t = target.join(file);
|
let t = target.join(file);
|
||||||
if let Err(e) = fs::remove_file(&t) {
|
if let Err(e) = fs::remove_file(&t) {
|
||||||
eprintln!("\n[warn] couldn't remove file {t:?}, keeping index file {i:?}: {e:?}\n If this error keeps appearing, check if the file was deleted on the target system but still exists in the index. if yes, consider manually deleting it.");
|
eprintln!("\n[warn] couldn't remove file {t:?}, keeping index file {i:?}: {e:?}\n If this error keeps appearing, check if the file was deleted on the target system but still exists in the index. if yes, consider manually deleting it.");
|
||||||
|
false
|
||||||
} else {
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
};
|
||||||
|
if ok {
|
||||||
fs::remove_file(i)?;
|
fs::remove_file(i)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
IndexChange::RemoveDir(dir) => {
|
IndexChange::RemoveDir(dir) => {
|
||||||
let i = index.join(dir);
|
let i = index.join(dir);
|
||||||
|
let ok = if let Some(target) = target {
|
||||||
let t = target.join(dir);
|
let t = target.join(dir);
|
||||||
if let Err(e) = fs::remove_dir_all(&t) {
|
if let Err(e) = fs::remove_dir_all(&t) {
|
||||||
eprintln!("\n[warn] couldn't remove directory {t:?}, keeping index files under {i:?}: {e:?}\n If this error keeps appearing, check if the directory was deleted on the target system but still exists in the index. if yes, consider manually deleting it.");
|
eprintln!("\n[warn] couldn't remove directory {t:?}, keeping index files under {i:?}: {e:?}\n If this error keeps appearing, check if the directory was deleted on the target system but still exists in the index. if yes, consider manually deleting it.");
|
||||||
|
false
|
||||||
} else {
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
};
|
||||||
|
if ok {
|
||||||
fs::remove_dir_all(i)?;
|
fs::remove_dir_all(i)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,13 @@ use clap::Parser;
|
|||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(author, version)]
|
#[command(author, version)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
|
/// the data to be backed up
|
||||||
#[arg()]
|
#[arg()]
|
||||||
pub source: PathBuf,
|
pub source: PathBuf,
|
||||||
|
/// the index used to determine which files have been modified
|
||||||
#[arg()]
|
#[arg()]
|
||||||
pub index: PathBuf,
|
pub index: PathBuf,
|
||||||
|
/// where your backup will be stored
|
||||||
#[arg()]
|
#[arg()]
|
||||||
pub target: PathBuf,
|
pub target: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,9 @@ fn main() {
|
|||||||
eprintln!(" - remove file");
|
eprintln!(" - remove file");
|
||||||
eprintln!(" [-] remove directory (and all contents!)");
|
eprintln!(" [-] remove directory (and all contents!)");
|
||||||
eprintln!("Press Enter to to apply these actions.");
|
eprintln!("Press Enter to to apply these actions.");
|
||||||
|
if args.target.is_none() {
|
||||||
|
eprintln!("[WARN] You didn't set a `target` directory!\n[WARN] Be careful not to update your index without actually applying the changes to the `target` filesystem!");
|
||||||
|
}
|
||||||
// apply changes
|
// apply changes
|
||||||
if std::io::stdin().read_line(&mut String::new()).is_ok() {
|
if std::io::stdin().read_line(&mut String::new()).is_ok() {
|
||||||
match apply_indexchanges(&args.source, &args.index, &args.target, &changes) {
|
match apply_indexchanges(&args.source, &args.index, &args.target, &changes) {
|
||||||
|
Loading…
Reference in New Issue
Block a user