I am trying to make a "backuper script" to keep a copy of a directory up to date on a USB-drive. Basically every time I open my computer it run rsync -qrup dir/path backupKey/path. I want a copy instead of an archive backup so that the USB-drive is usable on the go

Now my issue is that anytime I change the structure of my bookshelf dir tree. e.g: I move ~/bookshelf/studies/html to ~/bookshelf/studies/web-development/html

In my backupKey rsync will simply copy ~/bookshelf/studies/web-development/html(the new location) but not delete the original files at ~/bookshelf/studies/html.

This end-up creating duplicates that I have to manually delete.

Various things I've thought or read:

  • doing a full copy using cp, but this would be a lot more slow and resource intensive.
  • Using diff patch only work on text file (I saw advanced possibility but it getting complex and I don't understand fully)
  • Other binary diff checking tools like bsdiff jdiff don't seem to have a recursive option and I also don't know if it will work on various file type.
  • Looping trough all the file and delete the one that are at teh wrong place(duplicates) e.g: if file1 is in dirB but not in dirA delete it.

If someone as code examples of the loop. I need it to work with any file type digital books as much as notes and images. Sorry its been multiple hour I search online and can't find a solution that fits my idea.

Here is my script:

## Create an backup script to keep my usb device.

## check if media Bookshlef is available
exec &>> /home/$USER/.local/scripts/bookSUpdater.log

echo "-----"
echo `date +%Y-%m-%d-%T`

if [ -e /media/$USER/Bookshelf ]; then
echo "Bookshelf usb key is available"

## use rsync to copy documents to usb dev.
echo "Starting transfer"

rsync -qrup /home/$USER/bookshelf/ /media/$USER/Bookshelf
## d= copy dir wihtout recursing, l= keep symlink:  rsync -qdlup /home/$USER/bookshelf/* /media/$USER/Bookshelf
## try with binary difference tool
## make a loop to check if file are deleted. IF YES delete

echo "Operation succesful"
echo $? && exit 0

  echo 'Error: Usb device unavailable aborting'
  echo 'Usage: Usb device '\"'Bookshelf'\"' must be plugged in.'

echo $? && exit 2
  • for same need i use rsync -rt --delete /souce /dest See man rsync for --delete delete extraneous files from dest dirs – cmak.fr Aug 14 at 3:15
  • you can also find and delete duplicate files (moderns backup programs are currently doing it with some indexing). See simple duplicate deletion superuser.com/questions/386199/… – cmak.fr Aug 14 at 3:19
  • Thank you I will try it out @cmak.fr – Guillaume M-D Aug 14 at 15:26
  • the --delete flag is exactly what I needed. Thanks a lot. I wasnt sure of its definition and "extraneous" meaning. If you wish to post your comment as an answer I'd be glad to check it as accepted. Again appreciate – Guillaume M-D Aug 14 at 15:52

for same need i use

rsync -rt --delete /souce /dest

See man rsync
--delete is for delete extraneous files from dest dirs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.