keeping a Directory backup/copy up-to-date

by Guillaume M-D   Last Updated August 14, 2019 02:02 AM - source

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:

#!/bin/bash
## 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

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

echo $? && exit 2
fi
Tags : backup rsync copy


Related Questions





ubuntu copy to usb not working

Updated January 14, 2019 10:02 AM

Wildcards in exclude-filelist for duplicity

Updated March 28, 2015 15:00 PM