#!/bin/bash
# rsync some i5b dirs to fritz
# syncs all Dirs/files   that noticed in include-file   recursively to $dest_path
# files / DIRs depending on exclude-file / include-file
# recursive, links, times, devices, specials; deletes not yet existing files on target, shows progress, 
# run under root saves: permissions, group, owner
# builds two log-files: all and Help
# Variables to customize:
#    config-files needed:	excl
#    variables to set: DIRs, Files: $src_path, $src_dirs_to_sync, $dest_path
# 2005011
# (C)opyright by Gerd Hoffmann 2020



###############   function run_rsync   ###########################
function run_rsync {

  #### test incl und excl !!!

   if test -e $src_path ; then
        # begin to write log-file
      DATUM=`/bin/date '+%Y%m%d-%H%M'`
      echo "--- $DATUM --- $src_path" | tee -a $log_file
      echo "srcpath: $src_path" | tee -a $log_file
      if test ! -d $dest_path ; then 
         echo "destpath: missing $dest_path"   | tee -a $log_file
         echo "making dir: $dest_path" | tee -a $log_file
         mkdir -p "$dest_path" | tee -a $log_file
      fi
      echo "command: nice -n 15 rsync $dryrun $rsync_options $incl_excl_from --log-file=$log_file $src_path $dest_path" | tee -a $log_file
        # run it, writes output to logfile
      nice -n 15 rsync $dryrun $rsync_options $incl_excl_from --log-file=$log_file $src_path $dest_path | tee -a $log_file
  #      nice -n 15 rsync $dryrun $rsync_options --include-from=$incl --exclude-from=$excl $incl_excl_from --log-file=$log_file $src_path $dest_path | tee -a $log_file
      DATUM=`/bin/date '+%Y%m%d-%H%M'`
      echo "End of $src_path --- $DATUM -----" | tee -a $log_file
   else 
      echo "err: $src_path does not exist" | tee -a $log_file
   fi
}
###############   function run_rsync end   ###########################


## vars to control
## today's date for subdir- / logfile-name
filename="sync2fritz-easy"
DATUM=`/bin/date '+%Y%m%d-%H%M'`
pwd_path=`pwd`

## exclude files/DIRs/extensions listed in file:
## include / exclude options
incl_excl_from=--exclude-from="$pwd_path/fritz-excludes-DATA-all"
     # incl_excl_from="--exclude-from=\"$pwd_path/fritz-excludes-DATA\""
     # include_list=""
## Source-Directory from where files etc. are to be read
src_path="/media/sdb2/DATA/"
## Destination-Directory to which files etc. are to be wrote
dest_path="/media/Fritz-NAS/"
     #rsync_options=" -n -a       -v --progress --delete --delete-excluded --itemize-changes "
     #rsync_options=" -n -rlptgoD -v --progress --delete --delete-excluded --itemize-changes " # in short
     # rsync_options=" --dry-run --recursive --links --perms --times --group --owner --devices --specials -v --progress --delete --delete-excluded --itemize-changes " # in long
     # no permissions, no group   ( bkup-fs = cifs ! )
     # rsync_options=" --recursive --links --times --owner --devices --specials --progress --delete --delete-excluded --itemize-changes --modify-window=2" # in long

     # rsync_options="-avvz --exclude 'RECYCLE.BIN' --delete"
     # rsync_options="-avz"
rsync_options=" -v --recursive --times --owner --devices --specials --progress --delete --itemize-changes " # in long
     # rsync_options=" -vv --recursive --links --times --owner --devices --specials --progress --delete --delete-excluded --itemize-changes " # in long
log_file="$pwd_path/${filename}-$DATUM.log"
# dryrun="--dry-run"
## run function
run_rsync


## for Help/* only:
incl_excl_from=--exclude-from="$pwd_path/fritz-excludes-DATA-Help"
src_path="/media/sdb2/DATA/Html/g-h-o-de/"
dest_path="/media/Fritz-NAS/"
log_file="$pwd_path/${filename}-Help-$DATUM.log"
run_rsync

exit


