#!/bin/bash
# rsync-two-dirs
# syncs all Dirs/files   that noticed $src_path/$src_dirs_to_sync   recursively to $dest_path

# recursive, links, times, devices, specials, deletes not yet existing files on target, shows progress, 
# run under root saves: permissions, group, owner
# Variables to customize:
#    config-files needed:	excl
#    variables to set: DIRs, Files: $src_path, $src_dirs_to_sync, $dest_path
# 120811
# (C)opyright by Gerd Hoffmann 2012

# actuel date for subdir- / logfile-name
DATUM=`/bin/date '+%Y%m%d-%H%M'`
pwd_path=`pwd`

# exclude files/DIRs/extensions listed in file:
excl="$pwd_path/excludes-Video"
# Source-Directory from where files etc. are to be read
src_path="/media/sda7/Video/"
src_dirs_to_sync="MyDownloads/ Sonst/"
# Destination-Directory to which files etc. are to be wrote
dest_path="/media/NAS-1-Volume_1/Video/"
#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
rsync_options=" --recursive --links --perms --times --group --owner --devices --specials --progress --delete --delete-excluded --itemize-changes " # in long
log_file="./rsync-NAS-1-Video-$DATUM.log"

# no src_dirs ? then sync noticed dir only
if test -z $src_dirs_to_sync; then
   echo "no loop, srcpath-i = $src_path$i"
   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 "$dest_path" | tee -a $log_file
      fi
      echo "command: nice -n 15 rsync $rsync_options --exclude-from=$excl --log-file=$log_file $src_path $dest_path" | tee -a $log_file
      # run it, writes output to logfile, use rsync-option -n for dry run
      nice -n 15 rsync $rsync_options --exclude-from=$excl --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 "$src_path does not exist"
   fi
else
   # loop for backup_dir-extensions
   for i in   $src_dirs_to_sync ; do 
      echo "in loop, srcpath-i = $src_path$i"
      if test -e $src_path$i ; then 
         # begin to write log-file
         DATUM=`/bin/date '+%Y%m%d-%H%M'`
         echo "--- $DATUM --- $i" | tee -a $log_file
         echo "srcpath: $i" | tee -a $log_file
         if test ! -d $dest_path$i && test -d $src_path$i; then 
            echo "destpath: missing $dest_path$i"   | tee -a $log_file
   	    echo "making dir: $dest_path$i" | tee -a $log_file
            mkdir "$dest_path$i" | tee -a $log_file
         fi
         echo "command: nice -n 15 rsync $rsync_options --exclude-from=$excl --log-file=$log_file $src_path.$i $dest_path$i" | tee -a $log_file
         # run it, writes output to logfile, use rsync-option -n for dry run
         nice -n 15 rsync $rsync_options --exclude-from=$excl --log-file=$log_file $src_path$i $dest_path$i | tee -a $log_file
         DATUM=`/bin/date '+%Y%m%d-%H%M'`
         echo "End of $src_path.$i --- $DATUM -----" | tee -a $log_file
      fi
   done
fi

